@@ -20,9 +20,9 @@ import (
20
20
21
21
var testNamespace = "test-inventory-namespace"
22
22
var inventoryObjName = "test-inventory-obj"
23
- var pod1Name = "pod-1 "
24
- var pod2Name = "pod-2 "
25
- var pod3Name = "pod-3 "
23
+ var namespaceName = "namespace "
24
+ var pdbName = "pdb "
25
+ var roleName = "role "
26
26
27
27
var testInventoryLabel = "test-app-label"
28
28
@@ -40,58 +40,58 @@ var inventoryObj = unstructured.Unstructured{
40
40
},
41
41
}
42
42
43
- var pod1 = unstructured.Unstructured {
43
+ var namespace = unstructured.Unstructured {
44
44
Object : map [string ]interface {}{
45
45
"apiVersion" : "v1" ,
46
- "kind" : "Pod " ,
46
+ "kind" : "Namespace " ,
47
47
"metadata" : map [string ]interface {}{
48
- "name" : pod1Name ,
48
+ "name" : namespaceName ,
49
49
"namespace" : testNamespace ,
50
50
"uid" : "uid1" ,
51
51
},
52
52
},
53
53
}
54
54
55
- var pod1Info = & resource.Info {
55
+ var namespaceInfo = & resource.Info {
56
56
Namespace : testNamespace ,
57
- Name : pod1Name ,
58
- Object : & pod1 ,
57
+ Name : namespaceName ,
58
+ Object : & namespace ,
59
59
}
60
60
61
- var pod2 = unstructured.Unstructured {
61
+ var pdb = unstructured.Unstructured {
62
62
Object : map [string ]interface {}{
63
- "apiVersion" : "v1 " ,
64
- "kind" : "Pod " ,
63
+ "apiVersion" : "policy/v1beta1 " ,
64
+ "kind" : "PodDisruptionBudget " ,
65
65
"metadata" : map [string ]interface {}{
66
- "name" : pod2Name ,
66
+ "name" : pdbName ,
67
67
"namespace" : testNamespace ,
68
68
"uid" : "uid2" ,
69
69
},
70
70
},
71
71
}
72
72
73
- var pod2Info = & resource.Info {
73
+ var pdbInfo = & resource.Info {
74
74
Namespace : testNamespace ,
75
- Name : pod2Name ,
76
- Object : & pod2 ,
75
+ Name : pdbName ,
76
+ Object : & pdb ,
77
77
}
78
78
79
- var pod3 = unstructured.Unstructured {
79
+ var role = unstructured.Unstructured {
80
80
Object : map [string ]interface {}{
81
- "apiVersion" : "v1" ,
82
- "kind" : "Pod " ,
81
+ "apiVersion" : "rbac.authorization.k8s.io/ v1" ,
82
+ "kind" : "Role " ,
83
83
"metadata" : map [string ]interface {}{
84
- "name" : pod3Name ,
84
+ "name" : roleName ,
85
85
"namespace" : testNamespace ,
86
86
"uid" : "uid3" ,
87
87
},
88
88
},
89
89
}
90
90
91
- var pod3Info = & resource.Info {
91
+ var roleInfo = & resource.Info {
92
92
Namespace : testNamespace ,
93
- Name : pod3Name ,
94
- Object : & pod3 ,
93
+ Name : roleName ,
94
+ Object : & role ,
95
95
}
96
96
97
97
// Returns a inventory object with the inventory set from
@@ -151,32 +151,32 @@ func TestPrune(t *testing.T) {
151
151
isError : false ,
152
152
},
153
153
"Past and current objects are the same; no pruned objects" : {
154
- pastInfos : []* resource.Info {pod1Info , pod2Info },
155
- currentInfos : []* resource.Info {pod2Info , pod1Info },
154
+ pastInfos : []* resource.Info {namespaceInfo , pdbInfo },
155
+ currentInfos : []* resource.Info {pdbInfo , namespaceInfo },
156
156
prunedInfos : []* resource.Info {},
157
157
isError : false ,
158
158
},
159
159
"No past objects; no pruned objects" : {
160
160
pastInfos : []* resource.Info {},
161
- currentInfos : []* resource.Info {pod2Info , pod1Info },
161
+ currentInfos : []* resource.Info {pdbInfo , namespaceInfo },
162
162
prunedInfos : []* resource.Info {},
163
163
isError : false ,
164
164
},
165
- "No current objects; all previous objects pruned" : {
166
- pastInfos : []* resource.Info {pod1Info , pod2Info , pod3Info },
165
+ "No current objects; all previous objects pruned in correct order " : {
166
+ pastInfos : []* resource.Info {namespaceInfo , pdbInfo , roleInfo },
167
167
currentInfos : []* resource.Info {},
168
- prunedInfos : []* resource.Info {pod1Info , pod2Info , pod3Info },
168
+ prunedInfos : []* resource.Info {pdbInfo , roleInfo , namespaceInfo },
169
169
isError : false ,
170
170
},
171
171
"Omitted object is pruned" : {
172
- pastInfos : []* resource.Info {pod1Info , pod2Info },
173
- currentInfos : []* resource.Info {pod2Info , pod3Info },
174
- prunedInfos : []* resource.Info {pod1Info },
172
+ pastInfos : []* resource.Info {namespaceInfo , pdbInfo },
173
+ currentInfos : []* resource.Info {pdbInfo , roleInfo },
174
+ prunedInfos : []* resource.Info {namespaceInfo },
175
175
isError : false ,
176
176
},
177
177
"Prevent delete lifecycle annotation stops pruning" : {
178
- pastInfos : []* resource.Info {preventDeleteInfo , pod2Info },
179
- currentInfos : []* resource.Info {pod2Info , pod3Info },
178
+ pastInfos : []* resource.Info {preventDeleteInfo , pdbInfo },
179
+ currentInfos : []* resource.Info {pdbInfo , roleInfo },
180
180
prunedInfos : []* resource.Info {},
181
181
isError : false ,
182
182
},
@@ -191,29 +191,42 @@ func TestPrune(t *testing.T) {
191
191
// Set up the currently applied objects.
192
192
currentInventoryInfo := createInventoryInfo ("current-group" , tc .currentInfos ... )
193
193
currentInfos := append (tc .currentInfos , currentInventoryInfo )
194
- // The event channel can not block; make sure its bigger than all
195
- // the events that can be put on it.
196
- eventChannel := make (chan event.Event , len (tc .pastInfos )+ 1 ) // Add one for inventory object
197
- defer close (eventChannel )
198
194
// Set up the fake dynamic client to recognize all objects, and the RESTMapper.
199
195
po .client = fake .NewSimpleDynamicClient (scheme .Scheme ,
200
- pod1Info .Object , pod2Info .Object , pod3Info .Object )
196
+ namespaceInfo .Object , pdbInfo .Object , roleInfo .Object )
201
197
po .mapper = testrestmapper .TestOnlyStaticRESTMapper (scheme .Scheme ,
202
198
scheme .Scheme .PrioritizedVersionsAllGroups ()... )
203
- // Run the prune and validate.
204
- err := po .Prune (currentInfos , eventChannel , Options {
205
- DryRun : true ,
206
- })
199
+ // The event channel can not block; make sure its bigger than all
200
+ // the events that can be put on it.
201
+ eventChannel := make (chan event.Event , len (tc .pastInfos )+ 1 ) // Add one for inventory object
202
+ err := func () error {
203
+ defer close (eventChannel )
204
+ // Run the prune and validate.
205
+ return po .Prune (currentInfos , eventChannel , Options {
206
+ DryRun : true ,
207
+ })
208
+ }()
209
+
207
210
if ! tc .isError {
208
211
if err != nil {
209
212
t .Fatalf ("Unexpected error during Prune(): %#v" , err )
210
213
}
211
- // Validate the prune events on the event channel.
212
- expectedPruneEvents := len (tc .prunedInfos ) + 1 // One extra for pruning inventory object
213
- actualPruneEvents := len (eventChannel )
214
- if expectedPruneEvents != actualPruneEvents {
215
- t .Errorf ("Expected (%d) prune events, got (%d)" ,
216
- expectedPruneEvents , actualPruneEvents )
214
+
215
+ var actualPruneEvents []event.Event
216
+ for e := range eventChannel {
217
+ actualPruneEvents = append (actualPruneEvents , e )
218
+ }
219
+ if want , got := len (tc .prunedInfos )+ 1 , len (actualPruneEvents ); want != got {
220
+ t .Errorf ("Expected (%d) prune events, got (%d)" , want , got )
221
+ }
222
+
223
+ for i , info := range tc .prunedInfos {
224
+ e := actualPruneEvents [i ]
225
+ expKind := info .Object .GetObjectKind ().GroupVersionKind ().Kind
226
+ actKind := e .PruneEvent .Object .GetObjectKind ().GroupVersionKind ().Kind
227
+ if expKind != actKind {
228
+ t .Errorf ("Expected kind %s, got %s" , expKind , actKind )
229
+ }
217
230
}
218
231
} else if err == nil {
219
232
t .Fatalf ("Expected error during Prune() but received none" )
0 commit comments