Skip to content

Commit e0fe25a

Browse files
committed
test: add concurrency to OpenShift env setup
1 parent 6c51c9d commit e0fe25a

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ go 1.24.1
55
require (
66
github.com/fsnotify/fsnotify v1.9.0
77
github.com/mark3labs/mcp-go v0.27.1
8+
github.com/pkg/errors v0.9.1
89
github.com/spf13/afero v1.14.0
910
github.com/spf13/cobra v1.9.1
1011
github.com/spf13/viper v1.20.1
1112
golang.org/x/net v0.40.0
13+
golang.org/x/sync v0.14.0
1214
helm.sh/helm/v3 v3.17.3
1315
k8s.io/api v0.33.1
1416
k8s.io/apiextensions-apiserver v0.33.1
@@ -107,7 +109,6 @@ require (
107109
github.com/opencontainers/image-spec v1.1.0 // indirect
108110
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
109111
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
110-
github.com/pkg/errors v0.9.1 // indirect
111112
github.com/prometheus/client_golang v1.22.0 // indirect
112113
github.com/prometheus/client_model v0.6.1 // indirect
113114
github.com/prometheus/common v0.62.0 // indirect
@@ -135,7 +136,6 @@ require (
135136
go.uber.org/multierr v1.11.0 // indirect
136137
golang.org/x/crypto v0.38.0 // indirect
137138
golang.org/x/oauth2 v0.27.0 // indirect
138-
golang.org/x/sync v0.14.0 // indirect
139139
golang.org/x/sys v0.33.0 // indirect
140140
golang.org/x/term v0.32.0 // indirect
141141
golang.org/x/text v0.25.0 // indirect

pkg/mcp/common_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"github.com/mark3labs/mcp-go/client"
88
"github.com/mark3labs/mcp-go/mcp"
99
"github.com/mark3labs/mcp-go/server"
10+
"github.com/pkg/errors"
1011
"github.com/spf13/afero"
12+
"golang.org/x/sync/errgroup"
1113
corev1 "k8s.io/api/core/v1"
1214
rbacv1 "k8s.io/api/rbac/v1"
1315
apiextensionsv1spec "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -212,16 +214,28 @@ func inOpenShift(c *mcpContext) {
212214
"names": {"plural": "%s","singular": "%s","kind": "%s"}
213215
}
214216
}`
215-
c.crdApply(fmt.Sprintf(crdTemplate, "projects.project.openshift.io", "project.openshift.io",
216-
"Cluster", "projects", "project", "Project"))
217-
c.crdApply(fmt.Sprintf(crdTemplate, "routes.route.openshift.io", "route.openshift.io",
218-
"Namespaced", "routes", "route", "Route"))
217+
tasks, _ := errgroup.WithContext(c.ctx)
218+
tasks.Go(func() error {
219+
return c.crdApply(fmt.Sprintf(crdTemplate, "projects.project.openshift.io", "project.openshift.io",
220+
"Cluster", "projects", "project", "Project"))
221+
})
222+
tasks.Go(func() error {
223+
return c.crdApply(fmt.Sprintf(crdTemplate, "routes.route.openshift.io", "route.openshift.io",
224+
"Namespaced", "routes", "route", "Route"))
225+
})
226+
if err := tasks.Wait(); err != nil {
227+
panic(err)
228+
}
219229
}
220230

221231
// inOpenShiftClear clears the kubernetes environment so it no longer seems to be running OpenShift
222232
func inOpenShiftClear(c *mcpContext) {
223-
c.crdDelete("projects.project.openshift.io")
224-
c.crdDelete("routes.route.openshift.io")
233+
tasks, _ := errgroup.WithContext(c.ctx)
234+
tasks.Go(func() error { return c.crdDelete("projects.project.openshift.io") })
235+
tasks.Go(func() error { return c.crdDelete("routes.route.openshift.io") })
236+
if err := tasks.Wait(); err != nil {
237+
panic(err)
238+
}
225239
}
226240

227241
// newKubernetesClient creates a new Kubernetes client with the envTest kubeconfig
@@ -247,19 +261,20 @@ func (c *mcpContext) newApiExtensionsClient() *apiextensionsv1.ApiextensionsV1Cl
247261
}
248262

249263
// crdApply creates a CRD from the provided resource string and waits for it to be established
250-
func (c *mcpContext) crdApply(resource string) {
264+
func (c *mcpContext) crdApply(resource string) error {
251265
apiExtensionsV1Client := c.newApiExtensionsClient()
252266
var crd = &apiextensionsv1spec.CustomResourceDefinition{}
253267
err := json.Unmarshal([]byte(resource), crd)
254268
_, err = apiExtensionsV1Client.CustomResourceDefinitions().Create(c.ctx, crd, metav1.CreateOptions{})
255269
if err != nil {
256-
panic(fmt.Errorf("failed to create CRD %v", err))
270+
return fmt.Errorf("failed to create CRD %v", err)
257271
}
258272
c.crdWaitUntilReady(crd.Name)
273+
return nil
259274
}
260275

261276
// crdDelete deletes a CRD by name and waits for it to be removed
262-
func (c *mcpContext) crdDelete(name string) {
277+
func (c *mcpContext) crdDelete(name string) error {
263278
apiExtensionsV1Client := c.newApiExtensionsClient()
264279
err := apiExtensionsV1Client.CustomResourceDefinitions().Delete(c.ctx, name, metav1.DeleteOptions{
265280
GracePeriodSeconds: ptr.To(int64(0)),
@@ -273,8 +288,9 @@ func (c *mcpContext) crdDelete(name string) {
273288
iteration++
274289
}
275290
if err != nil {
276-
panic(fmt.Errorf("failed to delete CRD %v", err))
291+
return errors.Wrap(err, "failed to delete CRD")
277292
}
293+
return nil
278294
}
279295

280296
// crdWaitUntilReady waits for a CRD to be established

0 commit comments

Comments
 (0)