@@ -93,8 +93,8 @@ func TestMain(m *testing.M) {
9393
9494type mcpContext struct {
9595 profile Profile
96- before * func (* mcpContext )
97- after * func (* mcpContext )
96+ before func (* mcpContext )
97+ after func (* mcpContext )
9898 ctx context.Context
9999 tempDir string
100100 cancel context.CancelFunc
@@ -108,8 +108,11 @@ func (c *mcpContext) beforeEach(t *testing.T) {
108108 c .ctx , c .cancel = context .WithCancel (t .Context ())
109109 c .tempDir = t .TempDir ()
110110 c .withKubeConfig (nil )
111+ if c .profile == nil {
112+ c .profile = & FullProfile {}
113+ }
111114 if c .before != nil {
112- ( * c .before ) (c )
115+ c .before (c )
113116 }
114117 if c .mcpServer , err = NewSever (Configuration {Profile : c .profile }); err != nil {
115118 t .Fatal (err )
@@ -136,7 +139,7 @@ func (c *mcpContext) beforeEach(t *testing.T) {
136139
137140func (c * mcpContext ) afterEach () {
138141 if c .after != nil {
139- ( * c .after ) (c )
142+ c .after (c )
140143 }
141144 c .cancel ()
142145 c .mcpServer .Close ()
@@ -192,8 +195,8 @@ func (c *mcpContext) withEnvTest() {
192195}
193196
194197// 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 ( )
197200 crdTemplate := `
198201 {
199202 "apiVersion": "apiextensions.k8s.io/v1",
@@ -209,14 +212,16 @@ func (c *mcpContext) inOpenShift() func() {
209212 "names": {"plural": "%s","singular": "%s","kind": "%s"}
210213 }
211214 }`
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" ,
213216 "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" ,
215218 "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" )
220225}
221226
222227// newKubernetesClient creates a new Kubernetes client with the envTest kubeconfig
@@ -241,8 +246,8 @@ func (c *mcpContext) newApiExtensionsClient() *apiextensionsv1.ApiextensionsV1Cl
241246 return apiextensionsv1 .NewForConfigOrDie (envTestRestConfig )
242247}
243248
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 ) {
246251 apiExtensionsV1Client := c .newApiExtensionsClient ()
247252 var crd = & apiextensionsv1spec.CustomResourceDefinition {}
248253 err := json .Unmarshal ([]byte (resource ), crd )
@@ -251,21 +256,24 @@ func (c *mcpContext) crdApply(resource string) func() {
251256 panic (fmt .Errorf ("failed to create CRD %v" , err ))
252257 }
253258 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
268271 }
272+ time .Sleep (5 * time .Millisecond )
273+ iteration ++
274+ }
275+ if err != nil {
276+ panic (fmt .Errorf ("failed to delete CRD %v" , err ))
269277 }
270278}
271279
0 commit comments