From d0a633c2340f71e27cfa4de4a0f41520e7c46d1b Mon Sep 17 00:00:00 2001 From: "Molecule AI Dev Engineer A (Kimi)" Date: Tue, 9 Jun 2026 16:31:21 +0000 Subject: [PATCH] test(provisioner): add missing unit tests for InternalURL and applyTierResources Adds coverage for two previously untested helpers: - TestInternalURL: verifies the container-internal URL shape uses the full workspace ID (no truncation) and the default port. - TestApplyTierResources: verifies memory + NanoCPU limits are applied correctly per tier (T1 no-cap, T2/T3/T4 explicit limits, unknown/zero tier returns zero so ApplyTierConfig can fall back to T2). Full provisioner suite (41 tests) passes. Co-Authored-By: Claude Opus 4.8 --- .../internal/provisioner/provisioner_test.go | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/workspace-server/internal/provisioner/provisioner_test.go b/workspace-server/internal/provisioner/provisioner_test.go index 166866e1e..f647224a0 100644 --- a/workspace-server/internal/provisioner/provisioner_test.go +++ b/workspace-server/internal/provisioner/provisioner_test.go @@ -1393,3 +1393,65 @@ func TestApplyTierConfig_T3_DefaultCap(t *testing.T) { t.Errorf("T3 default NanoCPUs: got %d, want %d", hc.NanoCPUs, wantCPU) } } + +// TestInternalURL verifies the container-internal URL shape. +func TestInternalURL(t *testing.T) { + tests := []struct { + id string + want string + }{ + {"abc123", "http://ws-abc123:8000"}, + {"longer-than-twelve-characters", "http://ws-longer-than-twelve-characters:8000"}, + {"", "http://ws-:8000"}, + } + for _, tt := range tests { + got := InternalURL(tt.id) + if got != tt.want { + t.Errorf("InternalURL(%q) = %q, want %q", tt.id, got, tt.want) + } + } +} + +// TestApplyTierResources verifies the direct memory/CPU resource application. +// Note: the T2-fallback for unknown tiers is handled by ApplyTierConfig, not +// this low-level helper. +func TestApplyTierResources(t *testing.T) { + for _, k := range []string{"TIER2_MEMORY_MB", "TIER2_CPU_SHARES", "TIER3_MEMORY_MB", "TIER3_CPU_SHARES", "TIER4_MEMORY_MB", "TIER4_CPU_SHARES"} { + os.Unsetenv(k) + } + + tests := []struct { + name string + tier int + wantMemory int64 + wantNanoCPUs int64 + wantShares int64 + }{ + {"T1 no cap", 1, 0, 0, 0}, + {"T2 512MiB 1CPU", 2, 512 * 1024 * 1024, 1_000_000_000, 1024}, + {"T3 2048MiB 2CPU", 3, 2048 * 1024 * 1024, 2_000_000_000, 2048}, + {"T4 4096MiB 4CPU", 4, 4096 * 1024 * 1024, 4_000_000_000, 4096}, + {"unknown tier no cap", 99, 0, 0, 0}, + {"zero tier no cap", 0, 0, 0, 0}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + hc := &container.HostConfig{} + memMB, cpuShares := applyTierResources(hc, tt.tier) + + if memMB != tt.wantMemory/(1024*1024) { + t.Errorf("memMB = %d, want %d", memMB, tt.wantMemory/(1024*1024)) + } + if hc.Memory != tt.wantMemory { + t.Errorf("Memory = %d, want %d", hc.Memory, tt.wantMemory) + } + if hc.NanoCPUs != tt.wantNanoCPUs { + t.Errorf("NanoCPUs = %d, want %d", hc.NanoCPUs, tt.wantNanoCPUs) + } + if cpuShares != tt.wantShares { + t.Errorf("cpuShares = %d, want %d", cpuShares, tt.wantShares) + } + }) + } +} -- 2.52.0