package catalogue import ( "strings" "testing" ) // **A process's environment is composed as a container's is** (novox/hq to-be 38 WP4c). // // A module's code moving out of its container becomes a process on the machine, and what its // container's environment asked for — the port this machine gave the module, the place it put the // module's directory — it still has to be told. Filled for a container and not for a process, the // literal `${port:8080}` reached the process as its environment and was read as a port; the modules // that moved first wrote their run-once steps an env file instead. func processModule(env map[string]any) Manifest { return Manifest{ Module: "showcase", Listens: []Listening{{Port: 8080, From: FromMesh}}, Resources: []map[string]any{ {"id": "data", "type": "directory", "mode": "0700"}, {"id": "setup", "type": "process", "name": "showcase-setup", "run-once": true, "run": []any{"/usr/bin/showcase", "setup"}, "env": env}, }, } } func TestAProcessIsToldItsPortAndItsPlaceInItsEnvironment(t *testing.T) { env := map[string]any{ "SHOWCASE_URL": "http://127.0.0.1:${port:8080}", "SHOWCASE_DATA": "${dir:data}/objects", "SHOWCASE_DB": "127.0.0.1:${seat:mesh-store:5432}", "GREETING": "hello", } out, err := Resolution{Node: "anchor", Modules: []Manifest{processModule(env)}}.Declaration(Rendering{ Ports: map[string]map[int]int{"showcase": {8080: 21000}}, Seats: map[string]map[int]int{"mesh-store": {5432: 6852}}, }) if err != nil { t.Fatalf("a process asking for its port and its place does not compose: %v", err) } setup := fileNamed(out, "showcase.setup") if setup == nil { t.Fatalf("the process is not in the declaration: %v", out) } got, _ := setup["env"].(map[string]any) for key, want := range map[string]string{ "SHOWCASE_URL": "http://127.0.0.1:21000", "SHOWCASE_DATA": "/var/lib/showcase/data/objects", "SHOWCASE_DB": "127.0.0.1:6852", "GREETING": "hello", } { if got[key] != want { t.Errorf("the process is told %s=%v, want %q", key, got[key], want) } } if env["SHOWCASE_URL"] != "http://127.0.0.1:${port:8080}" { t.Fatalf("composing for one machine edited the module's own manifest: %v", env) } } // An unknown reference in a process's environment is refused as a container's is, naming the // process and the variable — left alone, it would reach the machine as a literal. func TestAProcessAskingAboutAnUndeclaredPortIsRefused(t *testing.T) { env := map[string]any{"SHOWCASE_URL": "http://127.0.0.1:${port:9999}"} _, err := Resolution{Node: "anchor", Modules: []Manifest{processModule(env)}}.Declaration(Rendering{}) if err == nil { t.Fatal("a process was told a port its module never said it listens on") } for _, said := range []string{"showcase-setup", "SHOWCASE_URL", "${port:9999}", "8080"} { if !strings.Contains(err.Error(), said) { t.Errorf("the refusal does not say %q: %v", said, err) } } env = map[string]any{"SHOWCASE_DATA": "${dir:date}/objects"} if _, err := (Resolution{Node: "anchor", Modules: []Manifest{processModule(env)}}).Declaration(Rendering{}); err == nil || !strings.Contains(err.Error(), "${dir:date}") { t.Fatalf("a process naming no directory of its module was not refused: %v", err) } }