@@ -158,12 +158,7 @@ func TestAdmitCaps(t *testing.T) {
158
158
159
159
func testSCCAdmit (testCaseName string , sccs []* securityapi.SecurityContextConstraints , pod * kapi.Pod , shouldPass bool , t * testing.T ) {
160
160
tc := setupClientSet ()
161
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
162
- cache := securitylisters .NewSecurityContextConstraintsLister (indexer )
163
- for _ , scc := range sccs {
164
- indexer .Add (scc )
165
- }
166
-
161
+ cache := createSCCLister (t , sccs )
167
162
plugin := NewTestAdmission (cache , tc )
168
163
169
164
attrs := kadmission .NewAttributesRecord (pod , nil , kapi .Kind ("Pod" ).WithVersion ("version" ), pod .Namespace , pod .Name , kapi .Resource ("pods" ).WithVersion ("version" ), "" , kadmission .Create , & user.DefaultInfo {})
@@ -236,11 +231,10 @@ func TestAdmitSuccess(t *testing.T) {
236
231
Groups : []string {"system:serviceaccounts" },
237
232
}
238
233
239
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
240
- cache := securitylisters .NewSecurityContextConstraintsLister (indexer )
241
-
242
- indexer .Add (saExactSCC )
243
- indexer .Add (saSCC )
234
+ cache := createSCCLister (t , []* securityapi.SecurityContextConstraints {
235
+ saExactSCC ,
236
+ saSCC ,
237
+ })
244
238
245
239
// create the admission plugin
246
240
p := NewTestAdmission (cache , tc )
@@ -810,11 +804,7 @@ func TestMatchingSecurityContextConstraints(t *testing.T) {
810
804
},
811
805
}
812
806
813
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
814
- cache := securitylisters .NewSecurityContextConstraintsLister (indexer )
815
- for _ , scc := range sccs {
816
- indexer .Add (scc )
817
- }
807
+ cache := createSCCLister (t , sccs )
818
808
819
809
// single match cases
820
810
testCases := map [string ]struct {
@@ -945,16 +935,7 @@ func TestAdmitWithPrioritizedSCC(t *testing.T) {
945
935
// is using the sort strategy we expect.
946
936
947
937
tc := setupClientSet ()
948
-
949
- indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
950
- cache := securitylisters .NewSecurityContextConstraintsLister (indexer )
951
-
952
- for _ , scc := range sccsToSort {
953
- err := indexer .Add (scc )
954
- if err != nil {
955
- t .Fatalf ("error adding sccs to store: %v" , err )
956
- }
957
- }
938
+ cache := createSCCLister (t , sccsToSort )
958
939
959
940
// create the admission plugin
960
941
plugin := NewTestAdmission (cache , tc )
@@ -1187,3 +1168,14 @@ func setupClientSet() *clientsetfake.Clientset {
1187
1168
1188
1169
return clientsetfake .NewSimpleClientset (namespace , serviceAccount )
1189
1170
}
1171
+
1172
+ func createSCCLister (t * testing.T , sccs []* securityapi.SecurityContextConstraints ) securitylisters.SecurityContextConstraintsLister {
1173
+ indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {cache .NamespaceIndex : cache .MetaNamespaceIndexFunc })
1174
+ lister := securitylisters .NewSecurityContextConstraintsLister (indexer )
1175
+ for _ , scc := range sccs {
1176
+ if err := indexer .Add (scc ); err != nil {
1177
+ t .Fatalf ("error adding SCC to store: %v" , err )
1178
+ }
1179
+ }
1180
+ return lister
1181
+ }
0 commit comments