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 "net/http"
7 "testing"
8
9 "github.com/carlosframework/platform/internal/release"
10)
11
12func metaPartSwap(t *testing.T, version, swap string) shipPart {
13 t.Helper()
14 raw, err := json.Marshal(map[string]string{"version": version, "kind": "binary", "swap": swap})
15 if err != nil {
16 t.Fatal(err)
17 }
18 return shipPart{name: "meta", body: raw}
19}
20
21func TestAPIShipStoresTheSwapDemand(t *testing.T) {
22 fx := newConsoleFixture(t)
23 liftShipBudget(fx)
24 token := cliToken(t, fx, "paul@keymail.dev", "code-ship-swap")
25 u := fx.ts.URL + "/api/cli/apps/" + fx.sqid + "/kass/releases"
26 art := shipPart{name: "linux-arm64", body: randomBytes(t, 64)}
27
28 code, out := postShip(t, u, token, []shipPart{metaPartSwap(t, "v9", "hold"), art})
29 if code != http.StatusCreated || out["swap"] != "hold" {
30 t.Fatalf("ship = %d %v, want 201 echoing swap hold", code, out)
31 }
32 m, err := release.GetManifest(context.Background(), fx.rel, fx.sqid, "kass", "v9")
33 if err != nil || m.Swap != release.DemandHold {
34 t.Fatalf("stored manifest swap = %q, %v", m.Swap, err)
35 }
36 if code, out = postShip(t, u, token, []shipPart{metaPartSwap(t, "v9", "hold"), art}); code != http.StatusOK || out["swap"] != "hold" {
37 t.Fatalf("identical re-ship = %d %v", code, out)
38 }
39 if code, out = postShip(t, u, token, []shipPart{metaPart(t, "v9", "binary"), art}); code != http.StatusConflict {
40 t.Fatalf("re-ship without the demand = %d %v, want 409", code, out)
41 }
42}
43
44func TestAPIShipRefusesAnyOtherDemand(t *testing.T) {
45 fx := newConsoleFixture(t)
46 liftShipBudget(fx)
47 token := cliToken(t, fx, "paul@keymail.dev", "code-ship-swap-bad")
48 u := fx.ts.URL + "/api/cli/apps/" + fx.sqid + "/kass/releases"
49 for _, bad := range []string{"overlap", "fast", "HOLD"} {
50 code, out := postShip(t, u, token, []shipPart{metaPartSwap(t, "v10", bad), {name: "linux-arm64", body: randomBytes(t, 8)}})
51 if code != http.StatusBadRequest {
52 t.Errorf("swap %q = %d %v, want 400", bad, code, out)
53 }
54 }
55 if _, err := release.GetManifest(context.Background(), fx.rel, fx.sqid, "kass", "v10"); err == nil {
56 t.Fatal("a refused ship stored a manifest")
57 }
58}
59
60func TestAPIShipWithoutSwapAnswersAsBefore(t *testing.T) {
61 fx := newConsoleFixture(t)
62 liftShipBudget(fx)
63 token := cliToken(t, fx, "paul@keymail.dev", "code-ship-noswap")
64 u := fx.ts.URL + "/api/cli/apps/" + fx.sqid + "/kass/releases"
65 code, out := postShip(t, u, token, []shipPart{metaPart(t, "v11", "binary"), {name: "linux-arm64", body: randomBytes(t, 8)}})
66 if code != http.StatusCreated {
67 t.Fatalf("ship = %d %v", code, out)
68 }
69 if _, present := out["swap"]; present {
70 t.Fatalf("a ship with no demand echoes swap: %v", out)
71 }
72}
73
74func TestAPIShipRefusesAddingTheDemandOnAReship(t *testing.T) {
75 fx := newConsoleFixture(t)
76 liftShipBudget(fx)
77 token := cliToken(t, fx, "paul@keymail.dev", "code-ship-swap-add")
78 u := fx.ts.URL + "/api/cli/apps/" + fx.sqid + "/kass/releases"
79 art := shipPart{name: "linux-arm64", body: randomBytes(t, 64)}
80
81 if code, out := postShip(t, u, token, []shipPart{metaPart(t, "v12", "binary"), art}); code != http.StatusCreated {
82 t.Fatalf("ship = %d %v", code, out)
83 }
84 code, out := postShip(t, u, token, []shipPart{metaPart(t, "v12", "binary"), art})
85 if code != http.StatusOK {
86 t.Fatalf("identical no-demand re-ship = %d %v, want 200", code, out)
87 }
88 if _, present := out["swap"]; present {
89 t.Fatalf("a re-ship with no demand echoes swap: %v", out)
90 }
91 if code, out = postShip(t, u, token, []shipPart{metaPartSwap(t, "v12", "hold"), art}); code != http.StatusConflict {
92 t.Fatalf("re-ship adding the demand = %d %v, want 409", code, out)
93 }
94 m, err := release.GetManifest(context.Background(), fx.rel, fx.sqid, "kass", "v12")
95 if err != nil || m.Swap != "" {
96 t.Fatalf("stored manifest swap = %q, %v, want it unchanged", m.Swap, err)
97 }
98}
99