1// SPDX-License-Identifier: MIT
2
3package main
4
5import (
6 "context"
7 "strings"
8 "testing"
9
10 "bnema.dev/ermite-runner/internal/actionscache"
11 "bnema.dev/ermite-runner/internal/cachesvc"
12 "bnema.dev/ermite-runner/internal/sandbox"
13)
14
15// The three size caps share one parser: a zero size in any unit turns the
16// registry proxy and the action archive cache off, and is refused for the
17// cache API, which ERMITE_RUNNER_CACHE=0 turns off. Each error names its
18// variable.
19func TestCacheSizeConfiguration(t *testing.T) {
20 t.Setenv(envCache, "1")
21 for _, tc := range []struct {
22 registry, actions, cache string
23 registryOn, archivesOn bool
24 }{
25 {"", "", "1M", true, true},
26 {"1M", "0", "2M", true, false},
27 {"0", "1M", "1M", false, true},
28 {"0G", "0GiB", "1M", false, false},
29 {" 0M ", "", "1M", false, true},
30 {"00", "", "1M", false, true},
31 {"0GiB", "", "1M", false, true},
32 {"10M", "", "1M", true, true},
33 } {
34 t.Setenv(envRegistryMax, tc.registry)
35 t.Setenv(actionscache.EnvMax, tc.actions)
36 t.Setenv(cachesvc.EnvMax, tc.cache)
37 cfg := sandbox.Config{StateDir: t.TempDir()}
38 svc, archives, err := setupCache(context.Background(), &cfg)
39 if err != nil {
40 t.Fatalf("%+v: %v", tc, err)
41 }
42 if svc.RegistryEnabled() != tc.registryOn || (archives != nil) != tc.archivesOn {
43 t.Fatalf("%+v: registry=%v archives=%v", tc, svc.RegistryEnabled(), archives != nil)
44 }
45 _ = svc.CloseAll()
46 }
47 t.Setenv(envRegistryMax, "")
48 t.Setenv(actionscache.EnvMax, "")
49 t.Setenv(cachesvc.EnvMax, "")
50 for _, tc := range []struct{ env, value string }{
51 {envRegistryMax, "invalid"}, {envRegistryMax, "G"}, {envRegistryMax, "-1G"},
52 {actionscache.EnvMax, "x"},
53 {cachesvc.EnvMax, "0"}, {cachesvc.EnvMax, "0G"}, {cachesvc.EnvMax, "-1"},
54 } {
55 t.Run(tc.env+"="+tc.value, func(t *testing.T) {
56 t.Setenv(tc.env, tc.value)
57 cfg := sandbox.Config{StateDir: t.TempDir()}
58 svc, _, err := setupCache(context.Background(), &cfg)
59 if err == nil {
60 _ = svc.CloseAll()
61 t.Fatal("accepted")
62 }
63 if !strings.HasPrefix(err.Error(), tc.env+": ") {
64 t.Fatalf("error does not name %s: %v", tc.env, err)
65 }
66 })
67 }
68 t.Setenv(envCache, "0")
69 t.Setenv(envRegistryMax, "invalid")
70 cfg := sandbox.Config{StateDir: t.TempDir()}
71 svc, _, err := setupCache(context.Background(), &cfg)
72 if err != nil || svc != nil {
73 t.Fatalf("cache off: %v %v", svc, err)
74 }
75}