carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package console
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "net/http"
8 "os"
9 "testing"
10
11 "github.com/carlosframework/platform/internal/hostclaim"
12 "github.com/carlosframework/platform/internal/provision"
13)
14
15// Re-review finding 10: on a claim console the delete's retirement can
16// delete the record and then park while obligations remain. A retried
17// delete in that interval must get the claim flow's answer (its pending
18// operation and phase), never a 200 that says the delete is done.
19func TestClaimDeleteRetryWhileParkingIsNotDone(t *testing.T) {
20 const host = "acme.go.tito.io"
21 cx := newClaimCase(t)
22 cx.instanceWorld(t)
23 cx.instance(t, `{"host":"`+host+`","region":"ie"}`)
24 url := cx.ts.URL + "/api/cli/apps/bab/titogo/instances/" + host
25 status, _, out := rawReq(t, http.MethodDelete, url, cx.bearer, "")
26 if status != http.StatusAccepted {
27 t.Fatalf("delete = %d %s, want 202", status, out)
28 }
29 op := claimReplyField(t, out, "operation_id")
30 parking := func() bool {
31 obs, err := hostclaim.Read(context.Background(), cx.rel, host)
32 if err != nil {
33 t.Fatal(err)
34 }
35 _, gone := provision.GetRecord(context.Background(), cx.rel, "bab", "titogo", host)
36 return !obs.Absent && obs.Claim.Op.Kind == hostclaim.KindParking && len(obs.Claim.Obligations) > 0 && errors.Is(gone, os.ErrNotExist)
37 }
38 for i := 0; i < 6 && !parking(); i++ {
39 cx.ackClaim(t, host, op)
40 claimWorker("StartClaimRecoveryPass")(t, cx)
41 }
42 if !parking() {
43 t.Fatal("the claim world did not reach parking with obligations left")
44 }
45 status, _, out = rawReq(t, http.MethodDelete, url, cx.bearer, "")
46 if status == http.StatusOK {
47 t.Fatalf("a retry while the claim parks answered %d %s: the delete is not done", status, out)
48 }
49 if status != http.StatusAccepted {
50 t.Fatalf("retry = %d %s, want the claim flow's 202", status, out)
51 }
52 var reply map[string]any
53 if err := json.Unmarshal([]byte(out), &reply); err != nil || reply["operation_id"] != op || reply["phase"] != hostclaim.KindParking {
54 t.Fatalf("retry answered %s, want operation %s at %s", out, op, hostclaim.KindParking)
55 }
56}
57