-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpvc_test.go
211 lines (180 loc) · 5.38 KB
/
pvc_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package kubeci
import (
"context"
"testing"
"github.com/google/go-github/v45/github"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8sfake "k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
)
func newPVC(name, namespace, org, repo, scope, branch string) runtime.Object {
pvc := &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{APIVersion: corev1.SchemeGroupVersion.String()},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
}
ls := map[string]string{
"somelabel": "unrelated", // assume we can have arbitrary labels on pvcs
}
anns := map[string]string{}
if org != "" {
ls["managedBy"] = "kube-ci"
ls[labelOrg] = labelSafe(org)
}
if repo != "" {
ls[labelRepo] = labelSafe(repo)
}
if scope != "" {
ls[labelScope] = labelSafe(scope)
}
if branch != "" {
ls[labelBranch] = labelSafe(branch)
anns[annBranch] = branch
}
pvc.SetLabels(ls)
pvc.SetAnnotations(anns)
return pvc
}
func TestWebhookDeleteBranchEvent(t *testing.T) {
ctx := context.Background()
expectdels := map[string]struct{}{}
pvcs := []runtime.Object{}
pvcs = append(pvcs, newPVC("unrelated", "default", "", "", "", ""))
pvcs = append(pvcs, newPVC("unrelated", "argo", "", "", "", ""))
pvcs = append(pvcs, newPVC("somepvcwecreated", "argo", "myorg", "myrepo", "project", ""))
pvcs = append(pvcs, newPVC("somepvcwecreated2", "argo", "myorg", "myrepo", "branch", "mybranch"))
expectdels["argo/somepvcwecreated2"] = struct{}{}
pvcs = append(pvcs, newPVC("somepvcwecreated3", "argo", "myorg", "myrepo", "branch", "mybranch2"))
pvcs = append(pvcs, newPVC("someother", "argo", "myorg", "myrepo2", "project", ""))
client := k8sfake.NewSimpleClientset(pvcs...)
sm := &K8sStorageManager{
KubeClient: client,
ManagedBy: "kube-ci",
}
hh := &HookHandler{
Storage: sm,
}
ev := &github.DeleteEvent{
Ref: github.String("mybranch"),
RefType: github.String("branch"),
Repo: &github.Repository{
Name: github.String("myrepo"),
Owner: &github.User{
Login: github.String("myorg"),
},
},
}
_, status := hh.webhookDeleteBranchEvent(ctx, ev)
if status != "OK" {
t.Fatalf("unexpected status, %v", status)
return
}
dels := 0
for _, act := range client.Actions() {
switch act := act.(type) {
case k8stesting.DeleteActionImpl:
dels++
key := act.GetNamespace() + "/" + act.Name
if _, ok := expectdels[key]; !ok {
t.Fatalf("unexpected delete action (key: %s) %v", key, act)
return
}
}
}
if dels != len(expectdels) {
t.Fatalf("wrong number of deletes, want %v, got %v", len(expectdels), dels)
return
}
}
func TestWebhookRepositoryDeleteEvent(t *testing.T) {
ctx := context.Background()
pvcs := []runtime.Object{}
expectdels := map[string]struct{}{}
pvcs = append(pvcs, newPVC("unrelated", "default", "", "", "", ""))
pvcs = append(pvcs, newPVC("unrelated", "argo", "", "", "", ""))
pvcs = append(pvcs, newPVC("somepvcwecreated", "argo", "myorg", "myrepo", "project", ""))
expectdels["argo/somepvcwecreated"] = struct{}{}
pvcs = append(pvcs, newPVC("somepvcwecreated2", "argo", "myorg", "myrepo", "branch", "mybranch"))
expectdels["argo/somepvcwecreated2"] = struct{}{}
pvcs = append(pvcs, newPVC("somepvcwecreated3", "argo", "myorg", "myrepo", "branch", "mybranch2"))
expectdels["argo/somepvcwecreated3"] = struct{}{}
pvcs = append(pvcs, newPVC("someother", "argo", "myorg", "myrepo2", "project", ""))
client := k8sfake.NewSimpleClientset(pvcs...)
sm := &K8sStorageManager{
KubeClient: client,
ManagedBy: "kube-ci",
}
hh := HookHandler{
Storage: sm,
}
ev := &github.RepositoryEvent{
Action: github.String("deleted"),
Repo: &github.Repository{
Name: github.String("myrepo"),
Owner: &github.User{
Login: github.String("myorg"),
},
},
}
_, status := hh.webhookRepositoryDeleteEvent(ctx, ev)
if status != "OK" {
t.Fatalf("unexpected status, %v", status)
return
}
dels := 0
for _, act := range client.Actions() {
switch act := act.(type) {
case k8stesting.DeleteActionImpl:
dels++
key := act.GetNamespace() + "/" + act.Name
if _, ok := expectdels[key]; !ok {
t.Fatalf("unexpected delete action (key: %s) %v", key, act)
return
}
}
}
if dels != len(expectdels) {
t.Fatalf("wrong number of deletes, want %v, got %v", len(expectdels), dels)
return
}
}
func TestWebhookDeleteBranchEvent_BadName(t *testing.T) {
ctx := context.Background()
pvcs := []runtime.Object{}
pvcs = append(pvcs, newPVC("unrelated", "default", "", "", "", ""))
pvcs = append(pvcs, newPVC("unrelated", "argo", "", "", "", ""))
pvcs = append(pvcs, newPVC("somepvcwecreated", "argo", "myorg", "myrepo", "project", ""))
client := k8sfake.NewSimpleClientset(pvcs...)
sm := &K8sStorageManager{
KubeClient: client,
ManagedBy: "kube-ci",
}
hh := &HookHandler{
Storage: sm,
}
ev := &github.DeleteEvent{
Ref: github.String("-my..bad..branch...name-"),
RefType: github.String("branch"),
Repo: &github.Repository{
Name: github.String("myrepo"),
Owner: &github.User{
Login: github.String("myorg"),
},
},
}
_, status := hh.webhookDeleteBranchEvent(ctx, ev)
if status != "OK" {
t.Fatalf("unexpected status, %v", status)
return
}
for _, act := range client.Actions() {
switch act := act.(type) {
case k8stesting.DeleteActionImpl:
t.Errorf("unexpected delete of %v/%v ", act.Namespace, act.Name)
}
}
}