-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmagefile.go
More file actions
673 lines (565 loc) · 21.2 KB
/
magefile.go
File metadata and controls
673 lines (565 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//go:build mage
package main
import (
"fmt"
"os"
"github.com/konflux-ci/caching/internal"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Kind manages kind cluster operations
type Kind mg.Namespace
// Build manages image building operations
type Build mg.Namespace
// SquidHelm manages squid helm chart operations
type SquidHelm mg.Namespace
// Test manages test execution operations
type Test mg.Namespace
const (
clusterName = "caching"
// SquidImageTag is the tag used for the squid container image
squidImageTag = "localhost/konflux-ci/squid:latest"
// SquidContainerfile is the path to the Containerfile for squid
squidContainerfile = "Containerfile"
// TestImageTag is the tag used for the test container image
testImageTag = "localhost/konflux-ci/squid-test:latest"
// TestContainerfile is the path to the Containerfile for tests
testContainerfile = "test.Containerfile"
)
// Default target - shows available targets
func Default() error {
return sh.Run("mage", "-l")
}
// Test:UnitExporter runs unit tests for the per-site prometheus exporter
func (Test) UnitExporter() error {
fmt.Println("🧪 Running per-site exporter unit tests")
if err := sh.RunV("go", "test", "./cmd/squid-per-site-exporter", "-v"); err != nil {
return fmt.Errorf("unit tests failed: %w", err)
}
return nil
}
// Test:UnitStoreID runs unit tests for the store-id helper
func (Test) UnitStoreID() error {
fmt.Println("🧪 Running store-id helper unit tests")
if err := sh.RunV("go", "test", "./cmd/squid-store-id", "-v"); err != nil {
return fmt.Errorf("Store ID helper unit tests failed: %w", err)
}
return nil
}
// Test:UnitICAPServer runs unit tests for the ICAP server
func (Test) UnitICAPServer() error {
fmt.Println("🧪 Running ICAP server unit tests")
if err := sh.RunV("go", "test", "./cmd/icap-server", "-v"); err != nil {
return fmt.Errorf("ICAP server unit tests failed: %w", err)
}
return nil
}
// Test:UnitHelmTemplate runs unit tests for Helm templates
func (Test) UnitHelmTemplate() error {
fmt.Println("🧪 Running Helm template unit tests")
if err := sh.RunV("go", "test", "./tests/helm/", "-v"); err != nil {
return fmt.Errorf("Helm template unit tests failed: %w", err)
}
return nil
}
// Test:Unit runs all unit tests (no cluster required)
func (Test) Unit() error {
fmt.Println("🧪 Running unit tests")
mg.SerialDeps(Test.UnitExporter, Test.UnitStoreID, Test.UnitICAPServer, Test.UnitHelmTemplate)
return nil
}
// Kind:Up creates or connects to a kind cluster named 'caching'
func (Kind) Up() error {
fmt.Println("🚀 Setting up kind cluster...")
// Check if cluster already exists
exists, err := internal.ClusterExists(clusterName)
if err != nil {
return fmt.Errorf("failed to check cluster existence: %w", err)
}
if exists {
fmt.Printf("✅ Cluster '%s' already exists\n", clusterName)
} else {
fmt.Printf("📦 Creating kind cluster '%s'...\n", clusterName)
err := internal.CreateCluster(clusterName)
if err != nil {
return fmt.Errorf("failed to create cluster: %w", err)
}
fmt.Printf("✅ Cluster '%s' created successfully\n", clusterName)
}
// Export kubeconfig
fmt.Printf("🔧 Exporting kubeconfig for cluster '%s'...\n", clusterName)
err = internal.ExportKubeconfig(clusterName)
if err != nil {
return fmt.Errorf("failed to export kubeconfig: %w", err)
}
fmt.Printf("✅ Kind cluster '%s' is ready!\n", clusterName)
return nil
}
// Kind:UpClean forces recreation of the kind cluster (deletes existing cluster and creates new one)
func (Kind) UpClean() error {
fmt.Println("🚀 Setting up kind cluster (clean recreation)...")
// Check if cluster already exists
exists, err := internal.ClusterExists(clusterName)
if err != nil {
return fmt.Errorf("failed to check cluster existence: %w", err)
}
if exists {
fmt.Printf("🔄 Deleting existing cluster '%s'...\n", clusterName)
err := internal.DeleteCluster(clusterName)
if err != nil {
return fmt.Errorf("failed to delete existing cluster: %w", err)
}
fmt.Printf("✅ Cluster '%s' deleted successfully\n", clusterName)
}
// Create new cluster
fmt.Printf("📦 Creating kind cluster '%s'...\n", clusterName)
err = internal.CreateCluster(clusterName)
if err != nil {
return fmt.Errorf("failed to create cluster: %w", err)
}
fmt.Printf("✅ Cluster '%s' created successfully\n", clusterName)
// Export kubeconfig
fmt.Printf("🔧 Exporting kubeconfig for cluster '%s'...\n", clusterName)
err = internal.ExportKubeconfig(clusterName)
if err != nil {
return fmt.Errorf("failed to export kubeconfig: %w", err)
}
fmt.Printf("✅ Kind cluster '%s' is ready!\n", clusterName)
return nil
}
// Kind:Down tears down the kind cluster
func (Kind) Down() error {
fmt.Println("🔥 Tearing down kind cluster...")
// Check if cluster exists first
exists, err := internal.ClusterExists(clusterName)
if err != nil {
return fmt.Errorf("failed to check cluster existence: %w", err)
}
if !exists {
fmt.Printf("ℹ️ Cluster '%s' does not exist\n", clusterName)
return nil
}
// Delete the cluster
fmt.Printf("🗑️ Deleting kind cluster '%s'...\n", clusterName)
err = internal.DeleteCluster(clusterName)
if err != nil {
return fmt.Errorf("failed to delete cluster: %w", err)
}
fmt.Printf("✅ Cluster '%s' deleted successfully\n", clusterName)
return nil
}
// Kind:Status shows the status of the kind cluster
func (Kind) Status() error {
fmt.Println("📊 Checking kind cluster status...")
// Check if cluster exists
exists, err := internal.ClusterExists(clusterName)
if err != nil {
return fmt.Errorf("failed to check cluster existence: %w", err)
}
if !exists {
fmt.Printf("❌ Cluster '%s' does not exist\n", clusterName)
return nil
}
fmt.Printf("✅ Cluster '%s' exists\n", clusterName)
// Check kubeconfig
kubeconfigPath := os.Getenv("KUBECONFIG")
if kubeconfigPath == "" {
kubeconfigPath = os.Getenv("HOME") + "/.kube/config"
}
// Try to get cluster info
fmt.Printf("🔍 Checking cluster connectivity...\n")
output, err := internal.GetClusterInfo(clusterName)
if err != nil {
fmt.Printf("⚠️ Could not connect to cluster: %v\n", err)
fmt.Printf("💡 Try running 'mage kind:up' to ensure kubeconfig is exported\n")
return nil
}
fmt.Printf("✅ Cluster is accessible:\n%s\n", output)
// Get node status
fmt.Printf("🖥️ Node status:\n")
err = internal.GetNodeStatus(clusterName)
if err != nil {
fmt.Printf("⚠️ Could not get node status: %v\n", err)
}
return nil
}
// Build:Squid builds the Squid container image
func (Build) Squid() error {
fmt.Println("🐳 Building Squid container image...")
// Build the squid image using podman
fmt.Printf("📦 Building image with tag '%s'...\n", squidImageTag)
err := sh.Run("podman", "build", "-t", squidImageTag, "-f", squidContainerfile, ".")
if err != nil {
return fmt.Errorf("failed to build squid image: %w", err)
}
fmt.Printf("✅ Squid image built successfully\n")
// Verify the image was built
fmt.Printf("🔍 Verifying image exists...\n")
err = sh.Run("podman", "images", squidImageTag)
if err != nil {
return fmt.Errorf("failed to verify squid image: %w", err)
}
fmt.Printf("✅ Squid image '%s' is ready!\n", squidImageTag)
return nil
}
// Build:LoadSquid loads the Squid image into the kind cluster
func (Build) LoadSquid() error {
// Ensure dependencies are met
mg.Deps(Kind.Up, Build.Squid)
fmt.Println("📦 Loading Squid image into kind cluster...")
// Load image into kind cluster using process substitution
fmt.Printf("📤 Loading image into kind cluster '%s'...\n", clusterName)
err := sh.Run("bash", "-c", fmt.Sprintf("kind load image-archive --name %s <(podman save %s)", clusterName, squidImageTag))
if err != nil {
return fmt.Errorf("failed to load image into kind cluster: %w", err)
}
// Verify image is available in cluster
fmt.Printf("🔍 Verifying image is available in cluster...\n")
err = internal.GetNodeStatus(clusterName)
if err != nil {
return fmt.Errorf("failed to connect to cluster for verification: %w", err)
}
fmt.Printf("✅ Squid image loaded successfully into kind cluster '%s'!\n", clusterName)
return nil
}
// Build:TestImage builds the test container image
func (Build) TestImage() error {
fmt.Println("🔨 Building test container image...")
// Build the test image using podman
fmt.Printf("📦 Building image with tag '%s'...\n", testImageTag)
err := sh.Run("podman", "build", "-t", testImageTag, "-f", testContainerfile, ".")
if err != nil {
return fmt.Errorf("failed to build test image: %w", err)
}
fmt.Printf("✅ Test image built successfully\n")
// Verify the image was built
fmt.Printf("🔍 Verifying image exists...\n")
err = sh.Run("podman", "images", testImageTag)
if err != nil {
return fmt.Errorf("failed to verify test image: %w", err)
}
fmt.Printf("✅ Test image '%s' is ready!\n", testImageTag)
return nil
}
// Build:LoadTestImage loads the test image into the kind cluster
func (Build) LoadTestImage() error {
// Ensure dependencies are met
mg.Deps(Kind.Up, Build.TestImage)
fmt.Println("📦 Loading test image into kind cluster...")
// Load image into kind cluster using process substitution
fmt.Printf("📤 Loading image into kind cluster '%s'...\n", clusterName)
err := sh.Run("bash", "-c", fmt.Sprintf("kind load image-archive --name %s <(podman save %s)", clusterName, testImageTag))
if err != nil {
return fmt.Errorf("failed to load test image into kind cluster: %w", err)
}
fmt.Printf("✅ Test image loaded successfully into kind cluster '%s'!\n", clusterName)
return nil
}
// SquidHelm:Up deploys the Squid Helm chart to the cluster
func (SquidHelm) Up() error {
// Ensure dependencies are met (squid and test images needed)
mg.Deps(Build.LoadSquid, Build.LoadTestImage)
fmt.Println("⚓ Deploying Squid Helm chart...")
// Ensure required helm repositories are available
fmt.Printf("📦 Ensuring helm repositories are available...\n")
err := internal.EnsureHelmRepo("jetstack", "https://charts.jetstack.io")
if err != nil {
return fmt.Errorf("failed to ensure jetstack repository: %w", err)
}
// Build helm dependencies from lock file
fmt.Printf("📦 Building helm dependencies...\n")
err = sh.Run("helm", "dependency", "build", "./squid")
if err != nil {
return fmt.Errorf("failed to build helm dependencies: %w", err)
}
fmt.Printf("⚓ Installing/upgrading squid helm chart and waiting for readiness...\n")
err = sh.Run(
"helm",
"upgrade",
"squid",
"./squid",
"--install",
"--set", "environment=dev",
"--set", "nginx.enabled=true",
"--set", "test.labelFilter="+os.Getenv("GINKGO_LABEL_FILTER"),
"--wait",
"--timeout=300s",
"--debug",
)
if err != nil {
return fmt.Errorf("failed to install/upgrade helm chart: %w", err)
}
// Show comprehensive deployment status
fmt.Printf("🔍 Verifying deployment status...\n")
err = (SquidHelm{}).Status()
if err != nil {
return fmt.Errorf("deployment verification failed: %w", err)
}
fmt.Printf("✅ Squid helm chart deployed successfully!\n")
return nil
}
// SquidHelm:Down removes the Squid Helm chart from the cluster
func (SquidHelm) Down() error {
fmt.Println("🗑️ Removing Squid Helm chart...")
// Check if release exists first
exists, err := internal.ReleaseExists("squid")
if err != nil {
return fmt.Errorf("failed to check release existence: %w", err)
}
if !exists {
fmt.Printf("ℹ️ Helm release 'squid' does not exist\n")
return nil
}
// Uninstall the helm release
fmt.Printf("🗑️ Uninstalling squid helm release...\n")
err = sh.Run("helm", "uninstall", "squid")
if err != nil {
return fmt.Errorf("failed to uninstall helm chart: %w", err)
}
// Wait for caching namespace to be fully deleted
err = internal.WaitForNamespaceDeleted("caching")
if err != nil {
fmt.Printf("⚠️ Warning: %v\n", err)
// Don't fail the function, just warn - the namespace might be stuck
}
fmt.Printf("✅ Squid helm chart removed successfully!\n")
return nil
}
// SquidHelm:UpClean forces redeployment of the Squid Helm chart (removes and reinstalls)
func (SquidHelm) UpClean() error {
fmt.Println("🔄 Force redeploying Squid Helm chart...")
// Remove existing release
err := (SquidHelm{}).Down()
if err != nil {
return fmt.Errorf("failed to remove existing release: %w", err)
}
// Install fresh release
fmt.Printf("⚓ Installing fresh squid helm chart...\n")
return (SquidHelm{}).Up()
}
// SquidHelm:Status shows the deployment status
func (SquidHelm) Status() error {
fmt.Println("📊 Checking deployment status...")
// Check if squid helm release exists
fmt.Printf("🔍 Checking helm release status...\n")
err := sh.Run("helm", "status", "squid")
if err != nil {
fmt.Printf("❌ Helm release 'squid' not found or not deployed\n")
return fmt.Errorf("helm release not found: %w", err)
}
// Show squid pod status
fmt.Printf("🖥️ Squid pod status:\n")
err = sh.RunV("kubectl", "get", "pods", "-n", "caching", "-l", "app.kubernetes.io/name=squid")
if err != nil {
fmt.Printf("⚠️ Could not get squid pod status: %v\n", err)
}
// Show squid service status
fmt.Printf("🌐 Squid service status:\n")
err = sh.RunV("kubectl", "get", "svc", "-n", "caching", "-l", "app.kubernetes.io/name=squid")
if err != nil {
fmt.Printf("⚠️ Could not get squid service status: %v\n", err)
}
// Show squid statefulset status
fmt.Printf("📦 Squid statefulset status:\n")
err = sh.RunV("kubectl", "get", "statefulset", "-n", "caching", "-l", "app.kubernetes.io/name=squid")
if err != nil {
fmt.Printf("⚠️ Could not get squid statefulset status: %v\n", err)
}
// Show nginx pod status
fmt.Printf("🖥️ Nginx pod status:\n")
err = sh.RunV("kubectl", "get", "pods", "-n", "caching", "-l", "app.kubernetes.io/name=nginx")
if err != nil {
fmt.Printf("⚠️ Could not get nginx pod status: %v\n", err)
}
// Show nginx service status
fmt.Printf("🌐 Nginx service status:\n")
err = sh.RunV("kubectl", "get", "svc", "-n", "caching", "-l", "app.kubernetes.io/name=nginx")
if err != nil {
fmt.Printf("⚠️ Could not get nginx service status: %v\n", err)
}
// Show nginx statefulset status
fmt.Printf("📦 Nginx statefulset status:\n")
err = sh.RunV("kubectl", "get", "statefulset", "-n", "caching", "-l", "app.kubernetes.io/name=nginx")
if err != nil {
fmt.Printf("⚠️ Could not get nginx statefulset status: %v\n", err)
}
fmt.Printf("✅ Deployment status check completed!\n")
return nil
}
// All runs the complete automation workflow
func All() error {
fmt.Println("🎯 Running complete automation workflow...")
fmt.Println("This will set up the complete local dev/test environment")
fmt.Println("(dependencies will be handled automatically)")
fmt.Println()
// Run unit tests first for fast feedback
mg.Deps(Test.Unit)
// SquidHelm.Up will automatically handle all dependencies:
// SquidHelm.Up -> Build.LoadSquid + Build.LoadSquidExporter + Build.LoadTestImage -> Kind.Up + Build.Squid + Build.TestImage
err := (SquidHelm{}).Up()
if err != nil {
return err
}
// Run helm tests to validate the deployment
fmt.Println()
fmt.Println("🧪 Running helm tests to validate deployment...")
err = sh.Run("helm", "test", "squid", "--timeout=15m")
if err != nil {
// Test failed - capture and display logs for debugging
fmt.Println()
fmt.Println("❌ Helm tests failed! Capturing test pod logs for debugging...")
fmt.Println()
fmt.Println("=== Squid Test Pod Logs ===")
logsError := sh.RunV("kubectl", "logs", "-n", "caching", "squid-test")
if logsError != nil {
fmt.Printf("⚠️ Could not retrieve test pod logs: %v\n", logsError)
}
fmt.Println()
return fmt.Errorf("helm tests failed: %w", err)
}
fmt.Println("✅ All helm tests passed!")
// Reset squid helm chart to values.yaml defaults after tests complete
resetSquidToDefaults()
fmt.Println()
fmt.Println("🎉 Complete automation workflow finished successfully!")
fmt.Println("Your local dev/test environment is ready:")
fmt.Println(" • Kind cluster: 'caching'")
fmt.Println(" • Squid forward proxy: http://squid.caching.svc.cluster.local:3128")
fmt.Println(" • Nginx reverse proxy: http://nginx.caching.svc.cluster.local:8080")
fmt.Println(" • Helm tests: ✅ All passing")
fmt.Println(" • Ready for development and testing!")
return nil
}
// Clean removes all resources (cluster, images, etc.)
func Clean() error {
fmt.Println("🧹 Cleaning up all resources...")
fmt.Println("This will remove:")
fmt.Println(" • Kind cluster (including all deployments)")
fmt.Println(" • Built container images")
fmt.Println()
fmt.Printf("🗑️ Removing kind cluster...\n")
err := (Kind{}).Down()
if err != nil {
fmt.Printf("⚠️ Warning: Failed to remove kind cluster: %v\n", err)
}
fmt.Printf("🗑️ Removing container images...\n")
err = sh.Run("podman", "rmi", squidImageTag)
if err != nil {
fmt.Printf("⚠️ Warning: Failed to remove squid image: %v\n", err)
}
err = sh.Run("podman", "rmi", testImageTag)
if err != nil {
fmt.Printf("⚠️ Warning: Failed to remove test image: %v\n", err)
}
fmt.Printf("✅ Resource cleanup completed!\n")
return nil
}
// runClusterTests executes the e2e test suite with mirrord
// replicaCount: if > 0, sets SQUID_REPLICA_COUNT env var for tests
func runClusterTests(replicaCount int) error {
fmt.Println("🔮 Running tests with cluster network access...")
fmt.Println("Tests run as if inside the cluster using mirrord")
fmt.Println("This provides the most realistic testing environment")
// Check if mirrord is available
err := sh.Run("which", "mirrord")
if err != nil {
return fmt.Errorf("mirrord not found in PATH - ensure it's installed: %w", err)
}
// Verify mirrord target pod is ready (deployed by Helm chart)
fmt.Println("⏳ Waiting for mirrord target pod to be ready...")
err = sh.Run("kubectl", "wait", "--for=condition=Ready", "pod/mirrord-test-target", "-n", "caching", "--timeout=60s")
if err != nil {
return fmt.Errorf("mirrord target pod not ready - check Helm deployment: %w", err)
}
// Build test binary with ginkgo for better output
fmt.Println("🔨 Building test binary with ginkgo...")
err = sh.RunWith(map[string]string{
"CGO_ENABLED": "1",
}, "ginkgo", "build", "./tests/e2e/")
if err != nil {
return fmt.Errorf("failed to build test binary with ginkgo: %w", err)
}
// Prepare environment variables for test execution
testEnv := map[string]string{
"CGO_ENABLED": "1",
}
// Add SQUID_REPLICA_COUNT if specified
if replicaCount > 0 {
testEnv["SQUID_REPLICA_COUNT"] = fmt.Sprintf("%d", replicaCount)
fmt.Printf("📝 Setting SQUID_REPLICA_COUNT=%d for test execution\n", replicaCount)
}
// Run tests with mirrord using ginkgo binary
fmt.Println("🚀 Running tests with mirrord connection stealing...")
return sh.RunWithV(map[string]string{
"CGO_ENABLED": "1",
}, "mirrord", "exec", "--config-file", ".mirrord/mirrord.json", "--",
"./tests/e2e/e2e.test", "-ginkgo.v", "-ginkgo.label-filter="+os.Getenv("GINKGO_LABEL_FILTER"))
}
// resetSquidToDefaults resets the squid helm release to values.yaml defaults
// Uses environment=dev and preserves GINKGO_LABEL_FILTER if set
// Errors are logged as warnings but don't cause the function to fail
func resetSquidToDefaults() {
fmt.Println("🔄 Resetting squid to values.yaml defaults...")
err := sh.Run(
"helm",
"upgrade",
"squid",
"./squid",
"--set", "environment=dev",
"--set", "nginx.enabled=true",
"--set", "test.labelFilter="+os.Getenv("GINKGO_LABEL_FILTER"),
"--wait",
"--timeout=300s",
)
if err != nil {
fmt.Printf("⚠️ Warning: Failed to reset squid to values.yaml defaults: %v\n", err)
}
}
// Test:Cluster runs tests with cluster network access via mirrord
func (Test) Cluster() error {
// Ensure cluster and deployment are ready (includes mirrord infrastructure)
mg.Deps(SquidHelm{}.Up)
// Run with default replica count (0 = use values.yaml default)
err := runClusterTests(0)
if err != nil {
return fmt.Errorf("failed to run tests: %w", err)
}
resetSquidToDefaults()
return nil
}
// Test:ClusterMultiReplica runs tests with 3 replicas
func (Test) ClusterMultiReplica() error {
// Set environment variable FIRST, before any dependencies run
// This ensures BeforeSuite in the test suite can read it
fmt.Println("📝 Setting SQUID_REPLICA_COUNT=3 for deployment and tests...")
os.Setenv("SQUID_REPLICA_COUNT", "3")
// Ensure cluster and deployment are ready
mg.Deps(SquidHelm{}.Up)
fmt.Println("🧪 Upgrading deployment to 3 replicas...")
// Upgrade deployment to 3 replicas
err := sh.RunWith(map[string]string{
"SQUID_REPLICA_COUNT": "3",
}, "helm", "upgrade", "squid", "./squid",
"-n=default", "--wait", "--timeout=300s",
"--set", "replicaCount=3",
"--set", "nginx.enabled=true",
"--set", "environment=dev")
if err != nil {
return fmt.Errorf("failed to set replica count to 3: %w", err)
}
// Wait for statefulset to be ready with 3 replicas
fmt.Println("⏳ Waiting for statefulset with 3 replicas to be ready...")
err = sh.Run("kubectl", "wait", "--for=condition=Ready",
"statefulset/squid", "-n", "caching", "--timeout=120s")
if err != nil {
return fmt.Errorf("statefulset not ready: %w", err)
}
// Run tests with replica count 3
// This will pass SQUID_REPLICA_COUNT=3 env var to the test process
err = runClusterTests(3)
if err != nil {
return fmt.Errorf("failed to run tests: %w", err)
}
resetSquidToDefaults()
return nil
}