packer-cn/command/validate_test.go
Wilken Rivera f672f5bd9b command/validate: Add support for HCL2 configuration files
* Update validate command to use FixConfig for checking against known
fixers
* Update validation command flag docs
* Add ConfigFixer method to PackerHandler Interface
* Implement ConfigFixer interface in PackerConfig
* Remove all stdout messaging (i.e calls to c.Ui.Say) in the validate
command. The command will only display hcl.Diagnotic messaging when there is an error or warning.

HCL2 Configs
```
⇶  packer validate docker_centos_shell_provisioner.pkr.hcl

```

JSON Configs
```
⇶  packer validate vmware-iso_ubuntu_minimal/vmware-iso_ubuntu_minimal.json
Error: Failed to prepare build: "vmware-iso"

1 error occurred:
        * Deprecated configuration key: 'iso_checksum_type'. Please call `packer fix`
against your template to update your template to be compatable with the current
version of Packer. Visit https://www.packer.io/docs/commands/fix/ for more
detail.

Warning: Fixable configuration found.
You may need to run `packer fix` to get your build to run correctly.
See debug log for more information.

  map[string]interface{}{
        "builders": []interface{}{
                map[string]interface{}{
                        ... // 3 identical entries
                        "guest_os_type":     string("ubuntu-64"),
                        "http_directory":    string("http"),
-                       "iso_checksum":
string("946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"),
+                       "iso_checksum":
string("sha256:946a6077af6f5f95a51f82fdc44051c7aa19f9cfc5f737954845a6050543d7c2"),
-                       "iso_checksum_type": string("sha256"),
                        "iso_url":
string("http://old-releases.ubuntu.com/releases/14.04.1/ubuntu-14.04.1-server-amd64.iso"),
                        "shutdown_command":  string("echo 'vagrant' | sudo -S shutdown -P now"),
                        ... // 4 identical entries
                },
        },
  }
```
2020-06-05 14:24:39 -04:00

101 lines
2.6 KiB
Go

package command
import (
"path/filepath"
"testing"
)
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},
}
c := &ValidateCommand{
Meta: testMetaFile(t),
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.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},
}
c := &ValidateCommand{
Meta: testMetaFile(t),
}
c.CoreConfig.Version = "102.0.0"
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
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 initializing core: This template requires Packer version 101.0.0 or higher; using 100.0.0
`
if stderr != expected {
t.Fatalf("Expected:\n%s\nFound:\n%s\n", expected, stderr)
}
t.Log(stdout)
}