@@ -93,8 +93,8 @@ func TestMain(m *testing.M) {
93
93
94
94
type mcpContext struct {
95
95
profile Profile
96
- before * func (* mcpContext )
97
- after * func (* mcpContext )
96
+ before func (* mcpContext )
97
+ after func (* mcpContext )
98
98
ctx context.Context
99
99
tempDir string
100
100
cancel context.CancelFunc
@@ -108,8 +108,11 @@ func (c *mcpContext) beforeEach(t *testing.T) {
108
108
c .ctx , c .cancel = context .WithCancel (t .Context ())
109
109
c .tempDir = t .TempDir ()
110
110
c .withKubeConfig (nil )
111
+ if c .profile == nil {
112
+ c .profile = & FullProfile {}
113
+ }
111
114
if c .before != nil {
112
- ( * c .before ) (c )
115
+ c .before (c )
113
116
}
114
117
if c .mcpServer , err = NewSever (Configuration {Profile : c .profile }); err != nil {
115
118
t .Fatal (err )
@@ -136,7 +139,7 @@ func (c *mcpContext) beforeEach(t *testing.T) {
136
139
137
140
func (c * mcpContext ) afterEach () {
138
141
if c .after != nil {
139
- ( * c .after ) (c )
142
+ c .after (c )
140
143
}
141
144
c .cancel ()
142
145
c .mcpServer .Close ()
@@ -192,8 +195,8 @@ func (c *mcpContext) withEnvTest() {
192
195
}
193
196
194
197
// inOpenShift sets up the kubernetes environment to seem to be running OpenShift
195
- func (c * mcpContext ) inOpenShift () func ( ) {
196
- c .withKubeConfig ( envTestRestConfig )
198
+ func inOpenShift (c * mcpContext ) {
199
+ c .withEnvTest ( )
197
200
crdTemplate := `
198
201
{
199
202
"apiVersion": "apiextensions.k8s.io/v1",
@@ -209,14 +212,16 @@ func (c *mcpContext) inOpenShift() func() {
209
212
"names": {"plural": "%s","singular": "%s","kind": "%s"}
210
213
}
211
214
}`
212
- removeProjects := c .crdApply (fmt .Sprintf (crdTemplate , "projects.project.openshift.io" , "project.openshift.io" ,
215
+ c .crdApply (fmt .Sprintf (crdTemplate , "projects.project.openshift.io" , "project.openshift.io" ,
213
216
"Cluster" , "projects" , "project" , "Project" ))
214
- removeRoutes := c .crdApply (fmt .Sprintf (crdTemplate , "routes.route.openshift.io" , "route.openshift.io" ,
217
+ c .crdApply (fmt .Sprintf (crdTemplate , "routes.route.openshift.io" , "route.openshift.io" ,
215
218
"Namespaced" , "routes" , "route" , "Route" ))
216
- return func () {
217
- removeProjects ()
218
- removeRoutes ()
219
- }
219
+ }
220
+
221
+ // inOpenShiftClear clears the kubernetes environment so it no longer seems to be running OpenShift
222
+ func inOpenShiftClear (c * mcpContext ) {
223
+ c .crdDelete ("projects.project.openshift.io" )
224
+ c .crdDelete ("routes.route.openshift.io" )
220
225
}
221
226
222
227
// newKubernetesClient creates a new Kubernetes client with the envTest kubeconfig
@@ -241,8 +246,8 @@ func (c *mcpContext) newApiExtensionsClient() *apiextensionsv1.ApiextensionsV1Cl
241
246
return apiextensionsv1 .NewForConfigOrDie (envTestRestConfig )
242
247
}
243
248
244
- // crdApply creates a CRD from the provided resource string and waits for it to be established, returns a cleanup function
245
- func (c * mcpContext ) crdApply (resource string ) func () {
249
+ // crdApply creates a CRD from the provided resource string and waits for it to be established
250
+ func (c * mcpContext ) crdApply (resource string ) {
246
251
apiExtensionsV1Client := c .newApiExtensionsClient ()
247
252
var crd = & apiextensionsv1spec.CustomResourceDefinition {}
248
253
err := json .Unmarshal ([]byte (resource ), crd )
@@ -251,21 +256,24 @@ func (c *mcpContext) crdApply(resource string) func() {
251
256
panic (fmt .Errorf ("failed to create CRD %v" , err ))
252
257
}
253
258
c .crdWaitUntilReady (crd .Name )
254
- return func () {
255
- err = apiExtensionsV1Client .CustomResourceDefinitions ().Delete (c .ctx , crd .Name , metav1.DeleteOptions {
256
- GracePeriodSeconds : ptr .To (int64 (0 )),
257
- })
258
- iteration := 0
259
- for iteration < 10 {
260
- if _ , derr := apiExtensionsV1Client .CustomResourceDefinitions ().Get (c .ctx , crd .Name , metav1.GetOptions {}); derr != nil {
261
- break
262
- }
263
- time .Sleep (50 * time .Millisecond )
264
- iteration ++
265
- }
266
- if err != nil {
267
- panic (fmt .Errorf ("failed to delete CRD %v" , err ))
259
+ }
260
+
261
+ // crdDelete deletes a CRD by name and waits for it to be removed
262
+ func (c * mcpContext ) crdDelete (name string ) {
263
+ apiExtensionsV1Client := c .newApiExtensionsClient ()
264
+ err := apiExtensionsV1Client .CustomResourceDefinitions ().Delete (c .ctx , name , metav1.DeleteOptions {
265
+ GracePeriodSeconds : ptr .To (int64 (0 )),
266
+ })
267
+ iteration := 0
268
+ for iteration < 100 {
269
+ if _ , derr := apiExtensionsV1Client .CustomResourceDefinitions ().Get (c .ctx , name , metav1.GetOptions {}); derr != nil {
270
+ break
268
271
}
272
+ time .Sleep (5 * time .Millisecond )
273
+ iteration ++
274
+ }
275
+ if err != nil {
276
+ panic (fmt .Errorf ("failed to delete CRD %v" , err ))
269
277
}
270
278
}
271
279
0 commit comments