package activator import ( "maps" "os" "path/filepath" "slices" "testing" "github.com/carlosframework/platform/internal/edge" "github.com/carlosframework/platform/internal/moves" "github.com/carlosframework/platform/internal/registry" ) // TestAgentOwnedVarsAreMovesBoxOwned pins moves.AgentOwnedVars against // agentOwnedVars (home part 2, Task 2b; fix round 2). A move's env pin and // T3 leave the agent-owned variables out, because each box composes them // for itself; a variable the agent starts to set, overwrite or remove // without joining that set would make every cross-region move's pin differ // on the target again, or let an instance-supplied value slip past it. // // Each feature configuration is asserted separately, against a tenant env // file that names every agent-owned variable: the exact value the agent // sets, or the variable's absence where the agent removes it. Removing any // one branch's set or deletion fails the test. The variables named here // are the ones the test knows: a new variable the agent only ever removes // (never sets) appears in no configuration's output, so it must be added // to moves.AgentOwnedVars and to the tenant file below by hand. func TestAgentOwnedVarsAreMovesBoxOwned(t *testing.T) { rt := registry.Route{Host: "shop.example.com", Socket: "/run/carlos/shop.example.com.sock"} control := rt.Socket + ".control" tenant := map[string]string{"APP_MODE": "tenant", "HOSTNAME": "tenant"} for k := range moves.AgentOwnedVars { tenant[k] = "tenant" } cache := edge.NewCache(16, 1<<20) absent := "" for _, c := range []struct { name string b *ExecBackend control bool want map[string]string }{ {"everything on", &ExecBackend{Region: "uk", Cache: cache}, true, map[string]string{"CARLOS_CONTROL_SOCKET": control, "CARLOS_PURGE": "header", "CARLOS_REGION": "uk", "CARLOS_HOST": rt.Host}}, {"everything off", &ExecBackend{}, false, map[string]string{"CARLOS_CONTROL_SOCKET": absent, "CARLOS_PURGE": absent, "CARLOS_REGION": absent, "CARLOS_HOST": rt.Host}}, {"no control channel", &ExecBackend{Region: "uk", Cache: cache}, false, map[string]string{"CARLOS_CONTROL_SOCKET": absent, "CARLOS_PURGE": "header", "CARLOS_REGION": "uk", "CARLOS_HOST": rt.Host}}, {"no cache", &ExecBackend{Region: "uk"}, true, map[string]string{"CARLOS_CONTROL_SOCKET": control, "CARLOS_PURGE": absent, "CARLOS_REGION": "uk", "CARLOS_HOST": rt.Host}}, {"a home box, no region", &ExecBackend{Cache: cache}, true, map[string]string{"CARLOS_CONTROL_SOCKET": control, "CARLOS_PURGE": "header", "CARLOS_REGION": absent, "CARLOS_HOST": rt.Host}}, } { t.Run(c.name, func(t *testing.T) { for _, file := range []map[string]string{{}, tenant} { got := c.b.agentOwnedVars(rt, control, file, c.control) for k, want := range c.want { v, ok := got[k] if want == absent && ok { t.Errorf("file %v: %s = %q, want it removed", file, k, v) } if want != absent && (!ok || v != want) { t.Errorf("file %v: %s = %q (present %v), want %q", file, k, v, ok, want) } } // Nothing else is the agent's: the tenant's own variables, // a box variable among them, pass through untouched. for k, v := range got { if _, owned := c.want[k]; !owned && file[k] != v { t.Errorf("file %v: the agent set %s = %q", file, k, v) } } } }) } // The configurations above cover exactly moves' set, and every member // is box-owned and never pinned. covered := slices.Sorted(maps.Keys(map[string]string{"CARLOS_CONTROL_SOCKET": "", "CARLOS_PURGE": "", "CARLOS_REGION": "", "CARLOS_HOST": ""})) if want := slices.Sorted(maps.Keys(moves.AgentOwnedVars)); !slices.Equal(covered, want) { t.Fatalf("the test covers %v, moves.AgentOwnedVars is %v", covered, want) } // LaunchLayers' list of the names the agent decides is the same set. if names := slices.Sorted(slices.Values(agentOwnedNames)); !slices.Equal(names, covered) { t.Fatalf("agentOwnedNames is %v, the agent decides %v", names, covered) } for _, k := range covered { if !moves.BoxOwned(k) || moves.Pinned(k, false) || moves.Class(k) == "equal" { t.Errorf("%s is agent-owned but pinned or compared when the box supplies it (class %q)", k, moves.Class(k)) } } } // TestLaunchLayersNamesTheInstanceLayers (fix round 2): LaunchLayers is // LaunchEnvironment with the names the instance's own layers supplied, the // rendered configuration file's and the own .env's. A box variable an // instance file sets is one of them; one inherited from the agent's // process is not, and neither is anything the agent adds. func TestLaunchLayersNamesTheInstanceLayers(t *testing.T) { b := &ExecBackend{DataDir: t.TempDir(), Region: "ie"} rt := registry.Route{Host: "a.go.example.com", Socket: filepath.Join(b.DataDir, "a.sock")} t.Setenv("PWD", "/inherited") if err := os.WriteFile(filepath.Join(b.DataDir, rt.Host+".env"), []byte("MINE=1\nHOSTNAME=instance\nCARLOS_REGION=fake\n"), 0o600); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(b.DataDir, rt.Host+".config.env"), []byte("FROM_CONFIG=c\n"), 0o600); err != nil { t.Fatal(err) } env, instance, shadowed, err := b.LaunchLayers(rt) if err != nil { t.Fatal(err) } plain, plainShadowed, err := b.LaunchEnvironment(rt) if err != nil || !maps.Equal(env, plain) || !slices.Equal(shadowed, plainShadowed) { t.Fatalf("LaunchLayers is not LaunchEnvironment: %v", err) } for _, k := range []string{"MINE", "HOSTNAME", "FROM_CONFIG"} { if !instance[k] { t.Errorf("%s came from the instance's files but is not named", k) } } if env["HOSTNAME"] != "instance" || env["PWD"] != "/inherited" { t.Fatalf("HOSTNAME %q PWD %q", env["HOSTNAME"], env["PWD"]) } // CARLOS_REGION is in the instance's .env, but the agent decides it: it // is not the instance's. for _, k := range []string{"PWD", "CARLOS_HOST", "CARLOS_CONTROL_SOCKET", "CARLOS_REGION"} { if instance[k] { t.Errorf("%s did not come from the instance's files but is named", k) } } // The instance's HOSTNAME is pinned; the inherited PWD is not. if !moves.Pinned("HOSTNAME", instance["HOSTNAME"]) || moves.Pinned("PWD", instance["PWD"]) { t.Fatal("the pin does not follow the layers") } }