|
| 1 | +package daemon |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/blox-eng/openblox/pkg/brokerapi" |
| 12 | +) |
| 13 | + |
| 14 | +// newCappedServer builds a server whose code-exec profile admits at most limit |
| 15 | +// concurrent sandboxes, with `existing` already live under the named profiles. |
| 16 | +func newCappedServer(t *testing.T, limit int, existing map[string]string) *Server { |
| 17 | + t.Helper() |
| 18 | + cfg := &Config{ |
| 19 | + Socket: "/tmp/unused.sock", |
| 20 | + Profiles: map[string]Profile{ |
| 21 | + "code-exec": {Image: "example.com/i@sha256:abc", Runtime: "runsc", MaxSandboxes: limit}, |
| 22 | + "browser": {Image: "example.com/b@sha256:def"}, |
| 23 | + }, |
| 24 | + } |
| 25 | + return New(&fakeBackend{existing: existing}, cfg) |
| 26 | +} |
| 27 | + |
| 28 | +func createSandbox(srv *Server, name, profile string) *httptest.ResponseRecorder { |
| 29 | + rec := httptest.NewRecorder() |
| 30 | + req := httptest.NewRequest(http.MethodPost, "/sandboxes", |
| 31 | + strings.NewReader(fmt.Sprintf(`{"name":%q,"profile":%q}`, name, profile))) |
| 32 | + srv.Handler().ServeHTTP(rec, req) |
| 33 | + return rec |
| 34 | +} |
| 35 | + |
| 36 | +func TestLoadRejectsNegativeMaxSandboxes(t *testing.T) { |
| 37 | + path := writeConfig(t, ` |
| 38 | +socket: /tmp/s.sock |
| 39 | +profiles: |
| 40 | + p: |
| 41 | + image: example.com/i@sha256:abc |
| 42 | + max_sandboxes: -1 |
| 43 | +`) |
| 44 | + _, err := Load(path) |
| 45 | + if err == nil { |
| 46 | + t.Fatal("expected an error: a negative cap reads as 'unlimited' but is an operator typo") |
| 47 | + } |
| 48 | + if !strings.Contains(err.Error(), "max_sandboxes") { |
| 49 | + t.Errorf("error %q should name the offending field", err) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestCreateRefusesWhenProfileIsAtCapacity(t *testing.T) { |
| 54 | + srv := newCappedServer(t, 2, map[string]string{"a": "code-exec", "b": "code-exec"}) |
| 55 | + |
| 56 | + rec := createSandbox(srv, "c", "code-exec") |
| 57 | + |
| 58 | + if rec.Code != http.StatusTooManyRequests { |
| 59 | + t.Fatalf("status = %d, want 429, body %s", rec.Code, rec.Body.String()) |
| 60 | + } |
| 61 | + var body struct { |
| 62 | + Kind string `json:"kind"` |
| 63 | + } |
| 64 | + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + if body.Kind != brokerapi.KindAtCapacity { |
| 68 | + t.Errorf("kind = %q, want %q — a caller must tell 'try later' from 'malformed'", |
| 69 | + body.Kind, brokerapi.KindAtCapacity) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestCreateAtCapacityStillServesAnExistingSandbox(t *testing.T) { |
| 74 | + srv := newCappedServer(t, 2, map[string]string{"a": "code-exec", "b": "code-exec"}) |
| 75 | + |
| 76 | + // "a" is already counted against the cap, so reusing it consumes no new |
| 77 | + // slot. Refusing here would break warm reuse exactly when the host is busy. |
| 78 | + rec := createSandbox(srv, "a", "code-exec") |
| 79 | + |
| 80 | + if rec.Code != http.StatusCreated { |
| 81 | + t.Fatalf("status = %d, want 201, body %s", rec.Code, rec.Body.String()) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestCreateCapacityCountsOnlyTheRequestedProfile(t *testing.T) { |
| 86 | + srv := newCappedServer(t, 2, map[string]string{"x": "browser", "y": "browser"}) |
| 87 | + |
| 88 | + rec := createSandbox(srv, "a", "code-exec") |
| 89 | + |
| 90 | + if rec.Code != http.StatusCreated { |
| 91 | + t.Fatalf("status = %d, want 201 — browser sandboxes must not consume code-exec capacity, body %s", |
| 92 | + rec.Code, rec.Body.String()) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestCreateIsUnlimitedWhenMaxSandboxesIsUnset(t *testing.T) { |
| 97 | + srv := newCappedServer(t, 0, map[string]string{"a": "code-exec", "b": "code-exec", "c": "code-exec"}) |
| 98 | + |
| 99 | + rec := createSandbox(srv, "d", "code-exec") |
| 100 | + |
| 101 | + if rec.Code != http.StatusCreated { |
| 102 | + t.Fatalf("status = %d, want 201 — unset max_sandboxes means unlimited, body %s", |
| 103 | + rec.Code, rec.Body.String()) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// TestConcurrentCreatesCannotExceedCapacity is the point of the feature: a |
| 108 | +// retry storm arrives in parallel, and counting live sandboxes without holding |
| 109 | +// a reservation would let every request pass the check before any of them had |
| 110 | +// created anything. |
| 111 | +func TestConcurrentCreatesCannotExceedCapacity(t *testing.T) { |
| 112 | + srv := newCappedServer(t, 1, nil) |
| 113 | + fake := srv.backend.(*fakeBackend) |
| 114 | + |
| 115 | + start := make(chan struct{}) |
| 116 | + fake.createBlock = start |
| 117 | + |
| 118 | + const callers = 8 |
| 119 | + codes := make(chan int, callers) |
| 120 | + for i := range callers { |
| 121 | + go func() { codes <- createSandbox(srv, fmt.Sprintf("s%d", i), "code-exec").Code }() |
| 122 | + } |
| 123 | + close(start) |
| 124 | + |
| 125 | + var created, refused int |
| 126 | + for range callers { |
| 127 | + switch code := <-codes; code { |
| 128 | + case http.StatusCreated: |
| 129 | + created++ |
| 130 | + case http.StatusTooManyRequests: |
| 131 | + refused++ |
| 132 | + default: |
| 133 | + t.Errorf("unexpected status %d", code) |
| 134 | + } |
| 135 | + } |
| 136 | + if created != 1 { |
| 137 | + t.Errorf("created = %d, want 1 — the cap must hold under concurrent creates", created) |
| 138 | + } |
| 139 | + if refused != callers-1 { |
| 140 | + t.Errorf("refused = %d, want %d", refused, callers-1) |
| 141 | + } |
| 142 | +} |
0 commit comments