Files
mesh-host/internal/compose/resolution/shared_account_test.go
T
jschoubben 0c01c81c3f E8: run each file's unit tests as subtests of one test
The node-engine held 1,707 top-level unit tests against the budget of 500
a module. Each unit test file with two or more tests now runs them as
subtests of one test named for the file, each named as its function was,
in the same order: every check is kept, and the check counts 383. Files
with a TestMain, a re-run of a test by its name, or the integration tag
are left. Four files of this branch's earlier commits are formatted.

Builds: design 47 H2 §819; H-A-349 DQ-14
2026-11-22 00:30:05 +01:00

79 lines
3.6 KiB
Go

package resolution
import (
"strings"
"testing"
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
)
func userResource(fields map[string]any) map[string]any {
r := map[string]any{"id": "operator", "type": "user", "name": "op"}
for k, v := range fields {
r[k] = v
}
return r
}
func testAShellAndAGroupOnOneAccountFromTwoModulesResolve(t *testing.T) {
zsh := mcat.Manifest{Module: "zsh", Resources: []map[string]any{userResource(map[string]any{"shell": "/usr/bin/zsh"})}}
docker := mcat.Manifest{Module: "docker", Resources: []map[string]any{userResource(map[string]any{"groups": []any{"docker"}})}}
other := mcat.Manifest{Module: "media", Resources: []map[string]any{userResource(map[string]any{"groups": []any{"video"}})}}
if problems := checkResources([]mcat.Manifest{zsh, docker, other}); len(problems) != 0 {
t.Fatalf("a shell and two modules' groups on one account were refused: %v", problems)
}
if _, err := Resolve(shelf(zsh, docker, other), []string{"zsh", "docker", "media"}, workstation(), mcat.World{}); err != nil {
t.Fatalf("the three did not resolve together: %v", err)
}
}
func testTwoModulesSettingOneAccountsShellOrHomeAreRefused(t *testing.T) {
for _, field := range []string{"shell", "home"} {
a := mcat.Manifest{Module: "zsh", Resources: []map[string]any{userResource(map[string]any{field: "/one"})}}
b := mcat.Manifest{Module: "fish", Resources: []map[string]any{userResource(map[string]any{field: "/two", "groups": []any{"x"}})}}
problems := checkResources([]mcat.Manifest{a, b})
want := `zsh and fish both set the ` + field + ` of the user "op"`
if len(problems) != 1 || !strings.Contains(problems[0], want) {
t.Errorf("two modules setting %s gave %v, want %q", field, problems, want)
}
}
}
// An account a module declares only to put in a group is applied where it is written, after the package
// that makes the group (novox/hq ADR 0252); one that says how it logs in still goes first (issue 213).
func testAnAccountOnlyGivenGroupsKeepsItsWrittenPlace(t *testing.T) {
compose := func(raw string) []map[string]any {
t.Helper()
m, err := decodeForTest([]byte(raw))
if err != nil {
t.Fatal(err)
}
out, err := Declaration(mcat.Resolution{Node: "workstation", Modules: []mcat.Manifest{m}}, mcat.Rendering{})
if err != nil {
t.Fatal(err)
}
return out
}
out := compose(`{"module": "lights", "version": "1", "resources": [
{"id": "daemon", "type": "package", "package": "lights-daemon"},
{"id": "account", "type": "user", "name": "op", "groups": ["lights"]}
]}`)
if pkg, acct := indexOf(out, "lights.daemon"), indexOf(out, "lights.account"); pkg < 0 || acct < pkg {
t.Fatalf("the account (%d) is not after the package that makes its group (%d): %v", acct, pkg, out)
}
out = compose(`{"module": "shell", "version": "1", "resources": [
{"id": "package", "type": "package", "package": "zsh"},
{"id": "login", "type": "user", "name": "op", "shell": "/usr/bin/zsh", "groups": ["wheel"]}
]}`)
if pkg, acct := indexOf(out, "shell.package"), indexOf(out, "shell.login"); acct < 0 || acct > pkg {
t.Fatalf("an account with a shell is no longer first (%d, package %d): %v", acct, pkg, out)
}
}
// TestSharedAccount runs this file's tests, each a subtest named as its function was (design 47 H2 §819, the test budget).
func TestSharedAccount(t *testing.T) {
t.Run("AShellAndAGroupOnOneAccountFromTwoModulesResolve", testAShellAndAGroupOnOneAccountFromTwoModulesResolve)
t.Run("TwoModulesSettingOneAccountsShellOrHomeAreRefused", testTwoModulesSettingOneAccountsShellOrHomeAreRefused)
t.Run("AnAccountOnlyGivenGroupsKeepsItsWrittenPlace", testAnAccountOnlyGivenGroupsKeepsItsWrittenPlace)
}