Skip to content

Commit ab8670b

Browse files
committed
add cli tests for source flag
1 parent e360f0a commit ab8670b

File tree

5 files changed

+147
-28
lines changed

5 files changed

+147
-28
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ dockers:
6767
use: buildx
6868
goarch: amd64
6969
image_templates:
70+
- docker.io/wagoodman/dive:latest
7071
- docker.io/wagoodman/dive:v{{.Version}}-amd64
7172
build_flag_templates:
7273
- "--build-arg=DOCKER_CLI_VERSION={{.Env.DOCKER_CLI_VERSION}}"

Taskfile.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ tasks:
202202
- |
203203
docker run \
204204
--rm \
205-
-t \
205+
-ti \
206206
-v /var/run/docker.sock:/var/run/docker.sock \
207207
'docker.io/wagoodman/dive:latest' \
208208
'{{ .TEST_IMAGE }}' \
@@ -213,6 +213,7 @@ tasks:
213213
cmds:
214214
- |
215215
docker run \
216+
--platform linux/amd64 \
216217
-v /var/run/docker.sock:/var/run/docker.sock \
217218
-v /${PWD}:/src \
218219
-w /src \
@@ -234,6 +235,7 @@ tasks:
234235
cmds:
235236
- |
236237
docker run \
238+
--platform linux/amd64 \
237239
-v /var/run/docker.sock:/var/run/docker.sock \
238240
-v /${PWD}:/src \
239241
-w /src \
@@ -326,6 +328,7 @@ tasks:
326328
deps: [tools, tmpdir]
327329
sources:
328330
- "**/*.go"
331+
- ".goreleaser.yaml"
329332
method: checksum
330333
cmds:
331334
- silent: true

cmd/dive/cli/cli_fetch_test.go

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,67 @@ import (
88
"testing"
99
)
1010

11-
func Test_FetchImage(t *testing.T) {
11+
func Test_LoadImage(t *testing.T) {
12+
image := "busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f"
13+
archive := repoPath(t, ".data/test-docker-image.tar")
1214

13-
t.Run("fetch from docker engine", func(t *testing.T) {
14-
rootCmd := getTestCommand(t, "docker://busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f")
15-
combined := Capture().WithStdout().WithStderr().Run(t, func() {
16-
require.NoError(t, rootCmd.Execute())
17-
})
15+
t.Run("from docker engine", func(t *testing.T) {
16+
runWithCombinedOutput(t, fmt.Sprintf("docker://%s", image))
17+
})
1818

19-
assert.Contains(t, combined, "Loading image")
20-
assert.Contains(t, combined, "Analyzing image")
21-
assert.Contains(t, combined, "Evaluating image")
19+
t.Run("from docker engine (flag)", func(t *testing.T) {
2220

23-
snaps.MatchSnapshot(t, combined)
21+
runWithCombinedOutput(t, fmt.Sprintf("--source docker %s", image))
2422
})
2523

26-
t.Run("fetch from podman engine", func(t *testing.T) {
24+
t.Run("from podman engine", func(t *testing.T) {
2725
if _, err := exec.LookPath("podman"); err != nil {
2826
t.Skip("podman not installed, skipping test")
2927
}
28+
// pull the image from podman first
29+
require.NoError(t, exec.Command("podman", "pull", image).Run())
30+
31+
runWithCombinedOutput(t, fmt.Sprintf("podman://%s", image))
32+
})
3033

31-
image := "busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f"
34+
t.Run("from podman engine (flag)", func(t *testing.T) {
35+
if _, err := exec.LookPath("podman"); err != nil {
36+
t.Skip("podman not installed, skipping test")
37+
}
3238

3339
// pull the image from podman first
3440
require.NoError(t, exec.Command("podman", "pull", image).Run())
3541

36-
rootCmd := getTestCommand(t, fmt.Sprintf("podman://%s", image))
37-
combined := Capture().WithStdout().WithStderr().Run(t, func() {
38-
require.NoError(t, rootCmd.Execute())
39-
})
42+
runWithCombinedOutput(t, fmt.Sprintf("--source podman %s", image))
43+
})
4044

41-
assert.Contains(t, combined, "Loading image")
42-
assert.Contains(t, combined, "Analyzing image")
43-
assert.Contains(t, combined, "Evaluating image")
45+
t.Run("from archive", func(t *testing.T) {
46+
runWithCombinedOutput(t, fmt.Sprintf("docker-archive://%s", archive))
47+
})
4448

45-
snaps.MatchSnapshot(t, combined)
49+
t.Run("from archive (flag)", func(t *testing.T) {
50+
runWithCombinedOutput(t, fmt.Sprintf("--source docker-archive %s", archive))
4651
})
4752
}
4853

54+
func runWithCombinedOutput(t testing.TB, cmd string) {
55+
t.Helper()
56+
rootCmd := getTestCommand(t, cmd)
57+
combined := Capture().WithStdout().WithStderr().Run(t, func() {
58+
require.NoError(t, rootCmd.Execute())
59+
})
60+
61+
assertLoadOutput(t, combined)
62+
}
63+
64+
func assertLoadOutput(t testing.TB, combined string) {
65+
t.Helper()
66+
assert.Contains(t, combined, "Loading image")
67+
assert.Contains(t, combined, "Analyzing image")
68+
assert.Contains(t, combined, "Evaluating image")
69+
snaps.MatchSnapshot(t, combined)
70+
}
71+
4972
func Test_FetchFailure(t *testing.T) {
5073
t.Run("nonexistent image", func(t *testing.T) {
5174
rootCmd := getTestCommand(t, "docker:wagoodman/nonexistent/image:tag")

cmd/dive/cli/internal/options/analysis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func DefaultAnalysis() Analysis {
2929
return Analysis{
3030
ContainerEngine: defaultContainerEngine,
3131
IgnoreErrors: false,
32-
AvailableContainerEngines: []string{"docker", "podman"},
32+
AvailableContainerEngines: dive.ImageSources,
3333
}
3434
}
3535

cmd/dive/cli/testdata/snapshots/cli_fetch_test.snap

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11

2-
[Test_FetchImage/fetch_from_docker_engine - 1]
2+
[Test_FetchFailure/nonexistent_image - 1]
3+
Loading image docker:wagoodman/nonexistent/image:tag
4+
5+
---
6+
7+
[Test_FetchFailure/invalid_image_name - 1]
8+
Loading image /wagoodman/invalid:image:format
9+
10+
---
11+
12+
[Test_LoadImage/from_docker_engine - 1]
313
Loading image busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f
414
Analyzing image [layers:1 files:441 size:4.3 MB]
515
Evaluating image [rules: 3]
@@ -20,17 +30,53 @@ PASS [pass:3]
2030

2131
---
2232

23-
[Test_FetchFailure/nonexistent_image - 1]
24-
Loading image docker:wagoodman/nonexistent/image:tag
33+
[Test_LoadImage/from_podman_engine - 1]
34+
Loading image busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f
35+
Analyzing image [layers:1 files:441 size:4.3 MB]
36+
Evaluating image [rules: 3]
37+
38+
Analysis:
39+
efficiency: 100.00 %
40+
wastedBytes: 0 bytes
41+
userWastedPercent: 0 %
42+
43+
Inefficient Files: (None)
44+
45+
Evaluation:
46+
PASS highestUserWastedPercent (0.90)
47+
PASS highestWastedBytes (20MB)
48+
PASS lowestEfficiency (0.9)
49+
50+
PASS [pass:3]
2551

2652
---
2753

28-
[Test_FetchFailure/invalid_image_name - 1]
29-
Loading image /wagoodman/invalid:image:format
54+
[Test_LoadImage/from_archive - 1]
55+
Loading image /Users/wagoodman/code/dive/.data/test-docker-image.tar
56+
Analyzing image [layers:14 files:451 size:1.2 MB]
57+
Evaluating image [rules: 3]
58+
59+
Analysis:
60+
efficiency: 98.44 %
61+
wastedBytes: 32025 bytes (32 kB)
62+
userWastedPercent: 48.35 %
63+
64+
Inefficient Files:
65+
Count Wasted Space File Path
66+
2 13 kB /root/saved.txt
67+
2 13 kB /root/example/somefile1.txt
68+
2 6.4 kB /root/example/somefile3.txt
69+
70+
Evaluation:
71+
PASS highestUserWastedPercent (0.90)
72+
PASS highestWastedBytes (20MB)
73+
PASS lowestEfficiency (0.9)
74+
75+
PASS [pass:3]
3076

3177
---
3278

33-
[Test_FetchImage/fetch_from_podman_engine - 1]
79+
[Test_LoadImage/from_docker_engine_(flag) - 1]
3480
Loading image busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f
3581
Analyzing image [layers:1 files:441 size:4.3 MB]
3682
Evaluating image [rules: 3]
@@ -50,3 +96,49 @@ Evaluation:
5096
PASS [pass:3]
5197

5298
---
99+
100+
[Test_LoadImage/from_podman_engine_(flag) - 1]
101+
Loading image busybox:1.37.0@sha256:ad9fa4d07136a83e69a54ef00102f579d04eba431932de3b0f098cc5d5948f9f
102+
Analyzing image [layers:1 files:441 size:4.3 MB]
103+
Evaluating image [rules: 3]
104+
105+
Analysis:
106+
efficiency: 100.00 %
107+
wastedBytes: 0 bytes
108+
userWastedPercent: 0 %
109+
110+
Inefficient Files: (None)
111+
112+
Evaluation:
113+
PASS highestUserWastedPercent (0.90)
114+
PASS highestWastedBytes (20MB)
115+
PASS lowestEfficiency (0.9)
116+
117+
PASS [pass:3]
118+
119+
---
120+
121+
[Test_LoadImage/from_archive_(flag) - 1]
122+
Loading image /Users/wagoodman/code/dive/.data/test-docker-image.tar
123+
Analyzing image [layers:14 files:451 size:1.2 MB]
124+
Evaluating image [rules: 3]
125+
126+
Analysis:
127+
efficiency: 98.44 %
128+
wastedBytes: 32025 bytes (32 kB)
129+
userWastedPercent: 48.35 %
130+
131+
Inefficient Files:
132+
Count Wasted Space File Path
133+
2 13 kB /root/saved.txt
134+
2 13 kB /root/example/somefile1.txt
135+
2 6.4 kB /root/example/somefile3.txt
136+
137+
Evaluation:
138+
PASS highestUserWastedPercent (0.90)
139+
PASS highestWastedBytes (20MB)
140+
PASS lowestEfficiency (0.9)
141+
142+
PASS [pass:3]
143+
144+
---

0 commit comments

Comments
 (0)