packer-cn/hcl2template/types.variables_test.go

724 lines
18 KiB
Go
Raw Normal View History

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-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{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"image_name": &Variable{
Name: "image_name",
DefaultValue: cty.StringVal("foo-image-{{user `my_secret`}}"),
},
"key": &Variable{
Name: "key",
DefaultValue: cty.StringVal("value"),
},
"my_secret": &Variable{
Name: "my_secret",
DefaultValue: cty.StringVal("foo"),
},
"image_id": &Variable{
Name: "image_id",
DefaultValue: cty.StringVal("image-id-default"),
},
"port": &Variable{
Name: "port",
DefaultValue: cty.NumberIntVal(42),
},
"availability_zone_names": &Variable{
Name: "availability_zone_names",
DefaultValue: cty.ListVal([]cty.Value{
cty.StringVal("us-west-1a"),
}),
Description: fmt.Sprintln("Describing is awesome ;D"),
},
"super_secret_password": &Variable{
Name: "super_secret_password",
Sensitive: true,
DefaultValue: cty.NullVal(cty.String),
Description: fmt.Sprintln("Handle with care plz"),
},
},
LocalVariables: Variables{
"owner": &Variable{
Name: "owner",
DefaultValue: cty.StringVal("Community Team"),
},
"service_name": &Variable{
Name: "service_name",
DefaultValue: cty.StringVal("forum"),
},
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,
[]packer.Build{},
false,
},
{"duplicate variable",
defaultParser,
parseTestArgs{"testdata/variables/duplicate_variable.pkr.hcl", nil, nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"boolean_value": &Variable{
Name: "boolean_value",
},
},
},
true, true,
[]packer.Build{},
false,
},
{"duplicate variable in variables",
defaultParser,
parseTestArgs{"testdata/variables/duplicate_variables.pkr.hcl", nil, nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"boolean_value": &Variable{
Name: "boolean_value",
},
},
},
true, true,
[]packer.Build{},
false,
},
{"invalid default type",
defaultParser,
parseTestArgs{"testdata/variables/invalid_default.pkr.hcl", nil, nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"broken_type": &Variable{
Name: "broken_type",
},
},
},
true, true,
[]packer.Build{},
false,
},
{"unknown key",
defaultParser,
parseTestArgs{"testdata/variables/unknown_key.pkr.hcl", nil, nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"broken_variable": &Variable{
Name: "broken_variable",
DefaultValue: cty.BoolVal(true),
},
},
},
2020-03-03 05:15:56 -05:00
true, false,
[]packer.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{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
},
2020-02-17 10:36:19 -05:00
},
},
true, true,
[]packer.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{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
},
},
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: []SourceRef{
{Type: "null", Name: "null-builder"},
},
2020-03-03 05:15:56 -05:00
},
},
},
true, true,
2020-03-03 05:15:56 -05:00
[]packer.Build{
&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{
Basedir: "testdata/variables/complicated",
InputVariables: Variables{
"name_prefix": &Variable{
2020-03-04 07:01:18 -05:00
Name: "name_prefix",
DefaultValue: cty.StringVal("foo"),
},
},
LocalVariables: Variables{
"name_prefix": &Variable{
2020-03-04 07:01:18 -05:00
Name: "name_prefix",
DefaultValue: cty.StringVal("foo"),
},
"foo": &Variable{
2020-03-04 07:01:18 -05:00
Name: "foo",
DefaultValue: cty.StringVal("foo"),
},
"bar": &Variable{
2020-03-04 07:01:18 -05:00
Name: "bar",
DefaultValue: cty.StringVal("foo"),
},
"for_var": &Variable{
2020-03-04 07:01:18 -05:00
Name: "for_var",
DefaultValue: cty.StringVal("foo"),
},
"bar_var": &Variable{
2020-03-04 07:01:18 -05:00
Name: "bar_var",
DefaultValue: cty.TupleVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("foo"),
cty.StringVal("foo"),
}),
},
},
},
false, false,
[]packer.Build{},
false,
},
{"recursive locals",
defaultParser,
parseTestArgs{"testdata/variables/recursive_locals.pkr.hcl", nil, nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
LocalVariables: Variables{},
},
true, true,
[]packer.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{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"foo": &Variable{
DefaultValue: cty.StringVal("bar"),
Name: "foo",
VarfileValue: cty.StringVal("wee"),
},
},
},
false, false,
[]packer.Build{},
false,
},
{"unknown variable from var-file",
defaultParser,
parseTestArgs{"testdata/variables/empty.pkr.hcl", nil, []string{"testdata/variables/set-foo-too-wee.hcl"}},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
},
true, false,
[]packer.Build{},
false,
},
{"provisioner variable decoding",
defaultParser,
parseTestArgs{"testdata/variables/provisioner_variable_decoding.pkr.hcl", nil, nil},
&PackerConfig{
Basedir: filepath.Join("testdata", "variables"),
InputVariables: Variables{
"max_retries": &Variable{
Name: "max_retries",
DefaultValue: cty.StringVal("1"),
Type: cty.String,
},
"max_retries_int": &Variable{
Name: "max_retries_int",
DefaultValue: cty.NumberIntVal(1),
Type: cty.Number,
},
},
Sources: map[SourceRef]SourceBlock{
SourceRef{Type: "null", Name: "null-builder"}: SourceBlock{
Name: "null-builder",
Type: "null",
},
},
Builds: Builds{
&BuildBlock{
Sources: []SourceRef{
{Type: "null", Name: "null-builder"},
},
ProvisionerBlocks: []*ProvisionerBlock{
{
PType: "shell",
MaxRetries: 1,
},
{
PType: "shell",
MaxRetries: 1,
},
},
},
},
},
false, false,
[]packer.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,
},
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{
DefaultValue: cty.StringVal("default_value"),
Type: 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=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{
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.String,
CmdValue: cty.StringVal("cmd_value"),
VarfileValue: cty.StringVal("varfile_value"),
EnvValue: cty.StringVal("env_value"),
DefaultValue: cty.StringVal("default_value"),
},
},
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{
DefaultValue: cty.StringVal(`"default_value"`),
Type: cty.String,
}},
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,
CmdValue: cty.StringVal(`"cmd_value"`),
VarfileValue: cty.StringVal(`"varfile_value"`),
EnvValue: cty.StringVal(`"env_value"`),
DefaultValue: cty.StringVal(`"default_value"`),
},
},
wantValues: map[string]cty.Value{
"quoted_string": cty.StringVal(`"cmd_value"`),
},
},
{name: "array of strings",
variables: Variables{"used_strings": &Variable{
DefaultValue: stringListVal("default_value_1"),
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),
CmdValue: stringListVal("cmd_value_1"),
VarfileValue: stringListVal("varfile_value_1"),
EnvValue: stringListVal("env_value_1", "env_value_2"),
DefaultValue: stringListVal("default_value_1"),
},
},
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{
DefaultValue: cty.False,
Type: cty.Bool,
}},
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,
CmdValue: cty.True,
VarfileValue: cty.False,
EnvValue: cty.True,
DefaultValue: cty.False,
},
},
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{
DefaultValue: cty.StringVal("default_value"),
Type: cty.String,
}},
args: args{
env: []string{`PKR_VAR_used_string`},
},
// output
wantDiags: false,
wantVariables: 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.String,
DefaultValue: cty.StringVal("default_value"),
},
},
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
wantDiags: true,
wantDiagsHasError: false,
wantVariables: Variables{},
wantValues: map[string]cty.Value{},
},
{name: "undefined but set value - pkrvar file - strict mode",
variables: Variables{},
validationOptions: ValidationOptions{
Strict: true,
},
args: args{
hclFiles: []string{`undefined_string="value"`},
},
// output
wantDiags: true,
wantDiagsHasError: true,
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{
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),
EnvValue: cty.DynamicVal,
},
},
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{
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,
VarfileValue: cty.DynamicVal,
},
},
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{
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,
CmdValue: cty.DynamicVal,
},
},
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_*"+hcl2VarFileExt, 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(fmt.Sprintf("%#v", tt.wantVariables), fmt.Sprintf("%#v", tt.variables)); 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()
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
}