From 79867ca26ebfa77323487955fa1b777ecffd7b81 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 17 Feb 2020 16:36:19 +0100 Subject: [PATCH] add test for unset variable --- hcl2template/common_test.go | 2 ++ .../variables/unset_string_variable.pkr.hcl | 14 +++++++++ hcl2template/types.variables_test.go | 29 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 hcl2template/testdata/variables/unset_string_variable.pkr.hcl diff --git a/hcl2template/common_test.go b/hcl2template/common_test.go index e930c3b74..c3b17b817 100644 --- a/hcl2template/common_test.go +++ b/hcl2template/common_test.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclparse" + "github.com/hashicorp/packer/builder/null" . "github.com/hashicorp/packer/hcl2template/internal" "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/packer" @@ -21,6 +22,7 @@ func getBasicParser() *Parser { BuilderSchemas: packer.MapOfBuilder{ "amazon-ebs": func() (packer.Builder, error) { return &MockBuilder{}, nil }, "virtualbox-iso": func() (packer.Builder, error) { return &MockBuilder{}, nil }, + "null": func() (packer.Builder, error) { return &null.Builder{}, nil }, }, ProvisionersSchemas: packer.MapOfProvisioner{ "shell": func() (packer.Provisioner, error) { return &MockProvisioner{}, nil }, diff --git a/hcl2template/testdata/variables/unset_string_variable.pkr.hcl b/hcl2template/testdata/variables/unset_string_variable.pkr.hcl new file mode 100644 index 000000000..3f4a9c7cb --- /dev/null +++ b/hcl2template/testdata/variables/unset_string_variable.pkr.hcl @@ -0,0 +1,14 @@ + +variable "foo" { + type = string +} + +build { + sources = [ + "source.null.null-builder", + ] +} + +source "null" "null-builder" { + communicator = var.foo +} diff --git a/hcl2template/types.variables_test.go b/hcl2template/types.variables_test.go index bafd371f2..fc6e6706d 100644 --- a/hcl2template/types.variables_test.go +++ b/hcl2template/types.variables_test.go @@ -10,6 +10,7 @@ import ( "github.com/zclconf/go-cty/cty/convert" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/packer/builder/null" "github.com/hashicorp/packer/packer" ) @@ -97,6 +98,34 @@ func TestParse_variables(t *testing.T) { []packer.Build{}, false, }, + {"unset variable", + defaultParser, + parseTestArgs{"testdata/variables/unset_string_variable.pkr.hcl", nil}, + &PackerConfig{ + Basedir: filepath.Join("testdata", "variables"), + InputVariables: Variables{ + "foo": &Variable{}, + }, + Sources: map[SourceRef]*SourceBlock{ + SourceRef{"null", "null-builder"}: &SourceBlock{ + Name: "null-builder", + Type: "null", + }, + }, + Builds: Builds{ + &BuildBlock{ + Sources: []SourceRef{SourceRef{"null", "null-builder"}}, + }, + }, + }, + false, false, + []packer.Build{ + &packer.CoreBuild{ + Builder: &null.Builder{}, + }, + }, + false, + }, } testParse(t, tests) }