-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathvm_test.go
101 lines (91 loc) · 2.29 KB
/
vm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//go:build vm_integration
package integration
import (
"path/filepath"
"strings"
"testing"
"github.com/aquasecurity/trivy/internal/testutil"
"github.com/aquasecurity/trivy/pkg/types"
)
func TestVM(t *testing.T) {
type args struct {
input string
format string
artifactType string
}
tests := []struct {
name string
args args
golden string
override types.Report
}{
{
name: "amazon linux 2 in VMDK, filesystem XFS",
args: args{
input: "testdata/fixtures/vm-images/amazon-2.vmdk.gz",
format: "json",
artifactType: "vm",
},
golden: "testdata/amazonlinux2-gp2-x86-vm.json.golden",
},
{
name: "amazon linux 2 in Snapshot, filesystem XFS",
args: args{
input: "testdata/fixtures/vm-images/amazon-2.img.gz",
format: "json",
artifactType: "vm",
},
golden: "testdata/amazonlinux2-gp2-x86-vm.json.golden",
},
{
name: "Ubuntu in Snapshot, filesystem EXT4",
args: args{
input: "testdata/fixtures/vm-images/ubuntu-2204.img.gz",
format: "json",
artifactType: "vm",
},
golden: "testdata/ubuntu-gp2-x86-vm.json.golden",
},
{
name: "Ubuntu in VMDK, filesystem EXT4",
args: args{
input: "testdata/fixtures/vm-images/ubuntu-2204.vmdk.gz",
format: "json",
artifactType: "vm",
},
golden: "testdata/ubuntu-gp2-x86-vm.json.golden",
},
}
// Set up testing DB
cacheDir := initDB(t)
const imageFile = "disk.img"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
osArgs := []string{
"--cache-dir",
cacheDir,
"vm",
"--scanners",
"vuln",
"-q",
"--skip-db-update",
"--format",
tt.args.format,
}
// Decompress the gzipped image file
imagePath := filepath.Join(t.TempDir(), imageFile)
testutil.DecompressSparseGzip(t, tt.args.input, imagePath)
osArgs = append(osArgs, imagePath)
// Run "trivy vm"
runTest(t, osArgs, tt.golden, "", types.FormatJSON, runOptions{
override: overrideFuncs(overrideUID, func(t *testing.T, _, got *types.Report) {
got.ArtifactName = "disk.img"
for i := range got.Results {
lastIndex := strings.LastIndex(got.Results[i].Target, "/")
got.Results[i].Target = got.Results[i].Target[lastIndex+1:]
}
}),
})
})
}
}