Skip to content

Commit

Permalink
fix(integration-test): use a snapshot database for Docker mode (#352)
Browse files Browse the repository at this point in the history
* fix(integration): add a binary name

The first argument is used for the program name. --skip-update was
ignored.

* fix(integration): use a snapshot database

After a new vulnerability is found, this test fails

* chore(integration): add t.Run
  • Loading branch information
knqyf263 authored Dec 30, 2019
1 parent 7abd416 commit 0b96d08
Showing 1 changed file with 59 additions and 58 deletions.
117 changes: 59 additions & 58 deletions integration/docker_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,70 +38,71 @@ func TestRun_WithDockerEngine(t *testing.T) {
}

for _, tc := range testCases {
ctx := context.Background()
defer ctx.Done()
t.Run(tc.name, func(t *testing.T) {
// Copy DB file
cacheDir := gunzipDB()
defer os.RemoveAll(cacheDir)

cli, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err, tc.name)
ctx := context.Background()
defer ctx.Done()

if !tc.invalidImage {
testfile, err := os.Open(tc.testfile)
cli, err := client.NewClientWithOpts(client.FromEnv)
require.NoError(t, err, tc.name)

// ensure image doesnt already exists
_, _ = cli.ImageRemove(ctx, tc.testfile, types.ImageRemoveOptions{
Force: true,
PruneChildren: true,
})
if !tc.invalidImage {
testfile, err := os.Open(tc.testfile)
require.NoError(t, err, tc.name)

// load image into docker engine
_, err = cli.ImageLoad(ctx, testfile, true)
require.NoError(t, err, tc.name)
// ensure image doesnt already exists
_, _ = cli.ImageRemove(ctx, tc.testfile, types.ImageRemoveOptions{
Force: true,
PruneChildren: true,
})

// load image into docker engine
_, err = cli.ImageLoad(ctx, testfile, true)
require.NoError(t, err, tc.name)

// tag our image to something unique
err = cli.ImageTag(ctx, "alpine:3.10", tc.testfile)
require.NoError(t, err, tc.name)
}

// tag our image to something unique
err = cli.ImageTag(ctx, "alpine:3.10", tc.testfile)
of, err := ioutil.TempFile("", "integration-docker-engine-output-file-*")
require.NoError(t, err, tc.name)
}

// run trivy
tmpDir, err := ioutil.TempDir("", "integration-docker-engine-*")
require.NoError(t, err)
defer func() {
os.RemoveAll(tmpDir)
}()

of, err := ioutil.TempFile(tmpDir, "integration-docker-engine-output-file-*")
require.NoError(t, err, tc.name)
app := internal.NewApp("dev")

trivyArgs := []string{"--skip-update", "--quiet", "--cache-dir", tmpDir, "--format=json"}
if !tc.invalidImage {
trivyArgs = append(trivyArgs, "--output", of.Name())
}
trivyArgs = append(trivyArgs, tc.testfile)

err = app.Run(trivyArgs)
switch {
case tc.expectedError != "":
assert.Equal(t, tc.expectedError, err.Error(), tc.name)
default:
assert.NoError(t, err, tc.name)
}

if !tc.invalidImage {
// check for vulnerability output info
got, err := ioutil.ReadAll(of)
assert.NoError(t, err, tc.name)
want, err := ioutil.ReadFile(tc.expectedOutputFile)
assert.NoError(t, err, tc.name)
assert.JSONEq(t, string(want), string(got), tc.name)

// cleanup
_, err = cli.ImageRemove(ctx, tc.testfile, types.ImageRemoveOptions{
Force: true,
PruneChildren: true,
})
assert.NoError(t, err, tc.name)
}
defer os.Remove(of.Name())

// run trivy
app := internal.NewApp("dev")
trivyArgs := []string{"trivy", "--skip-update", "--cache-dir", cacheDir, "--format=json"}
if !tc.invalidImage {
trivyArgs = append(trivyArgs, "--output", of.Name())
}
trivyArgs = append(trivyArgs, tc.testfile)

err = app.Run(trivyArgs)
switch {
case tc.expectedError != "":
assert.Equal(t, tc.expectedError, err.Error(), tc.name)
default:
assert.NoError(t, err, tc.name)
}

if !tc.invalidImage {
// check for vulnerability output info
got, err := ioutil.ReadAll(of)
assert.NoError(t, err, tc.name)
want, err := ioutil.ReadFile(tc.expectedOutputFile)
assert.NoError(t, err, tc.name)
assert.JSONEq(t, string(want), string(got), tc.name)

// cleanup
_, err = cli.ImageRemove(ctx, tc.testfile, types.ImageRemoveOptions{
Force: true,
PruneChildren: true,
})
assert.NoError(t, err, tc.name)
}
})
}
}

0 comments on commit 0b96d08

Please sign in to comment.