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
62 lines
2.5 KiB
Go
62 lines
2.5 KiB
Go
package declaration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A container may be given supplementary groups (novox/hq ADR 0480 clause 7, issue 1636): numbers, each above zero.
|
|
|
|
// 1636/AC1: a declaration with groups decodes, is written back with them, and a declaration without them has none.
|
|
func testAContainerWithGroupsDecodesAndIsWrittenBack(t *testing.T) {
|
|
d, err := Parse([]byte(`{"declaration":1,"resources":[
|
|
{"id":"n8n.server","type":"container","name":"n8n","image":"` + anImage + `","groups":[2000,2001]},
|
|
{"id":"n8n.plain","type":"container","name":"plain","image":"` + anImage + `"}]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c := d.Resources[0].(*Container)
|
|
if len(c.Groups) != 2 || c.Groups[0] != 2000 || c.Groups[1] != 2001 {
|
|
t.Fatalf("groups read as %v", c.Groups)
|
|
}
|
|
raw, err := json.Marshal(c)
|
|
if err != nil || !strings.Contains(string(raw), `"groups":[2000,2001]`) {
|
|
t.Fatalf("written as %s (%v)", raw, err)
|
|
}
|
|
plain, _ := json.Marshal(d.Resources[1].(*Container))
|
|
if strings.Contains(string(plain), "groups") {
|
|
t.Fatalf("a container without groups is written with the key: %s", plain)
|
|
}
|
|
if g := d.Resources[1].(*Container).Groups; g != nil {
|
|
t.Fatalf("a container without groups reads %v", g)
|
|
}
|
|
}
|
|
|
|
// 1636/AC2: zero, a number below it, one the kernel keeps for "no group" and a number twice are refused, naming why.
|
|
func testAContainersGroupsAreWholeNumbersAboveZeroAndNotRepeated(t *testing.T) {
|
|
for want, groups := range map[string]string{
|
|
"never root": `[0]`,
|
|
"below zero": `[-5]`,
|
|
"no group": `[4294967295]`,
|
|
"twice": `[2000,2000]`,
|
|
} {
|
|
_, err := Parse([]byte(`{"declaration":1,"resources":[{"id":"m.c","type":"container","name":"c","image":"` +
|
|
anImage + `","groups":` + groups + `}]}`))
|
|
if err == nil || !strings.Contains(err.Error(), want) {
|
|
t.Errorf("groups %s: want a refusal saying %q, got %v", groups, want, err)
|
|
}
|
|
}
|
|
// Text where a number stands is refused by the decoder itself.
|
|
if _, err := Parse([]byte(`{"declaration":1,"resources":[{"id":"m.c","type":"container","name":"c","image":"` +
|
|
anImage + `","groups":["media"]}]}`)); err == nil {
|
|
t.Error("a group named by text was accepted")
|
|
}
|
|
}
|
|
|
|
// TestGroups holds this file's cases, each a subtest of its own name (mesh-sdk's test budget: at most 500 unit tests).
|
|
func TestGroups(t *testing.T) {
|
|
t.Run("AContainerWithGroupsDecodesAndIsWrittenBack", testAContainerWithGroupsDecodesAndIsWrittenBack)
|
|
t.Run("AContainersGroupsAreWholeNumbersAboveZeroAndNotRepeated", testAContainersGroupsAreWholeNumbersAboveZeroAndNotRepeated)
|
|
}
|