|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + temporaliov1alpha1 "github.com/temporalio/temporal-worker-controller/api/v1alpha1" |
| 13 | + "github.com/temporalio/temporal-worker-controller/internal/controller" |
| 14 | + "github.com/temporalio/temporal-worker-controller/internal/controller/clientpool" |
| 15 | + "go.temporal.io/sdk/log" |
| 16 | + appsv1 "k8s.io/api/apps/v1" |
| 17 | + corev1 "k8s.io/api/core/v1" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | + "k8s.io/apimachinery/pkg/types" |
| 20 | + "k8s.io/client-go/kubernetes/scheme" |
| 21 | + "k8s.io/client-go/rest" |
| 22 | + ctrl "sigs.k8s.io/controller-runtime" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/envtest" |
| 25 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/log/zap" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/manager" |
| 28 | +) |
| 29 | + |
| 30 | +// setupKubebuilderAssets sets up the KUBEBUILDER_ASSETS environment variable if not already set |
| 31 | +func setupKubebuilderAssets() error { |
| 32 | + if os.Getenv("KUBEBUILDER_ASSETS") != "" { |
| 33 | + return nil // Already set |
| 34 | + } |
| 35 | + |
| 36 | + // Try to find the assets using setup-envtest |
| 37 | + cmd := exec.Command("setup-envtest", "use", "1.28.0", "--bin-dir", "bin") |
| 38 | + output, err := cmd.Output() |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + // Parse the output to get the path |
| 44 | + // The output format is typically: "export KUBEBUILDER_ASSETS=/path/to/assets" |
| 45 | + // We need to extract just the path |
| 46 | + assetsPath := string(output) |
| 47 | + if len(assetsPath) > 0 { |
| 48 | + // Remove any trailing newlines |
| 49 | + assetsPath = assetsPath[:len(assetsPath)-1] |
| 50 | + os.Setenv("KUBEBUILDER_ASSETS", assetsPath) |
| 51 | + } |
| 52 | + |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +// setupTestEnvironment sets up the test environment with envtest |
| 57 | +func setupTestEnvironment(t *testing.T) (*rest.Config, client.Client, manager.Manager, *clientpool.ClientPool, func()) { |
| 58 | + // Setup kubebuilder assets for IDE testing |
| 59 | + if err := setupKubebuilderAssets(); err != nil { |
| 60 | + t.Logf("Warning: Could not setup kubebuilder assets automatically: %v", err) |
| 61 | + t.Logf("You may need to run 'make envtest' first or set KUBEBUILDER_ASSETS manually") |
| 62 | + } |
| 63 | + |
| 64 | + logf.SetLogger(zap.New(zap.WriteTo(os.Stdout), zap.UseDevMode(true))) |
| 65 | + |
| 66 | + t.Log("bootstrapping test environment") |
| 67 | + //startDevServer(t) |
| 68 | + testEnv := &envtest.Environment{ |
| 69 | + CRDDirectoryPaths: []string{ |
| 70 | + filepath.Join("..", "helm", "temporal-worker-controller", "templates", "crds"), |
| 71 | + }, |
| 72 | + ErrorIfCRDPathMissing: true, |
| 73 | + } |
| 74 | + |
| 75 | + cfg, err := testEnv.Start() |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("failed to start test environment: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + err = temporaliov1alpha1.AddToScheme(scheme.Scheme) // is this installing CRDs? |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("failed to add scheme: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + k8sClient, err := client.New(cfg, client.Options{Scheme: scheme.Scheme}) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("failed to create k8s client: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + // Create manager |
| 91 | + mgr, err := ctrl.NewManager(cfg, ctrl.Options{ |
| 92 | + Scheme: scheme.Scheme, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("failed to create manager: %v", err) |
| 96 | + } |
| 97 | + |
| 98 | + // Create client pool |
| 99 | + clientPool := clientpool.New(log.NewStructuredLogger(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ |
| 100 | + AddSource: false, |
| 101 | + Level: nil, |
| 102 | + ReplaceAttr: nil, |
| 103 | + }))), k8sClient) |
| 104 | + |
| 105 | + // Setup controller |
| 106 | + reconciler := &controller.TemporalWorkerDeploymentReconciler{ |
| 107 | + Client: mgr.GetClient(), |
| 108 | + Scheme: mgr.GetScheme(), |
| 109 | + TemporalClientPool: clientPool, |
| 110 | + } |
| 111 | + |
| 112 | + err = reconciler.SetupWithManager(mgr) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("failed to setup controller: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + // Start manager |
| 118 | + ctx, cancel := context.WithCancel(context.Background()) |
| 119 | + go func() { |
| 120 | + if err := mgr.Start(ctx); err != nil { |
| 121 | + t.Errorf("failed to start manager: %v", err) |
| 122 | + } |
| 123 | + }() |
| 124 | + |
| 125 | + // Return cleanup function |
| 126 | + cleanup := func() { |
| 127 | + cancel() |
| 128 | + if err := testEnv.Stop(); err != nil { |
| 129 | + t.Errorf("failed to stop test environment: %v", err) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return cfg, k8sClient, mgr, clientPool, cleanup |
| 134 | +} |
| 135 | + |
| 136 | +func applyDeployment(t *testing.T, ctx context.Context, k8sClient client.Client, deploymentName, namespace string) { |
| 137 | + var deployment appsv1.Deployment |
| 138 | + if err := k8sClient.Get(ctx, types.NamespacedName{ |
| 139 | + Name: deploymentName, |
| 140 | + Namespace: namespace, |
| 141 | + }, &deployment); err != nil { |
| 142 | + t.Fatalf("failed to get deployment: %v", err) |
| 143 | + } |
| 144 | + |
| 145 | + // Set deployment status to Available to simulate a healthy deployment |
| 146 | + // This is necessary because envtest doesn't actually start pods |
| 147 | + now := metav1.Now() |
| 148 | + deployment.Status = appsv1.DeploymentStatus{ // todo: only do this if workers come up successfully |
| 149 | + Replicas: *deployment.Spec.Replicas, |
| 150 | + UpdatedReplicas: *deployment.Spec.Replicas, |
| 151 | + ReadyReplicas: *deployment.Spec.Replicas, |
| 152 | + AvailableReplicas: *deployment.Spec.Replicas, |
| 153 | + UnavailableReplicas: 0, |
| 154 | + Conditions: []appsv1.DeploymentCondition{ |
| 155 | + { |
| 156 | + Type: appsv1.DeploymentAvailable, |
| 157 | + Status: corev1.ConditionTrue, |
| 158 | + LastUpdateTime: now, |
| 159 | + LastTransitionTime: now, |
| 160 | + Reason: "MinimumReplicasAvailable", |
| 161 | + Message: "Deployment has minimum availability.", |
| 162 | + }, |
| 163 | + { |
| 164 | + Type: appsv1.DeploymentProgressing, |
| 165 | + Status: corev1.ConditionTrue, |
| 166 | + LastUpdateTime: now, |
| 167 | + LastTransitionTime: now, |
| 168 | + Reason: "NewReplicaSetAvailable", |
| 169 | + Message: "ReplicaSet is available.", |
| 170 | + }, |
| 171 | + }, |
| 172 | + } |
| 173 | + |
| 174 | + if err := k8sClient.Status().Update(ctx, &deployment); err != nil { |
| 175 | + t.Fatalf("failed to update deployment status: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + for i := int32(0); i < *(deployment.Spec.Replicas); i++ { |
| 179 | + go runHelloWorldWorker(ctx, deployment.Spec.Template) // todo: cancel these appropriately |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// createTestNamespace creates a test namespace |
| 184 | +func createTestNamespace(t *testing.T, k8sClient client.Client) *corev1.Namespace { |
| 185 | + testNamespace := &corev1.Namespace{ |
| 186 | + ObjectMeta: metav1.ObjectMeta{ |
| 187 | + Name: "test-integration-" + time.Now().Format("20060102150405"), |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + if err := k8sClient.Create(context.Background(), testNamespace); err != nil { |
| 192 | + t.Fatalf("failed to create test namespace: %v", err) |
| 193 | + } |
| 194 | + |
| 195 | + return testNamespace |
| 196 | +} |
| 197 | + |
| 198 | +// cleanupTestNamespace cleans up the test namespace |
| 199 | +func cleanupTestNamespace(t *testing.T, cfg *rest.Config, k8sClient client.Client, testNamespace *corev1.Namespace) { |
| 200 | + if testNamespace != nil { |
| 201 | + if err := k8sClient.Delete(context.Background(), testNamespace); err != nil { |
| 202 | + t.Errorf("failed to delete test namespace: %v", err) |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments