2015-07-13 17:57:35 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-07-24 04:58:03 -04:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-01-20 04:37:16 -05:00
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2021-02-02 12:05:04 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2015-07-13 17:57:35 -04:00
|
|
|
)
|
|
|
|
|
2020-06-02 14:58:33 -04:00
|
|
|
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},
|
2020-07-24 04:58:03 -04:00
|
|
|
{path: filepath.Join(testFixture("validate"), "null_var.json"), exitCode: 1},
|
|
|
|
{path: filepath.Join(testFixture("validate"), "var_foo_with_no_default.pkr.hcl"), exitCode: 1},
|
2020-10-27 05:03:36 -04:00
|
|
|
|
2021-04-26 11:31:22 -04:00
|
|
|
{path: testFixture("hcl", "validation", "wrong_pause_before.pkr.hcl"), exitCode: 1},
|
|
|
|
|
2020-10-27 05:03:36 -04:00
|
|
|
// 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},
|
2020-11-23 10:27:26 -05:00
|
|
|
|
|
|
|
// wrong packer block
|
|
|
|
{path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1},
|
2020-06-02 14:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.path, func(t *testing.T) {
|
2020-07-24 04:58:03 -04:00
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
2020-06-02 14:58:33 -04:00
|
|
|
tc := tc
|
|
|
|
args := []string{tc.path}
|
|
|
|
if code := c.Run(args); code != tc.exitCode {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 04:37:16 -05:00
|
|
|
func TestValidateCommand_SkipDatasourceExecution(t *testing.T) {
|
|
|
|
datasourceMock := &packersdk.MockDatasource{}
|
|
|
|
meta := testMetaFile(t)
|
2021-02-02 12:05:04 -05:00
|
|
|
meta.CoreConfig.Components.PluginConfig.DataSources = packer.MapOfDatasource{
|
2021-01-20 04:37:16 -05:00
|
|
|
"mock": func() (packersdk.Datasource, error) {
|
|
|
|
return datasourceMock, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: meta,
|
|
|
|
}
|
|
|
|
args := []string{filepath.Join(testFixture("validate"), "datasource.pkr.hcl")}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
if datasourceMock.ExecuteCalled {
|
|
|
|
t.Fatalf("Datasource should not be executed on validation")
|
|
|
|
}
|
|
|
|
if !datasourceMock.OutputSpecCalled {
|
|
|
|
t.Fatalf("Datasource OutPutSpec should be called on validation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:58:33 -04:00
|
|
|
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},
|
2020-07-24 04:58:03 -04:00
|
|
|
{path: filepath.Join(testFixture("validate"), "null_var.json")},
|
|
|
|
{path: filepath.Join(testFixture("validate"), "var_foo_with_no_default.pkr.hcl")},
|
2020-06-02 14:58:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.path, func(t *testing.T) {
|
2020-07-24 04:58:03 -04:00
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
c.CoreConfig.Version = "102.0.0"
|
2020-06-02 14:58:33 -04:00
|
|
|
tc := tc
|
|
|
|
args := []string{"-syntax-only", tc.path}
|
|
|
|
if code := c.Run(args); code != tc.exitCode {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:32:28 -04:00
|
|
|
func TestValidateCommandOKVersion(t *testing.T) {
|
2015-07-13 17:57:35 -04:00
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: testMetaFile(t),
|
|
|
|
}
|
|
|
|
args := []string{
|
|
|
|
filepath.Join(testFixture("validate"), "template.json"),
|
|
|
|
}
|
|
|
|
|
2015-07-13 22:32:28 -04:00
|
|
|
// This should pass with a valid configuration version
|
|
|
|
c.CoreConfig.Version = "102.0.0"
|
2015-07-13 17:57:35 -04:00
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
2015-07-13 22:32:28 -04:00
|
|
|
}
|
2015-07-13 17:57:35 -04:00
|
|
|
|
2015-07-13 22:32:28 -04:00
|
|
|
func TestValidateCommandBadVersion(t *testing.T) {
|
|
|
|
c := &ValidateCommand{
|
|
|
|
Meta: testMetaFile(t),
|
2015-07-13 17:57:35 -04:00
|
|
|
}
|
2015-07-13 22:32:28 -04:00
|
|
|
args := []string{
|
|
|
|
filepath.Join(testFixture("validate"), "template.json"),
|
2015-07-13 17:57:35 -04:00
|
|
|
}
|
2015-07-13 22:32:28 -04:00
|
|
|
|
|
|
|
// 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)
|
2020-07-24 04:58:03 -04:00
|
|
|
expected := `Error:
|
|
|
|
|
|
|
|
This template requires Packer version 101.0.0 or higher; using 100.0.0
|
|
|
|
|
|
|
|
|
2015-07-13 22:32:28 -04:00
|
|
|
`
|
2020-07-24 04:58:03 -04:00
|
|
|
|
|
|
|
if diff := cmp.Diff(expected, stderr); diff != "" {
|
|
|
|
t.Errorf("Unexpected output: %s", diff)
|
2015-07-13 17:57:35 -04:00
|
|
|
}
|
2015-07-13 22:32:28 -04:00
|
|
|
t.Log(stdout)
|
2015-07-13 17:57:35 -04:00
|
|
|
}
|
2020-06-09 09:23:29 -04:00
|
|
|
|
|
|
|
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) {
|
2020-06-09 11:35:53 -04:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-06-09 09:23:29 -04:00
|
|
|
tc := tc
|
|
|
|
if code := c.Run(tc.args); code != tc.exitCode {
|
|
|
|
fatalCommand(t, c.Meta)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|