mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 14:02:26 +01:00
Merge remote-tracking branch 'origin/main' into chore/build-syft-for-cli-tests
This commit is contained in:
@@ -6,6 +6,12 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
// this is the number of packages that should be found in the image-pkg-coverage fixture image
|
||||
// when analyzed with the squashed scope.
|
||||
coverageImageSquashedPackageCount = 24
|
||||
)
|
||||
|
||||
func TestPackagesCmdFlags(t *testing.T) {
|
||||
hiddenPackagesImage := "docker-archive:" + getFixtureImage(t, "image-hidden-packages")
|
||||
coverageImage := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
|
||||
@@ -36,6 +42,24 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "quiet-flag-with-logger",
|
||||
args: []string{"packages", "-qvv", "-o", "json", coverageImage},
|
||||
assertions: []traitAssertion{
|
||||
assertJsonReport,
|
||||
assertNoStderr,
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "quiet-flag-with-tui",
|
||||
args: []string{"packages", "-q", "-o", "json", coverageImage},
|
||||
assertions: []traitAssertion{
|
||||
assertJsonReport,
|
||||
assertNoStderr,
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "multiple-output-flags",
|
||||
args: []string{"packages", "-o", "table", "-o", "json=" + tmp + ".tmp/multiple-output-flag-test.json", coverageImage},
|
||||
@@ -96,7 +120,7 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
name: "squashed-scope-flag",
|
||||
args: []string{"packages", "-o", "json", "-s", "squashed", coverageImage},
|
||||
assertions: []traitAssertion{
|
||||
assertPackageCount(24),
|
||||
assertPackageCount(coverageImageSquashedPackageCount),
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -153,7 +177,7 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "responds-to-package-cataloger-search-options",
|
||||
args: []string{"packages", "-vv"},
|
||||
args: []string{"--help"},
|
||||
env: map[string]string{
|
||||
"SYFT_PACKAGE_SEARCH_UNINDEXED_ARCHIVES": "true",
|
||||
"SYFT_PACKAGE_SEARCH_INDEXED_ARCHIVES": "false",
|
||||
@@ -213,7 +237,7 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
// the application config in the log matches that of what we expect to have been configured.
|
||||
assertInOutput("parallelism: 2"),
|
||||
assertInOutput("parallelism=2"),
|
||||
assertPackageCount(24),
|
||||
assertPackageCount(coverageImageSquashedPackageCount),
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -224,7 +248,7 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
// the application config in the log matches that of what we expect to have been configured.
|
||||
assertInOutput("parallelism: 1"),
|
||||
assertInOutput("parallelism=1"),
|
||||
assertPackageCount(24),
|
||||
assertPackageCount(coverageImageSquashedPackageCount),
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -238,7 +262,7 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
assertions: []traitAssertion{
|
||||
assertNotInOutput("secret_password"),
|
||||
assertNotInOutput("secret_key_path"),
|
||||
assertPackageCount(24),
|
||||
assertPackageCount(coverageImageSquashedPackageCount),
|
||||
assertSuccessfulReturnCode,
|
||||
},
|
||||
},
|
||||
@@ -258,7 +282,7 @@ func TestPackagesCmdFlags(t *testing.T) {
|
||||
func TestRegistryAuth(t *testing.T) {
|
||||
host := "localhost:17"
|
||||
image := fmt.Sprintf("%s/something:latest", host)
|
||||
args := []string{"packages", "-vv", fmt.Sprintf("registry:%s", image)}
|
||||
args := []string{"packages", "-vvv", fmt.Sprintf("registry:%s", image)}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -272,7 +296,7 @@ func TestRegistryAuth(t *testing.T) {
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput("source=OciRegistry"),
|
||||
assertInOutput(image),
|
||||
assertInOutput("no registry credentials configured, using the default keychain"),
|
||||
assertInOutput(fmt.Sprintf("no registry credentials configured for %q, using the default keychain", host)),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -294,7 +318,7 @@ func TestRegistryAuth(t *testing.T) {
|
||||
args: args,
|
||||
env: map[string]string{
|
||||
"SYFT_REGISTRY_AUTH_AUTHORITY": host,
|
||||
"SYFT_REGISTRY_AUTH_TOKEN": "token",
|
||||
"SYFT_REGISTRY_AUTH_TOKEN": "my-token",
|
||||
},
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput("source=OciRegistry"),
|
||||
@@ -303,7 +327,7 @@ func TestRegistryAuth(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "not enough info fallsback to keychain",
|
||||
name: "not enough info fallback to keychain",
|
||||
args: args,
|
||||
env: map[string]string{
|
||||
"SYFT_REGISTRY_AUTH_AUTHORITY": host,
|
||||
@@ -311,7 +335,7 @@ func TestRegistryAuth(t *testing.T) {
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput("source=OciRegistry"),
|
||||
assertInOutput(image),
|
||||
assertInOutput(`no registry credentials configured, using the default keychain`),
|
||||
assertInOutput(fmt.Sprintf(`no registry credentials configured for %q, using the default keychain`, host)),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -324,6 +348,17 @@ func TestRegistryAuth(t *testing.T) {
|
||||
assertInOutput("insecure-use-http: true"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "use tls configuration",
|
||||
args: args,
|
||||
env: map[string]string{
|
||||
"SYFT_REGISTRY_AUTH_TLS_CERT": "place.crt",
|
||||
"SYFT_REGISTRY_AUTH_TLS_KEY": "place.key",
|
||||
},
|
||||
assertions: []traitAssertion{
|
||||
assertInOutput("using custom TLS credentials from"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM debian:sid
|
||||
FROM debian:sid@sha256:bee393b48f83fdeb978ce68abb9dc955d6398b984fca88b52a09dceb45ac74b5
|
||||
ADD sources.list /etc/apt/sources.list.d/sources.list
|
||||
RUN apt update -y && apt install -y dpkg-dev
|
||||
# this as a "macho-invalid" directory which is useful for testing
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM centos:7.9.2009
|
||||
FROM centos:7.9.2009@sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f
|
||||
# all-layers scope should pickup on vsftpd
|
||||
RUN yum install -y vsftpd
|
||||
RUN yum remove -y vsftpd
|
||||
RUN yum remove -y vsftpd
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
FROM openjdk:11
|
||||
FROM openjdk:11@sha256:e81b7f317654b0f26d3993e014b04bcb29250339b11b9de41e130feecd4cd43c
|
||||
|
||||
RUN wget --no-verbose https://github.com/spdx/tools-java/releases/download/v1.1.3/tools-java-1.1.3.zip && \
|
||||
unzip tools-java-1.1.3.zip && \
|
||||
|
||||
@@ -1 +1 @@
|
||||
FROM node:19-alpine3.15
|
||||
FROM node:19-alpine3.15@sha256:07050181369f52460e788738272bcb22b71c7cfc6fafee0b5cd27d2022513a86
|
||||
|
||||
@@ -1 +1 @@
|
||||
FROM alpine:latest
|
||||
FROM alpine@sha256:c5c5fda71656f28e49ac9c5416b3643eaa6a108a8093151d6d1afc9463be8e33
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -85,11 +83,20 @@ func assertNotInOutput(data string) traitAssertion {
|
||||
}
|
||||
}
|
||||
|
||||
func assertNoStderr(tb testing.TB, _, stderr string, _ int) {
|
||||
tb.Helper()
|
||||
if len(stderr) > 0 {
|
||||
tb.Errorf("expected stderr to be empty, but got %q", stderr)
|
||||
}
|
||||
}
|
||||
|
||||
func assertInOutput(data string) traitAssertion {
|
||||
return func(tb testing.TB, stdout, stderr string, _ int) {
|
||||
tb.Helper()
|
||||
if !strings.Contains(stripansi.Strip(stderr), data) && !strings.Contains(stripansi.Strip(stdout), data) {
|
||||
tb.Errorf("data=%q was NOT found in any output, but should have been there", data)
|
||||
stdout = stripansi.Strip(stdout)
|
||||
stderr = stripansi.Strip(stderr)
|
||||
if !strings.Contains(stdout, data) && !strings.Contains(stderr, data) {
|
||||
tb.Errorf("data=%q was NOT found in any output, but should have been there\nSTDOUT:%s\nSTDERR:%s", data, stdout, stderr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -148,46 +155,6 @@ func assertSuccessfulReturnCode(tb testing.TB, _, _ string, rc int) {
|
||||
}
|
||||
}
|
||||
|
||||
func assertVerifyAttestation(coverageImage string) traitAssertion {
|
||||
return func(tb testing.TB, stdout, _ string, _ int) {
|
||||
tb.Helper()
|
||||
cosignPath := filepath.Join(repoRoot(tb), ".tmp/cosign")
|
||||
err := os.WriteFile("attestation.json", []byte(stdout), 0664)
|
||||
if err != nil {
|
||||
tb.Errorf("could not write attestation to disk")
|
||||
}
|
||||
defer os.Remove("attestation.json")
|
||||
attachCmd := exec.Command(
|
||||
cosignPath,
|
||||
"attach",
|
||||
"attestation",
|
||||
"--attestation",
|
||||
"attestation.json",
|
||||
coverageImage, // TODO which remote image to use?
|
||||
)
|
||||
|
||||
stdout, stderr, _ := runCommand(attachCmd, nil)
|
||||
if attachCmd.ProcessState.ExitCode() != 0 {
|
||||
tb.Log("STDOUT", stdout)
|
||||
tb.Log("STDERR", stderr)
|
||||
tb.Fatalf("could not attach image")
|
||||
}
|
||||
|
||||
verifyCmd := exec.Command(
|
||||
cosignPath,
|
||||
"verify-attestation",
|
||||
coverageImage, // TODO which remote image to use?
|
||||
)
|
||||
|
||||
stdout, stderr, _ = runCommand(verifyCmd, nil)
|
||||
if attachCmd.ProcessState.ExitCode() != 0 {
|
||||
tb.Log("STDOUT", stdout)
|
||||
tb.Log("STDERR", stderr)
|
||||
tb.Fatalf("could not verify attestation")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assertFileExists(file string) traitAssertion {
|
||||
return func(tb testing.TB, _, _ string, _ int) {
|
||||
tb.Helper()
|
||||
|
||||
Reference in New Issue
Block a user