| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/caps" |
| 9 | "github.com/carlosframework/platform/internal/cgroup" |
| 10 | "github.com/carlosframework/platform/internal/registry" |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | // TestMemTier: the tier is a platform choice keyed on ONE manifest bit. |
| 15 | // Everything that isn't "declares children" — no manifest, no |
| 16 | // capabilities block, an unreadable sibling — is the default tier: the |
| 17 | // safe direction is the smaller cap, and the caps pass will say why the |
| 18 | // sibling is unreadable in its own advice. |
| 19 | func TestMemTier(t *testing.T) { |
| 20 | rt := registry.Route{Host: "h.example.com", App: "app"} |
| 21 | cases := []struct { |
| 22 | name string |
| 23 | m *release.Manifest |
| 24 | err error |
| 25 | want cgroup.Limit |
| 26 | }{ |
| 27 | {"no manifest", nil, nil, cgroup.TierDefault}, |
| 28 | {"unreadable", nil, errors.New("boom"), cgroup.TierDefault}, |
| 29 | {"no capabilities", &release.Manifest{}, nil, cgroup.TierDefault}, |
| 30 | {"capabilities, no children", &release.Manifest{Capabilities: &caps.Set{V: caps.Version}}, nil, cgroup.TierDefault}, |
| 31 | {"children", &release.Manifest{Capabilities: &caps.Set{V: caps.Version, Children: true}}, nil, cgroup.TierChildren}, |
| 32 | } |
| 33 | for _, c := range cases { |
| 34 | tier := memTier(func(registry.Route) (*release.Manifest, error) { return c.m, c.err }) |
| 35 | if got := tier(rt); got != c.want { |
| 36 | t.Errorf("%s: tier = %v, want %v", c.name, got, c.want) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // memTierOf reads the NAMED version's manifest, never the live link's: a |
| 42 | // generation's limits are its own artifact's (zero-downtime spec §7.3). |
| 43 | func TestMemTierOfReadsTheNamedVersion(t *testing.T) { |
| 44 | rt := registry.Route{Host: "h.example.com", App: "app"} |
| 45 | var asked []string |
| 46 | tier := memTierOf(func(_ registry.Route, v string) (*release.Manifest, error) { |
| 47 | asked = append(asked, v) |
| 48 | if v == "v1" { |
| 49 | return &release.Manifest{Capabilities: &caps.Set{V: caps.Version, Children: true}}, nil |
| 50 | } |
| 51 | return &release.Manifest{}, nil |
| 52 | }) |
| 53 | if got := tier(rt, "v1"); got != cgroup.TierChildren { |
| 54 | t.Errorf("v1 = %v, want the children tier", got) |
| 55 | } |
| 56 | if got := tier(rt, "v2"); got != cgroup.TierDefault { |
| 57 | t.Errorf("v2 = %v, want the default tier", got) |
| 58 | } |
| 59 | if strings.Join(asked, ",") != "v1,v2" { |
| 60 | t.Errorf("read the manifests of %v, want v1 then v2", asked) |
| 61 | } |
| 62 | } |
| 63 | |