|
| 1 | +// Package controllers contains integration tests for the VirtualMCPServer controller |
| 2 | +package controllers |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + "go.uber.org/zap/zapcore" |
| 13 | + appsv1 "k8s.io/api/apps/v1" |
| 14 | + corev1 "k8s.io/api/core/v1" |
| 15 | + rbacv1 "k8s.io/api/rbac/v1" |
| 16 | + "k8s.io/client-go/kubernetes/scheme" |
| 17 | + "k8s.io/client-go/rest" |
| 18 | + ctrl "sigs.k8s.io/controller-runtime" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 21 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 23 | + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" |
| 24 | + |
| 25 | + mcpv1alpha1 "github.com/stacklok/toolhive/cmd/thv-operator/api/v1alpha1" |
| 26 | + "github.com/stacklok/toolhive/cmd/thv-operator/controllers" |
| 27 | + ctrlutil "github.com/stacklok/toolhive/cmd/thv-operator/pkg/controllerutil" |
| 28 | +) |
| 29 | + |
| 30 | +// These tests use Ginkgo (BDD-style Go testing framework). Refer to |
| 31 | +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. |
| 32 | + |
| 33 | +var ( |
| 34 | + cfg *rest.Config |
| 35 | + k8sClient client.Client |
| 36 | + testEnv *envtest.Environment |
| 37 | + ctx context.Context |
| 38 | + cancel context.CancelFunc |
| 39 | +) |
| 40 | + |
| 41 | +func TestControllers(t *testing.T) { |
| 42 | + t.Parallel() |
| 43 | + RegisterFailHandler(Fail) |
| 44 | + |
| 45 | + suiteConfig, reporterConfig := GinkgoConfiguration() |
| 46 | + // Only show verbose output for failures |
| 47 | + reporterConfig.Verbose = false |
| 48 | + reporterConfig.VeryVerbose = false |
| 49 | + reporterConfig.FullTrace = false |
| 50 | + |
| 51 | + RunSpecs(t, "VirtualMCPServer Controller Integration Test Suite", suiteConfig, reporterConfig) |
| 52 | +} |
| 53 | + |
| 54 | +var _ = BeforeSuite(func() { |
| 55 | + // Only log errors unless a test fails |
| 56 | + logLevel := zapcore.ErrorLevel |
| 57 | + |
| 58 | + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true), zap.Level(logLevel))) |
| 59 | + |
| 60 | + ctx, cancel = context.WithCancel(context.TODO()) |
| 61 | + |
| 62 | + By("bootstrapping test environment") |
| 63 | + testEnv = &envtest.Environment{ |
| 64 | + CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "deploy", "charts", "operator-crds", "crds")}, |
| 65 | + ErrorIfCRDPathMissing: true, |
| 66 | + } |
| 67 | + |
| 68 | + var err error |
| 69 | + // cfg is defined in this file globally. |
| 70 | + cfg, err = testEnv.Start() |
| 71 | + Expect(err).NotTo(HaveOccurred()) |
| 72 | + Expect(cfg).NotTo(BeNil()) |
| 73 | + |
| 74 | + err = mcpv1alpha1.AddToScheme(scheme.Scheme) |
| 75 | + Expect(err).NotTo(HaveOccurred()) |
| 76 | + |
| 77 | + // Add other schemes that the controllers use |
| 78 | + err = appsv1.AddToScheme(scheme.Scheme) |
| 79 | + Expect(err).NotTo(HaveOccurred()) |
| 80 | + |
| 81 | + err = corev1.AddToScheme(scheme.Scheme) |
| 82 | + Expect(err).NotTo(HaveOccurred()) |
| 83 | + |
| 84 | + err = rbacv1.AddToScheme(scheme.Scheme) |
| 85 | + Expect(err).NotTo(HaveOccurred()) |
| 86 | + |
| 87 | + //+kubebuilder:scaffold:scheme |
| 88 | + |
| 89 | + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) |
| 90 | + Expect(err).NotTo(HaveOccurred()) |
| 91 | + Expect(k8sClient).NotTo(BeNil()) |
| 92 | + |
| 93 | + // Start the controller manager |
| 94 | + k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 95 | + Scheme: scheme.Scheme, |
| 96 | + Metrics: metricsserver.Options{ |
| 97 | + BindAddress: "0", // Disable metrics server for tests to avoid port conflicts |
| 98 | + }, |
| 99 | + HealthProbeBindAddress: "0", // Disable health probe for tests |
| 100 | + }) |
| 101 | + Expect(err).ToNot(HaveOccurred()) |
| 102 | + |
| 103 | + // Set up field indexing for MCPServer.Spec.GroupRef |
| 104 | + if err := k8sManager.GetFieldIndexer().IndexField(ctx, &mcpv1alpha1.MCPServer{}, "spec.groupRef", func(obj client.Object) []string { |
| 105 | + mcpServer := obj.(*mcpv1alpha1.MCPServer) |
| 106 | + if mcpServer.Spec.GroupRef == "" { |
| 107 | + return nil |
| 108 | + } |
| 109 | + return []string{mcpServer.Spec.GroupRef} |
| 110 | + }); err != nil { |
| 111 | + Expect(err).ToNot(HaveOccurred()) |
| 112 | + } |
| 113 | + |
| 114 | + // Register the MCPGroup controller (required by VirtualMCPServer) |
| 115 | + err = (&controllers.MCPGroupReconciler{ |
| 116 | + Client: k8sManager.GetClient(), |
| 117 | + }).SetupWithManager(k8sManager) |
| 118 | + Expect(err).ToNot(HaveOccurred()) |
| 119 | + |
| 120 | + // Register the VirtualMCPServer controller |
| 121 | + err = (&controllers.VirtualMCPServerReconciler{ |
| 122 | + Client: k8sManager.GetClient(), |
| 123 | + Scheme: k8sManager.GetScheme(), |
| 124 | + PlatformDetector: ctrlutil.NewSharedPlatformDetector(), |
| 125 | + }).SetupWithManager(k8sManager) |
| 126 | + Expect(err).ToNot(HaveOccurred()) |
| 127 | + |
| 128 | + // Start the manager in a goroutine |
| 129 | + go func() { |
| 130 | + defer GinkgoRecover() |
| 131 | + err = k8sManager.Start(ctx) |
| 132 | + Expect(err).ToNot(HaveOccurred(), "failed to run manager") |
| 133 | + }() |
| 134 | + |
| 135 | +}) |
| 136 | + |
| 137 | +var _ = AfterSuite(func() { |
| 138 | + By("tearing down the test environment") |
| 139 | + cancel() |
| 140 | + // Give it some time to shut down gracefully |
| 141 | + time.Sleep(100 * time.Millisecond) |
| 142 | + err := testEnv.Stop() |
| 143 | + Expect(err).NotTo(HaveOccurred()) |
| 144 | +}) |
0 commit comments