|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// verifyOperatorMode checks if the operator is deployed in the expected mode (OLM or non-OLM) |
| 15 | +func verifyOperatorMode(t *testing.T, expectOLM bool) { |
| 16 | + t.Helper() |
| 17 | + |
| 18 | + operatorNamespace := "rhacs-operator-system" |
| 19 | + subscriptionName := "stackrox-operator-subscription" |
| 20 | + |
| 21 | + // Check for OLM Subscription (OLM-specific resource) |
| 22 | + cmd := exec.Command("kubectl", "get", "subscription", subscriptionName, "-n", operatorNamespace) |
| 23 | + err := cmd.Run() |
| 24 | + olmExists := (err == nil) |
| 25 | + |
| 26 | + if expectOLM && !olmExists { |
| 27 | + t.Fatalf("Expected OLM operator but Subscription not found") |
| 28 | + } |
| 29 | + if !expectOLM && olmExists { |
| 30 | + t.Fatalf("Expected non-OLM operator but Subscription found (indicates OLM deployment)") |
| 31 | + } |
| 32 | + |
| 33 | + t.Logf("✓ Operator is deployed in expected mode (OLM: %v)", expectOLM) |
| 34 | +} |
| 35 | + |
| 36 | +// verifyOperatorDeploymentExists checks if the operator deployment exists |
| 37 | +func verifyOperatorDeploymentExists(t *testing.T) { |
| 38 | + t.Helper() |
| 39 | + |
| 40 | + operatorNamespace := "rhacs-operator-system" |
| 41 | + deploymentName := "rhacs-operator-controller-manager" |
| 42 | + |
| 43 | + cmd := exec.Command("kubectl", "get", "deployment", deploymentName, "-n", operatorNamespace) |
| 44 | + if err := cmd.Run(); err != nil { |
| 45 | + t.Fatalf("Operator deployment not found: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Also check if deployment is ready |
| 49 | + cmd = exec.Command("kubectl", "get", "deployment", deploymentName, "-n", operatorNamespace, |
| 50 | + "-o", "jsonpath={.status.readyReplicas}") |
| 51 | + output, err := cmd.Output() |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to check operator deployment readiness: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + readyReplicas := strings.TrimSpace(string(output)) |
| 57 | + if readyReplicas == "" || readyReplicas == "0" { |
| 58 | + t.Fatalf("Operator deployment exists but has no ready replicas") |
| 59 | + } |
| 60 | + |
| 61 | + t.Logf("✓ Operator deployment exists and is ready (%s replicas)", readyReplicas) |
| 62 | +} |
| 63 | + |
| 64 | +// TestOLMToNonOLMSwitch tests switching from OLM operator to non-OLM operator |
| 65 | +func TestOLMToNonOLMSwitch(t *testing.T) { |
| 66 | + if os.Getenv("SKIP_OLM_TESTS") != "" { |
| 67 | + t.Skip("SKIP_OLM_TESTS is set") |
| 68 | + } |
| 69 | + |
| 70 | + // Create temporary envrc file |
| 71 | + envrcFile, err := os.CreateTemp("", ".envrc.roxie-test-*") |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("Failed to create temp envrc: %v", err) |
| 74 | + } |
| 75 | + envrcPath := envrcFile.Name() |
| 76 | + envrcFile.Close() |
| 77 | + defer os.Remove(envrcPath) |
| 78 | + |
| 79 | + // Step 1: Deploy central with OLM operator |
| 80 | + t.Log("=== Step 1: Deploy central with OLM operator ===") |
| 81 | + args := append([]string{roxieBinary, "deploy", "central", "--olm", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 82 | + runCommand(t, deployTimeout, nil, args...) |
| 83 | + |
| 84 | + // Verify operator is in OLM mode |
| 85 | + t.Log("Verifying operator is in OLM mode") |
| 86 | + verifyOperatorMode(t, true) |
| 87 | + verifyOperatorDeploymentExists(t) |
| 88 | + |
| 89 | + // Verify central namespace exists |
| 90 | + verifyNamespaceExists(t, "acs-central") |
| 91 | + |
| 92 | + // Brief pause |
| 93 | + time.Sleep(10 * time.Second) |
| 94 | + |
| 95 | + // Step 2: Deploy central again without OLM (should switch modes) |
| 96 | + t.Log("=== Step 2: Redeploy central without OLM (triggering mode switch) ===") |
| 97 | + args = append([]string{roxieBinary, "deploy", "central", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 98 | + runCommand(t, deployTimeout, nil, args...) |
| 99 | + |
| 100 | + // Verify operator switched to non-OLM mode |
| 101 | + t.Log("Verifying operator switched to non-OLM mode") |
| 102 | + verifyOperatorMode(t, false) |
| 103 | + verifyOperatorDeploymentExists(t) |
| 104 | + |
| 105 | + // Verify central namespace still exists |
| 106 | + verifyNamespaceExists(t, "acs-central") |
| 107 | + |
| 108 | + // Cleanup |
| 109 | + t.Log("=== Cleaning up ===") |
| 110 | + teardownArgs := []string{roxieBinary, "teardown", "central"} |
| 111 | + runCommand(t, teardownTimeout, nil, teardownArgs...) |
| 112 | + |
| 113 | + verifyNamespaceAbsent(t, "acs-central") |
| 114 | +} |
| 115 | + |
| 116 | +// TestNonOLMToOLMSwitch tests switching from non-OLM operator to OLM operator |
| 117 | +func TestNonOLMToOLMSwitch(t *testing.T) { |
| 118 | + if os.Getenv("SKIP_OLM_TESTS") != "" { |
| 119 | + t.Skip("SKIP_OLM_TESTS is set") |
| 120 | + } |
| 121 | + |
| 122 | + // Create temporary envrc file |
| 123 | + envrcFile, err := os.CreateTemp("", ".envrc.roxie-test-*") |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("Failed to create temp envrc: %v", err) |
| 126 | + } |
| 127 | + envrcPath := envrcFile.Name() |
| 128 | + envrcFile.Close() |
| 129 | + defer os.Remove(envrcPath) |
| 130 | + |
| 131 | + // Step 1: Deploy central without OLM (non-OLM operator) |
| 132 | + t.Log("=== Step 1: Deploy central with non-OLM operator ===") |
| 133 | + args := append([]string{roxieBinary, "deploy", "central", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 134 | + runCommand(t, deployTimeout, nil, args...) |
| 135 | + |
| 136 | + // Verify operator is in non-OLM mode |
| 137 | + t.Log("Verifying operator is in non-OLM mode") |
| 138 | + verifyOperatorMode(t, false) |
| 139 | + verifyOperatorDeploymentExists(t) |
| 140 | + |
| 141 | + // Verify central namespace exists |
| 142 | + verifyNamespaceExists(t, "acs-central") |
| 143 | + |
| 144 | + // Brief pause |
| 145 | + time.Sleep(10 * time.Second) |
| 146 | + |
| 147 | + // Step 2: Deploy central again with OLM (should switch modes) |
| 148 | + t.Log("=== Step 2: Redeploy central with OLM (triggering mode switch) ===") |
| 149 | + args = append([]string{roxieBinary, "deploy", "central", "--olm", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 150 | + runCommand(t, deployTimeout, nil, args...) |
| 151 | + |
| 152 | + // Verify operator switched to OLM mode |
| 153 | + t.Log("Verifying operator switched to OLM mode") |
| 154 | + verifyOperatorMode(t, true) |
| 155 | + verifyOperatorDeploymentExists(t) |
| 156 | + |
| 157 | + // Verify central namespace still exists |
| 158 | + verifyNamespaceExists(t, "acs-central") |
| 159 | + |
| 160 | + // Cleanup |
| 161 | + t.Log("=== Cleaning up ===") |
| 162 | + teardownArgs := []string{roxieBinary, "teardown", "central"} |
| 163 | + runCommand(t, teardownTimeout, nil, teardownArgs...) |
| 164 | + |
| 165 | + verifyNamespaceAbsent(t, "acs-central") |
| 166 | +} |
| 167 | + |
| 168 | +// TestOLMOperatorVersionUpgrade tests that OLM operator version mismatches trigger teardown and redeploy |
| 169 | +func TestOLMOperatorVersionUpgrade(t *testing.T) { |
| 170 | + if os.Getenv("SKIP_OLM_TESTS") != "" { |
| 171 | + t.Skip("SKIP_OLM_TESTS is set") |
| 172 | + } |
| 173 | + |
| 174 | + // This test requires two different operator versions |
| 175 | + // We can simulate this by deploying twice with the same version, |
| 176 | + // but the logic should handle version changes by tearing down and redeploying |
| 177 | + |
| 178 | + // Create temporary envrc file |
| 179 | + envrcFile, err := os.CreateTemp("", ".envrc.roxie-test-*") |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Failed to create temp envrc: %v", err) |
| 182 | + } |
| 183 | + envrcPath := envrcFile.Name() |
| 184 | + envrcFile.Close() |
| 185 | + defer os.Remove(envrcPath) |
| 186 | + |
| 187 | + // Step 1: Deploy central with OLM operator |
| 188 | + t.Log("=== Step 1: Deploy central with OLM operator ===") |
| 189 | + args := append([]string{roxieBinary, "deploy", "central", "--olm", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 190 | + runCommand(t, deployTimeout, nil, args...) |
| 191 | + |
| 192 | + // Verify operator is in OLM mode |
| 193 | + t.Log("Verifying initial OLM operator deployment") |
| 194 | + verifyOperatorMode(t, true) |
| 195 | + verifyOperatorDeploymentExists(t) |
| 196 | + |
| 197 | + // Get the current operator version |
| 198 | + operatorNamespace := "rhacs-operator-system" |
| 199 | + deploymentName := "rhacs-operator-controller-manager" |
| 200 | + cmd := exec.Command("kubectl", "get", "deployment", deploymentName, "-n", operatorNamespace, |
| 201 | + "-o", "jsonpath={.spec.template.spec.containers[0].image}") |
| 202 | + output, err := cmd.Output() |
| 203 | + if err != nil { |
| 204 | + t.Fatalf("Failed to get operator image: %v", err) |
| 205 | + } |
| 206 | + initialImage := strings.TrimSpace(string(output)) |
| 207 | + t.Logf("Initial operator image: %s", initialImage) |
| 208 | + |
| 209 | + // Brief pause |
| 210 | + time.Sleep(10 * time.Second) |
| 211 | + |
| 212 | + // Step 2: Redeploy with same version (should skip if version matches) |
| 213 | + t.Log("=== Step 2: Redeploy with same version (should detect correct version) ===") |
| 214 | + args = append([]string{roxieBinary, "deploy", "central", "--olm", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 215 | + runCommand(t, deployTimeout, nil, args...) |
| 216 | + |
| 217 | + // Verify operator is still in OLM mode and deployment exists |
| 218 | + t.Log("Verifying operator is still deployed correctly") |
| 219 | + verifyOperatorMode(t, true) |
| 220 | + verifyOperatorDeploymentExists(t) |
| 221 | + |
| 222 | + // Note: In a real version upgrade test, we would set a different MAIN_IMAGE_TAG |
| 223 | + // and verify that the operator was torn down and redeployed with the new version. |
| 224 | + // For now, this test validates the basic flow. |
| 225 | + |
| 226 | + // Cleanup |
| 227 | + t.Log("=== Cleaning up ===") |
| 228 | + teardownArgs := []string{roxieBinary, "teardown", "central"} |
| 229 | + runCommand(t, teardownTimeout, nil, teardownArgs...) |
| 230 | + |
| 231 | + verifyNamespaceAbsent(t, "acs-central") |
| 232 | +} |
| 233 | + |
| 234 | +// TestSecuredClusterWithOLMSwitch tests that secured-cluster deployment also respects OLM mode switches |
| 235 | +func TestSecuredClusterWithOLMSwitch(t *testing.T) { |
| 236 | + if os.Getenv("SKIP_OLM_TESTS") != "" { |
| 237 | + t.Skip("SKIP_OLM_TESTS is set") |
| 238 | + } |
| 239 | + |
| 240 | + // Create temporary envrc file |
| 241 | + envrcFile, err := os.CreateTemp("", ".envrc.roxie-test-*") |
| 242 | + if err != nil { |
| 243 | + t.Fatalf("Failed to create temp envrc: %v", err) |
| 244 | + } |
| 245 | + envrcPath := envrcFile.Name() |
| 246 | + envrcFile.Close() |
| 247 | + defer os.Remove(envrcPath) |
| 248 | + |
| 249 | + // Step 1: Deploy central with OLM |
| 250 | + t.Log("=== Step 1: Deploy central with OLM ===") |
| 251 | + args := append([]string{roxieBinary, "deploy", "central", "--olm", "--envrc", envrcPath}, commonDeployArgsNoPortForward...) |
| 252 | + runCommand(t, deployTimeout, nil, args...) |
| 253 | + |
| 254 | + verifyOperatorMode(t, true) |
| 255 | + verifyNamespaceExists(t, "acs-central") |
| 256 | + |
| 257 | + // Load envrc for secured-cluster |
| 258 | + envrcEnv, err := loadEnvrcFile(envrcPath) |
| 259 | + if err != nil { |
| 260 | + t.Fatalf("Failed to load envrc file: %v", err) |
| 261 | + } |
| 262 | + |
| 263 | + // Step 2: Deploy secured-cluster (should reuse OLM operator) |
| 264 | + t.Log("=== Step 2: Deploy secured-cluster (should reuse OLM operator) ===") |
| 265 | + args = append([]string{roxieBinary, "deploy", "secured-cluster", "--olm"}, commonDeployArgsNoPortForward...) |
| 266 | + runCommand(t, deployTimeout, envrcEnv, args...) |
| 267 | + |
| 268 | + // Verify operator is still in OLM mode |
| 269 | + verifyOperatorMode(t, true) |
| 270 | + verifyNamespaceExists(t, "acs-sensor") |
| 271 | + |
| 272 | + // Step 3: Switch to non-OLM by redeploying secured-cluster without --olm |
| 273 | + t.Log("=== Step 3: Redeploy secured-cluster without OLM (triggering mode switch) ===") |
| 274 | + args = append([]string{roxieBinary, "deploy", "secured-cluster"}, commonDeployArgsNoPortForward...) |
| 275 | + runCommand(t, deployTimeout, envrcEnv, args...) |
| 276 | + |
| 277 | + // Verify operator switched to non-OLM mode |
| 278 | + verifyOperatorMode(t, false) |
| 279 | + verifyNamespaceExists(t, "acs-sensor") |
| 280 | + |
| 281 | + // Cleanup |
| 282 | + t.Log("=== Cleaning up ===") |
| 283 | + teardownArgs := []string{roxieBinary, "teardown", "both"} |
| 284 | + runCommand(t, teardownTimeout, nil, teardownArgs...) |
| 285 | + |
| 286 | + verifyNamespaceAbsent(t, "acs-central") |
| 287 | + verifyNamespaceAbsent(t, "acs-sensor") |
| 288 | +} |
0 commit comments