diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-10-17 05:32:14 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-10-17 05:47:12 +0900 |
| commit | 3f391329357789f55559404a3d28c60679111a74 (patch) | |
| tree | ae47e5b53764853d21c8a49c1c8212932593e0e8 /internal/app/spcontainer_test.go | |
| parent | c922c3f80e85e20a288fd390e8f4e3e0ead52849 (diff) | |
internal/app/dispatcher: reduce check code duplication
This also improves readability of test cases.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/spcontainer_test.go')
| -rw-r--r-- | internal/app/spcontainer_test.go | 57 |
1 files changed, 23 insertions, 34 deletions
diff --git a/internal/app/spcontainer_test.go b/internal/app/spcontainer_test.go index 458588af..6b2354a2 100644 --- a/internal/app/spcontainer_test.go +++ b/internal/app/spcontainer_test.go @@ -2,7 +2,6 @@ package app import ( "errors" - "maps" "os" "reflect" "syscall" @@ -72,16 +71,9 @@ func TestSpParamsOp(t *testing.T) { Proc(fhs.AbsProc).Tmpfs(hst.AbsPrivateTmp, 1<<12, 0755). DevWritable(fhs.AbsDev, true). Tmpfs(fhs.AbsDev.Append("shm"), 0, 01777), + }, paramsWantEnv(config, map[string]string{ + "TERM": "xterm", }, func(t *testing.T, state *outcomeStateParams) { - wantEnv := map[string]string{ - "TERM": "xterm", - } - maps.Copy(wantEnv, config.Container.Env) - if !maps.Equal(state.env, wantEnv) { - t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv) - } - - const wantAutoEtcPrefix = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" if state.as.AutoEtcPrefix != wantAutoEtcPrefix { t.Errorf("toContainer: as.AutoEtcPrefix = %q, want %q", state.as.AutoEtcPrefix, wantAutoEtcPrefix) } @@ -90,7 +82,7 @@ func TestSpParamsOp(t *testing.T) { if !reflect.DeepEqual(state.filesystem, wantFilesystems) { t.Errorf("toContainer: filesystem = %#v, want %#v", state.filesystem, wantFilesystems) } - }, nil}, + }), nil}, {"success", func(isShim, _ bool) outcomeOp { if !isShim { @@ -117,15 +109,9 @@ func TestSpParamsOp(t *testing.T) { Proc(fhs.AbsProc).Tmpfs(hst.AbsPrivateTmp, 1<<12, 0755). Bind(fhs.AbsDev, fhs.AbsDev, bits.BindWritable|bits.BindDevice). Tmpfs(fhs.AbsDev.Append("shm"), 0, 01777), + }, paramsWantEnv(config, map[string]string{ + "TERM": "xterm", }, func(t *testing.T, state *outcomeStateParams) { - wantEnv := map[string]string{ - "TERM": "xterm", - } - maps.Copy(wantEnv, config.Container.Env) - if !maps.Equal(state.env, wantEnv) { - t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv) - } - if state.as.AutoEtcPrefix != wantAutoEtcPrefix { t.Errorf("toContainer: as.AutoEtcPrefix = %q, want %q", state.as.AutoEtcPrefix, wantAutoEtcPrefix) } @@ -134,7 +120,7 @@ func TestSpParamsOp(t *testing.T) { if !reflect.DeepEqual(state.filesystem, wantFilesystems) { t.Errorf("toContainer: filesystem = %#v, want %#v", state.filesystem, wantFilesystems) } - }, nil}, + }), nil}, }) } @@ -159,6 +145,16 @@ func TestSpFilesystemOp(t *testing.T) { } configSmall := newConfigSmall() + needsApplyState := func(next pStateContainerFunc) pStateContainerFunc { + return func(state *outcomeStateParams) { + state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}} + + if next != nil { + next(state) + } + } + } + checkOpBehaviour(t, []opBehaviourTestCase{ {"readdir", func(bool, bool) outcomeOp { return new(spFilesystemOp) @@ -310,12 +306,9 @@ func TestSpFilesystemOp(t *testing.T) { call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.nixos/.ro-store"}, nePrefix+"/var/lib/hakurei/base/org.nixos/.ro-store", nil), call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.nixos/org.chromium.Chromium"}, nePrefix+"/var/lib/hakurei/base/org.nixos/org.chromium.Chromium", nil), call("verbosef", stub.ExpectArgs{"hiding path %q from %q", []any{"/proc/nonexistent/eval/etc/dbus", "/etc/"}}, nil, nil), - }, newI(), nil, nil, func(state *outcomeStateParams) { - state.filesystem = configSmall.Container.Filesystem - state.params.Ops = new(container.Ops) - state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}} - state.filesystem = append(state.filesystem, hst.FilesystemConfigJSON{}) - }, []stub.Call{ + }, newI(), nil, nil, insertsOps(needsApplyState(func(state *outcomeStateParams) { + state.filesystem = append(configSmall.Container.Filesystem, hst.FilesystemConfigJSON{}) + })), []stub.Call{ // this op configures the container state and does not make calls during toContainer }, nil, nil, &hst.AppError{ Step: "finalise", @@ -341,11 +334,9 @@ func TestSpFilesystemOp(t *testing.T) { call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.nixos/.ro-store"}, nePrefix+"/var/lib/hakurei/base/org.nixos/.ro-store", nil), call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.nixos/org.chromium.Chromium"}, nePrefix+"/var/lib/hakurei/base/org.nixos/org.chromium.Chromium", nil), call("verbosef", stub.ExpectArgs{"hiding path %q from %q", []any{"/proc/nonexistent/eval/etc/dbus", "/etc/"}}, nil, nil), - }, newI(), nil, nil, func(state *outcomeStateParams) { + }, newI(), nil, nil, insertsOps(needsApplyState(func(state *outcomeStateParams) { state.filesystem = configSmall.Container.Filesystem - state.params.Ops = new(container.Ops) - state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}} - }, []stub.Call{ + })), []stub.Call{ // this op configures the container state and does not make calls during toContainer }, &container.Params{ Ops: new(container.Ops). @@ -386,11 +377,9 @@ func TestSpFilesystemOp(t *testing.T) { call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.debian/sys"}, nePrefix+"/var/lib/hakurei/base/org.debian/sys", nil), call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.debian/usr"}, nePrefix+"/var/lib/hakurei/base/org.debian/usr", nil), call("evalSymlinks", stub.ExpectArgs{"/var/lib/hakurei/base/org.debian/var"}, nePrefix+"/var/lib/hakurei/base/org.debian/var", nil), - }, newI(), nil, nil, func(state *outcomeStateParams) { + }, newI(), nil, nil, insertsOps(needsApplyState(func(state *outcomeStateParams) { state.filesystem = config.Container.Filesystem[1:] - state.params.Ops = new(container.Ops) - state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}} - }, []stub.Call{ + })), []stub.Call{ // this op configures the container state and does not make calls during toContainer }, &container.Params{ Ops: new(container.Ops). |
