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 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "github.com/carlosframework/platform/internal/registry"
10)
11
12// A re-render never touches the instance's own env file, so the key minted
13// there survives every config change, including one that sets the same
14// name in the app's configuration (L21, never overwrite: re-render).
15func TestSyncExecLeavesTheInstanceKeyAlone(t *testing.T) {
16 ctx, s, reg, dd, priv, _ := execKit(t)
17 pk, err := ParsePrivateKey(priv, "test")
18 if err != nil {
19 t.Fatal(err)
20 }
21 own := filepath.Join(dd, "shop.example.com.env")
22 body := []byte("CARLOS_ADMIN_TOKEN=tok\nCARLOS_INSTANCE_KEY=q+/Zk3vJ0aW1Yx9sR7tUe2oPl4mN8bC6dF5gH0iJ1kA=\n")
23 if err := os.WriteFile(own, body, 0o600); err != nil {
24 t.Fatal(err)
25 }
26 rt := registry.Route{Host: "shop.example.com", Kind: "instance", App: "shop", Account: "acct", Status: "ready", Socket: "/run/carlos/shop.sock"}
27 if err := reg.Upsert(rt); err != nil {
28 t.Fatal(err)
29 }
30 for _, vars := range []map[string]string{{"A": "1"}, {"A": "2", "CARLOS_INSTANCE_KEY": "from-config"}} {
31 if err := SetEnv(ctx, s, "acct", "shop", vars); err != nil {
32 t.Fatal(err)
33 }
34 if err := SyncExec(ctx, reg, s, pk, dd, func(string) error { return nil }, "", nil); err != nil {
35 t.Fatal(err)
36 }
37 // The render happened: <host>.config.env carries this pass's
38 // variables, the configured key included. Without this the test
39 // would pass against a SyncExec that skipped the route.
40 rendered, err := os.ReadFile(ConfigEnvPath(dd, rt))
41 if err != nil {
42 t.Fatalf("no rendered configuration after SyncExec: %v", err)
43 }
44 got := map[string]string{}
45 for _, line := range strings.Split(string(rendered), "\n") {
46 if k, v, ok := strings.Cut(line, "="); ok {
47 got[k] = v
48 }
49 }
50 for k, v := range vars {
51 if got[k] != v {
52 t.Fatalf("rendered configuration lacks %s=%s (%d bytes, %d variables)", k, v, len(rendered), len(got))
53 }
54 }
55 after, err := os.ReadFile(own)
56 if err != nil {
57 t.Fatalf("own env file after the render: %v", err)
58 }
59 if string(after) != string(body) {
60 t.Fatalf("a re-render changed the instance's own env file: %d bytes, want %d", len(after), len(body))
61 }
62 }
63}
64