From 4e08ea6a92dc2a0687c3833b44ef07f9a2895a94 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 30 Oct 2020 12:41:29 +0100 Subject: [PATCH] add a test --- .../variables/validation/valid.pkr.hcl | 9 ++++++++ hcl2template/types.variables_test.go | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 hcl2template/testdata/variables/validation/valid.pkr.hcl diff --git a/hcl2template/testdata/variables/validation/valid.pkr.hcl b/hcl2template/testdata/variables/validation/valid.pkr.hcl new file mode 100644 index 000000000..a0bbe2cf5 --- /dev/null +++ b/hcl2template/testdata/variables/validation/valid.pkr.hcl @@ -0,0 +1,9 @@ + +variable "image_id" { + type = string + default = "ami-something-something" + validation { + condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-" + error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"." + } +} \ No newline at end of file diff --git a/hcl2template/types.variables_test.go b/hcl2template/types.variables_test.go index d1c589d5b..4a8f697c5 100644 --- a/hcl2template/types.variables_test.go +++ b/hcl2template/types.variables_test.go @@ -357,6 +357,28 @@ func TestParse_variables(t *testing.T) { }, false, }, + + {"valid validation block", + defaultParser, + parseTestArgs{"testdata/variables/validation/valid.pkr.hcl", nil, nil}, + &PackerConfig{ + Basedir: filepath.Join("testdata", "variables", "validation"), + InputVariables: Variables{ + "image_id": &Variable{ + DefaultValue: cty.StringVal("ami-something-something"), + Name: "image_id", + Validations: []*VariableValidation{ + &VariableValidation{ + ErrorMessage: `The image_id value must be a valid AMI id, starting with "ami-".`, + }, + }, + }, + }, + }, + false, false, + []packer.Build{}, + false, + }, } testParse(t, tests) }