| 1 | package activator |
| 2 | |
| 3 | import ( |
| 4 | "maps" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/edge" |
| 11 | "github.com/carlosframework/platform/internal/moves" |
| 12 | "github.com/carlosframework/platform/internal/registry" |
| 13 | ) |
| 14 | |
| 15 | // TestAgentOwnedVarsAreMovesBoxOwned pins moves.AgentOwnedVars against |
| 16 | // agentOwnedVars (home part 2, Task 2b; fix round 2). A move's env pin and |
| 17 | // T3 leave the agent-owned variables out, because each box composes them |
| 18 | // for itself; a variable the agent starts to set, overwrite or remove |
| 19 | // without joining that set would make every cross-region move's pin differ |
| 20 | // on the target again, or let an instance-supplied value slip past it. |
| 21 | // |
| 22 | // Each feature configuration is asserted separately, against a tenant env |
| 23 | // file that names every agent-owned variable: the exact value the agent |
| 24 | // sets, or the variable's absence where the agent removes it. Removing any |
| 25 | // one branch's set or deletion fails the test. The variables named here |
| 26 | // are the ones the test knows: a new variable the agent only ever removes |
| 27 | // (never sets) appears in no configuration's output, so it must be added |
| 28 | // to moves.AgentOwnedVars and to the tenant file below by hand. |
| 29 | func TestAgentOwnedVarsAreMovesBoxOwned(t *testing.T) { |
| 30 | rt := registry.Route{Host: "shop.example.com", Socket: "/run/carlos/shop.example.com.sock"} |
| 31 | control := rt.Socket + ".control" |
| 32 | tenant := map[string]string{"APP_MODE": "tenant", "HOSTNAME": "tenant"} |
| 33 | for k := range moves.AgentOwnedVars { |
| 34 | tenant[k] = "tenant" |
| 35 | } |
| 36 | cache := edge.NewCache(16, 1<<20) |
| 37 | absent := "<absent>" |
| 38 | for _, c := range []struct { |
| 39 | name string |
| 40 | b *ExecBackend |
| 41 | control bool |
| 42 | want map[string]string |
| 43 | }{ |
| 44 | {"everything on", &ExecBackend{Region: "uk", Cache: cache}, true, |
| 45 | map[string]string{"CARLOS_CONTROL_SOCKET": control, "CARLOS_PURGE": "header", "CARLOS_REGION": "uk", "CARLOS_HOST": rt.Host}}, |
| 46 | {"everything off", &ExecBackend{}, false, |
| 47 | map[string]string{"CARLOS_CONTROL_SOCKET": absent, "CARLOS_PURGE": absent, "CARLOS_REGION": absent, "CARLOS_HOST": rt.Host}}, |
| 48 | {"no control channel", &ExecBackend{Region: "uk", Cache: cache}, false, |
| 49 | map[string]string{"CARLOS_CONTROL_SOCKET": absent, "CARLOS_PURGE": "header", "CARLOS_REGION": "uk", "CARLOS_HOST": rt.Host}}, |
| 50 | {"no cache", &ExecBackend{Region: "uk"}, true, |
| 51 | map[string]string{"CARLOS_CONTROL_SOCKET": control, "CARLOS_PURGE": absent, "CARLOS_REGION": "uk", "CARLOS_HOST": rt.Host}}, |
| 52 | {"a home box, no region", &ExecBackend{Cache: cache}, true, |
| 53 | map[string]string{"CARLOS_CONTROL_SOCKET": control, "CARLOS_PURGE": "header", "CARLOS_REGION": absent, "CARLOS_HOST": rt.Host}}, |
| 54 | } { |
| 55 | t.Run(c.name, func(t *testing.T) { |
| 56 | for _, file := range []map[string]string{{}, tenant} { |
| 57 | got := c.b.agentOwnedVars(rt, control, file, c.control) |
| 58 | for k, want := range c.want { |
| 59 | v, ok := got[k] |
| 60 | if want == absent && ok { |
| 61 | t.Errorf("file %v: %s = %q, want it removed", file, k, v) |
| 62 | } |
| 63 | if want != absent && (!ok || v != want) { |
| 64 | t.Errorf("file %v: %s = %q (present %v), want %q", file, k, v, ok, want) |
| 65 | } |
| 66 | } |
| 67 | // Nothing else is the agent's: the tenant's own variables, |
| 68 | // a box variable among them, pass through untouched. |
| 69 | for k, v := range got { |
| 70 | if _, owned := c.want[k]; !owned && file[k] != v { |
| 71 | t.Errorf("file %v: the agent set %s = %q", file, k, v) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | }) |
| 76 | } |
| 77 | // The configurations above cover exactly moves' set, and every member |
| 78 | // is box-owned and never pinned. |
| 79 | covered := slices.Sorted(maps.Keys(map[string]string{"CARLOS_CONTROL_SOCKET": "", "CARLOS_PURGE": "", "CARLOS_REGION": "", "CARLOS_HOST": ""})) |
| 80 | if want := slices.Sorted(maps.Keys(moves.AgentOwnedVars)); !slices.Equal(covered, want) { |
| 81 | t.Fatalf("the test covers %v, moves.AgentOwnedVars is %v", covered, want) |
| 82 | } |
| 83 | // LaunchLayers' list of the names the agent decides is the same set. |
| 84 | if names := slices.Sorted(slices.Values(agentOwnedNames)); !slices.Equal(names, covered) { |
| 85 | t.Fatalf("agentOwnedNames is %v, the agent decides %v", names, covered) |
| 86 | } |
| 87 | for _, k := range covered { |
| 88 | if !moves.BoxOwned(k) || moves.Pinned(k, false) || moves.Class(k) == "equal" { |
| 89 | t.Errorf("%s is agent-owned but pinned or compared when the box supplies it (class %q)", k, moves.Class(k)) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // TestLaunchLayersNamesTheInstanceLayers (fix round 2): LaunchLayers is |
| 95 | // LaunchEnvironment with the names the instance's own layers supplied, the |
| 96 | // rendered configuration file's and the own .env's. A box variable an |
| 97 | // instance file sets is one of them; one inherited from the agent's |
| 98 | // process is not, and neither is anything the agent adds. |
| 99 | func TestLaunchLayersNamesTheInstanceLayers(t *testing.T) { |
| 100 | b := &ExecBackend{DataDir: t.TempDir(), Region: "ie"} |
| 101 | rt := registry.Route{Host: "a.go.example.com", Socket: filepath.Join(b.DataDir, "a.sock")} |
| 102 | t.Setenv("PWD", "/inherited") |
| 103 | if err := os.WriteFile(filepath.Join(b.DataDir, rt.Host+".env"), []byte("MINE=1\nHOSTNAME=instance\nCARLOS_REGION=fake\n"), 0o600); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | if err := os.WriteFile(filepath.Join(b.DataDir, rt.Host+".config.env"), []byte("FROM_CONFIG=c\n"), 0o600); err != nil { |
| 107 | t.Fatal(err) |
| 108 | } |
| 109 | env, instance, shadowed, err := b.LaunchLayers(rt) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | plain, plainShadowed, err := b.LaunchEnvironment(rt) |
| 114 | if err != nil || !maps.Equal(env, plain) || !slices.Equal(shadowed, plainShadowed) { |
| 115 | t.Fatalf("LaunchLayers is not LaunchEnvironment: %v", err) |
| 116 | } |
| 117 | for _, k := range []string{"MINE", "HOSTNAME", "FROM_CONFIG"} { |
| 118 | if !instance[k] { |
| 119 | t.Errorf("%s came from the instance's files but is not named", k) |
| 120 | } |
| 121 | } |
| 122 | if env["HOSTNAME"] != "instance" || env["PWD"] != "/inherited" { |
| 123 | t.Fatalf("HOSTNAME %q PWD %q", env["HOSTNAME"], env["PWD"]) |
| 124 | } |
| 125 | // CARLOS_REGION is in the instance's .env, but the agent decides it: it |
| 126 | // is not the instance's. |
| 127 | for _, k := range []string{"PWD", "CARLOS_HOST", "CARLOS_CONTROL_SOCKET", "CARLOS_REGION"} { |
| 128 | if instance[k] { |
| 129 | t.Errorf("%s did not come from the instance's files but is named", k) |
| 130 | } |
| 131 | } |
| 132 | // The instance's HOSTNAME is pinned; the inherited PWD is not. |
| 133 | if !moves.Pinned("HOSTNAME", instance["HOSTNAME"]) || moves.Pinned("PWD", instance["PWD"]) { |
| 134 | t.Fatal("the pin does not follow the layers") |
| 135 | } |
| 136 | } |
| 137 | |