packer/hcl2template/types.variables_test.go

1043 lines
26 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
package hcl2template
import (
"fmt"
"path/filepath"
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
2020-12-17 16:29:25 -05:00
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
2020-03-03 05:15:56 -05:00
"github.com/hashicorp/packer/builder/null"
. "github.com/hashicorp/packer/hcl2template/internal"
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
"github.com/hashicorp/packer/packer"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
)
func TestParse_variables(t *testing.T) {
defaultParser := getBasicParser()
tests := []parseTest{
{"basic variables",
defaultParser,
parseTestArgs{"testdata/variables/basic.pkr.hcl", nil, nil},
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
&PackerConfig{
CorePackerVersionString: lockedVersion,
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{
Type: "null",
Name: "test",
},
},
},
},
},
Sources: map[SourceRef]SourceBlock{
{
Type: "null",
Name: "test",
}: {
Type: "null",
Name: "test",
},
},
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"image_name": &Variable{
Name: "image_name",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo-image-{{user `my_secret`}}")}},
},
"key": &Variable{
Name: "key",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("value")}},
},
"my_secret": &Variable{
Name: "my_secret",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
},
"image_id": &Variable{
Name: "image_id",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("image-id-default")}},
},
"port": &Variable{
Name: "port",
Type: cty.Number,
Values: []VariableAssignment{{From: "default", Value: cty.NumberIntVal(42)}},
},
"availability_zone_names": &Variable{
Name: "availability_zone_names",
Values: []VariableAssignment{{
From: "default",
Value: cty.ListVal([]cty.Value{
cty.StringVal("us-west-1a"),
}),
}},
Type: cty.List(cty.String),
Description: fmt.Sprintln("Describing is awesome ;D"),
},
"super_secret_password": &Variable{
Name: "super_secret_password",
Sensitive: true,
Values: []VariableAssignment{{
From: "default",
Value: cty.NullVal(cty.String),
}},
Type: cty.String,
Description: fmt.Sprintln("Handle with care plz"),
},
},
LocalVariables: Variables{
"owner": &Variable{
Name: "owner",
Values: []VariableAssignment{{
From: "default",
Value: cty.StringVal("Community Team"),
}},
Type: cty.String,
},
"service_name": &Variable{
Name: "service_name",
Values: []VariableAssignment{{
From: "default",
Value: cty.StringVal("forum"),
}},
Type: cty.String,
},
"supersecret": &Variable{
Name: "supersecret",
Values: []VariableAssignment{{
From: "default",
Value: cty.StringVal("secretvar"),
}},
Type: cty.String,
Sensitive: true,
},
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
},
},
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
false,
},
{"duplicate variable",
defaultParser,
parseTestArgs{"testdata/variables/duplicate_variable.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"boolean_value": &Variable{
Name: "boolean_value",
Values: []VariableAssignment{{
From: "default",
Value: cty.BoolVal(false),
}},
Type: cty.Bool,
},
},
},
true, true,
[]packersdk.Build{},
false,
},
{"duplicate variable in variables",
defaultParser,
parseTestArgs{"testdata/variables/duplicate_variables.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"boolean_value": &Variable{
Name: "boolean_value",
Values: []VariableAssignment{{
From: "default",
Value: cty.BoolVal(false),
}},
Type: cty.Bool,
},
},
},
true, true,
[]packersdk.Build{},
false,
},
{"duplicate local block",
defaultParser,
parseTestArgs{"testdata/variables/duplicate_locals", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: "testdata/variables/duplicate_locals",
LocalVariables: Variables{
"sensible": &Variable{
Values: []VariableAssignment{
{
From: "default",
Value: cty.StringVal("something"),
},
},
Type: cty.String,
Name: "sensible",
},
},
},
true, true,
[]packersdk.Build{},
false,
},
{"invalid default type",
defaultParser,
parseTestArgs{"testdata/variables/invalid_default.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"broken_type": &Variable{
Name: "broken_type",
Values: []VariableAssignment{{
From: "default",
Value: cty.UnknownVal(cty.DynamicPseudoType),
}},
Type: cty.List(cty.String),
},
},
},
true, true,
[]packersdk.Build{},
false,
},
{"unknown key",
defaultParser,
parseTestArgs{"testdata/variables/unknown_key.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"broken_variable": &Variable{
Name: "broken_variable",
Values: []VariableAssignment{{From: "default", Value: cty.BoolVal(true)}},
Type: cty.Bool,
},
},
},
true, true,
[]packersdk.Build{},
false,
},
2020-03-03 05:15:56 -05:00
{"unset used variable",
2020-02-17 10:36:19 -05:00
defaultParser,
parseTestArgs{"testdata/variables/unset_used_string_variable.pkr.hcl", nil, nil},
2020-02-17 10:36:19 -05:00
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
2020-02-17 10:36:19 -05:00
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
Type: cty.String,
},
2020-02-17 10:36:19 -05:00
},
},
true, true,
[]packersdk.Build{},
2020-03-04 07:01:18 -05:00
true,
2020-03-03 05:15:56 -05:00
},
2020-03-04 07:01:18 -05:00
2020-03-03 05:15:56 -05:00
{"unset unused variable",
defaultParser,
parseTestArgs{"testdata/variables/unset_unused_string_variable.pkr.hcl", nil, nil},
2020-03-03 05:15:56 -05:00
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
2020-03-03 05:15:56 -05:00
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
Type: cty.String,
2020-03-03 05:15:56 -05:00
},
},
Sources: map[SourceRef]SourceBlock{
SourceRef{Type: "null", Name: "null-builder"}: SourceBlock{
2020-03-03 05:15:56 -05:00
Name: "null-builder",
Type: "null",
},
},
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{Type: "null", Name: "null-builder"},
},
},
2020-03-03 05:15:56 -05:00
},
},
},
true, true,
[]packersdk.Build{
2020-03-03 05:15:56 -05:00
&packer.CoreBuild{
Type: "null",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
2020-03-04 07:01:18 -05:00
Prepared: true,
2020-03-03 05:15:56 -05:00
},
},
2020-03-04 07:01:18 -05:00
false,
},
2020-03-04 07:01:18 -05:00
{"locals within another locals usage in different files",
defaultParser,
parseTestArgs{"testdata/variables/complicated", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{
Type: "null",
Name: "test",
},
},
},
},
},
Sources: map[SourceRef]SourceBlock{
{
Type: "null",
Name: "test",
}: {
Type: "null",
Name: "test",
},
},
Basedir: "testdata/variables/complicated",
InputVariables: Variables{
"name_prefix": &Variable{
Name: "name_prefix",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
},
LocalVariables: Variables{
"name_prefix": &Variable{
Name: "name_prefix",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"foo": &Variable{
Name: "foo",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"bar": &Variable{
Name: "bar",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"for_var": &Variable{
Name: "for_var",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"bar_var": &Variable{
2020-03-04 07:01:18 -05:00
Name: "bar_var",
Values: []VariableAssignment{{
From: "default",
Value: cty.TupleVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("foo"),
cty.StringVal("foo"),
}),
}},
Type: cty.Tuple([]cty.Type{
cty.String,
cty.String,
cty.String,
}),
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
},
},
false,
},
{"recursive locals",
defaultParser,
parseTestArgs{"testdata/variables/recursive_locals.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
LocalVariables: Variables{},
},
true, true,
[]packersdk.Build{},
2020-03-03 05:15:56 -05:00
false,
2020-02-17 10:36:19 -05:00
},
2020-03-12 12:08:53 -04:00
{"set variable from var-file",
defaultParser,
parseTestArgs{"testdata/variables/foo-string.variable.pkr.hcl", nil, []string{"testdata/variables/set-foo-too-wee.hcl"}},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{
Type: "null",
Name: "test",
},
},
},
},
},
Sources: map[SourceRef]SourceBlock{
{
Type: "null",
Name: "test",
}: {
Type: "null",
Name: "test",
},
},
2020-03-12 12:08:53 -04:00
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
Values: []VariableAssignment{
VariableAssignment{"default", cty.StringVal("bar"), nil},
VariableAssignment{"varfile", cty.StringVal("wee"), nil},
},
Type: cty.String,
2020-03-12 12:08:53 -04:00
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
},
},
2020-03-12 12:08:53 -04:00
false,
},
{"unknown variable from var-file",
defaultParser,
parseTestArgs{"testdata/variables/empty.pkr.hcl", nil, []string{"testdata/variables/set-foo-too-wee.hcl"}},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{
Type: "null",
Name: "test",
},
},
},
},
},
Sources: map[SourceRef]SourceBlock{
{
Type: "null",
Name: "test",
}: {
Type: "null",
Name: "test",
},
},
Basedir: filepath.Join("testdata", "variables"),
2020-03-12 12:08:53 -04:00
},
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
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
},
},
2020-03-12 12:08:53 -04:00
false,
},
{"provisioner variable decoding",
defaultParser,
parseTestArgs{"testdata/variables/provisioner_variable_decoding.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"max_retries": &Variable{
Name: "max_retries",
Values: []VariableAssignment{{"default", cty.StringVal("1"), nil}},
Type: cty.String,
},
"max_retries_int": &Variable{
Name: "max_retries_int",
Values: []VariableAssignment{{"default", cty.NumberIntVal(1), nil}},
Type: cty.Number,
},
},
Sources: map[SourceRef]SourceBlock{
SourceRef{Type: "null", Name: "null-builder"}: SourceBlock{
Name: "null-builder",
Type: "null",
},
},
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{Type: "null", Name: "null-builder"},
},
},
ProvisionerBlocks: []*ProvisionerBlock{
{
PType: "shell",
MaxRetries: 1,
},
{
PType: "shell",
MaxRetries: 1,
},
},
},
},
},
false, false,
[]packersdk.Build{&packer.CoreBuild{
Type: "null.null-builder",
Prepared: true,
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{
{
PType: "shell",
Provisioner: &packer.RetriedProvisioner{
MaxRetries: 1,
Provisioner: &HCL2Provisioner{
Provisioner: &MockProvisioner{
Config: MockConfig{
NestedMockConfig: NestedMockConfig{
Tags: []MockTag{},
},
NestedSlice: []NestedMockConfig{},
},
},
},
},
},
{
PType: "shell",
Provisioner: &packer.RetriedProvisioner{
MaxRetries: 1,
Provisioner: &HCL2Provisioner{
Provisioner: &MockProvisioner{
Config: MockConfig{
NestedMockConfig: NestedMockConfig{
Tags: []MockTag{},
},
NestedSlice: []NestedMockConfig{},
},
},
},
},
},
},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
},
},
false,
},
2020-10-30 07:41:29 -04:00
{"valid validation block",
defaultParser,
parseTestArgs{"testdata/variables/validation/valid.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables", "validation"),
Builds: Builds{
&BuildBlock{
Sources: []SourceUseBlock{
{
SourceRef: SourceRef{
Type: "null",
Name: "test",
},
},
},
},
},
Sources: map[SourceRef]SourceBlock{
{
Type: "null",
Name: "test",
}: {
Type: "null",
Name: "test",
},
},
2020-10-30 07:41:29 -04:00
InputVariables: Variables{
"image_id": &Variable{
Values: []VariableAssignment{
{"default", cty.StringVal("ami-something-something"), nil},
},
Name: "image_id",
Type: cty.String,
2020-10-30 07:41:29 -04:00
Validations: []*VariableValidation{
&VariableValidation{
ErrorMessage: `The image_id value must be a valid AMI id, starting with "ami-".`,
},
},
},
},
},
false, false,
[]packersdk.Build{
&packer.CoreBuild{
Type: "null.test",
Builder: &null.Builder{},
Provisioners: []packer.CoreBuildProvisioner{},
PostProcessors: [][]packer.CoreBuildPostProcessor{},
Prepared: true,
},
},
2020-10-30 07:41:29 -04:00
false,
},
{"valid validation block - invalid default",
defaultParser,
parseTestArgs{"testdata/variables/validation/invalid_default.pkr.hcl", nil, nil},
&PackerConfig{
CorePackerVersionString: lockedVersion,
Basedir: filepath.Join("testdata", "variables", "validation"),
InputVariables: Variables{
"image_id": &Variable{
Values: []VariableAssignment{{"default", cty.StringVal("potato"), nil}},
Name: "image_id",
Type: cty.String,
Validations: []*VariableValidation{
&VariableValidation{
ErrorMessage: `The image_id value must be a valid AMI id, starting with "ami-".`,
},
},
},
},
},
true, true,
nil,
false,
},
build using HCL2 (#8423) This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
2019-12-17 05:25:56 -05:00
}
testParse(t, tests)
}
func TestVariables_collectVariableValues(t *testing.T) {
type args struct {
env []string
hclFiles []string
argv map[string]string
}
tests := []struct {
2020-03-09 11:16:59 -04:00
name string
variables Variables
validationOptions ValidationOptions
args args
wantDiags bool
wantDiagsHasError bool
wantVariables Variables
wantValues map[string]cty.Value
}{
{name: "string",
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
variables: Variables{"used_string": &Variable{
Values: []VariableAssignment{
{"default", cty.StringVal("default_value"), nil},
},
Type: cty.String,
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
}},
args: args{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
env: []string{`PKR_VAR_used_string=env_value`},
hclFiles: []string{
`used_string="xy"`,
`used_string="varfile_value"`,
},
argv: map[string]string{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
"used_string": `cmd_value`,
},
},
// output
wantDiags: false,
wantVariables: Variables{
"used_string": &Variable{
Type: cty.String,
Values: []VariableAssignment{
{"default", cty.StringVal(`default_value`), nil},
{"env", cty.StringVal(`env_value`), nil},
{"varfile", cty.StringVal(`xy`), nil},
{"varfile", cty.StringVal(`varfile_value`), nil},
{"cmd", cty.StringVal(`cmd_value`), nil},
},
},
},
wantValues: map[string]cty.Value{
"used_string": cty.StringVal("cmd_value"),
},
},
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
{name: "quoted string",
variables: Variables{"quoted_string": &Variable{
Values: []VariableAssignment{
{"default", cty.StringVal(`"default_value"`), nil},
},
Type: cty.String,
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
}},
args: args{
env: []string{`PKR_VAR_quoted_string="env_value"`},
hclFiles: []string{
`quoted_string="\"xy\""`,
`quoted_string="\"varfile_value\""`,
},
argv: map[string]string{
"quoted_string": `"cmd_value"`,
},
},
// output
wantDiags: false,
wantVariables: Variables{
"quoted_string": &Variable{
Type: cty.String,
Values: []VariableAssignment{
{"default", cty.StringVal(`"default_value"`), nil},
{"env", cty.StringVal(`"env_value"`), nil},
{"varfile", cty.StringVal(`"xy"`), nil},
{"varfile", cty.StringVal(`"varfile_value"`), nil},
{"cmd", cty.StringVal(`"cmd_value"`), nil},
},
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
},
},
wantValues: map[string]cty.Value{
"quoted_string": cty.StringVal(`"cmd_value"`),
},
},
{name: "array of strings",
variables: Variables{"used_strings": &Variable{
Values: []VariableAssignment{
{"default", stringListVal("default_value_1"), nil},
},
Type: cty.List(cty.String),
}},
args: args{
env: []string{`PKR_VAR_used_strings=["env_value_1", "env_value_2"]`},
hclFiles: []string{
`used_strings=["xy"]`,
`used_strings=["varfile_value_1"]`,
},
argv: map[string]string{
"used_strings": `["cmd_value_1"]`,
},
},
// output
wantDiags: false,
wantVariables: Variables{
"used_strings": &Variable{
Type: cty.List(cty.String),
Values: []VariableAssignment{
{"default", stringListVal("default_value_1"), nil},
{"env", stringListVal("env_value_1", "env_value_2"), nil},
{"varfile", stringListVal("xy"), nil},
{"varfile", stringListVal("varfile_value_1"), nil},
{"cmd", stringListVal("cmd_value_1"), nil},
},
},
},
wantValues: map[string]cty.Value{
"used_strings": stringListVal("cmd_value_1"),
},
},
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
{name: "bool",
variables: Variables{"enabled": &Variable{
Values: []VariableAssignment{{"default", cty.False, nil}},
Type: cty.Bool,
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
}},
args: args{
env: []string{`PKR_VAR_enabled=true`},
hclFiles: []string{
`enabled="false"`,
},
argv: map[string]string{
"enabled": `true`,
},
},
// output
wantDiags: false,
wantVariables: Variables{
"enabled": &Variable{
Type: cty.Bool,
Values: []VariableAssignment{
{"default", cty.False, nil},
{"env", cty.True, nil},
{"varfile", cty.False, nil},
{"cmd", cty.True, nil},
},
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
},
},
wantValues: map[string]cty.Value{
"enabled": cty.True,
},
},
{name: "invalid env var",
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
variables: Variables{"used_string": &Variable{
Values: []VariableAssignment{{"default", cty.StringVal("default_value"), nil}},
Type: cty.String,
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
}},
args: args{
env: []string{`PKR_VAR_used_string`},
},
// output
wantDiags: false,
wantVariables: Variables{
"used_string": &Variable{
Type: cty.String,
Values: []VariableAssignment{{"default", cty.StringVal("default_value"), nil}},
},
},
wantValues: map[string]cty.Value{
"used_string": cty.StringVal("default_value"),
},
},
2020-03-09 11:16:59 -04:00
{name: "undefined but set value - pkrvar file - normal mode",
variables: Variables{},
args: args{
2020-03-09 11:16:59 -04:00
hclFiles: []string{`undefined_string="value"`},
},
// output
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
wantDiags: false,
2020-03-09 11:16:59 -04:00
wantDiagsHasError: false,
wantVariables: Variables{},
wantValues: map[string]cty.Value{},
},
{name: "undefined but set value - pkrvar file - strict mode",
variables: Variables{},
validationOptions: ValidationOptions{
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
WarnOnUndeclaredVar: true,
2020-03-09 11:16:59 -04:00
},
args: args{
hclFiles: []string{`undefined_string="value"`},
},
// output
wantDiags: true,
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
wantDiagsHasError: false,
2020-03-09 11:16:59 -04:00
wantVariables: Variables{},
wantValues: map[string]cty.Value{},
},
{name: "undefined but set value - env",
variables: Variables{},
args: args{
env: []string{`PKR_VAR_undefined_string=value`},
},
// output
wantDiags: false,
wantVariables: Variables{},
wantValues: map[string]cty.Value{},
},
2020-03-09 11:16:59 -04:00
{name: "undefined but set value - argv",
variables: Variables{},
args: args{
argv: map[string]string{
2020-03-09 11:16:59 -04:00
"undefined_string": "value",
},
},
// output
2020-03-09 11:16:59 -04:00
wantDiags: true,
wantDiagsHasError: true,
wantVariables: Variables{},
wantValues: map[string]cty.Value{},
},
{name: "value not corresponding to type - env",
variables: Variables{
"used_string": &Variable{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
Type: cty.List(cty.String),
},
},
args: args{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
env: []string{`PKR_VAR_used_string="string"`},
},
// output
2020-03-09 11:16:59 -04:00
wantDiags: true,
wantDiagsHasError: true,
wantVariables: Variables{
"used_string": &Variable{
Type: cty.List(cty.String),
Values: []VariableAssignment{{"env", cty.DynamicVal, nil}},
},
},
wantValues: map[string]cty.Value{
"used_string": cty.DynamicVal,
},
},
{name: "value not corresponding to type - cfg file",
variables: Variables{
"used_string": &Variable{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
Type: cty.Bool,
},
},
args: args{
hclFiles: []string{`used_string=["string"]`},
},
// output
2020-03-09 11:16:59 -04:00
wantDiags: true,
wantDiagsHasError: true,
wantVariables: Variables{
"used_string": &Variable{
Type: cty.Bool,
Values: []VariableAssignment{{"varfile", cty.DynamicVal, nil}},
},
},
wantValues: map[string]cty.Value{
"used_string": cty.DynamicVal,
},
},
{name: "value not corresponding to type - argv",
variables: Variables{
"used_string": &Variable{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
Type: cty.Bool,
},
},
args: args{
argv: map[string]string{
hcl2template/types.variables: Update logic for parsing literal value variables (#8834) * hcl2template/types.variables: Update logic for parsing literal value variables In running tests via the CLI it was determined that when using the variable block with no explicit type assigned the type of the default value was not being set within the map. This change updates the `decodeConfig` method so that a type is always set for any defined variable if not specified. The second change is to properly handle the evaluation of basic variable types (e.g String, Number, Bool). Previously variables defined on the CLI or via PKR_VAR required some additional quoting to for proper evaluation. This change fixes that issue so that it works like it does in Terraform :) Build results before the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo=home' . Error: Variables not allowed on <value for var.foo from arguments> line 1: (source code not available) Variables may not be used here. ==> Builds finished but no artifacts were created. ``` Build results after the change ``` ⇶ PKR_VAR_example='["one","two"]' ~/bin/packer build -var 'foo="home"' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell885249462 null: two null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ⇶ ~/bin/packer build -var 'foo=home' -var 'example=["one","another variable"]' . null: output will be in this color. ==> null: Running local shell script: /tmp/packer-shell123467506 null: another variable null: home Build 'null' finished. ==> Builds finished. The artifacts of successful builds are: --> null: Did not export anything. This is the null builder ``` * tests/hcl2template/types.variables: Update test to use Bool Turns out a string value won't actually complain if it's given a non string looking value. It will just covert the value to a string literal so using a type Bool which should fail if given anything that is not true or false. * tests/hcl2template/types.variables: Update unit tests During testing it was found that by default the variable stanza were defaulting to a cty.NilType, and not the Type of it's default value. This change sets the default type of the defined variable to ensure variable evaluation behaviors correctly. * Add a simple cty.Bool test case * tests/hcl2template/types.variables: Enable quoted_string test case * Update hcl2template/types.variables.go space Co-authored-by: Adrien Delorme <azr@users.noreply.github.com>
2020-03-06 09:12:26 -05:00
"used_string": `["true"]`,
},
},
// output
2020-03-09 11:16:59 -04:00
wantDiags: true,
wantDiagsHasError: true,
wantVariables: Variables{
"used_string": &Variable{
Type: cty.Bool,
Values: []VariableAssignment{{"cmd", cty.DynamicVal, nil}},
},
},
wantValues: map[string]cty.Value{
"used_string": cty.DynamicVal,
},
},
{name: "defining a variable block in a variables file is invalid ",
variables: Variables{},
args: args{
hclFiles: []string{`variable "something" {}`},
},
// output
2020-03-09 11:16:59 -04:00
wantDiags: true,
wantDiagsHasError: true,
wantVariables: Variables{},
wantValues: map[string]cty.Value{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var files []*hcl.File
parser := getBasicParser()
for i, hclContent := range tt.args.hclFiles {
file, diags := parser.ParseHCL([]byte(hclContent), fmt.Sprintf("test_file_%d_*"+hcl2AutoVarFileExt, i))
if diags != nil {
t.Fatalf("ParseHCLFile %d: %v", i, diags)
}
files = append(files, file)
}
2020-03-09 11:16:59 -04:00
cfg := &PackerConfig{
InputVariables: tt.variables,
ValidationOptions: tt.validationOptions,
}
gotDiags := cfg.collectInputVariableValues(tt.args.env, files, tt.args.argv)
if (gotDiags == nil) == tt.wantDiags {
t.Fatalf("Variables.collectVariableValues() = %v, want %v", gotDiags, tt.wantDiags)
}
2020-03-09 11:16:59 -04:00
if tt.wantDiagsHasError != gotDiags.HasErrors() {
t.Fatalf("Variables.collectVariableValues() unexpected diagnostics HasErrors. %s", gotDiags)
}
if diff := cmp.Diff(tt.wantVariables, tt.variables, cmpOpts...); diff != "" {
t.Fatalf("didn't get expected variables: %s", diff)
}
values := map[string]cty.Value{}
for k, v := range tt.variables {
value, diag := v.Value(), v.ValidateValue()
if diag != nil {
t.Fatalf("Value %s: %v", k, diag)
}
values[k] = value
}
if diff := cmp.Diff(fmt.Sprintf("%#v", values), fmt.Sprintf("%#v", tt.wantValues)); diff != "" {
t.Fatalf("didn't get expected values: %s", diff)
}
})
}
}
func stringListVal(strings ...string) cty.Value {
values := []cty.Value{}
for _, str := range strings {
values = append(values, cty.StringVal(str))
}
list, err := convert.Convert(cty.ListVal(values), cty.List(cty.String))
if err != nil {
panic(err)
}
return list
}