Skip to content

Commit 6cc84bf

Browse files
Thomas Meckeltmeckel
authored andcommitted
Added tests for AllOf setting and corrected tests for AtLeastOneOf in helper\schema\schema_test.go
1 parent 81ae466 commit 6cc84bf

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
@@ -6537,7 +6537,7 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
65376537
},
65386538

65396539
Config: map[string]interface{}{},
6540-
Err: true,
6540+
Err: false,
65416541
},
65426542

65436543
"Only Unknown Variable Value": {
@@ -6635,3 +6635,274 @@ func TestValidateAtLeastOneOfAttributes(t *testing.T) {
66356635
})
66366636
}
66376637
}
6638+
6639+
func TestValidateAllOfAttributes(t *testing.T) {
6640+
cases := map[string]struct {
6641+
Key string
6642+
Schema map[string]*Schema
6643+
Config map[string]interface{}
6644+
Err bool
6645+
}{
6646+
6647+
"two attributes specified": {
6648+
Key: "whitelist",
6649+
Schema: map[string]*Schema{
6650+
"whitelist": &Schema{
6651+
Type: TypeBool,
6652+
Optional: true,
6653+
AllOf: []string{"blacklist"},
6654+
},
6655+
"blacklist": &Schema{
6656+
Type: TypeBool,
6657+
Optional: true,
6658+
AllOf: []string{"whitelist"},
6659+
},
6660+
},
6661+
6662+
Config: map[string]interface{}{
6663+
"whitelist": true,
6664+
"blacklist": true,
6665+
},
6666+
Err: false,
6667+
},
6668+
6669+
"one attributes specified": {
6670+
Key: "whitelist",
6671+
Schema: map[string]*Schema{
6672+
"whitelist": &Schema{
6673+
Type: TypeBool,
6674+
Optional: true,
6675+
AllOf: []string{"blacklist"},
6676+
},
6677+
"blacklist": &Schema{
6678+
Type: TypeBool,
6679+
Optional: true,
6680+
AllOf: []string{"whitelist"},
6681+
},
6682+
},
6683+
6684+
Config: map[string]interface{}{
6685+
"whitelist": true,
6686+
},
6687+
Err: true,
6688+
},
6689+
6690+
"no attributes specified": {
6691+
Key: "whitelist",
6692+
Schema: map[string]*Schema{
6693+
"whitelist": &Schema{
6694+
Type: TypeBool,
6695+
Optional: true,
6696+
AllOf: []string{"blacklist"},
6697+
},
6698+
"blacklist": &Schema{
6699+
Type: TypeBool,
6700+
Optional: true,
6701+
AllOf: []string{"whitelist"},
6702+
},
6703+
},
6704+
6705+
Config: map[string]interface{}{},
6706+
Err: false,
6707+
},
6708+
6709+
"two attributes of three specified": {
6710+
Key: "whitelist",
6711+
Schema: map[string]*Schema{
6712+
"whitelist": &Schema{
6713+
Type: TypeBool,
6714+
Optional: true,
6715+
AllOf: []string{"purplelist"},
6716+
},
6717+
"blacklist": &Schema{
6718+
Type: TypeBool,
6719+
Optional: true,
6720+
AllOf: []string{"whitelist", "purplelist"},
6721+
},
6722+
"purplelist": &Schema{
6723+
Type: TypeBool,
6724+
Optional: true,
6725+
AllOf: []string{"whitelist"},
6726+
},
6727+
},
6728+
6729+
Config: map[string]interface{}{
6730+
"whitelist": true,
6731+
"purplelist": true,
6732+
},
6733+
Err: false,
6734+
},
6735+
6736+
"three attributes of three specified": {
6737+
Key: "whitelist",
6738+
Schema: map[string]*Schema{
6739+
"whitelist": &Schema{
6740+
Type: TypeBool,
6741+
Optional: true,
6742+
AllOf: []string{"blacklist", "purplelist"},
6743+
},
6744+
"blacklist": &Schema{
6745+
Type: TypeBool,
6746+
Optional: true,
6747+
AllOf: []string{"whitelist", "purplelist"},
6748+
},
6749+
"purplelist": &Schema{
6750+
Type: TypeBool,
6751+
Optional: true,
6752+
AllOf: []string{"whitelist", "blacklist"},
6753+
},
6754+
},
6755+
6756+
Config: map[string]interface{}{
6757+
"whitelist": true,
6758+
"purplelist": true,
6759+
"blacklist": true,
6760+
},
6761+
Err: false,
6762+
},
6763+
6764+
"one attributes of three specified": {
6765+
Key: "whitelist",
6766+
Schema: map[string]*Schema{
6767+
"whitelist": &Schema{
6768+
Type: TypeBool,
6769+
Optional: true,
6770+
AllOf: []string{"blacklist", "purplelist"},
6771+
},
6772+
"blacklist": &Schema{
6773+
Type: TypeBool,
6774+
Optional: true,
6775+
AllOf: []string{"whitelist", "purplelist"},
6776+
},
6777+
"purplelist": &Schema{
6778+
Type: TypeBool,
6779+
Optional: true,
6780+
AllOf: []string{"whitelist", "blacklist"},
6781+
},
6782+
},
6783+
6784+
Config: map[string]interface{}{
6785+
"purplelist": true,
6786+
},
6787+
Err: true,
6788+
},
6789+
6790+
"no attributes of three specified": {
6791+
Key: "whitelist",
6792+
Schema: map[string]*Schema{
6793+
"whitelist": &Schema{
6794+
Type: TypeBool,
6795+
Optional: true,
6796+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6797+
},
6798+
"blacklist": &Schema{
6799+
Type: TypeBool,
6800+
Optional: true,
6801+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6802+
},
6803+
"purplelist": &Schema{
6804+
Type: TypeBool,
6805+
Optional: true,
6806+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6807+
},
6808+
},
6809+
6810+
Config: map[string]interface{}{},
6811+
Err: false,
6812+
},
6813+
6814+
"Only Unknown Variable Value": {
6815+
Schema: map[string]*Schema{
6816+
"whitelist": &Schema{
6817+
Type: TypeBool,
6818+
Optional: true,
6819+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6820+
},
6821+
"blacklist": &Schema{
6822+
Type: TypeBool,
6823+
Optional: true,
6824+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6825+
},
6826+
"purplelist": &Schema{
6827+
Type: TypeBool,
6828+
Optional: true,
6829+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6830+
},
6831+
},
6832+
6833+
Config: map[string]interface{}{
6834+
"whitelist": hcl2shim.UnknownVariableValue,
6835+
},
6836+
6837+
Err: true,
6838+
},
6839+
6840+
"only unknown list value": {
6841+
Schema: map[string]*Schema{
6842+
"whitelist": &Schema{
6843+
Type: TypeList,
6844+
Optional: true,
6845+
Elem: &Schema{Type: TypeString},
6846+
AllOf: []string{"whitelist", "blacklist"},
6847+
},
6848+
"blacklist": &Schema{
6849+
Type: TypeList,
6850+
Optional: true,
6851+
Elem: &Schema{Type: TypeString},
6852+
AllOf: []string{"whitelist", "blacklist"},
6853+
},
6854+
},
6855+
6856+
Config: map[string]interface{}{
6857+
"whitelist": []interface{}{hcl2shim.UnknownVariableValue},
6858+
},
6859+
6860+
Err: true,
6861+
},
6862+
6863+
"Unknown Variable Value and Known Value": {
6864+
Schema: map[string]*Schema{
6865+
"whitelist": &Schema{
6866+
Type: TypeBool,
6867+
Optional: true,
6868+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6869+
},
6870+
"blacklist": &Schema{
6871+
Type: TypeBool,
6872+
Optional: true,
6873+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6874+
},
6875+
"purplelist": &Schema{
6876+
Type: TypeBool,
6877+
Optional: true,
6878+
AllOf: []string{"whitelist", "blacklist", "purplelist"},
6879+
},
6880+
},
6881+
6882+
Config: map[string]interface{}{
6883+
"whitelist": hcl2shim.UnknownVariableValue,
6884+
"blacklist": true,
6885+
},
6886+
6887+
Err: true,
6888+
},
6889+
}
6890+
6891+
for tn, tc := range cases {
6892+
t.Run(tn, func(t *testing.T) {
6893+
c := terraform.NewResourceConfigRaw(tc.Config)
6894+
_, es := schemaMap(tc.Schema).Validate(c)
6895+
if len(es) > 0 != tc.Err {
6896+
if len(es) == 0 {
6897+
t.Fatalf("expected error")
6898+
}
6899+
6900+
for _, e := range es {
6901+
t.Fatalf("didn't expect error, got error: %+v", e)
6902+
}
6903+
6904+
t.FailNow()
6905+
}
6906+
})
6907+
}
6908+
}

0 commit comments

Comments
 (0)