Files
mesh-sdk/go/declaration/module_test.go
T
jschoubben 3218da4ee4 J3: mesh-sdk within its test budget, each file of declaration and the provider harness one test of subtests
The unit test functions fall from 587 to 476 (the budget is 500): the cases of each of twenty test files in
declaration and provider/harness become subtests of one test named for the file, under their own names, their bodies
unchanged. Integration tests stay at 30 (budget 50).

Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
2026-11-04 14:28:23 +01:00

68 lines
2.7 KiB
Go

package declaration
import "testing"
// 1132/AC1: a resource says its module, and the declaration says each module's build; a resource that does
// not say gets today's guess, so an older controller still works.
func testAResourceSaysItsModuleAndTheDeclarationItsBuilds(t *testing.T) {
d, err := Parse([]byte(`{"declaration":1,
"builds":{"novox.be":"b-9","web":"b-4"},
"resources":[
{"id":"novox.be.data","type":"directory","path":"/srv/a","module":"novox.be"},
{"id":"web.conf","type":"file","path":"/etc/w","content":"x","module":"web"},
{"id":"old.dir","type":"directory","path":"/srv/old"},
{"id":"base","type":"directory","path":"/srv/base"}]}`))
if err != nil {
t.Fatal(err)
}
for id, want := range map[string]string{"novox.be.data": "novox.be", "web.conf": "web", "old.dir": "old"} {
got, ok := d.ModuleOfID(id)
if !ok || got != want {
t.Errorf("%s belongs to %q (%v), want %q", id, got, ok, want)
}
}
if m, ok := d.ModuleOfID("base"); ok {
t.Errorf("a resource with no module and no dot belongs to %q, want none", m)
}
if d.Builds["web"] != "b-4" || d.Builds["novox.be"] != "b-9" || len(d.Builds) != 2 {
t.Errorf("builds read as %v", d.Builds)
}
}
// 1132/AC1: the module the declaration gives wins over the guess, even when the id would guess another.
func testTheDeclaredModuleWinsOverTheGuess(t *testing.T) {
d, err := Parse([]byte(`{"declaration":1,"resources":[
{"id":"a.b.c","type":"directory","path":"/srv/a","module":"a.b"},
{"id":"x.y","type":"directory","path":"/srv/x","module":"other"}]}`))
if err != nil {
t.Fatal(err)
}
if m, _ := d.ModuleOfID("x.y"); m != "other" {
t.Errorf("x.y belongs to %q, want the declared %q", m, "other")
}
}
// 1132/AC1: every kind accepts the field.
func testEveryKindAcceptsAModule(t *testing.T) {
for _, typ := range []Type{TypeDirectory, TypeFile, TypeUser, TypeNetwork, TypeAccess, TypeArchive, TypeProcess,
TypeService, TypeOpening, TypePackage, TypeContainer, TypeAction} {
r := newOf(typ)
if r == nil {
t.Fatalf("no kind %s", typ)
}
if _, ok := r.(interface{ DeclaredModule() string }); !ok {
t.Errorf("%s does not say its module", typ)
}
if unknown := unknownFields([]byte(`{"id":"m.x","type":"`+string(typ)+`","module":"m"}`), r); len(unknown) != 0 {
t.Errorf("%s refuses %v", typ, unknown)
}
}
}
// TestModule holds this file's cases, each a subtest of its own name (mesh-sdk's test budget: at most 500 unit tests).
func TestModule(t *testing.T) {
t.Run("AResourceSaysItsModuleAndTheDeclarationItsBuilds", testAResourceSaysItsModuleAndTheDeclarationItsBuilds)
t.Run("TheDeclaredModuleWinsOverTheGuess", testTheDeclaredModuleWinsOverTheGuess)
t.Run("EveryKindAcceptsAModule", testEveryKindAcceptsAModule)
}