-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_test.go
129 lines (118 loc) · 2.93 KB
/
validate_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"log"
"testing"
mapset "github.com/deckarep/golang-set/v2"
corev1 "github.com/kubewarden/k8s-objects/api/core/v1"
apimachinery "github.com/kubewarden/k8s-objects/apimachinery/pkg/apis/meta/v1"
kubewardenProtocol "github.com/kubewarden/policy-sdk-go/protocol"
)
func TestValidateAdmissionReview(t *testing.T) {
cases := []struct {
name string
currentAnnotations map[string]string
requiredAnnotations map[string]string
forbiddenAnnotations mapset.Set[string]
isAccepted bool
isMutated bool
}{
{
"object has already the required annotations",
map[string]string{
"cc-center": "marketing",
},
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string](),
true,
false,
},
{
"object has a forbidden annotation",
map[string]string{
"team": "marketing",
},
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string]("team"),
false,
false,
},
{
"mutate object - add key",
map[string]string{},
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string]("team"),
true,
true,
},
{
"mutate object - update key",
map[string]string{
"cc-center": "foo",
},
map[string]string{
"cc-center": "marketing",
},
mapset.NewSet[string]("team"),
true,
true,
},
}
for _, testCase := range cases {
settings := Settings{
RequiredAnnotations: testCase.requiredAnnotations,
ForbiddenAnnotations: testCase.forbiddenAnnotations,
}
obj := buildPodJSON(testCase.currentAnnotations)
t.Run(testCase.name, func(t *testing.T) {
admissionRequest := kubewardenProtocol.KubernetesAdmissionRequest{
Object: obj,
}
responseJSON, err := validateAdmissionReview(settings, admissionRequest)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
response := kubewardenProtocol.ValidationResponse{}
if err = json.Unmarshal(responseJSON, &response); err != nil {
t.Errorf("cannot unmarshal validation response: %v", err)
}
if response.Accepted != testCase.isAccepted {
t.Errorf(
"didn't get the expected validation outcome, %v was expected, got %v instead",
testCase.isAccepted, response.Accepted)
if response.Message != nil {
t.Errorf(
"policy message: %s",
*response.Message)
}
}
if response.MutatedObject == nil && testCase.isMutated {
t.Errorf("object has not been mutated")
}
if response.MutatedObject != nil && !testCase.isMutated {
t.Errorf("object should not have been mutated")
}
})
}
}
func buildPodJSON(annotations map[string]string) json.RawMessage {
metadata := apimachinery.ObjectMeta{
Name: "test",
Namespace: "default",
Annotations: annotations,
}
pod := corev1.Pod{
Metadata: &metadata,
}
podJSON, err := json.Marshal(&pod)
if err != nil {
log.Fatalf("cannot marshall namespace: %v", err)
}
return podJSON
}