mounts: test the clearing loop, the look's own clearing and a disk's presence; clear only a taken-over unit's earlier failure; clear and re-arm an automount that gave up; refuse a transient unit of the same name (review of #162)

This commit is contained in:
jochen
2026-10-09 14:43:16 +02:00
parent 0f330fc57d
commit 02eab5aa8e
4 changed files with 219 additions and 18 deletions
+22
View File
@@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
@@ -16,6 +17,8 @@ const testPassword = "s3cr3t-Pa55"
// fakeSystem is a machine under a temporary root with a recorded service manager.
type fakeSystem struct {
// mu guards calls, units and reachable, for a test that runs RunApply beside itself.
mu sync.Mutex
t *testing.T
root string
calls []string
@@ -55,6 +58,8 @@ func (f *fakeSystem) read(p string) string {
}
func (f *fakeSystem) run(_ context.Context, name string, args ...string) (string, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.calls = append(f.calls, name+" "+strings.Join(args, " "))
if name != "systemctl" || len(args) == 0 {
return "", nil
@@ -77,6 +82,12 @@ func (f *fakeSystem) run(_ context.Context, name string, args ...string) (string
case "enable":
u := args[len(args)-1]
f.units[u] = map[string]string{"ActiveState": "active", "UnitFileState": "enabled"}
case "start":
u := args[len(args)-1]
if f.units[u] == nil {
f.units[u] = map[string]string{}
}
f.units[u]["ActiveState"], f.units[u]["Result"] = "active", "success"
case "reset-failed":
u := args[len(args)-1]
if f.units[u]["ActiveState"] == "failed" {
@@ -93,6 +104,8 @@ func (f *fakeSystem) machine() *Machine {
func (f *fakeSystem) applier(now time.Time) *Applier {
return &Applier{M: f.machine(), Act: true, Judge: true,
Dial: func(addr string, _ time.Duration) error {
f.mu.Lock()
defer f.mu.Unlock()
if f.reachable[addr] {
return nil
}
@@ -104,6 +117,8 @@ func (f *fakeSystem) applier(now time.Time) *Applier {
}
func (f *fakeSystem) called(prefix string) bool {
f.mu.Lock()
defer f.mu.Unlock()
for _, c := range f.calls {
if strings.HasPrefix(c, prefix) {
return true
@@ -565,3 +580,10 @@ func TestApplyActsAndWatchJudgesAndNeitherDoesTheOthersWork(t *testing.T) {
t.Error("the password leaked")
}
}
// setUnit sets one unit's state as the service manager answers it, under the lock.
func (f *fakeSystem) setUnit(unit string, state map[string]string) {
f.mu.Lock()
defer f.mu.Unlock()
f.units[unit] = state
}