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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
package command
import (
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestValidateCommand(t *testing.T) {
tt := []struct {
path string
exitCode int
}{
{path: filepath.Join(testFixture("validate"), "build.json")},
{path: filepath.Join(testFixture("validate"), "build.pkr.hcl")},
{path: filepath.Join(testFixture("validate"), "build_with_vars.pkr.hcl")},
{path: filepath.Join(testFixture("validate-invalid"), "bad_provisioner.json"), exitCode: 1},
{path: filepath.Join(testFixture("validate-invalid"), "missing_build_block.pkr.hcl"), exitCode: 1},
{path: filepath.Join(testFixture("validate"), "null_var.json"), exitCode: 1},
{path: filepath.Join(testFixture("validate"), "var_foo_with_no_default.pkr.hcl"), exitCode: 1},
// wrong version fails
{path: filepath.Join(testFixture("version_req", "base_failure")), exitCode: 1},
{path: filepath.Join(testFixture("version_req", "base_success")), exitCode: 0},
// wrong version field
{path: filepath.Join(testFixture("version_req", "wrong_field_name")), exitCode: 1},
// wrong packer block
{path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1},
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
Meta: testMetaFile(t),
}
tc := tc
args := []string{tc.path}
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
})
}
}
func TestValidateCommand_SyntaxOnly(t *testing.T) {
tt := []struct {
path string
exitCode int
}{
{path: filepath.Join(testFixture("validate"), "build.json")},
{path: filepath.Join(testFixture("validate"), "build.pkr.hcl")},
{path: filepath.Join(testFixture("validate"), "build_with_vars.pkr.hcl")},
{path: filepath.Join(testFixture("validate-invalid"), "bad_provisioner.json")},
{path: filepath.Join(testFixture("validate-invalid"), "missing_build_block.pkr.hcl")},
{path: filepath.Join(testFixture("validate-invalid"), "broken.json"), exitCode: 1},
{path: filepath.Join(testFixture("validate"), "null_var.json")},
{path: filepath.Join(testFixture("validate"), "var_foo_with_no_default.pkr.hcl")},
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
Meta: testMetaFile(t),
}
c.CoreConfig.Version = "102.0.0"
tc := tc
args := []string{"-syntax-only", tc.path}
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
})
}
}
func TestValidateCommandOKVersion(t *testing.T) {
c := &ValidateCommand{
Meta: testMetaFile(t),
}
args := []string{
filepath.Join(testFixture("validate"), "template.json"),
}
// This should pass with a valid configuration version
c.CoreConfig.Version = "102.0.0"
if code := c.Run(args); code != 0 {
fatalCommand(t, c.Meta)
}
}
func TestValidateCommandBadVersion(t *testing.T) {
c := &ValidateCommand{
Meta: testMetaFile(t),
}
args := []string{
filepath.Join(testFixture("validate"), "template.json"),
}
// This should fail with an invalid configuration version
c.CoreConfig.Version = "100.0.0"
if code := c.Run(args); code != 1 {
t.Errorf("Expected exit code 1")
}
stdout, stderr := outputCommand(t, c.Meta)
expected := `Error:
This template requires Packer version 101.0.0 or higher; using 100.0.0
`
if diff := cmp.Diff(expected, stderr); diff != "" {
t.Errorf("Unexpected output: %s", diff)
}
t.Log(stdout)
}
func TestValidateCommandExcept(t *testing.T) {
tt := []struct {
name string
args []string
exitCode int
}{
{
name: "JSON: validate except build and post-processor",
args: []string{
"-except=vanilla,pear",
filepath.Join(testFixture("validate"), "validate_except.json"),
},
},
{
name: "JSON: fail validate except build and post-processor",
args: []string{
"-except=chocolate,apple",
filepath.Join(testFixture("validate"), "validate_except.json"),
},
exitCode: 1,
},
{
name: "HCL2: validate except build and post-processor",
args: []string{
"-except=file.vanilla,pear",
filepath.Join(testFixture("validate"), "validate_except.pkr.hcl"),
},
},
{
name: "HCL2: fail validation except build and post-processor",
args: []string{
"-except=file.chocolate,apple",
filepath.Join(testFixture("validate"), "validate_except.pkr.hcl"),
},
exitCode: 1,
},
}
c := &ValidateCommand{
Meta: testMetaFile(t),
}
c.CoreConfig.Version = "102.0.0"
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
defer cleanup()
tc := tc
if code := c.Run(tc.args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
})
}
}
|