carlos / platform Public

instancectl: the egress sweep's cgroup tests are linux-only (#328)

They read a fixture tree through the linux populatedHostSlices; the
other platforms' stub always errors, so they move to a //go:build linux
file. The Hosts-error loop test no longer needs the fixture and stays
portable.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev cbdb23f32c18aca762d8231322c94c6a261768e5 parent 82236ad
2 files changed, +235 −220
  • internal/instancectl/egress_sweep_linux_test.go +234 −0
  • internal/instancectl/egress_test.go +1 −220
diff --git a/internal/instancectl/egress_sweep_linux_test.go b/internal/instancectl/egress_sweep_linux_test.go
new file mode 100644
index 000000000..83b8efa94
--- /dev/null
+++ b/internal/instancectl/egress_sweep_linux_test.go
@@ -0,0 +1,234 @@
+//go:build linux
+
+package instancectl
+
+// The sweep's tests: they read a fixture cgroup tree through the linux
+// populatedHostSlices (the other platforms' stub always errors).
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "path/filepath"
+ "reflect"
+ "slices"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+)
+
+// cgroupSlice writes a per-host slice's cgroup.events into a fixture tree
+// laid out as /sys/fs/cgroup is (server_test.go's CgroupRoot).
+func cgroupSlice(t *testing.T, root, slice string, populated bool) {
+ t.Helper()
+ dir := filepath.Join(root, "carlos.slice", "carlos-instances.slice", slice)
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ ev := "populated 0\nfrozen 0\n"
+ if populated {
+ ev = "populated 1\nfrozen 0\n"
+ }
+ if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte(ev), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// longEgressHost is longer than provision.MaxInstanceID, so its slice is
+// named by a hashed instance id ("_shop-a-long-...-<hash>").
+const longEgressHost = "shop-a-long-name-for-an-instance.example.com"
+
+func TestEgressSweepAssertsEveryPopulatedSlice(t *testing.T) {
+ root := t.TempDir()
+ cgroupSlice(t, root, SliceName("shop-a.example.com"), true)
+ cgroupSlice(t, root, SliceName("shop-b.example.com"), true)
+ cgroupSlice(t, root, SliceName("shop-c.example.com"), false)
+ // The parent's own files and a non-slice child are not host slices.
+ if err := os.WriteFile(filepath.Join(root, "carlos.slice", "carlos-instances.slice", "cgroup.events"), []byte("populated 1\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ cgroupSlice(t, root, "carlos-instance-probe.service", true)
+
+ ctl := newEgressCtl()
+ var asked []string
+ var mu sync.Mutex
+ e := newEgressAsserter(ctl, func(_ context.Context, host string) (EgressPolicy, error) {
+ mu.Lock()
+ asked = append(asked, host)
+ mu.Unlock()
+ return EgressPolicy{Allow: []string{"10.0.5.20"}}, nil
+ })
+ e.CgroupRoot = root
+ e.Sweep(context.Background())
+
+ wantCalls(t, ctl.takeCalls(),
+ showCall("shop-a.example.com"), setCall("shop-a.example.com", "10.0.5.20/32"), showCall("shop-a.example.com"),
+ showCall("shop-b.example.com"), setCall("shop-b.example.com", "10.0.5.20/32"), showCall("shop-b.example.com"),
+ )
+ if want := []string{"shop-a.example.com", "shop-b.example.com"}; !reflect.DeepEqual(asked, want) {
+ t.Fatalf("source asked for %q, want %q", asked, want)
+ }
+ reps := e.Reports()
+ if len(reps) != 2 || reps["shop-c.example.com"].Allowed != nil {
+ t.Fatalf("reports = %+v, want shop-a and shop-b only", reps)
+ }
+
+ // A second sweep with nothing changed reads each back and writes nothing.
+ e.Sweep(context.Background())
+ wantCalls(t, ctl.takeCalls(), showCall("shop-a.example.com"), showCall("shop-b.example.com"))
+}
+
+func TestEgressSweepResetsAnUnattributableSlice(t *testing.T) {
+ slice := SliceName(longEgressHost)
+ if !strings.HasPrefix(slice, "carlos-instances-_shop") {
+ t.Fatalf("fixture: %s is not a hashed slice name", slice)
+ }
+ t.Run("holds an allow", func(t *testing.T) {
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ var logs []string
+ e := newEgressAsserter(ctl, func(context.Context, string) (EgressPolicy, error) {
+ t.Error("the source was asked for a slice with no host")
+ return EgressPolicy{Allow: []string{"10.0.5.20"}}, nil
+ })
+ e.CgroupRoot = root
+ e.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
+ e.Sweep(context.Background())
+ show := "show -p IPAddressAllow --value " + slice
+ wantCalls(t, ctl.takeCalls(), show, "set-property --runtime "+slice+" IPAddressAllow=", show)
+ want := "egress: " + slice + ": no host for this slice; its allow is closed"
+ if !slices.Contains(logs, want) {
+ t.Fatalf("logs = %q, want %q", logs, want)
+ }
+ if len(e.Reports()) != 0 {
+ t.Fatalf("reports = %+v, want none", e.Reports())
+ }
+ })
+ t.Run("holds nothing", func(t *testing.T) {
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ ctl := newEgressCtl()
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
+ e.CgroupRoot = root
+ e.Sweep(context.Background())
+ wantCalls(t, ctl.takeCalls(), "show -p IPAddressAllow --value "+slice)
+ })
+ t.Run("named once an assert has seen the host", func(t *testing.T) {
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ ctl := newEgressCtl()
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
+ e.CgroupRoot = root
+ mustAssert(t, e, longEgressHost)
+ ctl.takeCalls()
+ ctl.setHeld(slice, "")
+ e.Sweep(context.Background())
+ wantCalls(t, ctl.takeCalls(), showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost))
+ })
+}
+
+func TestEgressLoopSeedsNamesFromHosts(t *testing.T) {
+ root := t.TempDir()
+ slice := SliceName(longEgressHost)
+ cgroupSlice(t, root, slice, true)
+ ctl := newEgressCtl()
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
+ e.CgroupRoot = root
+ e.Hosts = func() ([]string, error) { return []string{"other.example.com", longEgressHost}, nil }
+ written := make(chan struct{})
+ var once sync.Once
+ e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
+ out, err := ctl.run(ctx, args...)
+ if args[0] == "show" && strings.Contains(out, "10.0.5.20") {
+ once.Do(func() { close(written) })
+ }
+ return out, err
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ done := make(chan struct{})
+ go func() { defer close(done); e.Loop(ctx, time.Hour) }()
+ select {
+ case <-written: // the first sweep, at once
+ case <-time.After(10 * time.Second):
+ cancel()
+ <-done
+ t.Fatalf("Loop's first sweep did not write the allow: %q", ctl.takeCalls())
+ }
+ cancel()
+ <-done
+ wantCalls(t, ctl.takeCalls(), showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost))
+}
+
+func TestEgressSweepLogsAHostFailingThreeSweeps(t *testing.T) {
+ root := t.TempDir()
+ cgroupSlice(t, root, SliceName(egressHost), true)
+ ctl := newEgressCtl()
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
+ e.CgroupRoot = root
+ ctl.ignore = true // every write leaves the slice as it was: Assert fails
+ var logs []string
+ e.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
+ streak := func() int {
+ n := 0
+ for _, l := range logs {
+ if strings.Contains(l, "sweeps in a row") {
+ n++
+ }
+ }
+ return n
+ }
+ for i, want := range []int{0, 0, 1, 2} {
+ e.Sweep(context.Background())
+ if got := streak(); got != want {
+ t.Fatalf("after sweep %d: %d streak lines, want %d: %q", i+1, got, want, logs)
+ }
+ }
+ if !slices.ContainsFunc(logs, func(l string) bool {
+ return strings.Contains(l, "egress: "+egressHost+": failing for 4 sweeps in a row")
+ }) {
+ t.Fatalf("logs = %q, want the fourth sweep's count", logs)
+ }
+ ctl.ignore = false
+ e.Sweep(context.Background()) // holds now
+ ctl.ignore = true
+ ctl.setHeld(SliceName(egressHost), "") // and fails again from here
+ logs = nil
+ e.Sweep(context.Background())
+ e.Sweep(context.Background())
+ if got := streak(); got != 0 {
+ t.Fatalf("a success did not restart the count: %q", logs)
+ }
+ if !slices.ContainsFunc(logs, func(l string) bool { return strings.Contains(l, "egress: "+SliceName(egressHost)+": after the write") }) {
+ t.Fatalf("the two sweeps after the success did not fail: %q", logs)
+ }
+}
+
+func TestPopulatedHostSlicesMissingTreeIsAnError(t *testing.T) {
+ if _, err := populatedHostSlices(filepath.Join(t.TempDir(), "absent")); err == nil {
+ t.Fatal("populatedHostSlices(absent) = nil error")
+ }
+}
+
+func TestEgressSeededNamesOutliveAnAssert(t *testing.T) {
+ root := t.TempDir()
+ slice := SliceName(longEgressHost)
+ cgroupSlice(t, root, slice, true)
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
+ e.CgroupRoot = root
+ e.Hosts = func() ([]string, error) { return []string{longEgressHost}, nil }
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ e.Loop(ctx, time.Hour) // names from Hosts; the sweep stops at the done context
+ mustAssert(t, e, egressHost)
+ ctl.takeCalls()
+ e.Sweep(context.Background())
+ wantCalls(t, ctl.takeCalls(), showCall(longEgressHost))
+ if _, ok := e.Reports()[longEgressHost]; !ok {
+ t.Fatalf("reports = %+v, want %s asserted by name", e.Reports(), longEgressHost)
+ }
+}
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go
index 15e7f77fb..68f6dccbc 100644
--- a/internal/instancectl/egress_test.go
+++ b/internal/instancectl/egress_test.go
@@ -8,8 +8,6 @@ import (
"io"
"net/http"
"net/netip"
- "os"
- "path/filepath"
"reflect"
"slices"
"strconv"
@@ -513,156 +511,10 @@ func TestEgressRefusesABadHost(t *testing.T) {
wantCalls(t, ctl.takeCalls())
}
-// cgroupSlice writes a per-host slice's cgroup.events into a fixture tree
-// laid out as /sys/fs/cgroup is (server_test.go's CgroupRoot).
-func cgroupSlice(t *testing.T, root, slice string, populated bool) {
- t.Helper()
- dir := filepath.Join(root, "carlos.slice", "carlos-instances.slice", slice)
- if err := os.MkdirAll(dir, 0o755); err != nil {
- t.Fatal(err)
- }
- ev := "populated 0\nfrozen 0\n"
- if populated {
- ev = "populated 1\nfrozen 0\n"
- }
- if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte(ev), 0o644); err != nil {
- t.Fatal(err)
- }
-}
-
-// longEgressHost is longer than provision.MaxInstanceID, so its slice is
-// named by a hashed instance id ("_shop-a-long-...-<hash>").
-const longEgressHost = "shop-a-long-name-for-an-instance.example.com"
-
-func TestEgressSweepAssertsEveryPopulatedSlice(t *testing.T) {
- root := t.TempDir()
- cgroupSlice(t, root, SliceName("shop-a.example.com"), true)
- cgroupSlice(t, root, SliceName("shop-b.example.com"), true)
- cgroupSlice(t, root, SliceName("shop-c.example.com"), false)
- // The parent's own files and a non-slice child are not host slices.
- if err := os.WriteFile(filepath.Join(root, "carlos.slice", "carlos-instances.slice", "cgroup.events"), []byte("populated 1\n"), 0o644); err != nil {
- t.Fatal(err)
- }
- cgroupSlice(t, root, "carlos-instance-probe.service", true)
-
- ctl := newEgressCtl()
- var asked []string
- var mu sync.Mutex
- e := newEgressAsserter(ctl, func(_ context.Context, host string) (EgressPolicy, error) {
- mu.Lock()
- asked = append(asked, host)
- mu.Unlock()
- return EgressPolicy{Allow: []string{"10.0.5.20"}}, nil
- })
- e.CgroupRoot = root
- e.Sweep(context.Background())
-
- wantCalls(t, ctl.takeCalls(),
- showCall("shop-a.example.com"), setCall("shop-a.example.com", "10.0.5.20/32"), showCall("shop-a.example.com"),
- showCall("shop-b.example.com"), setCall("shop-b.example.com", "10.0.5.20/32"), showCall("shop-b.example.com"),
- )
- if want := []string{"shop-a.example.com", "shop-b.example.com"}; !reflect.DeepEqual(asked, want) {
- t.Fatalf("source asked for %q, want %q", asked, want)
- }
- reps := e.Reports()
- if len(reps) != 2 || reps["shop-c.example.com"].Allowed != nil {
- t.Fatalf("reports = %+v, want shop-a and shop-b only", reps)
- }
-
- // A second sweep with nothing changed reads each back and writes nothing.
- e.Sweep(context.Background())
- wantCalls(t, ctl.takeCalls(), showCall("shop-a.example.com"), showCall("shop-b.example.com"))
-}
-
-func TestEgressSweepResetsAnUnattributableSlice(t *testing.T) {
- slice := SliceName(longEgressHost)
- if !strings.HasPrefix(slice, "carlos-instances-_shop") {
- t.Fatalf("fixture: %s is not a hashed slice name", slice)
- }
- t.Run("holds an allow", func(t *testing.T) {
- root := t.TempDir()
- cgroupSlice(t, root, slice, true)
- ctl := newEgressCtl()
- ctl.setHeld(slice, "10.0.5.20/32")
- var logs []string
- e := newEgressAsserter(ctl, func(context.Context, string) (EgressPolicy, error) {
- t.Error("the source was asked for a slice with no host")
- return EgressPolicy{Allow: []string{"10.0.5.20"}}, nil
- })
- e.CgroupRoot = root
- e.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
- e.Sweep(context.Background())
- show := "show -p IPAddressAllow --value " + slice
- wantCalls(t, ctl.takeCalls(), show, "set-property --runtime "+slice+" IPAddressAllow=", show)
- want := "egress: " + slice + ": no host for this slice; its allow is closed"
- if !slices.Contains(logs, want) {
- t.Fatalf("logs = %q, want %q", logs, want)
- }
- if len(e.Reports()) != 0 {
- t.Fatalf("reports = %+v, want none", e.Reports())
- }
- })
- t.Run("holds nothing", func(t *testing.T) {
- root := t.TempDir()
- cgroupSlice(t, root, slice, true)
- ctl := newEgressCtl()
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
- e.CgroupRoot = root
- e.Sweep(context.Background())
- wantCalls(t, ctl.takeCalls(), "show -p IPAddressAllow --value "+slice)
- })
- t.Run("named once an assert has seen the host", func(t *testing.T) {
- root := t.TempDir()
- cgroupSlice(t, root, slice, true)
- ctl := newEgressCtl()
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
- e.CgroupRoot = root
- mustAssert(t, e, longEgressHost)
- ctl.takeCalls()
- ctl.setHeld(slice, "")
- e.Sweep(context.Background())
- wantCalls(t, ctl.takeCalls(), showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost))
- })
-}
-
-func TestEgressLoopSeedsNamesFromHosts(t *testing.T) {
- root := t.TempDir()
- slice := SliceName(longEgressHost)
- cgroupSlice(t, root, slice, true)
- ctl := newEgressCtl()
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
- e.CgroupRoot = root
- e.Hosts = func() ([]string, error) { return []string{"other.example.com", longEgressHost}, nil }
- written := make(chan struct{})
- var once sync.Once
- e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
- out, err := ctl.run(ctx, args...)
- if args[0] == "show" && strings.Contains(out, "10.0.5.20") {
- once.Do(func() { close(written) })
- }
- return out, err
- }
- ctx, cancel := context.WithCancel(context.Background())
- done := make(chan struct{})
- go func() { defer close(done); e.Loop(ctx, time.Hour) }()
- select {
- case <-written: // the first sweep, at once
- case <-time.After(10 * time.Second):
- cancel()
- <-done
- t.Fatalf("Loop's first sweep did not write the allow: %q", ctl.takeCalls())
- }
- cancel()
- <-done
- wantCalls(t, ctl.takeCalls(), showCall(longEgressHost), setCall(longEgressHost, "10.0.5.20/32"), showCall(longEgressHost))
-}
-
func TestEgressLoopLogsAHostsError(t *testing.T) {
- root := t.TempDir()
- cgroupSlice(t, root, SliceName("shop-a.example.com"), true)
ctl := newEgressCtl()
e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil))
- e.CgroupRoot = root
+ e.CgroupRoot = t.TempDir()
e.Hosts = func() ([]string, error) { return nil, errors.New("registry: locked") }
var mu sync.Mutex
var logs []string
@@ -677,56 +529,6 @@ func TestEgressLoopLogsAHostsError(t *testing.T) {
}
}
-func TestEgressSweepLogsAHostFailingThreeSweeps(t *testing.T) {
- root := t.TempDir()
- cgroupSlice(t, root, SliceName(egressHost), true)
- ctl := newEgressCtl()
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
- e.CgroupRoot = root
- ctl.ignore = true // every write leaves the slice as it was: Assert fails
- var logs []string
- e.Logf = func(f string, a ...any) { logs = append(logs, fmt.Sprintf(f, a...)) }
- streak := func() int {
- n := 0
- for _, l := range logs {
- if strings.Contains(l, "sweeps in a row") {
- n++
- }
- }
- return n
- }
- for i, want := range []int{0, 0, 1, 2} {
- e.Sweep(context.Background())
- if got := streak(); got != want {
- t.Fatalf("after sweep %d: %d streak lines, want %d: %q", i+1, got, want, logs)
- }
- }
- if !slices.ContainsFunc(logs, func(l string) bool {
- return strings.Contains(l, "egress: "+egressHost+": failing for 4 sweeps in a row")
- }) {
- t.Fatalf("logs = %q, want the fourth sweep's count", logs)
- }
- ctl.ignore = false
- e.Sweep(context.Background()) // holds now
- ctl.ignore = true
- ctl.setHeld(SliceName(egressHost), "") // and fails again from here
- logs = nil
- e.Sweep(context.Background())
- e.Sweep(context.Background())
- if got := streak(); got != 0 {
- t.Fatalf("a success did not restart the count: %q", logs)
- }
- if !slices.ContainsFunc(logs, func(l string) bool { return strings.Contains(l, "egress: "+SliceName(egressHost)+": after the write") }) {
- t.Fatalf("the two sweeps after the success did not fail: %q", logs)
- }
-}
-
-func TestPopulatedHostSlicesMissingTreeIsAnError(t *testing.T) {
- if _, err := populatedHostSlices(filepath.Join(t.TempDir(), "absent")); err == nil {
- t.Fatal("populatedHostSlices(absent) = nil error")
- }
-}
-
// egressServer serves a Server whose Egress writes through ctl.
func egressServer(t *testing.T, ectl *egressCtl, src EgressSource, configure ...func(*Server)) (*fakeCtl, *http.Client) {
t.Helper()
@@ -823,24 +625,3 @@ func TestEgressRoutesServedWithClaimsOn(t *testing.T) {
t.Fatalf("the legacy start reached systemctl: %s", ctl.joined())
}
}
-
-func TestEgressSeededNamesOutliveAnAssert(t *testing.T) {
- root := t.TempDir()
- slice := SliceName(longEgressHost)
- cgroupSlice(t, root, slice, true)
- ctl := newEgressCtl()
- ctl.setHeld(slice, "10.0.5.20/32")
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil))
- e.CgroupRoot = root
- e.Hosts = func() ([]string, error) { return []string{longEgressHost}, nil }
- ctx, cancel := context.WithCancel(context.Background())
- cancel()
- e.Loop(ctx, time.Hour) // names from Hosts; the sweep stops at the done context
- mustAssert(t, e, egressHost)
- ctl.takeCalls()
- e.Sweep(context.Background())
- wantCalls(t, ctl.takeCalls(), showCall(longEgressHost))
- if _, ok := e.Reports()[longEgressHost]; !ok {
- t.Fatalf("reports = %+v, want %s asserted by name", e.Reports(), longEgressHost)
- }
-}