Skip to content

Commit

Permalink
fix(cli): inconsistent behavior across CLI flags, environment variabl…
Browse files Browse the repository at this point in the history
…es, and config files (aquasecurity#5843)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 authored Feb 1, 2024
1 parent 5924c02 commit 59e5433
Show file tree
Hide file tree
Showing 43 changed files with 1,714 additions and 1,425 deletions.
7 changes: 4 additions & 3 deletions integration/aws_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"testing"
"time"

awscommands "github.com/aquasecurity/trivy/pkg/cloud/aws/commands"
"github.com/aquasecurity/trivy/pkg/flag"
dockercontainer "github.com/docker/docker/api/types/container"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
testcontainers "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/localstack"

awscommands "github.com/aquasecurity/trivy/pkg/cloud/aws/commands"
"github.com/aquasecurity/trivy/pkg/flag"
)

func TestAwsCommandRun(t *testing.T) {
Expand Down
98 changes: 29 additions & 69 deletions integration/client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration
import (
"context"
"fmt"
"github.com/aquasecurity/trivy/pkg/types"
"os"
"path/filepath"
"strings"
Expand All @@ -15,16 +16,15 @@ import (
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
testcontainers "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go"

"github.com/aquasecurity/trivy/pkg/report"
"github.com/aquasecurity/trivy/pkg/uuid"
)

type csArgs struct {
Command string
RemoteAddrOption string
Format string
Format types.Format
TemplatePath string
IgnoreUnfixed bool
Severity []string
Expand Down Expand Up @@ -265,19 +265,15 @@ func TestClientServer(t *testing.T) {

addr, cacheDir := setup(t, setupOptions{})

for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
osArgs, outputFile := setupClient(t, c.args, addr, cacheDir, c.golden)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)

if c.args.secretConfig != "" {
osArgs = append(osArgs, "--secret-config", c.args.secretConfig)
if tt.args.secretConfig != "" {
osArgs = append(osArgs, "--secret-config", tt.args.secretConfig)
}

//
err := execute(osArgs)
require.NoError(t, err)

compareReports(t, c.golden, outputFile, nil)
runTest(t, osArgs, tt.golden, "", types.FormatJSON, runOptions{})
})
}
}
Expand Down Expand Up @@ -389,19 +385,9 @@ func TestClientServerWithFormat(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("AWS_REGION", "test-region")
t.Setenv("AWS_ACCOUNT_ID", "123456789012")
osArgs, outputFile := setupClient(t, tt.args, addr, cacheDir, tt.golden)

// Run Trivy client
err := execute(osArgs)
require.NoError(t, err)

want, err := os.ReadFile(tt.golden)
require.NoError(t, err)

got, err := os.ReadFile(outputFile)
require.NoError(t, err)
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)

assert.EqualValues(t, string(want), string(got))
runTest(t, osArgs, tt.golden, "", tt.args.Format, runOptions{})
})
}
}
Expand All @@ -425,21 +411,16 @@ func TestClientServerWithCycloneDX(t *testing.T) {
addr, cacheDir := setup(t, setupOptions{})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
uuid.SetFakeUUID(t, "3ff14136-e09f-4df9-80ea-%012d")

osArgs, outputFile := setupClient(t, tt.args, addr, cacheDir, tt.golden)

// Run Trivy client
err := execute(osArgs)
require.NoError(t, err)

compareCycloneDX(t, tt.golden, outputFile)
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)
runTest(t, osArgs, tt.golden, "", types.FormatCycloneDX, runOptions{
fakeUUID: "3ff14136-e09f-4df9-80ea-%012d",
})
})
}
}

func TestClientServerWithToken(t *testing.T) {
cases := []struct {
tests := []struct {
name string
args csArgs
golden string
Expand Down Expand Up @@ -481,20 +462,10 @@ func TestClientServerWithToken(t *testing.T) {
tokenHeader: serverTokenHeader,
})

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
osArgs, outputFile := setupClient(t, c.args, addr, cacheDir, c.golden)

// Run Trivy client
err := execute(osArgs)
if c.wantErr != "" {
require.Error(t, err, c.name)
assert.Contains(t, err.Error(), c.wantErr, c.name)
return
}

require.NoError(t, err, c.name)
compareReports(t, c.golden, outputFile, nil)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)
runTest(t, osArgs, tt.golden, "", types.FormatJSON, runOptions{wantErr: tt.wantErr})
})
}
}
Expand All @@ -517,25 +488,22 @@ func TestClientServerWithRedis(t *testing.T) {
golden := "testdata/alpine-39.json.golden"

t.Run("alpine 3.9", func(t *testing.T) {
osArgs, outputFile := setupClient(t, testArgs, addr, cacheDir, golden)
osArgs := setupClient(t, testArgs, addr, cacheDir, golden)

// Run Trivy client
err := execute(osArgs)
require.NoError(t, err)

compareReports(t, golden, outputFile, nil)
runTest(t, osArgs, golden, "", types.FormatJSON, runOptions{})
})

// Terminate the Redis container
require.NoError(t, redisC.Terminate(ctx))

t.Run("sad path", func(t *testing.T) {
osArgs, _ := setupClient(t, testArgs, addr, cacheDir, golden)
osArgs := setupClient(t, testArgs, addr, cacheDir, golden)

// Run Trivy client
err := execute(osArgs)
require.Error(t, err)
assert.Contains(t, err.Error(), "unable to store cache")
runTest(t, osArgs, "", "", types.FormatJSON, runOptions{
wantErr: "unable to store cache",
})
})
}

Expand Down Expand Up @@ -595,7 +563,7 @@ func setupServer(addr, token, tokenHeader, cacheDir, cacheBackend string) []stri
return osArgs
}

func setupClient(t *testing.T, c csArgs, addr string, cacheDir string, golden string) ([]string, string) {
func setupClient(t *testing.T, c csArgs, addr string, cacheDir string, golden string) []string {
if c.Command == "" {
c.Command = "image"
}
Expand All @@ -612,7 +580,7 @@ func setupClient(t *testing.T, c csArgs, addr string, cacheDir string, golden st
}

if c.Format != "" {
osArgs = append(osArgs, "--format", c.Format)
osArgs = append(osArgs, "--format", string(c.Format))
if c.TemplatePath != "" {
osArgs = append(osArgs, "--template", c.TemplatePath)
}
Expand Down Expand Up @@ -642,19 +610,11 @@ func setupClient(t *testing.T, c csArgs, addr string, cacheDir string, golden st
osArgs = append(osArgs, "--input", c.Input)
}

// Set up the output file
outputFile := filepath.Join(t.TempDir(), "output.json")
if *update {
outputFile = golden
}

osArgs = append(osArgs, "--output", outputFile)

if c.Target != "" {
osArgs = append(osArgs, c.Target)
}

return osArgs, outputFile
return osArgs
}

func setupRedis(t *testing.T, ctx context.Context) (testcontainers.Container, string) {
Expand Down
Loading

0 comments on commit 59e5433

Please sign in to comment.