add command/ tests

This commit is contained in:
Adrien Delorme 2020-11-02 15:50:38 +01:00
parent 6911495fc4
commit 6dd06fad14
4 changed files with 79 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {}

View File

@ -0,0 +1,7 @@
image_metadata = {
key: "value",
something: {
foo: "woo",
}
}

View File

@ -0,0 +1,7 @@
image_metadata = {
key: "value",
something: {
foo: "barwoo",
}
}