carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "context"
5 "os"
6 "path/filepath"
7 "testing"
8)
9
10func TestPlan14GenerationLegacyRelay(t *testing.T) {
11 for _, mode := range []string{"pinned", "unknown", "unreserved", "legacy", "fresh", "claims-enabled"} {
12 t.Run(mode, func(t *testing.T) {
13 t.Setenv("CARLOS_HOST_CLAIM", "")
14 ctx, s, opt := heldFixture(t)
15 data := t.TempDir()
16 request := writeHeldRequest(t, data, heldTestHost+".json", goodHeldRequest())
17 path := VersionPath(opt, "bab", "v1", "console")
18 if mode != "fresh" {
19 if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
20 t.Fatal(err)
21 }
22 }
23 calls, published := 0, false
24 source := PinSourceFunc(func(context.Context, string) (StagedPaths, error) {
25 calls++
26 if published {
27 t.Fatal("pin read inside publication")
28 }
29 switch mode {
30 case "unknown", "fresh":
31 return nil, os.ErrPermission
32 case "pinned":
33 return StagedPaths{filepath.Dir(path): true}, nil
34 }
35 return StagedPaths{}, nil
36 })
37 auth := func(_ HeldStageRequest, then func() error) error {
38 if then != nil {
39 published = true
40 return then()
41 }
42 return nil
43 }
44 var err error
45 if mode == "claims-enabled" {
46 t.Setenv("CARLOS_HOST_CLAIM", "1")
47 }
48 if mode == "legacy" {
49 err = ServeHeldStageRequests(ctx, s, heldPub, nr(t), opt, data, offGate(t), auth)
50 } else {
51 err = ServeHeldStageRequestsWithPins(ctx, s, heldPub, nr(t), opt, data, offGate(t), auth, source)
52 }
53 if err != nil {
54 t.Fatal(err)
55 }
56 refused := mode == "pinned" || mode == "unknown" || mode == "claims-enabled"
57 if refused {
58 if published {
59 t.Fatal("refused stage published")
60 }
61 if _, err := os.Stat(request); err != nil {
62 t.Fatal("refused request not retained", err)
63 }
64 for _, p := range []string{path, LiveLink(opt, heldTestHost), filepath.Join(HeldStagedDir(data), heldTestHost+".json")} {
65 if _, err := os.Lstat(p); !os.IsNotExist(err) {
66 t.Fatal("refused stage wrote artifact/link/ack", p, err)
67 }
68 }
69 if mode != "claims-enabled" && calls == 0 {
70 t.Fatal("relay omitted pin reader")
71 }
72 if mode == "claims-enabled" && calls != 0 {
73 t.Fatal("claims refusal read pins")
74 }
75 } else {
76 if !published {
77 t.Fatal("permitted stage not published")
78 }
79 if _, err := os.Stat(request); !os.IsNotExist(err) {
80 t.Fatal("served request retained", err)
81 }
82 if raw, err := os.ReadFile(path); err != nil || string(raw) != "pinned build" {
83 t.Fatal("permitted artifact missing", err)
84 }
85 if v, err := LiveVersion(opt, heldTestHost); err != nil || v != "v1" {
86 t.Fatal("wrong live target", v, err)
87 }
88 if _, err := os.Stat(filepath.Join(HeldStagedDir(data), heldTestHost+".json")); err != nil {
89 t.Fatal("missing acknowledgement", err)
90 }
91 if mode == "fresh" && calls != 0 {
92 t.Fatal("fresh tree required completeness")
93 }
94 }
95 })
96 }
97}
98