README
¶
Binary end-to-end tests
These tests run the scanner binary from bin/, the way a user would. They check
its exit code, its output and the files it generates.
Run
make test-e2e # builds bin/datadog-iac-scanner, then runs the suite
go test ./e2e -count=1 -timeout 1500s # same, with an existing build
go test ./e2e -count=1 -run Test_E2E_Config # one group
- Skips. Without a binary in
bin/, every test is skipped.-shortskips them too. - Network. The scans fetch their rules from the Datadog API, so the tests need network access.
- CI.
.github/workflows/go-e2e.yamlbuilds the binary and runs this suite, but the job ignores test failures (|| cat results.json). Check the results yourself.
Layout
cli_test.goruns every case registered intestcases/, in parallel.config_test.gochecks the exit codes for configuration errors:- an invalid local configuration exits with
127; - an unreachable Datadog backend exits with
126.
- an invalid local configuration exits with
testcases/has one file per case (e2e-cli-NNN_<name>.go). Each file'sinit()appends aTestCasetotestcases.Tests.fixtures/holds the inputs (samples/), the expected files (for exampleE2E_CLI_005_PAYLOAD.json) and the JSON schemas (schemas/).utils/runs the binary and compares outputs.
Writing a test case
// E2E-CLI-005 - scan with --payload-path writes the payload of the scanned files.
func init() { //nolint
Tests = append(Tests, TestCase{
Name: "should create a payload file [E2E-CLI-005]",
Args: args{
Args: []cmdArgs{
[]string{"scan", "-p", "/path/e2e/fixtures/samples/terraform.tf",
"--payload-path", "/path/e2e/output/E2E_CLI_005_PAYLOAD.json"},
},
ExpectedPayload: []string{"E2E_CLI_005_PAYLOAD.json"},
},
WantStatus: []int{50},
})
}
- Commands.
Argsholds one or more command lines. Every command line needs an entry inWantStatus. It also gets one in eachExpected*field you use. - Paths. A path starting with
/path/e2e/is rewritten to thee2e/directory. Any other/path/<dir>/prefix is rewritten to the repository root (seeutils.DevPathAdapter). - Generated files. Write them under
/path/e2e/output/and put the case ID in their name. Cases run in parallel, andoutput/is deleted after the run. - Checks. Only the exit code is required.
WantStatusis the exit code of each command.0means no finding;20to60give the highest severity found, from INFO to CRITICAL.Validationis a function that receives the whole output, joined with;.ExpectedOutputFuncis a function per command that receives its output, joined with newlines.ExpectedPayloadcomparesoutput/<file>withfixtures/<file>.ExpectedResulttakes oneResultsValidationper command, for example{ResultsFile: "datadog-iac-scanner-result", ResultsFormats: []string{"sarif"}}(seetestcases/e2e-cli-030_output-path.go).- When
fixtures/<ResultsFile>.jsonexists, the runner comparesoutput/<ResultsFile>.jsonwith it. - The runner validates
output/<ResultsFile>.sarifagainstfixtures/schemas/result-sarif.jsononly ifResultsFormatscontains"sarif". Without it, the SARIF is not checked.
- When
ExpectedLogis aLogValidation{LogFile: "<log file>", ValidationFunc: ...}. The function receives the content ofoutput/<log file>. The check runs only whenValidationFuncis set.
Click to show internal directories.
Click to hide internal directories.