|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// resLimitsProject wraps a service body into a minimal but valid two-file tree. |
| 9 | +func resLimitsProject(t *testing.T, svcBody string) (*Model, error) { |
| 10 | + t.Helper() |
| 11 | + ws := `apiVersion: devstack/v1 |
| 12 | +kind: Workspace |
| 13 | +name: acme |
| 14 | +shared: |
| 15 | + postgres: { template: postgres } |
| 16 | +projects: |
| 17 | + - { name: api, path: api } |
| 18 | +` |
| 19 | + proj := "apiVersion: devstack/v1\nkind: Project\nname: api\nservices:\n api:\n template: t\n" + svcBody |
| 20 | + root := writeTree(t, map[string]string{"workspace.yaml": ws, "api/devstack.yaml": proj}) |
| 21 | + return LoadAt(root) |
| 22 | +} |
| 23 | + |
| 24 | +// TestResourceLimitsParse — the spec-18 resources block + platform selector parse |
| 25 | +// onto a project service, and EffectiveMemoryMB prefers resources.memoryMB. |
| 26 | +func TestResourceLimitsParse(t *testing.T) { |
| 27 | + m, err := resLimitsProject(t, ` platform: linux/amd64 |
| 28 | + resources: |
| 29 | + cpus: "1.5" |
| 30 | + memoryMB: 512 |
| 31 | + memoryReserveMB: 256 |
| 32 | + pidsLimit: 128 |
| 33 | +`) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("LoadAt: %v", err) |
| 36 | + } |
| 37 | + svc := m.Projects["api"].Services["api"] |
| 38 | + if svc.Platform != "linux/amd64" { |
| 39 | + t.Errorf("platform = %q, want linux/amd64", svc.Platform) |
| 40 | + } |
| 41 | + if svc.Resources == nil { |
| 42 | + t.Fatal("resources block did not parse") |
| 43 | + } |
| 44 | + if svc.Resources.CPUs != "1.5" || svc.Resources.MemoryMB != 512 || |
| 45 | + svc.Resources.MemoryReserveMB != 256 || svc.Resources.PidsLimit != 128 { |
| 46 | + t.Errorf("resources = %+v", *svc.Resources) |
| 47 | + } |
| 48 | + if got := svc.EffectiveMemoryMB(); got != 512 { |
| 49 | + t.Errorf("EffectiveMemoryMB = %d, want 512 (resources.memoryMB wins)", got) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// TestEffectiveMemoryShorthand — with no resources.memoryMB, the top-level |
| 54 | +// memoryMB shorthand is the effective limit; with neither, it is 0. |
| 55 | +func TestEffectiveMemoryShorthand(t *testing.T) { |
| 56 | + m, err := resLimitsProject(t, " memoryMB: 768\n") |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("LoadAt: %v", err) |
| 59 | + } |
| 60 | + if got := m.Projects["api"].Services["api"].EffectiveMemoryMB(); got != 768 { |
| 61 | + t.Errorf("EffectiveMemoryMB = %d, want 768 (shorthand)", got) |
| 62 | + } |
| 63 | + |
| 64 | + m2, err := resLimitsProject(t, "") |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("LoadAt: %v", err) |
| 67 | + } |
| 68 | + if got := m2.Projects["api"].Services["api"].EffectiveMemoryMB(); got != 0 { |
| 69 | + t.Errorf("EffectiveMemoryMB = %d, want 0 (unset)", got) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// TestPlatformValidation — a malformed platform selector is rejected with a |
| 74 | +// file-scoped error; well-formed selectors pass. |
| 75 | +func TestPlatformValidation(t *testing.T) { |
| 76 | + if _, err := resLimitsProject(t, " platform: not-a-platform\n"); err == nil || |
| 77 | + !strings.Contains(err.Error(), "platform") { |
| 78 | + t.Fatalf("want a platform validation error, got %v", err) |
| 79 | + } |
| 80 | + for _, p := range []string{"linux/amd64", "linux/arm64/v8", "darwin/arm64"} { |
| 81 | + if _, err := resLimitsProject(t, " platform: "+p+"\n"); err != nil { |
| 82 | + t.Errorf("platform %q should be valid, got %v", p, err) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// TestCPUsValidation — a non-numeric or non-positive cpus quantity is rejected. |
| 88 | +func TestCPUsValidation(t *testing.T) { |
| 89 | + for _, bad := range []string{"lots", "0", "-1"} { |
| 90 | + if _, err := resLimitsProject(t, " resources: { cpus: \""+bad+"\" }\n"); err == nil || |
| 91 | + !strings.Contains(err.Error(), "cpu") { |
| 92 | + t.Errorf("cpus %q should be rejected, got %v", bad, err) |
| 93 | + } |
| 94 | + } |
| 95 | + if _, err := resLimitsProject(t, " resources: { cpus: \"1.5\" }\n"); err != nil { |
| 96 | + t.Errorf("cpus 1.5 should be valid, got %v", err) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// TestSharedResourcesParse — shared services accept the same resources/platform |
| 101 | +// knobs from workspace.yaml (spec 18, shared stack). |
| 102 | +func TestSharedResourcesParse(t *testing.T) { |
| 103 | + ws := `apiVersion: devstack/v1 |
| 104 | +kind: Workspace |
| 105 | +name: acme |
| 106 | +shared: |
| 107 | + postgres: |
| 108 | + template: postgres |
| 109 | + platform: linux/amd64 |
| 110 | + resources: { cpus: "2", memoryMB: 1024 } |
| 111 | +projects: |
| 112 | + - { name: api, path: api } |
| 113 | +` |
| 114 | + proj := "apiVersion: devstack/v1\nkind: Project\nname: api\nservices:\n api: { template: t }\n" |
| 115 | + root := writeTree(t, map[string]string{"workspace.yaml": ws, "api/devstack.yaml": proj}) |
| 116 | + m, err := LoadAt(root) |
| 117 | + if err != nil { |
| 118 | + t.Fatalf("LoadAt: %v", err) |
| 119 | + } |
| 120 | + pg := m.Workspace.Shared["postgres"] |
| 121 | + if pg.Platform != "linux/amd64" { |
| 122 | + t.Errorf("shared platform = %q, want linux/amd64", pg.Platform) |
| 123 | + } |
| 124 | + if pg.EffectiveMemoryMB() != 1024 { |
| 125 | + t.Errorf("shared EffectiveMemoryMB = %d, want 1024", pg.EffectiveMemoryMB()) |
| 126 | + } |
| 127 | +} |
0 commit comments