carlos / platform Public

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

Plain git — no account needed to clone.

Download

Download this file

1package appconfig
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "github.com/carlosframework/platform/internal/registry"
10)
11
12// Astra plan 6 R1 #1 (plan Q3): the first non-default swap policy on an app
13// with no config.json creates the bundle, which renders no vars. Config
14// sync must not turn that into a new, empty config file and a restart of a
15// running instance: the instance's file is absent before and after, which
16// is the state of every app never configured. No file, no restart, no
17// delivered revision for the release scheduler, no ack. The first real var
18// then converges as it always did: written, restarted, delivered, acked.
19func TestAPolicyOnlyBundleNeitherWritesTheFileNorRestarts(t *testing.T) {
20 ctx, s, reg, dd, priv, _ := execKit(t)
21 pk, err := ParsePrivateKey(priv, "test")
22 if err != nil {
23 t.Fatal(err)
24 }
25 if err := reg.Upsert(registry.Route{
26 Host: "inst.example.com", Kind: "instance", App: "eleven", Account: "bab",
27 Status: "ready", Socket: "/run/carlos/inst.sock",
28 }); err != nil {
29 t.Fatal(err)
30 }
31 var bounced, delivered []string
32 var acks []ConfigAck
33 cb := Callbacks{
34 Restart: func(h string) error { bounced = append(bounced, h); return nil },
35 Ack: func(a ConfigAck) { acks = append(acks, a) },
36 Delivered: func(h, rev string) { delivered = append(delivered, h+" "+rev) },
37 }
38 pass := func() {
39 t.Helper()
40 bounced, delivered, acks = nil, nil, nil
41 if err := SyncExecWithCallbacks(ctx, reg, s, pk, dd, "", cb); err != nil {
42 t.Fatalf("SyncExec: %v", err)
43 }
44 }
45 path := filepath.Join(dd, "inst.example.com.config.env")
46
47 pass() // no config.json: nothing to do
48 if _, err := SetSwapPolicy(ctx, s, "bab", "eleven", SwapHold, 3); err != nil {
49 t.Fatalf("SetSwapPolicy: %v", err)
50 }
51 if m, err := Get(ctx, s, "bab", "eleven"); err != nil || m == nil {
52 t.Fatalf("the policy write did not create the bundle: %v, %v", m, err)
53 }
54 pass()
55 if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
56 t.Errorf("a policy-only bundle created the config file: %v", err)
57 }
58 if len(bounced)+len(delivered)+len(acks) != 0 {
59 t.Fatalf("a policy-only bundle restarted %v, delivered %v, acked %d", bounced, delivered, len(acks))
60 }
61 pass() // and it stays quiet
62 if len(bounced)+len(delivered)+len(acks) != 0 {
63 t.Fatalf("a second pass restarted %v, delivered %v, acked %d", bounced, delivered, len(acks))
64 }
65
66 if err := SetEnv(ctx, s, "bab", "eleven", map[string]string{"LOG_LEVEL": "debug"}); err != nil {
67 t.Fatal(err)
68 }
69 pass()
70 if got, err := os.ReadFile(path); err != nil || string(got) != "LOG_LEVEL=debug\n" {
71 t.Fatalf("config env = %q, %v", got, err)
72 }
73 if len(bounced) != 1 || len(delivered) != 1 || len(acks) != 1 {
74 t.Fatalf("the first real var restarted %v, delivered %v, acked %d: want one of each", bounced, delivered, len(acks))
75 }
76}
77
78// The skip is for an ABSENT file only. A file that exists keeps converging
79// on its bytes: an empty file an older agent wrote stays (no restart), and
80// a file with vars whose bundle now renders none is emptied and restarted,
81// as before.
82func TestAnEmptyRenderStillConvergesAnExistingFile(t *testing.T) {
83 ctx, s, reg, dd, priv, _ := execKit(t)
84 pk, err := ParsePrivateKey(priv, "test")
85 if err != nil {
86 t.Fatal(err)
87 }
88 for _, h := range []string{"empty.example.com", "full.example.com"} {
89 if err := reg.Upsert(registry.Route{Host: h, Kind: "instance", App: "eleven", Account: "bab",
90 Status: "ready", Socket: "/run/carlos/" + h + ".sock"}); err != nil {
91 t.Fatal(err)
92 }
93 }
94 if err := os.WriteFile(filepath.Join(dd, "empty.example.com.config.env"), nil, 0o600); err != nil {
95 t.Fatal(err)
96 }
97 if err := os.WriteFile(filepath.Join(dd, "full.example.com.config.env"), []byte("LOG_LEVEL=debug\n"), 0o600); err != nil {
98 t.Fatal(err)
99 }
100 if _, err := SetSwapPolicy(ctx, s, "bab", "eleven", SwapHold, 0); err != nil {
101 t.Fatal(err)
102 }
103 var bounced []string
104 if err := SyncExec(ctx, reg, s, pk, dd, func(h string) error { bounced = append(bounced, h); return nil }, "", nil); err != nil {
105 t.Fatal(err)
106 }
107 if got, err := os.ReadFile(filepath.Join(dd, "full.example.com.config.env")); err != nil || len(got) != 0 {
108 t.Fatalf("full file = %q, %v: want it emptied", got, err)
109 }
110 if _, err := os.Stat(filepath.Join(dd, "empty.example.com.config.env")); err != nil {
111 t.Fatalf("the existing empty file went away: %v", err)
112 }
113 if len(bounced) != 1 || bounced[0] != "full.example.com" {
114 t.Fatalf("bounced %v, want only full.example.com", bounced)
115 }
116}
117