-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings_test.go
89 lines (83 loc) · 1.9 KB
/
settings_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"encoding/json"
"testing"
mapset "github.com/deckarep/golang-set/v2"
kubewardenProtocol "github.com/kubewarden/policy-sdk-go/protocol"
)
func TestValidateSettings(t *testing.T) {
cases := []struct {
name string
requiredAnnotations map[string]string
forbiddenAnnotations mapset.Set[string]
isValid bool
}{
{
"empty",
map[string]string{},
mapset.NewSet[string](),
true,
},
{
"only required annotations",
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string](),
true,
},
{
"only forbidden annotations",
map[string]string{},
mapset.NewSet[string]("priority"),
true,
},
{
"no contradictions",
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string]("priority"),
true,
},
{
"contradictions",
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string]("cc-center"),
false,
},
}
for _, testCase := range cases {
t.Run(testCase.name, func(t *testing.T) {
settings := Settings{
RequiredAnnotations: testCase.requiredAnnotations,
ForbiddenAnnotations: testCase.forbiddenAnnotations,
}
settingsJSON, err := json.Marshal(&settings)
if err != nil {
t.Errorf("cannot marshal settings: %v", err)
}
responseJSON, err := validateSettings(settingsJSON)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
var response kubewardenProtocol.SettingsValidationResponse
err = json.Unmarshal(responseJSON, &response)
if err != nil {
t.Errorf("cannot unmarshal response: %v", err)
}
if response.Valid != testCase.isValid {
t.Errorf(
"didn't get the expected validation outcome, %v was expected, got %v instead",
testCase.isValid, response.Valid)
if response.Message != nil {
t.Errorf(
"validation message: %s",
*response.Message)
}
}
})
}
}