Skip to content

Commit e5d0fa2

Browse files
Thomas Meckelappilon
authored andcommitted
Added tests for AllOf setting and corrected tests for AtLeastOneOf in helper\schema\schema_test.go
1 parent 4fd4117 commit e5d0fa2

File tree

1 file changed

+272
-1
lines changed

1 file changed

+272
-1
lines changed

helper/schema/schema_test.go

Lines changed: 272 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6359,7 +6359,7 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
63596359
},
63606360

63616361
Config: map[string]interface{}{},
6362-
Err: true,
6362+
Err: false,
63636363
},
63646364

63656365
"Only Unknown Variable Value": {
@@ -6501,3 +6501,274 @@ func Test_panicOnErrUnparsableDefaultTrue(t *testing.T) {
65016501

65026502
os.Setenv(PanicOnErr, oldEnv)
65036503
}
6504+
6505+
func TestValidateAllOfAttributes(t *testing.T) {
6506+
cases := map[string]struct {
6507+
Key string
6508+
Schema map[string]*Schema
6509+
Config map[string]interface{}
6510+
Err bool
6511+
}{
6512+
6513+
"two attributes specified": {
6514+
Key: "whitelist",
6515+
Schema: map[string]*Schema{
6516+
"whitelist": &Schema{
6517+
Type: TypeBool,
6518+
Optional: true,
6519+
AllOf: []string{"blacklist"},
6520+
},
6521+
"blacklist": &Schema{
6522+
Type: TypeBool,
6523+
Optional: true,
6524+
AllOf: []string{"whitelist"},
6525+
},
6526+
},
6527+
6528+
Config: map[string]interface{}{
6529+
"whitelist": true,
6530+
"blacklist": true,
6531+
},
6532+
Err: false,
6533+
},
6534+
6535+
"one attributes specified": {
6536+
Key: "whitelist",
6537+
Schema: map[string]*Schema{
6538+
"whitelist": &Schema{
6539+
Type: TypeBool,
6540+
Optional: true,
6541+
AllOf: []string{"blacklist"},
6542+
},
6543+
"blacklist": &Schema{
6544+
Type: TypeBool,
6545+
Optional: true,
6546+
AllOf: []string{"whitelist"},
6547+
},
6548+
},
6549+
6550+
Config: map[string]interface{}{
6551+
"whitelist": true,
6552+
},
6553+
Err: true,
6554+
},
6555+
6556+
"no attributes specified": {
6557+
Key: "whitelist",
6558+
Schema: map[string]*Schema{
6559+
"whitelist": &Schema{
6560+
Type: TypeBool,
6561+
Optional: true,
6562+
AllOf: []string{"blacklist"},
6563+
},
6564+
"blacklist": &Schema{
6565+
Type: TypeBool,
6566+
Optional: true,
6567+
AllOf: []string{"whitelist"},
6568+
},
6569+
},
6570+
6571+
Config: map[string]interface{}{},
6572+
Err: false,
6573+
},
6574+
6575+
"two attributes of three specified": {
6576+
Key: "whitelist",
6577+
Schema: map[string]*Schema{
6578+
"whitelist": &Schema{
6579+
Type: TypeBool,
6580+
Optional: true,
6581+
AllOf: []string{"purplelist"},
6582+
},
6583+
"blacklist": &Schema{
6584+
Type: TypeBool,
6585+
Optional: true,
6586+
AllOf: []string{"whitelist", "purplelist"},
6587+
},
6588+
"purplelist": &Schema{
6589+
Type: TypeBool,
6590+
Optional: true,
6591+
AllOf: []string{"whitelist"},
6592+
},
6593+
},
6594+
6595+
Config: map[string]interface{}{
6596+
"whitelist": true,
6597+
"purplelist": true,
6598+
},
6599+
Err: false,
6600+
},
6601+
6602+
"three attributes of three specified": {
6603+
Key: "whitelist",
6604+
Schema: map[string]*Schema{
6605+
"whitelist": &Schema{
6606+
Type: TypeBool,
6607+
Optional: true,
6608+
AllOf: []string{"blacklist", "purplelist"},
6609+
},
6610+
"blacklist": &Schema{
6611+
Type: TypeBool,
6612+
Optional: true,
6613+
AllOf: []string{"whitelist", "purplelist"},
6614+
},
6615+
"purplelist": &Schema{
6616+
Type: TypeBool,
6617+
Optional: true,
6618+
AllOf: []string{"whitelist", "blacklist"},
6619+
},
6620+
},
6621+
6622+
Config: map[string]interface{}{
6623+
"whitelist": true,
6624+
"purplelist": true,
6625+
"blacklist": true,
6626+
},
6627+
Err: false,
6628+
},
6629+
6630+
"one attributes of three specified": {
6631+
Key: "whitelist",
6632+
Schema: map[string]*Schema{
6633+
"whitelist": &Schema{
6634+
Type: TypeBool,
6635+
Optional: true,
6636+
AllOf: []string{"blacklist", "purplelist"},
6637+
},
6638+
"blacklist": &Schema{
6639+
Type: TypeBool,
6640+
Optional: true,
6641+
AllOf: []string{"whitelist", "purplelist"},
6642+
},
6643+
"purplelist": &Schema{
6644+
Type: TypeBool,
6645+
Optional: true,
6646+
AllOf: []string{"whitelist", "blacklist"},
6647+
},
6648+
},
6649+
6650+
Config: map[string]interface{}{
6651+
"purplelist": true,
6652+
},
6653+
Err: true,
6654+
},
6655+
6656+
"no attributes of three specified": {
6657+
Key: "whitelist",
6658+
Schema: map[string]*Schema{
6659+
"whitelist": &Schema{
6660+
Type: TypeBool,
6661+
Optional: true,
6662+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6663+
},
6664+
"blacklist": &Schema{
6665+
Type: TypeBool,
6666+
Optional: true,
6667+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6668+
},
6669+
"purplelist": &Schema{
6670+
Type: TypeBool,
6671+
Optional: true,
6672+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6673+
},
6674+
},
6675+
6676+
Config: map[string]interface{}{},
6677+
Err: false,
6678+
},
6679+
6680+
"Only Unknown Variable Value": {
6681+
Schema: map[string]*Schema{
6682+
"whitelist": &Schema{
6683+
Type: TypeBool,
6684+
Optional: true,
6685+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6686+
},
6687+
"blacklist": &Schema{
6688+
Type: TypeBool,
6689+
Optional: true,
6690+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6691+
},
6692+
"purplelist": &Schema{
6693+
Type: TypeBool,
6694+
Optional: true,
6695+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6696+
},
6697+
},
6698+
6699+
Config: map[string]interface{}{
6700+
"whitelist": hcl2shim.UnknownVariableValue,
6701+
},
6702+
6703+
Err: true,
6704+
},
6705+
6706+
"only unknown list value": {
6707+
Schema: map[string]*Schema{
6708+
"whitelist": &Schema{
6709+
Type: TypeList,
6710+
Optional: true,
6711+
Elem: &Schema{Type: TypeString},
6712+
AllOf: []string{"whitelist", "blacklist"},
6713+
},
6714+
"blacklist": &Schema{
6715+
Type: TypeList,
6716+
Optional: true,
6717+
Elem: &Schema{Type: TypeString},
6718+
AllOf: []string{"whitelist", "blacklist"},
6719+
},
6720+
},
6721+
6722+
Config: map[string]interface{}{
6723+
"whitelist": []interface{}{hcl2shim.UnknownVariableValue},
6724+
},
6725+
6726+
Err: true,
6727+
},
6728+
6729+
"Unknown Variable Value and Known Value": {
6730+
Schema: map[string]*Schema{
6731+
"whitelist": &Schema{
6732+
Type: TypeBool,
6733+
Optional: true,
6734+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6735+
},
6736+
"blacklist": &Schema{
6737+
Type: TypeBool,
6738+
Optional: true,
6739+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6740+
},
6741+
"purplelist": &Schema{
6742+
Type: TypeBool,
6743+
Optional: true,
6744+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6745+
},
6746+
},
6747+
6748+
Config: map[string]interface{}{
6749+
"whitelist": hcl2shim.UnknownVariableValue,
6750+
"blacklist": true,
6751+
},
6752+
6753+
Err: true,
6754+
},
6755+
}
6756+
6757+
for tn, tc := range cases {
6758+
t.Run(tn, func(t *testing.T) {
6759+
c := terraform.NewResourceConfigRaw(tc.Config)
6760+
_, es := schemaMap(tc.Schema).Validate(c)
6761+
if len(es) > 0 != tc.Err {
6762+
if len(es) == 0 {
6763+
t.Fatalf("expected error")
6764+
}
6765+
6766+
for _, e := range es {
6767+
t.Fatalf("didn't expect error, got error: %+v", e)
6768+
}
6769+
6770+
t.FailNow()
6771+
}
6772+
})
6773+
}
6774+
}

0 commit comments

Comments
 (0)