MCPcopy
hub / github.com/hashicorp/packer / TestValidateCommand

Function TestValidateCommand

command/validate_test.go:16–70  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestValidateCommand(t *testing.T) {
17 tt := []struct {
18 path string
19 exitCode int
20 extraArgs []string
21 }{
22 {path: filepath.Join(testFixture("validate"), "build.json")},
23 {path: filepath.Join(testFixture("validate"), "build.pkr.hcl")},
24 {path: filepath.Join(testFixture("validate"), "build_with_vars.pkr.hcl")},
25 {path: filepath.Join(testFixture("validate-invalid"), "bad_provisioner.json"), exitCode: 1},
26 {path: filepath.Join(testFixture("validate-invalid"), "missing_build_block.pkr.hcl"), exitCode: 1},
27 {path: filepath.Join(testFixture("validate"), "null_var.json"), exitCode: 1},
28 {path: filepath.Join(testFixture("validate"), "var_foo_with_no_default.pkr.hcl"), exitCode: 1},
29
30 {path: testFixture("hcl", "validation", "wrong_pause_before.pkr.hcl"), exitCode: 1},
31
32 // wrong version fails
33 {path: filepath.Join(testFixture("version_req", "base_failure")), exitCode: 1},
34 {path: filepath.Join(testFixture("version_req", "base_success")), exitCode: 0},
35
36 // wrong version field
37 {path: filepath.Join(testFixture("version_req", "wrong_field_name")), exitCode: 1},
38
39 // wrong packer block type
40 {path: filepath.Join(testFixture("validate", "invalid_block_type.pkr.hcl")), exitCode: 1},
41
42 // wrong packer block
43 {path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1},
44
45 // Should return multiple errors,
46 {path: filepath.Join(testFixture("validate", "circular_error.pkr.hcl")), exitCode: 1},
47
48 // datasource could be unknown at that moment
49 {path: filepath.Join(testFixture("hcl", "data-source-validation.pkr.hcl")), exitCode: 0},
50
51 // datasource unknown at validation-time without datasource evaluation -> fail on provisioner
52 {path: filepath.Join(testFixture("hcl", "local-ds-validate.pkr.hcl")), exitCode: 1},
53 // datasource unknown at validation-time with datasource evaluation -> success
54 {path: filepath.Join(testFixture("hcl", "local-ds-validate.pkr.hcl")), exitCode: 0, extraArgs: []string{"--evaluate-datasources"}},
55 }
56
57 for _, tc := range tt {
58 t.Run(tc.path, func(t *testing.T) {
59 c := &ValidateCommand{
60 Meta: TestMetaFile(t),
61 }
62 tc := tc
63 args := tc.extraArgs
64 args = append(args, tc.path)
65 if code := c.Run(args); code != tc.exitCode {
66 fatalCommand(t, c.Meta)
67 }
68 })
69 }
70}
71
72func TestValidateCommand_SkipDatasourceExecution(t *testing.T) {
73 datasourceMock := &packersdk.MockDatasource{}

Callers

nothing calls this directly

Calls 5

RunMethod · 0.95
testFixtureFunction · 0.85
TestMetaFileFunction · 0.85
fatalCommandFunction · 0.85
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…