| index 3ec774378..399f765ce 100644 |
| --- a/internal/instancectl/server_gen_test.go |
| +++ b/internal/instancectl/server_gen_test.go |
| @@ -28,11 +28,11 @@ func genServer(t *testing.T, ctl *fakeCtl) (*Server, string, string) { |
| |
| func genCtl() *fakeCtl { |
| return &fakeCtl{answers: map[string]string{ |
| - "show -p Slice --value": SliceName(genHost) + "\n", |
| - "show -p InvocationID --value": "abc123\n", |
| - "list-units": "", |
| - "list-jobs": "", |
| - "show -p ActiveState --value": "inactive\n", |
| + "show -p Slice,NeedDaemonReload": "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=no\n", |
| + "show -p InvocationID --value": "abc123\n", |
| + "list-units": "", |
| + "list-jobs": "", |
| + "show -p ActiveState --value": "inactive\n", |
| }} |
| } |
| |
| @@ -54,7 +54,9 @@ MemorySwapMax=536870912 |
| } |
| } |
| |
| -func TestGenStartWritesTheDropInReloadsChecksTheSliceThenStarts(t *testing.T) { |
| +// The wake path's case: a generation's unit is new, so systemd reads its |
| +// drop-in on the first reference and the box-wide reload is skipped. |
| +func TestGenStartWritesTheDropInChecksItThenStartsWithoutAReload(t *testing.T) { |
| ctl := genCtl() |
| s, sock, dropins := genServer(t, ctl) |
| inv, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax) |
| @@ -69,8 +71,7 @@ func TestGenStartWritesTheDropInReloadsChecksTheSliceThenStarts(t *testing.T) { |
| t.Fatalf("exe app link = %q, %v", target, err) |
| } |
| want := strings.Join([]string{ |
| - "daemon-reload", |
| - "show -p Slice --value " + genUnit, |
| + "show -p Slice,NeedDaemonReload " + genUnit, |
| `set-property --runtime carlos-instances-bill\x2d2.example.com.slice MemoryMax=536870912 MemoryHigh=483183819 MemorySwapMax=536870912 CPUWeight=100 CPUQuota=100% IOWeight=100`, |
| "start " + genUnit, |
| "show -p InvocationID --value " + genUnit, |
| @@ -80,6 +81,62 @@ func TestGenStartWritesTheDropInReloadsChecksTheSliceThenStarts(t *testing.T) { |
| } |
| } |
| |
| +// staleUntilReload makes systemd hold a stale copy of the unit (answer) |
| +// until a daemon-reload, after which the drop-in reads as applied. |
| +func staleUntilReload(ctl *fakeCtl, answer string) { |
| + ctl.answers["show -p Slice,NeedDaemonReload"] = answer |
| + ctl.before = map[string]func(){"daemon-reload": func() { |
| + ctl.mu.Lock() |
| + defer ctl.mu.Unlock() |
| + ctl.answers["show -p Slice,NeedDaemonReload"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=no\n" |
| + }} |
| +} |
| + |
| +// systemd still holding the unit loaded from older files says so with |
| +// NeedDaemonReload, or shows a slice the drop-in does not name. Either way |
| +// the start reloads, checks again, and only then starts. |
| +func TestGenStartReloadsWhenSystemdHoldsAStaleCopy(t *testing.T) { |
| + for name, answer := range map[string]string{ |
| + "files changed since load": "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n", |
| + "loaded without drop-in": `Slice=system-carlos\x2dinstance\x2dgen.slice` + "\nNeedDaemonReload=no\n", |
| + "show failed to answer": "", |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + ctl := genCtl() |
| + staleUntilReload(ctl, answer) |
| + _, sock, _ := genServer(t, ctl) |
| + if _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax); err != nil { |
| + t.Fatal(err) |
| + } |
| + want := strings.Join([]string{ |
| + "show -p Slice,NeedDaemonReload " + genUnit, |
| + "daemon-reload", |
| + "show -p Slice,NeedDaemonReload " + genUnit, |
| + }, "\n") |
| + if got := ctl.joined(); !strings.HasPrefix(got, want+"\n") || !strings.Contains(got, "\nstart "+genUnit) { |
| + t.Errorf("calls =\n%s\nwant them to begin\n%s\nand then start", got, want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// A reload that leaves systemd still saying its files changed proves |
| +// nothing about what it loaded: refuse rather than start on a guess. |
| +func TestGenStartRefusesWhenTheReloadDidNotSettle(t *testing.T) { |
| + ctl := genCtl() |
| + ctl.answers["show -p Slice,NeedDaemonReload"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" |
| + s, sock, dropins := genServer(t, ctl) |
| + if _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax); err == nil { |
| + t.Fatal("started a generation systemd still held a stale copy of") |
| + } |
| + if strings.Contains(ctl.joined(), "start "+genUnit) { |
| + t.Errorf("the unit was started:\n%s", ctl.joined()) |
| + } |
| + if dropIn, exe := genFilesPresent(t, s, dropins); dropIn || exe { |
| + t.Errorf("files left behind: drop-in %v exe %v", dropIn, exe) |
| + } |
| +} |
| + |
| // A successor's start during an overlap: the slice holds the incumbent, |
| // and its raised limit is the swap's. The start leaves it alone. |
| func TestGenStartLeavesAPopulatedSliceAlone(t *testing.T) { |
| @@ -123,7 +180,7 @@ func TestGenStartResetsAnEmptySlice(t *testing.T) { |
| // and an empty /data. Refuse, and leave nothing behind. |
| func TestGenStartRefusesWhenTheDropInDidNotApply(t *testing.T) { |
| ctl := genCtl() |
| - ctl.answers["show -p Slice --value"] = `system-carlos\x2dinstance\x2dgen.slice` + "\n" |
| + ctl.answers["show -p Slice,NeedDaemonReload"] = `Slice=system-carlos\x2dinstance\x2dgen.slice` + "\nNeedDaemonReload=no\n" |
| s, sock, dropins := genServer(t, ctl) |
| if _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax); err == nil { |
| t.Fatal("started a generation whose drop-in did not apply") |
| @@ -388,6 +445,7 @@ func TestGenStartDropInWriteFailureRemovesTheFiles(t *testing.T) { |
| |
| func TestGenStartDaemonReloadFailureRemovesTheFiles(t *testing.T) { |
| ctl := genCtl() |
| + ctl.answers["show -p Slice,NeedDaemonReload"] = "Slice=" + SliceName(genHost) + "\nNeedDaemonReload=yes\n" |
| ctl.fail = map[string]error{"daemon-reload": errors.New("bus gone")} |
| s, sock, dropins := genServer(t, ctl) |
| _, err := NewClient(sock).StartGen(context.Background(), genHost, 7, cgroup.TierDefault.MemoryMax) |