fix tests to actually check cty values & types

This commit is contained in:
Adrien Delorme 2020-11-02 16:52:21 +01:00
parent 6dd06fad14
commit 88175873e5
3 changed files with 70 additions and 8 deletions

View File

@ -219,11 +219,25 @@ var (
}
)
var ctyValueComparer = cmp.Comparer(func(x, y cty.Value) bool {
return x.RawEquals(y)
})
var ctyTypeComparer = cmp.Comparer(func(x, y cty.Type) bool {
if x == cty.NilType && y == cty.NilType {
return true
}
if x == cty.NilType || y == cty.NilType {
return false
}
return x.Equals(y)
})
var cmpOpts = []cmp.Option{
ctyValueComparer,
ctyTypeComparer,
cmpopts.IgnoreUnexported(
PackerConfig{},
cty.Value{},
cty.Type{},
Variable{},
SourceBlock{},
ProvisionerBlock{},

View File

@ -27,14 +27,17 @@ func TestParser_complete(t *testing.T) {
"foo": &Variable{
Name: "foo",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("value")}},
Type: cty.String,
},
"image_id": &Variable{
Name: "image_id",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("image-id-default")}},
Type: cty.String,
},
"port": &Variable{
Name: "port",
Values: []VariableAssignment{{From: "default", Value: cty.NumberIntVal(42)}},
Type: cty.Number,
},
"availability_zone_names": &Variable{
Name: "availability_zone_names",
@ -46,12 +49,14 @@ func TestParser_complete(t *testing.T) {
cty.StringVal("C"),
}),
}},
Type: cty.List(cty.String),
},
},
LocalVariables: Variables{
"feefoo": &Variable{
Name: "feefoo",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("value_image-id-default")}},
Type: cty.String,
},
"standard_tags": &Variable{
Name: "standard_tags",
@ -61,6 +66,10 @@ func TestParser_complete(t *testing.T) {
"Environment": cty.StringVal("production"),
}),
}},
Type: cty.Object(map[string]cty.Type{
"Component": cty.String,
"Environment": cty.String,
}),
},
"abc_map": &Variable{
Name: "abc_map",
@ -77,6 +86,17 @@ func TestParser_complete(t *testing.T) {
}),
}),
}},
Type: cty.Tuple([]cty.Type{
cty.Object(map[string]cty.Type{
"id": cty.String,
}),
cty.Object(map[string]cty.Type{
"id": cty.String,
}),
cty.Object(map[string]cty.Type{
"id": cty.String,
}),
}),
},
},
Sources: map[SourceRef]SourceBlock{

View File

@ -26,22 +26,27 @@ func TestParse_variables(t *testing.T) {
InputVariables: Variables{
"image_name": &Variable{
Name: "image_name",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo-image-{{user `my_secret`}}")}},
},
"key": &Variable{
Name: "key",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("value")}},
},
"my_secret": &Variable{
Name: "my_secret",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
},
"image_id": &Variable{
Name: "image_id",
Type: cty.String,
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("image-id-default")}},
},
"port": &Variable{
Name: "port",
Type: cty.Number,
Values: []VariableAssignment{{From: "default", Value: cty.NumberIntVal(42)}},
},
"availability_zone_names": &Variable{
@ -52,6 +57,7 @@ func TestParse_variables(t *testing.T) {
cty.StringVal("us-west-1a"),
}),
}},
Type: cty.List(cty.String),
Description: fmt.Sprintln("Describing is awesome ;D"),
},
"super_secret_password": &Variable{
@ -61,6 +67,7 @@ func TestParse_variables(t *testing.T) {
From: "default",
Value: cty.NullVal(cty.String),
}},
Type: cty.String,
Description: fmt.Sprintln("Handle with care plz"),
},
},
@ -71,6 +78,7 @@ func TestParse_variables(t *testing.T) {
From: "default",
Value: cty.StringVal("Community Team"),
}},
Type: cty.String,
},
"service_name": &Variable{
Name: "service_name",
@ -78,6 +86,7 @@ func TestParse_variables(t *testing.T) {
From: "default",
Value: cty.StringVal("forum"),
}},
Type: cty.String,
},
},
},
@ -97,6 +106,7 @@ func TestParse_variables(t *testing.T) {
From: "default",
Value: cty.BoolVal(false),
}},
Type: cty.Bool,
},
},
},
@ -116,6 +126,7 @@ func TestParse_variables(t *testing.T) {
From: "default",
Value: cty.BoolVal(false),
}},
Type: cty.Bool,
},
},
},
@ -135,6 +146,7 @@ func TestParse_variables(t *testing.T) {
From: "default",
Value: cty.UnknownVal(cty.DynamicPseudoType),
}},
Type: cty.List(cty.String),
},
},
},
@ -152,6 +164,7 @@ func TestParse_variables(t *testing.T) {
"broken_variable": &Variable{
Name: "broken_variable",
Values: []VariableAssignment{{From: "default", Value: cty.BoolVal(true)}},
Type: cty.Bool,
},
},
},
@ -168,6 +181,7 @@ func TestParse_variables(t *testing.T) {
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
Type: cty.String,
},
},
},
@ -184,6 +198,7 @@ func TestParse_variables(t *testing.T) {
InputVariables: Variables{
"foo": &Variable{
Name: "foo",
Type: cty.String,
},
},
Sources: map[SourceRef]SourceBlock{
@ -222,24 +237,29 @@ func TestParse_variables(t *testing.T) {
"name_prefix": &Variable{
Name: "name_prefix",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
},
LocalVariables: Variables{
"name_prefix": &Variable{
Name: "name_prefix",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"foo": &Variable{
Name: "foo",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"bar": &Variable{
Name: "bar",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"for_var": &Variable{
Name: "for_var",
Values: []VariableAssignment{{From: "default", Value: cty.StringVal("foo")}},
Type: cty.String,
},
"bar_var": &Variable{
Name: "bar_var",
@ -251,6 +271,11 @@ func TestParse_variables(t *testing.T) {
cty.StringVal("foo"),
}),
}},
Type: cty.Tuple([]cty.Type{
cty.String,
cty.String,
cty.String,
}),
},
},
},
@ -282,6 +307,7 @@ func TestParse_variables(t *testing.T) {
VariableAssignment{"default", cty.StringVal("bar"), nil},
VariableAssignment{"varfile", cty.StringVal("wee"), nil},
},
Type: cty.String,
},
},
},
@ -398,6 +424,7 @@ func TestParse_variables(t *testing.T) {
{"default", cty.StringVal("ami-something-something"), nil},
},
Name: "image_id",
Type: cty.String,
Validations: []*VariableValidation{
&VariableValidation{
ErrorMessage: `The image_id value must be a valid AMI id, starting with "ami-".`,
@ -418,8 +445,9 @@ func TestParse_variables(t *testing.T) {
Basedir: filepath.Join("testdata", "variables", "validation"),
InputVariables: Variables{
"image_id": &Variable{
Values: []VariableAssignment{{"default", cty.StringVal("ami-something-something"), nil}},
Values: []VariableAssignment{{"default", cty.StringVal("potato"), nil}},
Name: "image_id",
Type: cty.String,
Validations: []*VariableValidation{
&VariableValidation{
ErrorMessage: `The image_id value must be a valid AMI id, starting with "ami-".`,
@ -477,11 +505,11 @@ func TestVariables_collectVariableValues(t *testing.T) {
"used_string": &Variable{
Type: cty.String,
Values: []VariableAssignment{
{"default", cty.StringVal(`"default_value"`), nil},
{"env", cty.StringVal(`"env_value"`), nil},
{"varfile", cty.StringVal(`"xy"`), nil},
{"varfile", cty.StringVal(`"varfile_value"`), nil},
{"cmd", cty.StringVal(`"cmd_value"`), nil},
{"default", cty.StringVal(`default_value`), nil},
{"env", cty.StringVal(`env_value`), nil},
{"varfile", cty.StringVal(`xy`), nil},
{"varfile", cty.StringVal(`varfile_value`), nil},
{"cmd", cty.StringVal(`cmd_value`), nil},
},
},
},