Modules talk over the bus. The power module's bundle puts each fresh reading of its own sampler in its state draw, keyed by the machine; the bar's block shows that state, which the bar's bundle watches under the read the mesh grants it. A new catalogue check refuses a module naming another module's path under /run or /var/lib.
64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func catalogue(t *testing.T, files map[string]string) string {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
for p, c := range files {
|
|
full := filepath.Join(root, p)
|
|
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(full, []byte(c), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return root
|
|
}
|
|
|
|
const owner = `{"module":"power","resources":[{"id":"d","type":"directory","path":"/run/mesh-power"}]}`
|
|
|
|
func TestAModuleReadingAnothersStateIsFound(t *testing.T) {
|
|
root := catalogue(t, map[string]string{
|
|
"modules/power/module.json": owner,
|
|
"modules/power/cmd/power/draw.go": `const f = "/run/mesh-power/draw"`,
|
|
"modules/bar/module.json": `{"module":"bar","resources":[{"id":"x","type":"file","path":"/etc/x","content":"cat /run/mesh-power/draw"}]}`,
|
|
"modules/other/module.json": `{"module":"other"}`,
|
|
"modules/other/files/bin/read": "#!/bin/sh\ncat /run/mesh-power\n",
|
|
"modules/other/README.md": "it once read /run/mesh-power/draw",
|
|
"modules/other/cmd/x/x_test.go": `"/run/mesh-power"`,
|
|
"modules/near/module.json": `{"module":"near","resources":[{"id":"y","type":"file","path":"/etc/y","content":"/run/mesh-power-other/x"}]}`,
|
|
})
|
|
found, err := Check(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []string
|
|
for _, f := range found {
|
|
got = append(got, f.Module+" "+f.File)
|
|
}
|
|
want := []string{"bar modules/bar/module.json", "other modules/other/files/bin/read"}
|
|
if strings.Join(got, "|") != strings.Join(want, "|") {
|
|
t.Fatalf("found %v, want %v", got, want)
|
|
}
|
|
if !strings.Contains(found[0].String(), "power's") {
|
|
t.Fatalf("%s", found[0])
|
|
}
|
|
}
|
|
|
|
func TestACatalogueWhereEachKeepsToItsOwnIsClean(t *testing.T) {
|
|
root := catalogue(t, map[string]string{
|
|
"modules/power/module.json": owner,
|
|
"modules/power/cmd/power/draw.go": `const f = "/run/mesh-power/draw"`,
|
|
})
|
|
if found, err := Check(root); err != nil || len(found) != 0 {
|
|
t.Fatalf("%v %v", found, err)
|
|
}
|
|
}
|