integration/README.md
This directory contains integration tests for Trivy. These tests verify Trivy's behavior by running actual commands and comparing the output against golden files.
# Run standard integration tests (excludes VM, K8s, and module tests)
mage test:integration
# Run all types of integration tests separately
mage test:integration # Standard integration tests
mage test:module # Wasm module tests
mage test:vm # VM integration tests
mage test:k8s # Kubernetes integration tests
GOEXPERIMENT=jsonv2 go test -tags=integration -run TestRepository ./integration -v
Golden files store the expected output for integration tests. They are located in integration/testdata/*.golden.
When you make changes that affect test output, you need to update the golden files:
# Update golden files for standard integration tests
mage test:updateGolden
# Update golden files for Wasm module tests
mage test:updateModuleGolden
# Update golden files for VM integration tests
mage test:updateVMGolden
# Update specific golden files manually
GOEXPERIMENT=jsonv2 go test -tags=integration -run TestRepository ./integration -v -update
Important:
-update flagoverride: nil comment in test code to identify canonical source testsThese tests generate golden files and should have:
override: nil comment in the codet.Skipf() for the -update flagExample:
func TestRepository(t *testing.T) {
// ...
runTest(t, osArgs, tt.golden, format, runOptions{
fakeUUID: "3ff14136-e09f-4df9-80ea-%012d",
override: nil, // Do not use overrides - golden files are generated from this test as the canonical source
})
}
These tests reuse golden files from canonical source tests and should have:
if *update { t.Skipf(...) } at the beginning of the test functionoverride functions to adjust for differences (e.g., different artifact names, paths)Golden files are shared with TestXXX.Example:
// TestClientServer tests the client-server mode of Trivy.
//
// Golden files are shared with TestTar or TestRepository.
func TestClientServer(t *testing.T) {
if *update {
t.Skipf("Skipping TestClientServer when -update flag is set. Golden files should be updated via TestTar or TestRepository.")
}
// ...
runTest(t, osArgs, tt.golden, types.FormatJSON, runOptions{
override: overrideFuncs(overrideUID, func(_ *testing.T, want, _ *types.Report) {
want.ArtifactName = "https://github.com/knqyf263/trivy-ci-test"
}),
fakeUUID: "3ff14136-e09f-4df9-80ea-%012d",
})
}
Critical constraint: Each golden file must be updated by exactly one test function.
If multiple tests update the same golden file, they may introduce subtle differences in the output. This causes the golden file to change every time tests are run, depending on which test executed last. This makes the golden files unstable and defeats their purpose.
Solution: Designate one test as the "canonical source" for each golden file. Other tests that want to verify equivalent results share the golden file in read-only mode (with t.Skipf() during updates).
Share golden files between tests when you want to verify that different commands, flags, or configurations produce equivalent results with the same output format:
Good reasons to share:
Use override functions to handle:
Example: TestTar generates golden files for image scanning, and these are reused by:
All of these produce the same JSON format with the same vulnerability data, but with different artifact names and metadata.
The test framework automatically validates that:
*update == true) cannot use override functionsIf you try to update a golden file with an override function, the test will fail with:
invalid test configuration: cannot use override functions when update=true
Tests are organized by functionality:
standalone_tar_test.go - Container image scanning from tar archivesrepo_test.go - Repository and filesystem scanningsbom_test.go - SBOM scanning and generationclient_server_test.go - Client-server modedocker_engine_test.go - Docker Engine API integrationregistry_test.go - Container registry integrationconfig_test.go - Configuration handling (CLI flags, env vars, config files)vm_test.go - Virtual machine image scanningmodule_test.go - Wasm module integrationintegration/testdata/
├── *.golden # Golden files (expected test outputs)
└── fixtures/ # Test input files
├── images/ # Container images (auto-downloaded)
├── vm-images/ # VM images (auto-downloaded)
├── repo/ # Repository and filesystem test data
├── sbom/ # SBOM test files
└── ...
Important: testdata/fixtures/images/ and testdata/fixtures/vm-images/ are automatically downloaded by mage commands:
mage test:integration downloads container imagesmage test:vm downloads VM imagesIf you run tests directly with go test without using mage commands, these fixtures will not be present and tests will fail. Use mage commands to ensure fixtures are properly set up.
override: nil)if *update { t.Skipf(...) } - this prevents updatesmage test:updateGolden: This automatically updates all golden files from canonical source testsoverride: nil comments: Clearly mark canonical source tests in the code