| new file mode 100644 |
| index 000000000..501fd3434 |
| --- /dev/null |
| +++ b/internal/activator/rootfsenv_test.go |
| @@ -0,0 +1,189 @@ |
| +package activator |
| + |
| +import ( |
| + "context" |
| + "path/filepath" |
| + "slices" |
| + "sort" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/provision" |
| + "github.com/carlosframework/platform/internal/registry" |
| +) |
| + |
| +// The tenant's file in these tests tries to point both agent-owned rootfs |
| +// variables elsewhere, and to hand a command the control channel. |
| +const rootfsContractEnvFile = "SHOP_MODE=live\nCARLOS_SOCKET=/tmp/x\nCARLOS_DB=/x\nCARLOS_CONTROL_SOCKET=/tmp/c\nCARLOS_PURGE=header\n" |
| + |
| +// startRootfsGen7 starts generation 7 of the rootfs test row on its own |
| +// socket through StartGenExe, with envFile as the tenant's file, and |
| +// answers the handle and the generation's socket. |
| +func startRootfsGen7(t *testing.T, b *ExecBackend, ctl *fakeControl, rt registry.Route, envFile string) (*unitProc, string) { |
| + t.Helper() |
| + b.GenContainment = true |
| + writeInstanceEnv(t, b, rt, envFile) |
| + id := stagedRootfsIdentity(t, b, "v2") |
| + sock := nestedGenSocketIn(b.pathsFor(rt).runDir, 7) |
| + ctl.onStart = serveHealthy(t, sock) |
| + p, g, err := b.StartGenExe(context.Background(), rt, 7, true, id) |
| + if err != nil { |
| + t.Fatalf("StartGenExe: %v", err) |
| + } |
| + t.Cleanup(func() { p.Stop(context.Background()) }) //nolint:errcheck |
| + if g.Socket != sock { |
| + t.Fatalf("generation socket %q, want %q", g.Socket, sock) |
| + } |
| + return p.(*unitProc), sock |
| +} |
| + |
| +func sortedKeys(m map[string]string) []string { |
| + keys := make([]string, 0, len(m)) |
| + for k := range m { |
| + keys = append(keys, k) |
| + } |
| + sort.Strings(keys) |
| + return keys |
| +} |
| + |
| +// Plan 3 D1, the web half: a rootfs generation's launch file hands the |
| +// process the generation's own socket (never rt.Socket, never the |
| +// tenant's), the instance's database, the row's host and the generation's |
| +// control socket, over the tenant's own variables. Nothing else, and |
| +// nothing of the agent's environment. |
| +func TestRootfsWebLaunchCarriesTheContract(t *testing.T) { |
| + t.Setenv("PATH", "/agent/bin") |
| + t.Setenv("AGENT_ONLY", "box") |
| + b, ctl := newRootfsBackend(t) |
| + b.Region = "eu-west-2" |
| + rt := rootfsRoute(b) |
| + _, sock := startRootfsGen7(t, b, ctl, rt, rootfsContractEnvFile) |
| + l := readLaunchNamed(t, b, rt, instancectl.GenLaunchFileName(7)) |
| + if l.Schema != instancectl.LaunchSchemaRootfs || len(l.Argv) != 0 { |
| + t.Errorf("launch schema %d argv %q, want schema %d and no argv", l.Schema, l.Argv, instancectl.LaunchSchemaRootfs) |
| + } |
| + env := envMap(t, l.Env) |
| + want := map[string]string{ |
| + "CARLOS_SOCKET": sock, // the generation's own, not rt.Socket and not the .env's |
| + "CARLOS_DB": provision.NestedDBPath(b.DataDir, rt.Host), |
| + "CARLOS_HOST": rt.Host, |
| + "CARLOS_CONTROL_SOCKET": sock + ".control", |
| + "CARLOS_REGION": "eu-west-2", |
| + "SHOP_MODE": "live", |
| + } |
| + if sock == rt.Socket { |
| + t.Fatalf("the generation socket is the canonical one: %q", sock) |
| + } |
| + for k, v := range want { |
| + if env[k] != v { |
| + t.Errorf("%s = %q, want %q", k, env[k], v) |
| + } |
| + } |
| + if len(env["CARLOS_ADMIN_TOKEN"]) < 20 { |
| + t.Errorf("CARLOS_ADMIN_TOKEN missing or short: %q", env["CARLOS_ADMIN_TOKEN"]) |
| + } |
| + if len(env["CARLOS_INSTANCE_KEY"]) < 20 { |
| + t.Errorf("CARLOS_INSTANCE_KEY missing or short: %q", env["CARLOS_INSTANCE_KEY"]) |
| + } |
| + // The whole list, so a variable that starts reaching the web process |
| + // is a visible change here. CARLOS_PURGE is the agent's (no cache on |
| + // this backend: removed, the tenant's value with it). |
| + wantKeys := []string{"CARLOS_ADMIN_TOKEN", "CARLOS_CONTROL_SOCKET", "CARLOS_DB", "CARLOS_HOST", "CARLOS_INSTANCE_KEY", "CARLOS_REGION", "CARLOS_SOCKET", "SHOP_MODE"} |
| + if got := sortedKeys(env); !slices.Equal(got, wantKeys) { |
| + t.Errorf("launch variables %v, want %v", got, wantKeys) |
| + } |
| + if !slices.IsSorted(l.Env) { |
| + t.Errorf("env not one sorted entry per name: %v", l.Env) |
| + } |
| +} |
| + |
| +// Plan 3 D1, the run half: a carlos run on a rootfs row gets the row's |
| +// canonical socket and the instance's database as environment, set after |
| +// the file environment its generation launched with, so neither can be |
| +// pointed elsewhere; no control channel, no purge channel; the run's id |
| +// and directory. |
| +func TestRootfsCmdVarsFollowTheRunRule(t *testing.T) { |
| + t.Setenv("PATH", "/agent/bin") |
| + b, ctl := newRootfsBackend(t) |
| + b.Region = "eu-west-2" |
| + rt := rootfsRoute(b) |
| + up, _ := startRootfsGen7(t, b, ctl, rt, rootfsContractEnvFile) |
| + genEnv := up.LaunchEnv() |
| + if genEnv["CARLOS_SOCKET"] != "/tmp/x" || genEnv["CARLOS_DB"] != "/x" { |
| + t.Fatalf("the generation's file environment lost the tenant's values before the composer ran: %v", genEnv) |
| + } |
| + p := b.pathsFor(rt) |
| + runDir := filepath.Join(p.dataDir, CmdRunDirName, "r1") |
| + vars := b.rootfsCmdVars(rt, genEnv, p.dbPath, "r1", runDir) |
| + want := map[string]string{ |
| + "CARLOS_SOCKET": rt.Socket, |
| + "CARLOS_DB": provision.NestedDBPath(b.DataDir, rt.Host), |
| + "CARLOS_HOST": rt.Host, |
| + "CARLOS_REGION": "eu-west-2", |
| + "CARLOS_RUN_ID": "r1", |
| + "CARLOS_RUN_DIR": runDir, |
| + "SHOP_MODE": "live", |
| + } |
| + for k, v := range want { |
| + if vars[k] != v { |
| + t.Errorf("%s = %q, want %q", k, vars[k], v) |
| + } |
| + } |
| + for _, k := range []string{"CARLOS_CONTROL_SOCKET", "CARLOS_PURGE"} { |
| + if v, ok := vars[k]; ok { |
| + t.Errorf("a run carries %s=%q", k, v) |
| + } |
| + } |
| + // The serving generation's token and key, never ones of its own, and |
| + // nothing else beyond the above. |
| + for _, k := range []string{"CARLOS_ADMIN_TOKEN", "CARLOS_INSTANCE_KEY"} { |
| + if vars[k] == "" || vars[k] != genEnv[k] { |
| + t.Errorf("%s %q, want the generation's %q", k, vars[k], genEnv[k]) |
| + } |
| + } |
| + wantKeys := []string{"CARLOS_ADMIN_TOKEN", "CARLOS_DB", "CARLOS_HOST", "CARLOS_INSTANCE_KEY", "CARLOS_REGION", "CARLOS_RUN_DIR", "CARLOS_RUN_ID", "CARLOS_SOCKET", "SHOP_MODE"} |
| + if got := sortedKeys(vars); !slices.Equal(got, wantKeys) { |
| + t.Errorf("run variables %v, want %v", got, wantKeys) |
| + } |
| + // The caller's map is never written. |
| + if genEnv["CARLOS_SOCKET"] != "/tmp/x" || genEnv["CARLOS_RUN_ID"] != "" { |
| + t.Errorf("rootfsCmdVars wrote the generation's environment: %v", genEnv) |
| + } |
| + // What a launch file would carry: these and nothing of the agent's. |
| + env := rootfsLaunchEnv(vars) |
| + if m := envMap(t, env); len(m) != len(vars) || m["PATH"] != "" { |
| + t.Errorf("launch env %v", env) |
| + } |
| +} |
| + |
| +// Binary rows take no composer of this file: the socket and database stay |
| +// on the argv, and the agent sets no CARLOS_SOCKET or CARLOS_DB. |
| +func TestBinaryLaunchUnchanged(t *testing.T) { |
| + t.Parallel() |
| + b, ctl := newRootfsBackend(t) |
| + rt := nestedRoute(b, "blog.example.com") |
| + stageLive(t, b, rt.Host, "v1") |
| + writeInstanceEnv(t, b, rt, "SHOP_MODE=live\n") |
| + ctl.onStart = serveHealthy(t, rt.Socket) |
| + p, _, err := b.Start(context.Background(), rt) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer p.Stop(context.Background()) //nolint:errcheck |
| + l := readLaunchFile(t, b, rt, instancectl.RoleRun) |
| + if want := []string{b.liveLink(rt.Host), "--socket", rt.Socket, "--db", rt.DBPath}; !slices.Equal(l.Argv, want) { |
| + t.Errorf("binary argv %q, want %q", l.Argv, want) |
| + } |
| + if l.Schema != instancectl.LaunchSchema || l.Runtime != "" { |
| + t.Errorf("binary launch schema %d runtime %q", l.Schema, l.Runtime) |
| + } |
| + env := envMap(t, l.Env) |
| + for _, k := range rootfsOwnedNames { |
| + if v, ok := env[k]; ok { |
| + t.Errorf("a binary launch carries %s=%q", k, v) |
| + } |
| + } |
| + if env["SHOP_MODE"] != "live" { |
| + t.Errorf("the tenant's variable is missing from the binary launch: %v", env) |
| + } |
| +} |