internal/app/dispatcher: reduce check code duplication
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m6s
Test / Hakurei (push) Successful in 3m3s
Test / Sandbox (race detector) (push) Successful in 3m56s
Test / Hpkg (push) Successful in 3m58s
Test / Hakurei (race detector) (push) Successful in 4m42s
Test / Flake checks (push) Successful in 1m28s
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m6s
Test / Hakurei (push) Successful in 3m3s
Test / Sandbox (race detector) (push) Successful in 3m56s
Test / Hpkg (push) Successful in 3m58s
Test / Hakurei (race detector) (push) Successful in 4m42s
Test / Flake checks (push) Successful in 1m28s
This also improves readability of test cases. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
c922c3f80e
commit
3f39132935
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -26,30 +27,97 @@ func call(name string, args stub.ExpectArgs, ret any, err error) stub.Call {
|
|||||||
return stub.Call{Name: name, Args: args, Ret: ret, Err: err}
|
return stub.Call{Name: name, Args: args, Ret: ret, Err: err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkExpectUid is the uid value used by checkOpBehaviour to initialise [system.I].
|
const (
|
||||||
const checkExpectUid = 0xcafebabe
|
// checkExpectUid is the uid value used by checkOpBehaviour to initialise [system.I].
|
||||||
|
checkExpectUid = 0xcafebabe
|
||||||
// wantAutoEtcPrefix is the autoetc prefix corresponding to checkExpectInstanceId.
|
// wantAutoEtcPrefix is the autoetc prefix corresponding to checkExpectInstanceId.
|
||||||
const wantAutoEtcPrefix = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
wantAutoEtcPrefix = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||||
|
// wantInstancePrefix is the SharePath corresponding to checkExpectInstanceId.
|
||||||
|
wantInstancePrefix = container.Nonexistent + "/tmp/hakurei.0/" + wantAutoEtcPrefix
|
||||||
|
)
|
||||||
|
|
||||||
// checkExpectInstanceId is the [state.ID] value used by checkOpBehaviour to initialise outcomeState.
|
// checkExpectInstanceId is the [state.ID] value used by checkOpBehaviour to initialise outcomeState.
|
||||||
var checkExpectInstanceId = *(*state.ID)(bytes.Repeat([]byte{0xaa}, len(state.ID{})))
|
var checkExpectInstanceId = *(*state.ID)(bytes.Repeat([]byte{0xaa}, len(state.ID{})))
|
||||||
|
|
||||||
|
type (
|
||||||
|
// pStateSysFunc is called before each test case is run to prepare outcomeStateSys.
|
||||||
|
pStateSysFunc = func(state *outcomeStateSys)
|
||||||
|
// pStateContainerFunc is called before each test case is run to prepare outcomeStateParams.
|
||||||
|
pStateContainerFunc = func(state *outcomeStateParams)
|
||||||
|
|
||||||
|
// extraCheckSysFunc is called to check outcomeStateSys and must not have side effects.
|
||||||
|
extraCheckSysFunc = func(t *testing.T, state *outcomeStateSys)
|
||||||
|
// extraCheckParamsFunc is called to check outcomeStateParams and must not have side effects.
|
||||||
|
extraCheckParamsFunc = func(t *testing.T, state *outcomeStateParams)
|
||||||
|
)
|
||||||
|
|
||||||
|
// insertsOps prepares outcomeStateParams to allow [container.Op] to be inserted.
|
||||||
|
func insertsOps(next pStateContainerFunc) pStateContainerFunc {
|
||||||
|
return func(state *outcomeStateParams) {
|
||||||
|
state.params.Ops = new(container.Ops)
|
||||||
|
|
||||||
|
if next != nil {
|
||||||
|
next(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// afterSpRuntimeOp prepares outcomeStateParams for an outcomeOp meant to run after spRuntimeOp.
|
||||||
|
func afterSpRuntimeOp(next pStateContainerFunc) pStateContainerFunc {
|
||||||
|
return func(state *outcomeStateParams) {
|
||||||
|
// emulates spRuntimeOp
|
||||||
|
state.runtimeDir = m("/run/user/1000")
|
||||||
|
|
||||||
|
if next != nil {
|
||||||
|
next(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sysUsesInstance checks for use of the outcomeStateSys.instance method.
|
||||||
|
func sysUsesInstance(next extraCheckSysFunc) extraCheckSysFunc {
|
||||||
|
return func(t *testing.T, state *outcomeStateSys) {
|
||||||
|
if want := m(wantInstancePrefix); !reflect.DeepEqual(state.sharePath, want) {
|
||||||
|
t.Errorf("outcomeStateSys: sharePath = %v, want %v", state.sharePath, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
if next != nil {
|
||||||
|
next(t, state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// paramsWantEnv checks outcomeStateParams.env for inserted entries on top of [hst.Config].
|
||||||
|
func paramsWantEnv(config *hst.Config, wantEnv map[string]string, next extraCheckParamsFunc) extraCheckParamsFunc {
|
||||||
|
want := make(map[string]string, len(wantEnv)+len(config.Container.Env))
|
||||||
|
maps.Copy(want, wantEnv)
|
||||||
|
maps.Copy(want, config.Container.Env)
|
||||||
|
return func(t *testing.T, state *outcomeStateParams) {
|
||||||
|
if !maps.Equal(state.env, want) {
|
||||||
|
t.Errorf("toContainer: env = %#v, want %#v", state.env, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
if next != nil {
|
||||||
|
next(t, state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type opBehaviourTestCase struct {
|
type opBehaviourTestCase struct {
|
||||||
name string
|
name string
|
||||||
newOp func(isShim, clearUnexported bool) outcomeOp
|
newOp func(isShim, clearUnexported bool) outcomeOp
|
||||||
newConfig func() *hst.Config
|
newConfig func() *hst.Config
|
||||||
|
|
||||||
pStateSys func(state *outcomeStateSys)
|
pStateSys pStateSysFunc
|
||||||
toSystem []stub.Call
|
toSystem []stub.Call
|
||||||
wantSys *system.I
|
wantSys *system.I
|
||||||
extraCheckSys func(t *testing.T, state *outcomeStateSys)
|
extraCheckSys extraCheckSysFunc
|
||||||
wantErrSystem error
|
wantErrSystem error
|
||||||
|
|
||||||
pStateContainer func(state *outcomeStateParams)
|
pStateContainer pStateContainerFunc
|
||||||
toContainer []stub.Call
|
toContainer []stub.Call
|
||||||
wantParams *container.Params
|
wantParams *container.Params
|
||||||
extraCheckParams func(t *testing.T, state *outcomeStateParams)
|
extraCheckParams extraCheckParamsFunc
|
||||||
wantErrContainer error
|
wantErrContainer error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
@ -42,48 +41,32 @@ func TestSpAccountOp(t *testing.T) {
|
|||||||
return c
|
return c
|
||||||
}, nil, []stub.Call{
|
}, nil, []stub.Call{
|
||||||
// this op performs basic validation and does not make calls during toSystem
|
// this op performs basic validation and does not make calls during toSystem
|
||||||
}, newI(), nil, nil, func(state *outcomeStateParams) {
|
}, newI(), nil, nil, insertsOps(nil), []stub.Call{
|
||||||
state.params.Ops = new(container.Ops)
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, &container.Params{
|
}, &container.Params{
|
||||||
Dir: config.Container.Home,
|
Dir: config.Container.Home,
|
||||||
Ops: new(container.Ops).
|
Ops: new(container.Ops).
|
||||||
Place(m("/etc/passwd"), []byte("chronos:x:1000:100:Hakurei:/data/data/org.chromium.Chromium:/run/current-system/sw/bin/zsh\n")).
|
Place(m("/etc/passwd"), []byte("chronos:x:1000:100:Hakurei:/data/data/org.chromium.Chromium:/run/current-system/sw/bin/zsh\n")).
|
||||||
Place(m("/etc/group"), []byte("hakurei:x:100:\n")),
|
Place(m("/etc/group"), []byte("hakurei:x:100:\n")),
|
||||||
}, func(t *testing.T, state *outcomeStateParams) {
|
}, paramsWantEnv(config, map[string]string{
|
||||||
wantEnv := map[string]string{
|
"HOME": config.Container.Home.String(),
|
||||||
"HOME": config.Container.Home.String(),
|
"USER": config.Container.Username,
|
||||||
"USER": config.Container.Username,
|
"SHELL": config.Container.Shell.String(),
|
||||||
"SHELL": config.Container.Shell.String(),
|
}, nil), nil},
|
||||||
}
|
|
||||||
maps.Copy(wantEnv, config.Container.Env)
|
|
||||||
if !maps.Equal(state.env, wantEnv) {
|
|
||||||
t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv)
|
|
||||||
}
|
|
||||||
}, nil},
|
|
||||||
|
|
||||||
{"success", func(bool, bool) outcomeOp { return spAccountOp{} }, hst.Template, nil, []stub.Call{
|
{"success", func(bool, bool) outcomeOp { return spAccountOp{} }, hst.Template, nil, []stub.Call{
|
||||||
// this op performs basic validation and does not make calls during toSystem
|
// this op performs basic validation and does not make calls during toSystem
|
||||||
}, newI(), nil, nil, func(state *outcomeStateParams) {
|
}, newI(), nil, nil, insertsOps(nil), []stub.Call{
|
||||||
state.params.Ops = new(container.Ops)
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, &container.Params{
|
}, &container.Params{
|
||||||
Dir: config.Container.Home,
|
Dir: config.Container.Home,
|
||||||
Ops: new(container.Ops).
|
Ops: new(container.Ops).
|
||||||
Place(m("/etc/passwd"), []byte("chronos:x:1000:100:Hakurei:/data/data/org.chromium.Chromium:/run/current-system/sw/bin/zsh\n")).
|
Place(m("/etc/passwd"), []byte("chronos:x:1000:100:Hakurei:/data/data/org.chromium.Chromium:/run/current-system/sw/bin/zsh\n")).
|
||||||
Place(m("/etc/group"), []byte("hakurei:x:100:\n")),
|
Place(m("/etc/group"), []byte("hakurei:x:100:\n")),
|
||||||
}, func(t *testing.T, state *outcomeStateParams) {
|
}, paramsWantEnv(config, map[string]string{
|
||||||
wantEnv := map[string]string{
|
"HOME": config.Container.Home.String(),
|
||||||
"HOME": config.Container.Home.String(),
|
"USER": config.Container.Username,
|
||||||
"USER": config.Container.Username,
|
"SHELL": config.Container.Shell.String(),
|
||||||
"SHELL": config.Container.Shell.String(),
|
}, nil), nil},
|
||||||
}
|
|
||||||
maps.Copy(wantEnv, config.Container.Env)
|
|
||||||
if !maps.Equal(state.env, wantEnv) {
|
|
||||||
t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv)
|
|
||||||
}
|
|
||||||
}, nil},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"maps"
|
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"syscall"
|
"syscall"
|
||||||
@ -72,16 +71,9 @@ func TestSpParamsOp(t *testing.T) {
|
|||||||
Proc(fhs.AbsProc).Tmpfs(hst.AbsPrivateTmp, 1<<12, 0755).
|
Proc(fhs.AbsProc).Tmpfs(hst.AbsPrivateTmp, 1<<12, 0755).
|
||||||
DevWritable(fhs.AbsDev, true).
|
DevWritable(fhs.AbsDev, true).
|
||||||
Tmpfs(fhs.AbsDev.Append("shm"), 0, 01777),
|
Tmpfs(fhs.AbsDev.Append("shm"), 0, 01777),
|
||||||
|
}, paramsWantEnv(config, map[string]string{
|
||||||
|
"TERM": "xterm",
|
||||||
}, func(t *testing.T, state *outcomeStateParams) {
|
}, 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 {
|
if state.as.AutoEtcPrefix != wantAutoEtcPrefix {
|
||||||
t.Errorf("toContainer: as.AutoEtcPrefix = %q, want %q", 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) {
|
if !reflect.DeepEqual(state.filesystem, wantFilesystems) {
|
||||||
t.Errorf("toContainer: filesystem = %#v, want %#v", state.filesystem, wantFilesystems)
|
t.Errorf("toContainer: filesystem = %#v, want %#v", state.filesystem, wantFilesystems)
|
||||||
}
|
}
|
||||||
}, nil},
|
}), nil},
|
||||||
|
|
||||||
{"success", func(isShim, _ bool) outcomeOp {
|
{"success", func(isShim, _ bool) outcomeOp {
|
||||||
if !isShim {
|
if !isShim {
|
||||||
@ -117,15 +109,9 @@ func TestSpParamsOp(t *testing.T) {
|
|||||||
Proc(fhs.AbsProc).Tmpfs(hst.AbsPrivateTmp, 1<<12, 0755).
|
Proc(fhs.AbsProc).Tmpfs(hst.AbsPrivateTmp, 1<<12, 0755).
|
||||||
Bind(fhs.AbsDev, fhs.AbsDev, bits.BindWritable|bits.BindDevice).
|
Bind(fhs.AbsDev, fhs.AbsDev, bits.BindWritable|bits.BindDevice).
|
||||||
Tmpfs(fhs.AbsDev.Append("shm"), 0, 01777),
|
Tmpfs(fhs.AbsDev.Append("shm"), 0, 01777),
|
||||||
|
}, paramsWantEnv(config, map[string]string{
|
||||||
|
"TERM": "xterm",
|
||||||
}, func(t *testing.T, state *outcomeStateParams) {
|
}, 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 {
|
if state.as.AutoEtcPrefix != wantAutoEtcPrefix {
|
||||||
t.Errorf("toContainer: as.AutoEtcPrefix = %q, want %q", 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) {
|
if !reflect.DeepEqual(state.filesystem, wantFilesystems) {
|
||||||
t.Errorf("toContainer: filesystem = %#v, want %#v", 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()
|
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{
|
checkOpBehaviour(t, []opBehaviourTestCase{
|
||||||
{"readdir", func(bool, bool) outcomeOp {
|
{"readdir", func(bool, bool) outcomeOp {
|
||||||
return new(spFilesystemOp)
|
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/.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("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),
|
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.filesystem = append(configSmall.Container.Filesystem, hst.FilesystemConfigJSON{})
|
||||||
state.params.Ops = new(container.Ops)
|
})), []stub.Call{
|
||||||
state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}}
|
|
||||||
state.filesystem = append(state.filesystem, hst.FilesystemConfigJSON{})
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, nil, nil, &hst.AppError{
|
}, nil, nil, &hst.AppError{
|
||||||
Step: "finalise",
|
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/.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("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),
|
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.filesystem = configSmall.Container.Filesystem
|
||||||
state.params.Ops = new(container.Ops)
|
})), []stub.Call{
|
||||||
state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}}
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, &container.Params{
|
}, &container.Params{
|
||||||
Ops: new(container.Ops).
|
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/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/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),
|
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.filesystem = config.Container.Filesystem[1:]
|
||||||
state.params.Ops = new(container.Ops)
|
})), []stub.Call{
|
||||||
state.as = hst.ApplyState{AutoEtcPrefix: wantAutoEtcPrefix, Ops: opsAdapter{state.params.Ops}}
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, &container.Params{
|
}, &container.Params{
|
||||||
Ops: new(container.Ops).
|
Ops: new(container.Ops).
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
func init() { gob.Register(new(spDBusOp)) }
|
func init() { gob.Register(new(spDBusOp)) }
|
||||||
|
|
||||||
// spDBusOp maintains an xdg-dbus-proxy instance for the container.
|
// spDBusOp maintains an xdg-dbus-proxy instance for the container.
|
||||||
|
// Runs after spRuntimeOp.
|
||||||
type spDBusOp struct {
|
type spDBusOp struct {
|
||||||
// Whether to bind the system bus socket. Populated during toSystem.
|
// Whether to bind the system bus socket. Populated during toSystem.
|
||||||
ProxySystem bool
|
ProxySystem bool
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"maps"
|
|
||||||
"reflect"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -18,7 +16,6 @@ import (
|
|||||||
|
|
||||||
func TestSpDBusOp(t *testing.T) {
|
func TestSpDBusOp(t *testing.T) {
|
||||||
config := hst.Template()
|
config := hst.Template()
|
||||||
const instancePrefix = container.Nonexistent + "/tmp/hakurei.0/" + wantAutoEtcPrefix
|
|
||||||
|
|
||||||
checkOpBehaviour(t, []opBehaviourTestCase{
|
checkOpBehaviour(t, []opBehaviourTestCase{
|
||||||
{"not enabled", func(bool, bool) outcomeOp {
|
{"not enabled", func(bool, bool) outcomeOp {
|
||||||
@ -41,11 +38,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
"unix:path=/run/user/1000/bus",
|
"unix:path=/run/user/1000/bus",
|
||||||
"unix:path=/var/run/dbus/system_bus_socket",
|
"unix:path=/var/run/dbus/system_bus_socket",
|
||||||
}, nil),
|
}, nil),
|
||||||
}, nil, func(t *testing.T, state *outcomeStateSys) {
|
}, nil, sysUsesInstance(nil), &system.OpError{
|
||||||
if want := m(instancePrefix); !reflect.DeepEqual(state.sharePath, want) {
|
|
||||||
t.Errorf("outcomeStateSys: sharePath = %v, want %v", state.sharePath, want)
|
|
||||||
}
|
|
||||||
}, &system.OpError{
|
|
||||||
Op: "dbus",
|
Op: "dbus",
|
||||||
Err: syscall.EINVAL,
|
Err: syscall.EINVAL,
|
||||||
Msg: "message bus proxy configuration contains NUL byte",
|
Msg: "message bus proxy configuration contains NUL byte",
|
||||||
@ -66,7 +59,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
call("isVerbose", stub.ExpectArgs{}, true, nil),
|
call("isVerbose", stub.ExpectArgs{}, true, nil),
|
||||||
call("verbose", stub.ExpectArgs{[]any{"session bus proxy:", []string{
|
call("verbose", stub.ExpectArgs{[]any{"session bus proxy:", []string{
|
||||||
"unix:path=/run/user/1000/bus",
|
"unix:path=/run/user/1000/bus",
|
||||||
instancePrefix + "/bus",
|
wantInstancePrefix + "/bus",
|
||||||
"--filter",
|
"--filter",
|
||||||
"--talk=org.freedesktop.DBus",
|
"--talk=org.freedesktop.DBus",
|
||||||
"--talk=org.freedesktop.Notifications",
|
"--talk=org.freedesktop.Notifications",
|
||||||
@ -77,7 +70,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
}}}, nil, nil),
|
}}}, nil, nil),
|
||||||
call("verbose", stub.ExpectArgs{[]any{"message bus proxy final args:", helper.MustNewCheckedArgs(
|
call("verbose", stub.ExpectArgs{[]any{"message bus proxy final args:", helper.MustNewCheckedArgs(
|
||||||
"unix:path=/run/user/1000/bus",
|
"unix:path=/run/user/1000/bus",
|
||||||
instancePrefix+"/bus",
|
wantInstancePrefix+"/bus",
|
||||||
"--filter",
|
"--filter",
|
||||||
"--talk=org.freedesktop.DBus",
|
"--talk=org.freedesktop.DBus",
|
||||||
"--talk=org.freedesktop.Notifications",
|
"--talk=org.freedesktop.Notifications",
|
||||||
@ -88,40 +81,25 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
)}}, nil, nil),
|
)}}, nil, nil),
|
||||||
}, func() *system.I {
|
}, func() *system.I {
|
||||||
sys := system.New(panicMsgContext{}, message.NewMsg(nil), checkExpectUid)
|
sys := system.New(panicMsgContext{}, message.NewMsg(nil), checkExpectUid)
|
||||||
sys.Ephemeral(system.Process, m(instancePrefix), 0711)
|
sys.Ephemeral(system.Process, m(wantInstancePrefix), 0711)
|
||||||
if err := sys.ProxyDBus(
|
if err := sys.ProxyDBus(
|
||||||
dbus.NewConfig(config.ID, true, true), nil,
|
dbus.NewConfig(config.ID, true, true), nil,
|
||||||
dbus.ProxyPair{"unix:path=/run/user/1000/bus", instancePrefix + "/bus"},
|
dbus.ProxyPair{"unix:path=/run/user/1000/bus", wantInstancePrefix + "/bus"},
|
||||||
dbus.ProxyPair{"unix:path=/var/run/dbus/system_bus_socket", instancePrefix + "/system_bus_socket"},
|
dbus.ProxyPair{"unix:path=/var/run/dbus/system_bus_socket", wantInstancePrefix + "/system_bus_socket"},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
t.Fatalf("cannot prepare sys: %v", err)
|
t.Fatalf("cannot prepare sys: %v", err)
|
||||||
}
|
}
|
||||||
sys.UpdatePerm(m(instancePrefix+"/bus"), acl.Read, acl.Write)
|
sys.UpdatePerm(m(wantInstancePrefix+"/bus"), acl.Read, acl.Write)
|
||||||
return sys
|
return sys
|
||||||
}(), func(t *testing.T, state *outcomeStateSys) {
|
}(), sysUsesInstance(nil), nil, insertsOps(afterSpRuntimeOp(nil)), []stub.Call{
|
||||||
if want := m(instancePrefix); !reflect.DeepEqual(state.sharePath, want) {
|
|
||||||
t.Errorf("outcomeStateSys: sharePath = %v, want %v", state.sharePath, want)
|
|
||||||
}
|
|
||||||
}, nil, func(state *outcomeStateParams) {
|
|
||||||
state.params.Ops = new(container.Ops)
|
|
||||||
|
|
||||||
// emulates spRuntimeOp
|
|
||||||
state.runtimeDir = m("/run/user/1000")
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, &container.Params{
|
}, &container.Params{
|
||||||
Ops: new(container.Ops).
|
Ops: new(container.Ops).
|
||||||
Bind(m(instancePrefix+"/bus"),
|
Bind(m(wantInstancePrefix+"/bus"),
|
||||||
m("/run/user/1000/bus"), 0),
|
m("/run/user/1000/bus"), 0),
|
||||||
}, func(t *testing.T, state *outcomeStateParams) {
|
}, paramsWantEnv(config, map[string]string{
|
||||||
wantEnv := map[string]string{
|
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
|
||||||
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
|
}, nil), nil},
|
||||||
}
|
|
||||||
maps.Copy(wantEnv, config.Container.Env)
|
|
||||||
if !maps.Equal(state.env, wantEnv) {
|
|
||||||
t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv)
|
|
||||||
}
|
|
||||||
}, nil},
|
|
||||||
|
|
||||||
{"success", func(isShim, _ bool) outcomeOp {
|
{"success", func(isShim, _ bool) outcomeOp {
|
||||||
if !isShim {
|
if !isShim {
|
||||||
@ -136,7 +114,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
call("isVerbose", stub.ExpectArgs{}, true, nil),
|
call("isVerbose", stub.ExpectArgs{}, true, nil),
|
||||||
call("verbose", stub.ExpectArgs{[]any{"session bus proxy:", []string{
|
call("verbose", stub.ExpectArgs{[]any{"session bus proxy:", []string{
|
||||||
"unix:path=/run/user/1000/bus",
|
"unix:path=/run/user/1000/bus",
|
||||||
instancePrefix + "/bus",
|
wantInstancePrefix + "/bus",
|
||||||
"--filter",
|
"--filter",
|
||||||
"--talk=org.freedesktop.Notifications",
|
"--talk=org.freedesktop.Notifications",
|
||||||
"--talk=org.freedesktop.FileManager1",
|
"--talk=org.freedesktop.FileManager1",
|
||||||
@ -153,7 +131,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
}}}, nil, nil),
|
}}}, nil, nil),
|
||||||
call("verbose", stub.ExpectArgs{[]any{"system bus proxy:", []string{
|
call("verbose", stub.ExpectArgs{[]any{"system bus proxy:", []string{
|
||||||
"unix:path=/var/run/dbus/system_bus_socket",
|
"unix:path=/var/run/dbus/system_bus_socket",
|
||||||
instancePrefix + "/system_bus_socket",
|
wantInstancePrefix + "/system_bus_socket",
|
||||||
"--filter",
|
"--filter",
|
||||||
"--talk=org.bluez",
|
"--talk=org.bluez",
|
||||||
"--talk=org.freedesktop.Avahi",
|
"--talk=org.freedesktop.Avahi",
|
||||||
@ -161,7 +139,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
}}}, nil, nil),
|
}}}, nil, nil),
|
||||||
call("verbose", stub.ExpectArgs{[]any{"message bus proxy final args:", helper.MustNewCheckedArgs(
|
call("verbose", stub.ExpectArgs{[]any{"message bus proxy final args:", helper.MustNewCheckedArgs(
|
||||||
"unix:path=/run/user/1000/bus",
|
"unix:path=/run/user/1000/bus",
|
||||||
instancePrefix+"/bus",
|
wantInstancePrefix+"/bus",
|
||||||
"--filter",
|
"--filter",
|
||||||
"--talk=org.freedesktop.Notifications",
|
"--talk=org.freedesktop.Notifications",
|
||||||
"--talk=org.freedesktop.FileManager1",
|
"--talk=org.freedesktop.FileManager1",
|
||||||
@ -177,7 +155,7 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
"--broadcast=org.freedesktop.portal.*=@/org/freedesktop/portal/*",
|
"--broadcast=org.freedesktop.portal.*=@/org/freedesktop/portal/*",
|
||||||
|
|
||||||
"unix:path=/var/run/dbus/system_bus_socket",
|
"unix:path=/var/run/dbus/system_bus_socket",
|
||||||
instancePrefix+"/system_bus_socket",
|
wantInstancePrefix+"/system_bus_socket",
|
||||||
"--filter",
|
"--filter",
|
||||||
"--talk=org.bluez",
|
"--talk=org.bluez",
|
||||||
"--talk=org.freedesktop.Avahi",
|
"--talk=org.freedesktop.Avahi",
|
||||||
@ -185,43 +163,28 @@ func TestSpDBusOp(t *testing.T) {
|
|||||||
)}}, nil, nil),
|
)}}, nil, nil),
|
||||||
}, func() *system.I {
|
}, func() *system.I {
|
||||||
sys := system.New(panicMsgContext{}, message.NewMsg(nil), checkExpectUid)
|
sys := system.New(panicMsgContext{}, message.NewMsg(nil), checkExpectUid)
|
||||||
sys.Ephemeral(system.Process, m(instancePrefix), 0711)
|
sys.Ephemeral(system.Process, m(wantInstancePrefix), 0711)
|
||||||
if err := sys.ProxyDBus(
|
if err := sys.ProxyDBus(
|
||||||
config.SessionBus, config.SystemBus,
|
config.SessionBus, config.SystemBus,
|
||||||
dbus.ProxyPair{"unix:path=/run/user/1000/bus", instancePrefix + "/bus"},
|
dbus.ProxyPair{"unix:path=/run/user/1000/bus", wantInstancePrefix + "/bus"},
|
||||||
dbus.ProxyPair{"unix:path=/var/run/dbus/system_bus_socket", instancePrefix + "/system_bus_socket"},
|
dbus.ProxyPair{"unix:path=/var/run/dbus/system_bus_socket", wantInstancePrefix + "/system_bus_socket"},
|
||||||
); err != nil {
|
); err != nil {
|
||||||
t.Fatalf("cannot prepare sys: %v", err)
|
t.Fatalf("cannot prepare sys: %v", err)
|
||||||
}
|
}
|
||||||
sys.UpdatePerm(m(instancePrefix+"/bus"), acl.Read, acl.Write).
|
sys.UpdatePerm(m(wantInstancePrefix+"/bus"), acl.Read, acl.Write).
|
||||||
UpdatePerm(m(instancePrefix+"/system_bus_socket"), acl.Read, acl.Write)
|
UpdatePerm(m(wantInstancePrefix+"/system_bus_socket"), acl.Read, acl.Write)
|
||||||
return sys
|
return sys
|
||||||
}(), func(t *testing.T, state *outcomeStateSys) {
|
}(), sysUsesInstance(nil), nil, insertsOps(afterSpRuntimeOp(nil)), []stub.Call{
|
||||||
if want := m(instancePrefix); !reflect.DeepEqual(state.sharePath, want) {
|
|
||||||
t.Errorf("outcomeStateSys: sharePath = %v, want %v", state.sharePath, want)
|
|
||||||
}
|
|
||||||
}, nil, func(state *outcomeStateParams) {
|
|
||||||
state.params.Ops = new(container.Ops)
|
|
||||||
|
|
||||||
// emulates spRuntimeOp
|
|
||||||
state.runtimeDir = m("/run/user/1000")
|
|
||||||
}, []stub.Call{
|
|
||||||
// this op configures the container state and does not make calls during toContainer
|
// this op configures the container state and does not make calls during toContainer
|
||||||
}, &container.Params{
|
}, &container.Params{
|
||||||
Ops: new(container.Ops).
|
Ops: new(container.Ops).
|
||||||
Bind(m(instancePrefix+"/bus"),
|
Bind(m(wantInstancePrefix+"/bus"),
|
||||||
m("/run/user/1000/bus"), 0).
|
m("/run/user/1000/bus"), 0).
|
||||||
Bind(m(instancePrefix+"/system_bus_socket"),
|
Bind(m(wantInstancePrefix+"/system_bus_socket"),
|
||||||
m("/var/run/dbus/system_bus_socket"), 0),
|
m("/var/run/dbus/system_bus_socket"), 0),
|
||||||
}, func(t *testing.T, state *outcomeStateParams) {
|
}, paramsWantEnv(config, map[string]string{
|
||||||
wantEnv := map[string]string{
|
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
|
||||||
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
|
"DBUS_SYSTEM_BUS_ADDRESS": "unix:path=/var/run/dbus/system_bus_socket",
|
||||||
"DBUS_SYSTEM_BUS_ADDRESS": "unix:path=/var/run/dbus/system_bus_socket",
|
}, nil), nil},
|
||||||
}
|
|
||||||
maps.Copy(wantEnv, config.Container.Env)
|
|
||||||
if !maps.Equal(state.env, wantEnv) {
|
|
||||||
t.Errorf("toContainer: env = %#v, want %#v", state.env, wantEnv)
|
|
||||||
}
|
|
||||||
}, nil},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user