github.com/khulnasoft/trivy@v0.48.1-0.20231207234930-27df843a75e0/integration/vm_test.go (about) 1 //go:build vm_integration 2 3 package integration 4 5 import ( 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/khulnasoft/trivy/internal/testutil" 13 "github.com/khulnasoft/trivy/pkg/types" 14 ) 15 16 func TestVM(t *testing.T) { 17 type args struct { 18 input string 19 format string 20 artifactType string 21 } 22 tests := []struct { 23 name string 24 args args 25 golden string 26 override types.Report 27 }{ 28 { 29 name: "amazon linux 2 in VMDK, filesystem XFS", 30 args: args{ 31 input: "testdata/fixtures/vm-images/amazon-2.vmdk.gz", 32 format: "json", 33 artifactType: "vm", 34 }, 35 golden: "testdata/amazonlinux2-gp2-x86-vm.json.golden", 36 }, 37 { 38 name: "amazon linux 2 in Snapshot, filesystem XFS", 39 args: args{ 40 input: "testdata/fixtures/vm-images/amazon-2.img.gz", 41 format: "json", 42 artifactType: "vm", 43 }, 44 golden: "testdata/amazonlinux2-gp2-x86-vm.json.golden", 45 }, 46 { 47 name: "Ubuntu in Snapshot, filesystem EXT4", 48 args: args{ 49 input: "testdata/fixtures/vm-images/ubuntu-2204.img.gz", 50 format: "json", 51 artifactType: "vm", 52 }, 53 golden: "testdata/ubuntu-gp2-x86-vm.json.golden", 54 }, 55 { 56 name: "Ubuntu in VMDK, filesystem EXT4", 57 args: args{ 58 input: "testdata/fixtures/vm-images/ubuntu-2204.vmdk.gz", 59 format: "json", 60 artifactType: "vm", 61 }, 62 golden: "testdata/ubuntu-gp2-x86-vm.json.golden", 63 }, 64 } 65 66 // Set up testing DB 67 cacheDir := initDB(t) 68 69 // Keep the current working directory 70 currentDir, err := os.Getwd() 71 require.NoError(t, err) 72 73 const imageFile = "disk.img" 74 75 for _, tt := range tests { 76 t.Run(tt.name, func(t *testing.T) { 77 osArgs := []string{ 78 "--cache-dir", 79 cacheDir, 80 "vm", 81 "--scanners", 82 "vuln", 83 "-q", 84 "--skip-db-update", 85 "--format", 86 tt.args.format, 87 } 88 89 tmpDir := t.TempDir() 90 91 // Set up the output file 92 outputFile := filepath.Join(tmpDir, "output.json") 93 if *update { 94 outputFile = filepath.Join(currentDir, tt.golden) 95 } 96 97 // Get the absolute path of the golden file 98 goldenFile, err := filepath.Abs(tt.golden) 99 require.NoError(t, err) 100 101 // Decompress the gzipped image file 102 imagePath := filepath.Join(tmpDir, imageFile) 103 testutil.DecompressSparseGzip(t, tt.args.input, imagePath) 104 105 // Change the current working directory so that targets in the result could be the same as golden files. 106 err = os.Chdir(tmpDir) 107 require.NoError(t, err) 108 defer os.Chdir(currentDir) 109 110 osArgs = append(osArgs, "--output", outputFile) 111 osArgs = append(osArgs, imageFile) 112 113 // Run "trivy vm" 114 err = execute(osArgs) 115 require.NoError(t, err) 116 compareReports(t, goldenFile, outputFile, nil) 117 }) 118 } 119 }