Skip to content

Commit 20cb331

Browse files
committed
test: openshift environment using hooks
1 parent 1f00601 commit 20cb331

File tree

4 files changed

+41
-45
lines changed

4 files changed

+41
-45
lines changed

pkg/mcp/common_test.go

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func TestMain(m *testing.M) {
9393

9494
type 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

137140
func (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

pkg/mcp/namespaces_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ func TestNamespacesList(t *testing.T) {
4747
}
4848

4949
func TestProjectsListInOpenShift(t *testing.T) {
50-
testCase(t, func(c *mcpContext) {
51-
defer c.inOpenShift()() // n.b. two sets of parentheses to invoke the first function
52-
c.mcpServer.server.AddTools(c.mcpServer.initNamespaces()...)
50+
testCaseWithContext(t, &mcpContext{before: inOpenShift, after: inOpenShiftClear}, func(c *mcpContext) {
5351
dynamicClient := dynamic.NewForConfigOrDie(envTestRestConfig)
5452
_, _ = dynamicClient.Resource(schema.GroupVersionResource{Group: "project.openshift.io", Version: "v1", Resource: "projects"}).
5553
Create(c.ctx, &unstructured.Unstructured{Object: map[string]interface{}{

pkg/mcp/pods_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,7 @@ func TestPodsDelete(t *testing.T) {
418418
}
419419

420420
func TestPodsDeleteInOpenShift(t *testing.T) {
421-
testCase(t, func(c *mcpContext) {
422-
// Managed Pod in OpenShift
423-
defer c.inOpenShift()() // n.b. two sets of parentheses to invoke the first function
421+
testCaseWithContext(t, &mcpContext{before: inOpenShift, after: inOpenShiftClear}, func(c *mcpContext) {
424422
managedLabels := map[string]string{
425423
"app.kubernetes.io/managed-by": "kubernetes-mcp-server",
426424
"app.kubernetes.io/name": "a-manged-pod-to-delete",
@@ -710,8 +708,7 @@ func TestPodsRun(t *testing.T) {
710708
}
711709

712710
func TestPodsRunInOpenShift(t *testing.T) {
713-
testCase(t, func(c *mcpContext) {
714-
defer c.inOpenShift()() // n.b. two sets of parentheses to invoke the first function
711+
testCaseWithContext(t, &mcpContext{before: inOpenShift, after: inOpenShiftClear}, func(c *mcpContext) {
715712
t.Run("pods_run with image, namespace, and port returns route with port", func(t *testing.T) {
716713
podsRunInOpenShift, err := c.callTool("pods_run", map[string]interface{}{"image": "nginx", "port": 80})
717714
if err != nil {

pkg/mcp/profiles_test.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,10 @@ func TestFullProfileTools(t *testing.T) {
5252
}
5353

5454
func TestFullProfileToolsInOpenShift(t *testing.T) {
55-
var after func(c *mcpContext)
56-
before := func(c *mcpContext) {
57-
cleanCrds := c.inOpenShift()
58-
after = func(_ *mcpContext) {
59-
cleanCrds()
60-
}
61-
}
6255
mcpCtx := &mcpContext{
6356
profile: &FullProfile{},
64-
before: &before,
65-
after: &after,
57+
before: inOpenShift,
58+
after: inOpenShiftClear,
6659
}
6760
testCaseWithContext(t, mcpCtx, func(c *mcpContext) {
6861
tools, err := c.mcpClient.ListTools(c.ctx, mcp.ListToolsRequest{})

0 commit comments

Comments
 (0)