|
6 | 6 | "fmt" |
7 | 7 | "scripts/camunda-deployer/pkg/types" |
8 | 8 | "strings" |
| 9 | + "sync/atomic" |
9 | 10 | "testing" |
10 | 11 | "time" |
11 | 12 | ) |
@@ -505,3 +506,148 @@ func TestUpgradeInstall_NoRetryOnNonTransient(t *testing.T) { |
505 | 506 | t.Errorf("expected exactly 1 helm invocation for non-transient error, got %d", attempts) |
506 | 507 | } |
507 | 508 | } |
| 509 | + |
| 510 | +func TestDeployCompanionCharts_DeploysInParallel(t *testing.T) { |
| 511 | + restore := stubHelm( |
| 512 | + func(ctx context.Context, args []string, workDir string) error { return nil }, |
| 513 | + func(ctx context.Context, name, url string) error { return nil }, |
| 514 | + func(ctx context.Context) error { return nil }, |
| 515 | + ) |
| 516 | + defer restore() |
| 517 | + |
| 518 | + var inFlight, maxInFlight, total int32 |
| 519 | + helmRunCapturing = func(ctx context.Context, args []string, workDir string) (string, error) { |
| 520 | + atomic.AddInt32(&total, 1) |
| 521 | + cur := atomic.AddInt32(&inFlight, 1) |
| 522 | + for { |
| 523 | + prev := atomic.LoadInt32(&maxInFlight) |
| 524 | + if cur <= prev || atomic.CompareAndSwapInt32(&maxInFlight, prev, cur) { |
| 525 | + break |
| 526 | + } |
| 527 | + } |
| 528 | + time.Sleep(50 * time.Millisecond) |
| 529 | + atomic.AddInt32(&inFlight, -1) |
| 530 | + return "", nil |
| 531 | + } |
| 532 | + |
| 533 | + err := deployCompanionCharts(context.Background(), types.Options{ |
| 534 | + Namespace: "ns", |
| 535 | + CompanionCharts: []types.CompanionChart{ |
| 536 | + {ChartRef: "/charts/a", ReleaseName: "a"}, |
| 537 | + {ChartRef: "/charts/b", ReleaseName: "b"}, |
| 538 | + }, |
| 539 | + }) |
| 540 | + if err != nil { |
| 541 | + t.Fatalf("unexpected error: %v", err) |
| 542 | + } |
| 543 | + if total != 2 { |
| 544 | + t.Errorf("expected both companions to deploy, got %d invocations", total) |
| 545 | + } |
| 546 | + if maxInFlight < 2 { |
| 547 | + t.Errorf("expected companions to overlap (maxInFlight>=2), got %d — deploys ran serially", maxInFlight) |
| 548 | + } |
| 549 | +} |
| 550 | + |
| 551 | +func TestDeployCompanionCharts_ErrorCancelsSiblings(t *testing.T) { |
| 552 | + restore := stubHelm( |
| 553 | + func(ctx context.Context, args []string, workDir string) error { return nil }, |
| 554 | + func(ctx context.Context, name, url string) error { return nil }, |
| 555 | + func(ctx context.Context) error { return nil }, |
| 556 | + ) |
| 557 | + defer restore() |
| 558 | + |
| 559 | + var siblingCancelled atomic.Bool |
| 560 | + helmRunCapturing = func(ctx context.Context, args []string, workDir string) (string, error) { |
| 561 | + if containsArg(args, "fails") { |
| 562 | + return "", fmt.Errorf("boom") |
| 563 | + } |
| 564 | + // Slow sibling: block until the failing companion cancels the group ctx. |
| 565 | + select { |
| 566 | + case <-ctx.Done(): |
| 567 | + siblingCancelled.Store(true) |
| 568 | + return "", ctx.Err() |
| 569 | + case <-time.After(2 * time.Second): |
| 570 | + return "", nil |
| 571 | + } |
| 572 | + } |
| 573 | + |
| 574 | + err := deployCompanionCharts(context.Background(), types.Options{ |
| 575 | + Namespace: "ns", |
| 576 | + CompanionCharts: []types.CompanionChart{ |
| 577 | + {ChartRef: "/charts/fails", ReleaseName: "fails"}, |
| 578 | + {ChartRef: "/charts/slow", ReleaseName: "slow"}, |
| 579 | + }, |
| 580 | + }) |
| 581 | + if err == nil { |
| 582 | + t.Fatal("expected error from failing companion, got nil") |
| 583 | + } |
| 584 | + if !strings.Contains(err.Error(), "companion chart") { |
| 585 | + t.Errorf("error = %q, want it to mention the failing companion chart", err.Error()) |
| 586 | + } |
| 587 | + if !siblingCancelled.Load() { |
| 588 | + t.Error("expected the in-flight sibling to observe context cancellation") |
| 589 | + } |
| 590 | +} |
| 591 | + |
| 592 | +func TestDeployCompanionCharts_SingleCompanionSucceeds(t *testing.T) { |
| 593 | + restore := stubHelm( |
| 594 | + func(ctx context.Context, args []string, workDir string) error { return nil }, |
| 595 | + func(ctx context.Context, name, url string) error { return nil }, |
| 596 | + func(ctx context.Context) error { return nil }, |
| 597 | + ) |
| 598 | + defer restore() |
| 599 | + |
| 600 | + var calls int32 |
| 601 | + helmRunCapturing = func(ctx context.Context, args []string, workDir string) (string, error) { |
| 602 | + atomic.AddInt32(&calls, 1) |
| 603 | + return "", nil |
| 604 | + } |
| 605 | + |
| 606 | + err := deployCompanionCharts(context.Background(), types.Options{ |
| 607 | + Namespace: "ns", |
| 608 | + CompanionCharts: []types.CompanionChart{ |
| 609 | + {ChartRef: "/charts/only", ReleaseName: "only"}, |
| 610 | + }, |
| 611 | + }) |
| 612 | + if err != nil { |
| 613 | + t.Fatalf("unexpected error: %v", err) |
| 614 | + } |
| 615 | + if calls != 1 { |
| 616 | + t.Errorf("expected exactly 1 companion deploy, got %d", calls) |
| 617 | + } |
| 618 | +} |
| 619 | + |
| 620 | +func TestDeployCompanionCharts_RepoRegistrationSerialized(t *testing.T) { |
| 621 | + var inFlight, maxRepoInFlight int32 |
| 622 | + repoOp := func() { |
| 623 | + cur := atomic.AddInt32(&inFlight, 1) |
| 624 | + for { |
| 625 | + prev := atomic.LoadInt32(&maxRepoInFlight) |
| 626 | + if cur <= prev || atomic.CompareAndSwapInt32(&maxRepoInFlight, prev, cur) { |
| 627 | + break |
| 628 | + } |
| 629 | + } |
| 630 | + time.Sleep(30 * time.Millisecond) |
| 631 | + atomic.AddInt32(&inFlight, -1) |
| 632 | + } |
| 633 | + restore := stubHelm( |
| 634 | + func(ctx context.Context, args []string, workDir string) error { return nil }, |
| 635 | + func(ctx context.Context, name, url string) error { repoOp(); return nil }, |
| 636 | + func(ctx context.Context) error { return nil }, |
| 637 | + ) |
| 638 | + defer restore() |
| 639 | + |
| 640 | + err := deployCompanionCharts(context.Background(), types.Options{ |
| 641 | + Namespace: "ns", |
| 642 | + CompanionCharts: []types.CompanionChart{ |
| 643 | + {ChartRef: "x/a", ReleaseName: "a", RepoName: "x", RepoURL: "https://x"}, |
| 644 | + {ChartRef: "y/b", ReleaseName: "b", RepoName: "y", RepoURL: "https://y"}, |
| 645 | + }, |
| 646 | + }) |
| 647 | + if err != nil { |
| 648 | + t.Fatalf("unexpected error: %v", err) |
| 649 | + } |
| 650 | + if maxRepoInFlight != 1 { |
| 651 | + t.Errorf("repo registration must be serialized (maxRepoInFlight=1), got %d", maxRepoInFlight) |
| 652 | + } |
| 653 | +} |
0 commit comments