Skip to content

Commit

Permalink
refactor: integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed Dec 31, 2023
1 parent 82005e6 commit f10d5d1
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 360 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: 30 additions & 68 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,17 +16,16 @@ 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/clock"
"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 @@ -266,19 +266,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 @@ -392,19 +388,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)
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)

got, err := os.ReadFile(outputFile)
require.NoError(t, err)

assert.EqualValues(t, string(want), string(got))
runTest(t, osArgs, tt.golden, "", tt.args.Format, runOptions{})
})
}
}
Expand All @@ -428,22 +414,18 @@ func TestClientServerWithCycloneDX(t *testing.T) {
addr, cacheDir := setup(t, setupOptions{})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clock.SetFakeTime(t, time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC))
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)
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)

compareCycloneDX(t, tt.golden, outputFile)
runTest(t, osArgs, tt.golden, "", types.FormatCycloneDX, runOptions{
fakeTime: time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC),
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 @@ -485,20 +467,11 @@ 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
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
osArgs := setupClient(t, tt.args, addr, cacheDir, tt.golden)

require.NoError(t, err, c.name)
compareReports(t, c.golden, outputFile, nil)
runTest(t, osArgs, tt.golden, "", types.FormatJSON, runOptions{wantErr: tt.wantErr})
})
}
}
Expand All @@ -521,25 +494,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 @@ -599,7 +569,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 @@ -616,7 +586,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 @@ -646,19 +616,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
56 changes: 30 additions & 26 deletions integration/docker_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package integration

import (
"context"
"github.com/aquasecurity/trivy/pkg/types"
"io"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -40,18 +40,24 @@ func TestDockerEngine(t *testing.T) {
golden: "testdata/alpine-39.json.golden",
},
{
name: "alpine:3.9, with high and critical severity",
severity: []string{"HIGH", "CRITICAL"},
name: "alpine:3.9, with high and critical severity",
severity: []string{
"HIGH",
"CRITICAL",
},
imageTag: "ghcr.io/aquasecurity/trivy-test-images:alpine-39",
input: "testdata/fixtures/images/alpine-39.tar.gz",
golden: "testdata/alpine-39-high-critical.json.golden",
},
{
name: "alpine:3.9, with .trivyignore",
imageTag: "ghcr.io/aquasecurity/trivy-test-images:alpine-39",
ignoreIDs: []string{"CVE-2019-1549", "CVE-2019-14697"},
input: "testdata/fixtures/images/alpine-39.tar.gz",
golden: "testdata/alpine-39-ignore-cveids.json.golden",
name: "alpine:3.9, with .trivyignore",
imageTag: "ghcr.io/aquasecurity/trivy-test-images:alpine-39",
ignoreIDs: []string{
"CVE-2019-1549",
"CVE-2019-14697",
},
input: "testdata/fixtures/images/alpine-39.tar.gz",
golden: "testdata/alpine-39-ignore-cveids.json.golden",
},
{
name: "alpine:3.10",
Expand Down Expand Up @@ -246,24 +252,32 @@ func TestDockerEngine(t *testing.T) {
require.NoError(t, err, tt.name)
}

tmpDir := t.TempDir()
output := filepath.Join(tmpDir, "result.json")

osArgs := []string{"--cache-dir", cacheDir, "image",
"--skip-update", "--format=json", "--output", output}
osArgs := []string{
"--cache-dir",
cacheDir,
"image",
"--skip-update",
"--format=json",
}

if tt.ignoreUnfixed {
osArgs = append(osArgs, "--ignore-unfixed")
}

if len(tt.ignoreStatus) != 0 {
osArgs = append(osArgs,
[]string{"--ignore-status", strings.Join(tt.ignoreStatus, ",")}...,
[]string{
"--ignore-status",
strings.Join(tt.ignoreStatus, ","),
}...,
)
}
if len(tt.severity) != 0 {
osArgs = append(osArgs,
[]string{"--severity", strings.Join(tt.severity, ",")}...,
[]string{
"--severity",
strings.Join(tt.severity, ","),
}...,
)
}
if len(tt.ignoreIDs) != 0 {
Expand All @@ -275,17 +289,7 @@ func TestDockerEngine(t *testing.T) {
osArgs = append(osArgs, tt.input)

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

assert.NoError(t, err, tt.name)

// check for vulnerability output info
compareReports(t, tt.golden, output, nil)
runTest(t, osArgs, tt.golden, "", types.FormatJSON, runOptions{wantErr: tt.wantErr})

// cleanup
_, err = cli.ImageRemove(ctx, tt.input, api.ImageRemoveOptions{
Expand Down
Loading

0 comments on commit f10d5d1

Please sign in to comment.