packer/command/validate_test.go

390 lines
11 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
2015-07-13 17:57:35 -04:00
package command
import (
"fmt"
2015-07-13 17:57:35 -04:00
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
2021-01-20 04:37:16 -05:00
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer/packer"
2015-07-13 17:57:35 -04:00
)
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-02 14:58:33 -04:00
func TestValidateCommand(t *testing.T) {
tt := []struct {
path string
exitCode int
extraArgs []string
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-02 14:58:33 -04:00
}{
{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},
2021-04-26 11:31:22 -04:00
{path: testFixture("hcl", "validation", "wrong_pause_before.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 type
{path: filepath.Join(testFixture("validate", "invalid_block_type.pkr.hcl")), exitCode: 1},
// wrong packer block
{path: filepath.Join(testFixture("validate", "invalid_packer_block.pkr.hcl")), exitCode: 1},
// Should return multiple errors,
{path: filepath.Join(testFixture("validate", "circular_error.pkr.hcl")), exitCode: 1},
// datasource could be unknown at that moment
{path: filepath.Join(testFixture("hcl", "data-source-validation.pkr.hcl")), exitCode: 0},
// datasource unknown at validation-time without datasource evaluation -> fail on provisioner
{path: filepath.Join(testFixture("hcl", "local-ds-validate.pkr.hcl")), exitCode: 1},
// datasource unknown at validation-time with datasource evaluation -> success
{path: filepath.Join(testFixture("hcl", "local-ds-validate.pkr.hcl")), exitCode: 0, extraArgs: []string{"--evaluate-datasources"}},
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-02 14:58:33 -04:00
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
2021-12-01 09:58:33 -05:00
Meta: TestMetaFile(t),
}
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-02 14:58:33 -04:00
tc := tc
args := tc.extraArgs
args = append(args, tc.path)
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-02 14:58:33 -04:00
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{}
2021-12-01 09:58:33 -05:00
meta := TestMetaFile(t)
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")
}
}
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-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},
{path: filepath.Join(testFixture("validate"), "null_var.json")},
{path: filepath.Join(testFixture("validate"), "var_foo_with_no_default.pkr.hcl")},
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-02 14:58:33 -04:00
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
2021-12-01 09:58:33 -05:00
Meta: TestMetaFile(t),
}
c.CoreConfig.Version = "102.0.0"
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-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)
}
})
}
}
func TestValidateCommandOKVersion(t *testing.T) {
2015-07-13 17:57:35 -04:00
c := &ValidateCommand{
2021-12-01 09:58:33 -05:00
Meta: TestMetaFile(t),
2015-07-13 17:57:35 -04:00
}
args := []string{
filepath.Join(testFixture("validate"), "template.json"),
}
// 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 17:57:35 -04:00
func TestValidateCommandBadVersion(t *testing.T) {
c := &ValidateCommand{
2021-12-01 09:58:33 -05:00
Meta: TestMetaFile(t),
2015-07-13 17:57:35 -04:00
}
args := []string{
filepath.Join(testFixture("validate"), "template.json"),
2015-07-13 17:57:35 -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 := GetStdoutAndErrFromTestMeta(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)
2015-07-13 17:57:35 -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{
2021-12-01 09:58:33 -05:00
Meta: TestMetaFile(t),
2020-06-09 09:23:29 -04:00
}
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)
}
})
}
}
core: Update validation options for undeclared variables (#12104) * Update validation options for undeclared variables In an effort to help users move from JSON to HCL2 templates the support for variable definitions files are being updated to ignore undeclared variable warnings on build execution. For legacy JSON templates builds no warnings are displayed when var-files contain undeclared variables. Since preferred mode HCL2 templates is to be explicit with variable declarations - they must be declared to be used - validation for undeclared variables still warns when running `packer validate`. A new flag has been added to the validate command that can be used to disable undeclared variable warnings. * Update validation test for unused variables Example Run ``` ~> go run . validate -no-warn-undeclared-var -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl The configuration is valid. ~> go run . validate -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl Warning: Undefined variable The variable "unused" was set but was not declared as an input variable. To declare variable "unused" place this block in one of your .pkr.hcl files, such as variables.pkr.hcl variable "unused" { type = string default = null } The configuration is valid. ~> go run . build -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl file.chocolate: output will be in this color. Build 'file.chocolate' finished after 744 microseconds. ==> Wait completed after 798 microseconds ==> Builds finished. The artifacts of successful builds are: --> file.chocolate: Stored file: chocolate.txt ``` * Rename Strict field to WarnOnUndeclaredVar The field name Strict is a bit vague since it is only used for checking against undeclared variables within a var-file definition. To mitigate against potential overloading of this field it is being renamed to be more explicit on its usage. * command/build: Add warn-on-undeclared-var flag Now that the default behaviour is to not display warnings for undeclared variables an optional flag has been added to toggle the old behaviour. ``` ~> go run . build -warn-on-undeclared-var -var-file command/test-fixtures/validate/var-file-tests/undeclared.pkrvars.hcl command/test-fixtures/validate/var-file-tests/basic.pkr.hcl Warning: Undefined variable The variable "unused" was set but was not declared as an input variable. To declare variable "unused" place this block in one of your .pkr.hcl files, such as variables.pkr.hcl variable "unused" { type = string default = null } file.chocolate: output will be in this color. Build 'file.chocolate' finished after 762 microseconds. ==> Wait completed after 799 microseconds ==> Builds finished. The artifacts of successful builds are: --> file.chocolate: Stored file: chocolate.txt ```
2022-11-14 17:06:45 -05:00
func TestValidateCommand_VarFiles(t *testing.T) {
tt := []struct {
name string
path string
varfile string
exitCode int
}{
{name: "with basic HCL var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkrvars.hcl"),
exitCode: 0,
},
{name: "with unused variable in var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.pkrvars.hcl"),
exitCode: 0,
},
{name: "with unused variable in JSON var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.json"),
exitCode: 0,
},
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
Meta: TestMetaFile(t),
}
tc := tc
args := []string{"-var-file", tc.varfile, tc.path}
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
})
}
}
func TestValidateCommand_VarFilesWarnOnUndeclared(t *testing.T) {
tt := []struct {
name string
path string
varfile string
exitCode int
}{
{name: "default warning with unused variable in HCL var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.pkrvars.hcl"),
exitCode: 0,
},
{name: "default warning with unused variable in JSON var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.json"),
exitCode: 0,
},
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
Meta: TestMetaFile(t),
}
tc := tc
args := []string{"-var-file", tc.varfile, tc.path}
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
stdout, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta)
expected := `Warning: Undefined variable
The variable "unused" was set but was not declared as an input variable.
To declare variable "unused" place this block in one of your .pkr.hcl files,
such as variables.pkr.hcl
variable "unused" {
type = string
default = null
}
The configuration is valid.
`
if diff := cmp.Diff(expected, stdout); diff != "" {
t.Errorf("Unexpected output: %s", diff)
}
t.Log(stderr)
})
}
}
func TestValidateCommand_VarFilesDisableWarnOnUndeclared(t *testing.T) {
tt := []struct {
name string
path string
varfile string
exitCode int
}{
{name: "no-warn-undeclared-var with unused variable in HCL var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.pkrvars.hcl"),
exitCode: 0,
},
{name: "no-warn-undeclared-var with unused variable in JSON var-file definition",
path: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "basic.pkr.hcl"),
varfile: filepath.Join(testFixture(filepath.Join("validate", "var-file-tests")), "undeclared.json"),
exitCode: 0,
},
}
for _, tc := range tt {
t.Run(tc.path, func(t *testing.T) {
c := &ValidateCommand{
Meta: TestMetaFile(t),
}
tc := tc
args := []string{"-no-warn-undeclared-var", "-var-file", tc.varfile, tc.path}
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
stdout, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta)
expected := `The configuration is valid.
`
if diff := cmp.Diff(expected, stdout); diff != "" {
t.Errorf("Unexpected output: %s", diff)
}
t.Log(stderr)
})
}
}
func TestValidateCommand_ShowLineNumForMissing(t *testing.T) {
tt := []struct {
path string
exitCode int
extraArgs []string
}{
{path: filepath.Join(testFixture("validate-invalid"), "missing_build_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 := tc.extraArgs
args = append(args, tc.path)
if code := c.Run(args); code != tc.exitCode {
fatalCommand(t, c.Meta)
}
stdout, stderr := GetStdoutAndErrFromTestMeta(t, c.Meta)
expected := fmt.Sprintf(`Error: Unknown source file.cho
on %s line 6:
(source code not available)
Known: [file.chocolate]
`, tc.path)
if diff := cmp.Diff(expected, stderr); diff != "" {
t.Errorf("Unexpected output: %s", diff)
}
t.Log(stdout)
})
}
}