package main import ( "encoding/json" "fmt" "strings" "testing" ) // A module only pulled in by another on this node has a value of its own in the assignments mirror (mesh-controller // internal/inventory AssignmentValues, issue 765): its plan, what requires it, and no time it was assigned. The // client fills it exactly like an assigned module: its files, its secrets from the vault and its items. // pullIn writes a module's value as the controller writes one only pulled in, by requiredBy. func (b *aBus) pullIn(t *testing.T, module, requiredBy string, plan Plan) { t.Helper() p, err := json.Marshal(plan) if err != nil { t.Fatal(err) } b.put(t, PlansBucket, node+"."+module, fmt.Sprintf(`{"node":%q,"module":%q,"required-by":%q,`+ `"provisioning":%s}`, node, module, requiredBy, p)) } func TestAPulledInModulesValueIsReadForItsPlan(t *testing.T) { plan := contributor(t, "shell:zsh:normal", "alias nox=mesh-cli") p, err := json.Marshal(plan) if err != nil { t.Fatal(err) } value := fmt.Sprintf(`{"node":"g14","module":"mesh-cli","required-by":"desk","provisioning":%s}`, p) module, got, err := planOf("g14", "g14.mesh-cli", []byte(value)) if err != nil || module != "mesh-cli" || len(got.Items) != 1 || got.Items[0].Collection != "shell:zsh:normal" { t.Fatalf("a pulled-in module's value reads as %s %+v (%v)", module, got, err) } // Still strict in the plan. spoilt := strings.Replace(value, `"items":`, `"surprise":1,"items":`, 1) if _, _, err := planOf("g14", "g14.mesh-cli", []byte(spoilt)); err == nil { t.Fatalf("a pulled-in module's plan with an unknown field is acted on: %s", spoilt) } } func TestNatsAPulledInModuleIsFilledWithItsSecretAndItsItemIsRecorded(t *testing.T) { b := startBus(t) heard := b.listen(t) vault := startVault(t, b) const token, password = "tok-765-never-on-the-bus", "pw-765-never-on-the-bus" vault.keep("own/g14/app/app-token", token, false) vault.keep("own/g14/app/db-password", password, false) e := startEngine(t, map[string]string{appConf: "owned", appDB: "owned", zshrc: "owned"}) b.config(t, "app", "font-size", "13") b.pullIn(t, "app", "desk", app(t)) b.assign(t, "zsh", zshCollector(t)) b.pullIn(t, "mesh-cli", "desk", contributor(t, "shell:zsh:normal", "alias nox=mesh-cli\n")) aClient(t, b, e, t.TempDir()) eventually(t, "the pulled-in app.conf is filled with its secret", func() bool { return e.read(t, appConf) == "token = "+token+"\nfont = 13\n" }) eventually(t, "its secret's own file holds it alone", func() bool { return e.read(t, appDB) == password }) eventually(t, "the pulled-in contributor's item is recorded", func() bool { got := b.keys(t, CollectBucket, node+".") return len(got) == 1 && got[0] == "g14.shell=3Azsh=3Anormal.mesh-cli" }) eventually(t, "zshrc holds the pulled-in contributor's item", func() bool { return e.read(t, zshrc) == "# zsh\n# mesh-cli\nalias nox=mesh-cli\n# then\n# end\n" }) eventually(t, "prov.applied says app applied", func() bool { r, _ := heard.last("app") return r.Status == StatusApplied && len(r.Files) == 2 }) // What required it lets it go: its item leaves the collection. b.del(t, PlansBucket, node+".mesh-cli") eventually(t, "zshrc loses the item of a module no longer pulled in", func() bool { return e.read(t, zshrc) == "# zsh\n# then\n# end\n" }) }