From 6dd06fad144ecde83923c7b3fa8a8fc83ab15bd4 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 2 Nov 2020 15:50:38 +0100 Subject: [PATCH] add command/ tests --- command/build_test.go | 46 +++++++++++++++++++ .../hcl/validation/map/definition.pkr.hcl | 19 ++++++++ .../validation/map/invalid_value.pkrvars.hcl | 7 +++ .../validation/map/valid_value.pkrvars.hcl | 7 +++ 4 files changed, 79 insertions(+) create mode 100644 command/test-fixtures/hcl/validation/map/definition.pkr.hcl create mode 100644 command/test-fixtures/hcl/validation/map/invalid_value.pkrvars.hcl create mode 100644 command/test-fixtures/hcl/validation/map/valid_value.pkrvars.hcl diff --git a/command/build_test.go b/command/build_test.go index 9e08e6dd8..60660ff09 100644 --- a/command/build_test.go +++ b/command/build_test.go @@ -324,6 +324,52 @@ func TestBuild(t *testing.T) { }, }, }, + + { + name: "hcl - valid validation rule for default value", + args: []string{ + filepath.Join(testFixture("hcl", "validation", "map")), + }, + expectedCode: 0, + }, + + { + name: "hcl - valid setting from varfile", + args: []string{ + "-var-file", filepath.Join(testFixture("hcl", "validation", "map", "valid_value.pkrvars.hcl")), + filepath.Join(testFixture("hcl", "validation", "map")), + }, + expectedCode: 0, + }, + + { + name: "hcl - invalid setting from varfile", + args: []string{ + "-var-file", filepath.Join(testFixture("hcl", "validation", "map", "invalid_value.pkrvars.hcl")), + filepath.Join(testFixture("hcl", "validation", "map")), + }, + expectedCode: 1, + }, + + { + name: "hcl - valid cmd ( invalid varfile bypased )", + args: []string{ + "-var-file", filepath.Join(testFixture("hcl", "validation", "map", "invalid_value.pkrvars.hcl")), + "-var", `image_metadata={key = "new_value", something = { foo = "bar" }}`, + filepath.Join(testFixture("hcl", "validation", "map")), + }, + expectedCode: 0, + }, + + { + name: "hcl - invalid cmd ( valid varfile bypased )", + args: []string{ + "-var-file", filepath.Join(testFixture("hcl", "validation", "map", "valid_value.pkrvars.hcl")), + "-var", `image_metadata={key = "?", something = { foo = "wrong" }}`, + filepath.Join(testFixture("hcl", "validation", "map")), + }, + expectedCode: 1, + }, } for _, tt := range tc { diff --git a/command/test-fixtures/hcl/validation/map/definition.pkr.hcl b/command/test-fixtures/hcl/validation/map/definition.pkr.hcl new file mode 100644 index 000000000..1e60d70bd --- /dev/null +++ b/command/test-fixtures/hcl/validation/map/definition.pkr.hcl @@ -0,0 +1,19 @@ + +variable "image_metadata" { + default = { + key: "value", + something: { + foo: "bar", + } + } + validation { + condition = length(var.image_metadata.key) > 4 + error_message = "The image_metadata.key field must be more than 4 runes." + } + validation { + condition = substr(var.image_metadata.something.foo, 0, 3) == "bar" + error_message = "The image_metadata.something.foo field must start with \"bar\"." + } +} + +build {} diff --git a/command/test-fixtures/hcl/validation/map/invalid_value.pkrvars.hcl b/command/test-fixtures/hcl/validation/map/invalid_value.pkrvars.hcl new file mode 100644 index 000000000..9734f36f6 --- /dev/null +++ b/command/test-fixtures/hcl/validation/map/invalid_value.pkrvars.hcl @@ -0,0 +1,7 @@ + +image_metadata = { + key: "value", + something: { + foo: "woo", + } +} diff --git a/command/test-fixtures/hcl/validation/map/valid_value.pkrvars.hcl b/command/test-fixtures/hcl/validation/map/valid_value.pkrvars.hcl new file mode 100644 index 000000000..250fcfc8f --- /dev/null +++ b/command/test-fixtures/hcl/validation/map/valid_value.pkrvars.hcl @@ -0,0 +1,7 @@ + +image_metadata = { + key: "value", + something: { + foo: "barwoo", + } +}