Files
mesh-sdk/go/catalogue/invokes_test.go
T

73 lines
3.5 KiB
Go

package catalogue
import (
"encoding/json"
"strings"
"testing"
)
// A manifest may say which tools its module calls (novox/hq ADR 0152), and the parser accepts the
// two shapes the grant has: a named tool, and every tool.
func TestAManifestMaySayWhatItInvokes(t *testing.T) {
m, err := decodeForTest([]byte(`{"module":"mesh-console","version":"1",` +
`"invokes":["mesh-catalog.catalog_modules","*"]}`))
if err != nil {
t.Fatal(err)
}
if len(m.Invokes) != 2 || m.Invokes[1] != "*" {
t.Fatalf("invokes not read: %v", m.Invokes)
}
// And that its own secrets are the core's credentials (issue 774): read, and written back as said.
for raw, core := range map[string]bool{`{"module":"bus","version":"1","core-credentials":true}`: true, `{"module":"app","version":"1"}`: false} {
m, err := decodeForTest([]byte(raw))
if err != nil || m.CoreCredentials != core || m.UnknownField() != "" {
t.Errorf("%s: core credentials %v, %v, %q", raw, m.CoreCredentials, err, m.UnknownField())
}
back, _ := json.Marshal(m)
if strings.Contains(string(back), "core-credentials") != core {
t.Errorf("%s written back as %s", raw, back)
}
if CarriesCoreCredentials(m) != core {
t.Errorf("%s: carries the core's credentials %v", raw, !core)
}
}
// And that it is the tool runner (issue 774): read, written back, and found among others by that alone.
runner, err := decodeForTest([]byte(`{"module":"any-name","version":"1","tool-runner":true}`))
if err != nil || !IsToolRunner(runner) || runner.UnknownField() != "" {
t.Fatalf("a tool runner: %v %q", err, runner.UnknownField())
}
if back, _ := json.Marshal(runner); !strings.Contains(string(back), `"tool-runner":true`) {
t.Errorf("a tool runner written back as %s", back)
}
other, _ := decodeForTest([]byte(`{"module":"node-tools","version":"1"}`))
if got, ok := ToolRunnerOf([]Manifest{other, runner}); !ok || got.Module != "any-name" {
t.Errorf("the tool runner found is %q, %v: by what it says, never by its name", got.Module, ok)
}
// A module's seats are derived from what it declares, whatever its name: a service on a machine is the service
// manager's, the node-engine's name exempts nothing; a module whose resources are computed depends on none.
host, _ := decodeForTest([]byte(`{"module":"mesh-host","version":"1","resources":[{"id":"s","type":"service","name":"s.service"}]}`))
if got := DependsOn(nil, host); len(got) != 1 || got[0] != ServiceManagerSeat {
t.Errorf("a service by the node-engine's name depends on %v", got)
}
if got := DependsOn(nil, Manifest{Module: "net", Computed: "net", Resources: host.Resources}); len(got) != 0 {
t.Errorf("a computed module depends on %v", got)
}
// And its upgrade policy, which holds for the bus as for any module: providing the bus derives nothing (issue 774).
for raw, want := range map[string]string{
`{"module":"bus","version":"1","provides":[{"name":"mesh-bus"}],"upgrade":{"policy":"record","why":"its streams"}}`: PolicyRecord + " " + FromModule,
`{"module":"bus","version":"1","provides":[{"name":"mesh-bus"}]}`: PolicyRoll + " " + FromDefault,
} {
m, err := decodeForTest([]byte(raw))
if err != nil {
t.Fatal(err)
}
if policy, from, _ := DerivedUpgrade(m); policy+" "+from != want {
t.Errorf("%s: derived %s %s, want %s", raw, policy, from, want)
}
chosen := &PersonPolicy{Policy: PolicyTogether, Why: "w", By: "a person"}
if policy, from, _, _ := Upgrade(m, chosen); policy != PolicyTogether || from != FromPerson {
t.Errorf("%s: a person's choice gave %s %s", raw, policy, from)
}
}
}