| 1 | package isolatedjobtest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "slices" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 14 | ) |
| 15 | |
| 16 | // Harness is what Contract needs from one Runner implementation. |
| 17 | type Harness struct { |
| 18 | Runner isolatedjob.Runner |
| 19 | // Image is a certified image the runner can launch. |
| 20 | Image isolatedjob.ImageDigest |
| 21 | // Settle waits until the VM's state satisfies want and returns the job |
| 22 | // as last read. It returns an error when the VM cannot be read (which |
| 23 | // may wrap isolatedjob.ErrNotFound) or when the state does not arrive in |
| 24 | // the implementation's own time bound, and it bounds each read it makes. |
| 25 | Settle func(ctx context.Context, ref isolatedjob.Ref, want func(isolatedjob.State) bool) (isolatedjob.Job, error) |
| 26 | // ListLag is how long List may take to show a VM that Get already shows |
| 27 | // as running. Zero means at once, as for the fake. |
| 28 | ListLag time.Duration |
| 29 | // Visibility is how long Get may answer ErrNotFound for a VM that |
| 30 | // Launch has just returned, as a provider whose reads are eventually |
| 31 | // consistent does. It bounds only the wait for a new VM to appear, never |
| 32 | // a wait for a state. Zero means at once, as for the fake. |
| 33 | Visibility time.Duration |
| 34 | // CallTimeout bounds each Runner call the contract makes, so a stalled |
| 35 | // request fails the run instead of hanging it. Zero means |
| 36 | // DefaultCallTimeout. |
| 37 | CallTimeout time.Duration |
| 38 | // Logf, when set, gets the measured delays (how long Get took to show |
| 39 | // each new VM, and List to show a running one) in place of t.Logf. A |
| 40 | // live run leaves it nil, so they appear with go test -v. |
| 41 | Logf func(format string, args ...any) |
| 42 | } |
| 43 | |
| 44 | // logf writes one measurement through h.Logf, or t.Logf when it is nil. |
| 45 | func (h Harness) logf(t *testing.T, format string, args ...any) { |
| 46 | t.Helper() |
| 47 | if h.Logf != nil { |
| 48 | h.Logf(format, args...) |
| 49 | return |
| 50 | } |
| 51 | t.Logf(format, args...) |
| 52 | } |
| 53 | |
| 54 | // DefaultCallTimeout bounds each Runner call when the Harness sets no |
| 55 | // CallTimeout. |
| 56 | const DefaultCallTimeout = 30 * time.Second |
| 57 | |
| 58 | // call returns the context for one Runner call, bounded by CallTimeout. |
| 59 | func (h Harness) call() (context.Context, context.CancelFunc) { |
| 60 | d := h.CallTimeout |
| 61 | if d <= 0 { |
| 62 | d = DefaultCallTimeout |
| 63 | } |
| 64 | return context.WithTimeout(context.Background(), d) |
| 65 | } |
| 66 | |
| 67 | func (h Harness) launch(s isolatedjob.LaunchSpec) (isolatedjob.Ref, error) { |
| 68 | ctx, cancel := h.call() |
| 69 | defer cancel() |
| 70 | return h.Runner.Launch(ctx, s) |
| 71 | } |
| 72 | |
| 73 | func (h Harness) get(ref isolatedjob.Ref) (isolatedjob.Job, error) { |
| 74 | ctx, cancel := h.call() |
| 75 | defer cancel() |
| 76 | return h.Runner.Get(ctx, ref) |
| 77 | } |
| 78 | |
| 79 | func (h Harness) list() ([]isolatedjob.Job, error) { |
| 80 | ctx, cancel := h.call() |
| 81 | defer cancel() |
| 82 | return h.Runner.List(ctx) |
| 83 | } |
| 84 | |
| 85 | func (h Harness) stop(ref isolatedjob.Ref, reason string) error { |
| 86 | ctx, cancel := h.call() |
| 87 | defer cancel() |
| 88 | return h.Runner.Stop(ctx, ref, reason) |
| 89 | } |
| 90 | |
| 91 | func (h Harness) suspend(ref isolatedjob.Ref) error { |
| 92 | ctx, cancel := h.call() |
| 93 | defer cancel() |
| 94 | return h.Runner.Suspend(ctx, ref) |
| 95 | } |
| 96 | |
| 97 | func (h Harness) resume(ref isolatedjob.Ref) error { |
| 98 | ctx, cancel := h.call() |
| 99 | defer cancel() |
| 100 | return h.Runner.Resume(ctx, ref) |
| 101 | } |
| 102 | |
| 103 | // contractLifetime keeps VMs launched by a live run short. |
| 104 | const contractLifetime = 15 * time.Minute |
| 105 | |
| 106 | // Contract checks the behaviour every Runner must share. It uses only the |
| 107 | // Runner interface and the Harness, so it runs against the fake and, behind |
| 108 | // the hostedlive build tag, against a real provider. Each subtest stops the |
| 109 | // VMs it launched. Attempt IDs carry a random part, so a second run against |
| 110 | // a provider that remembers client tokens does not get the first run's VMs. |
| 111 | // Every VM the contract launches must show in Get within h.Visibility |
| 112 | // before any check reads it. |
| 113 | func Contract(t *testing.T, h Harness) { |
| 114 | t.Helper() |
| 115 | run := randomHex(t, 6) |
| 116 | n := 0 |
| 117 | attempt := func() string { |
| 118 | n++ |
| 119 | return fmt.Sprintf("contract-%s-%d", run, n) |
| 120 | } |
| 121 | spec := func() isolatedjob.LaunchSpec { |
| 122 | return isolatedjob.LaunchSpec{ |
| 123 | AttemptID: attempt(), |
| 124 | Image: h.Image, |
| 125 | Size: isolatedjob.SizeSmall, |
| 126 | MaxLifetime: min(contractLifetime, h.Runner.Limits().MaxLifetime), |
| 127 | Bootstrap: []byte("contract-bootstrap"), |
| 128 | } |
| 129 | } |
| 130 | launch := func(t *testing.T, s isolatedjob.LaunchSpec) isolatedjob.Ref { |
| 131 | t.Helper() |
| 132 | ref, err := h.launch(s) |
| 133 | if err != nil { |
| 134 | t.Fatalf("Launch(%s) = %v, want a ref", s.AttemptID, err) |
| 135 | } |
| 136 | t.Cleanup(func() { |
| 137 | if err := h.stop(ref, "contract cleanup"); err != nil { |
| 138 | t.Errorf("cleanup Stop(%s) = %v", ref, err) |
| 139 | } |
| 140 | }) |
| 141 | appear(t, h, ref) |
| 142 | return ref |
| 143 | } |
| 144 | settle := func(t *testing.T, ref isolatedjob.Ref, want isolatedjob.State) isolatedjob.Job { |
| 145 | t.Helper() |
| 146 | job, err := h.Settle(context.Background(), ref, func(s isolatedjob.State) bool { return s == want }) |
| 147 | if err != nil { |
| 148 | t.Fatalf("Settle(%s, %s) = %v", ref, want, err) |
| 149 | } |
| 150 | return job |
| 151 | } |
| 152 | |
| 153 | t.Run("LaunchIdempotentOnAttempt", func(t *testing.T) { |
| 154 | s := spec() |
| 155 | first := launch(t, s) |
| 156 | again, err := h.launch(s) |
| 157 | if err != nil { |
| 158 | t.Fatalf("second Launch(%s) = %v, want the first ref", s.AttemptID, err) |
| 159 | } |
| 160 | if again != first { |
| 161 | t.Fatalf("second Launch(%s) = %s, want %s: one attempt must be one VM", s.AttemptID, again, first) |
| 162 | } |
| 163 | job, err := h.get(first) |
| 164 | if err != nil { |
| 165 | t.Fatalf("Get(%s) = %v", first, err) |
| 166 | } |
| 167 | if !job.State.Live() { |
| 168 | t.Fatalf("Get(%s).State = %s, want a live state", first, job.State) |
| 169 | } |
| 170 | }) |
| 171 | |
| 172 | t.Run("ChangedSpecConflicts", func(t *testing.T) { |
| 173 | s := spec() |
| 174 | first := launch(t, s) |
| 175 | changed := s |
| 176 | changed.Bootstrap = []byte("another-bootstrap") |
| 177 | ref, err := h.launch(changed) |
| 178 | if !errors.Is(err, isolatedjob.ErrConflict) { |
| 179 | t.Fatalf("Launch with the same attempt and a new bootstrap = (%q, %v), want ErrConflict", ref, err) |
| 180 | } |
| 181 | if ref == first { |
| 182 | t.Fatalf("Launch with a changed spec returned the first VM %s", first) |
| 183 | } |
| 184 | }) |
| 185 | |
| 186 | t.Run("StopTwice", func(t *testing.T) { |
| 187 | ref := launch(t, spec()) |
| 188 | for i := 1; i <= 2; i++ { |
| 189 | if err := h.stop(ref, "contract stop twice"); err != nil { |
| 190 | t.Fatalf("Stop %d of %s = %v, want nil", i, ref, err) |
| 191 | } |
| 192 | } |
| 193 | }) |
| 194 | |
| 195 | t.Run("StopUnknownRef", func(t *testing.T) { |
| 196 | ref := isolatedjob.Ref("contract-never-launched-" + run) |
| 197 | if err := h.stop(ref, "contract unknown ref"); err != nil { |
| 198 | t.Fatalf("Stop(%s) of a ref the provider never held = %v, want nil", ref, err) |
| 199 | } |
| 200 | }) |
| 201 | |
| 202 | t.Run("GetAfterStop", func(t *testing.T) { |
| 203 | ref := launch(t, spec()) |
| 204 | if err := h.stop(ref, "contract get after stop"); err != nil { |
| 205 | t.Fatalf("Stop(%s) = %v", ref, err) |
| 206 | } |
| 207 | _, err := h.Settle(context.Background(), ref, isolatedjob.State.Terminal) |
| 208 | if err != nil && !errors.Is(err, isolatedjob.ErrNotFound) { |
| 209 | t.Fatalf("Settle(%s, terminal) = %v, want a terminal state or ErrNotFound", ref, err) |
| 210 | } |
| 211 | job, err := h.get(ref) |
| 212 | switch { |
| 213 | case errors.Is(err, isolatedjob.ErrNotFound): |
| 214 | case err != nil: |
| 215 | t.Fatalf("Get(%s) after stop = %v, want a terminal state or ErrNotFound", ref, err) |
| 216 | case !job.State.Terminal(): |
| 217 | t.Fatalf("Get(%s) after stop = %s, want a terminal state or ErrNotFound, never %s", ref, job.State, isolatedjob.StateUnknown) |
| 218 | } |
| 219 | }) |
| 220 | |
| 221 | t.Run("SuspendResumeRoundTrip", func(t *testing.T) { |
| 222 | ref := launch(t, spec()) |
| 223 | settle(t, ref, isolatedjob.StateRunning) |
| 224 | if err := h.suspend(ref); err != nil { |
| 225 | t.Fatalf("Suspend(%s) = %v", ref, err) |
| 226 | } |
| 227 | settle(t, ref, isolatedjob.StateSuspended) |
| 228 | if err := h.resume(ref); err != nil { |
| 229 | t.Fatalf("Resume(%s) = %v", ref, err) |
| 230 | } |
| 231 | settle(t, ref, isolatedjob.StateRunning) |
| 232 | }) |
| 233 | |
| 234 | t.Run("ResumeRunningIsNoop", func(t *testing.T) { |
| 235 | ref := launch(t, spec()) |
| 236 | settle(t, ref, isolatedjob.StateRunning) |
| 237 | if err := h.resume(ref); err != nil { |
| 238 | t.Fatalf("Resume(%s) of a running VM = %v, want nil", ref, err) |
| 239 | } |
| 240 | job, err := h.get(ref) |
| 241 | if err != nil { |
| 242 | t.Fatalf("Get(%s) = %v", ref, err) |
| 243 | } |
| 244 | if job.State != isolatedjob.StateRunning { |
| 245 | t.Fatalf("Get(%s).State after a no-op resume = %s, want %s", ref, job.State, isolatedjob.StateRunning) |
| 246 | } |
| 247 | }) |
| 248 | |
| 249 | t.Run("OversizeBootstrapRefusedBeforeProvider", func(t *testing.T) { |
| 250 | // A VM the contract launched must show in List first, so a List |
| 251 | // that always comes back empty cannot pass the check below. |
| 252 | known := launch(t, spec()) |
| 253 | settle(t, known, isolatedjob.StateRunning) |
| 254 | before := listUntil(t, h, known) |
| 255 | s := spec() |
| 256 | s.Bootstrap = make([]byte, h.Runner.Limits().MaxBootstrapBytes+1) |
| 257 | for i := range s.Bootstrap { |
| 258 | s.Bootstrap[i] = 'x' |
| 259 | } |
| 260 | ref, err := h.launch(s) |
| 261 | if ref != "" { |
| 262 | t.Cleanup(func() { _ = h.stop(ref, "contract cleanup") }) |
| 263 | } |
| 264 | if !errors.Is(err, isolatedjob.ErrBootstrapTooLarge) { |
| 265 | t.Fatalf("Launch with a %d-byte bootstrap = (%q, %v), want ErrBootstrapTooLarge", len(s.Bootstrap), ref, err) |
| 266 | } |
| 267 | after := listRefs(t, h) |
| 268 | if !slices.Contains(after, known) { |
| 269 | t.Fatalf("List after a refused launch = %v, want it still to hold running VM %s", after, known) |
| 270 | } |
| 271 | for _, r := range after { |
| 272 | if !slices.Contains(before, r) { |
| 273 | t.Fatalf("List after a refused launch holds new VM %s; the refusal must come before the provider", r) |
| 274 | } |
| 275 | } |
| 276 | }) |
| 277 | |
| 278 | t.Run("LimitsAreSane", func(t *testing.T) { |
| 279 | l := h.Runner.Limits() |
| 280 | if l.MaxLifetime <= 0 || l.MaxLifetime > isolatedjob.HardMaxLifetime { |
| 281 | t.Errorf("Limits().MaxLifetime = %v, want above zero and at most %v", l.MaxLifetime, isolatedjob.HardMaxLifetime) |
| 282 | } |
| 283 | if l.MaxBootstrapBytes <= 0 { |
| 284 | t.Errorf("Limits().MaxBootstrapBytes = %d, want above zero", l.MaxBootstrapBytes) |
| 285 | } |
| 286 | }) |
| 287 | } |
| 288 | |
| 289 | // appear reads ref with Get until the provider shows it, for at most |
| 290 | // h.Visibility after the first read. Only ErrNotFound and ErrThrottled are |
| 291 | // read again; any other error, or ref still missing when the time is up, |
| 292 | // fails the test. Zero Visibility is one read. Reads are spaced a |
| 293 | // twentieth of Visibility apart, at most a second, so a short window in a |
| 294 | // test costs little. |
| 295 | func appear(t *testing.T, h Harness, ref isolatedjob.Ref) { |
| 296 | t.Helper() |
| 297 | start := time.Now() |
| 298 | deadline := start.Add(h.Visibility) |
| 299 | gap := min(time.Second, max(h.Visibility/20, time.Millisecond)) |
| 300 | for reads := 1; ; reads++ { |
| 301 | _, err := h.get(ref) |
| 302 | if err == nil { |
| 303 | h.logf(t, "visibility: Get showed new VM %s %v after Launch returned (%d reads)", ref, time.Since(start), reads) |
| 304 | return |
| 305 | } |
| 306 | if !errors.Is(err, isolatedjob.ErrNotFound) && !errors.Is(err, isolatedjob.ErrThrottled) { |
| 307 | t.Fatalf("Get(%s) of a VM Launch just returned = %v", ref, err) |
| 308 | } |
| 309 | if !time.Now().Before(deadline) { |
| 310 | t.Fatalf("Get(%s) = %v for %v after Launch returned it, want the VM to appear", ref, err, h.Visibility) |
| 311 | } |
| 312 | time.Sleep(min(gap, time.Until(deadline))) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | func listRefs(t *testing.T, h Harness) []isolatedjob.Ref { |
| 317 | t.Helper() |
| 318 | jobs, err := h.list() |
| 319 | if err != nil { |
| 320 | t.Fatalf("List = %v", err) |
| 321 | } |
| 322 | refs := make([]isolatedjob.Ref, 0, len(jobs)) |
| 323 | for _, j := range jobs { |
| 324 | refs = append(refs, j.Ref) |
| 325 | } |
| 326 | return refs |
| 327 | } |
| 328 | |
| 329 | // listUntil lists until the result holds a live job for ref, for at most |
| 330 | // h.ListLag, and returns the refs of that list. It fails the test when ref |
| 331 | // never shows. Each List is bounded by h.CallTimeout, so a stalled request |
| 332 | // overruns ListLag by at most that; zero ListLag is one List. |
| 333 | func listUntil(t *testing.T, h Harness, ref isolatedjob.Ref) []isolatedjob.Ref { |
| 334 | t.Helper() |
| 335 | start := time.Now() |
| 336 | deadline := start.Add(h.ListLag) |
| 337 | for lists := 1; ; lists++ { |
| 338 | jobs, err := h.list() |
| 339 | if err != nil { |
| 340 | t.Fatalf("List = %v", err) |
| 341 | } |
| 342 | refs := make([]isolatedjob.Ref, 0, len(jobs)) |
| 343 | found := false |
| 344 | for _, j := range jobs { |
| 345 | refs = append(refs, j.Ref) |
| 346 | found = found || (j.Ref == ref && j.State.Live()) |
| 347 | } |
| 348 | if found { |
| 349 | h.logf(t, "list lag: List showed running VM %s %v after Get did (%d lists)", ref, time.Since(start), lists) |
| 350 | return refs |
| 351 | } |
| 352 | if !time.Now().Before(deadline) { |
| 353 | t.Fatalf("List = %v after %v, want it to hold running VM %s", refs, h.ListLag, ref) |
| 354 | } |
| 355 | time.Sleep(min(time.Second, time.Until(deadline))) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | func randomHex(t *testing.T, n int) string { |
| 360 | t.Helper() |
| 361 | b := make([]byte, n) |
| 362 | if _, err := rand.Read(b); err != nil { |
| 363 | t.Fatalf("random attempt id: %v", err) |
| 364 | } |
| 365 | return hex.EncodeToString(b) |
| 366 | } |
| 367 | |