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") } }