hst/fs: valid method on underlying interface
All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 1m59s
Test / Hakurei (push) Successful in 3m6s
Test / Hpkg (push) Successful in 4m16s
Test / Sandbox (race detector) (push) Successful in 4m24s
Test / Hakurei (race detector) (push) Successful in 5m7s
Test / Flake checks (push) Successful in 1m39s

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-08-14 21:36:22 +09:00
parent c989e7785a
commit 0e543a58b3
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
6 changed files with 38 additions and 22 deletions

View File

@ -13,6 +13,8 @@ import (
type FilesystemConfig interface { type FilesystemConfig interface {
// Type returns the type of this mount point. // Type returns the type of this mount point.
Type() string Type() string
// Valid returns whether the configuration is valid.
Valid() bool
// Target returns the pathname of the mount point in the container. // Target returns the pathname of the mount point in the container.
Target() *container.Absolute Target() *container.Absolute
// Host returns a slice of all host paths used by this mount point. // Host returns a slice of all host paths used by this mount point.
@ -60,7 +62,9 @@ type FilesystemConfigJSON struct {
} }
// Valid returns whether the [FilesystemConfigJSON] is valid. // Valid returns whether the [FilesystemConfigJSON] is valid.
func (f *FilesystemConfigJSON) Valid() bool { return f != nil && f.FilesystemConfig != nil } func (f *FilesystemConfigJSON) Valid() bool {
return f != nil && f.FilesystemConfig != nil && f.FilesystemConfig.Valid()
}
func (f *FilesystemConfigJSON) MarshalJSON() ([]byte, error) { func (f *FilesystemConfigJSON) MarshalJSON() ([]byte, error) {
if f == nil || f.FilesystemConfig == nil { if f == nil || f.FilesystemConfig == nil {

View File

@ -144,7 +144,7 @@ func TestFilesystemConfigJSON(t *testing.T) {
t.Errorf("Valid: %v, want false", got) t.Errorf("Valid: %v, want false", got)
} }
if got := (&hst.FilesystemConfigJSON{FilesystemConfig: new(hst.FSBind)}).Valid(); !got { if got := (&hst.FilesystemConfigJSON{FilesystemConfig: &hst.FSBind{Src: m("/etc")}}).Valid(); !got {
t.Errorf("Valid: %v, want true", got) t.Errorf("Valid: %v, want true", got)
} }
}) })
@ -192,6 +192,7 @@ type stubFS struct {
} }
func (s stubFS) Type() string { return s.typeName } func (s stubFS) Type() string { return s.typeName }
func (s stubFS) Valid() bool { return false }
func (s stubFS) Target() *container.Absolute { panic("unreachable") } func (s stubFS) Target() *container.Absolute { panic("unreachable") }
func (s stubFS) Host() []*container.Absolute { panic("unreachable") } func (s stubFS) Host() []*container.Absolute { panic("unreachable") }
func (s stubFS) Apply(*container.Ops) { panic("unreachable") } func (s stubFS) Apply(*container.Ops) { panic("unreachable") }
@ -205,6 +206,7 @@ type sCheck struct {
type fsTestCase struct { type fsTestCase struct {
name string name string
fs hst.FilesystemConfig fs hst.FilesystemConfig
valid bool
ops container.Ops ops container.Ops
target *container.Absolute target *container.Absolute
host []*container.Absolute host []*container.Absolute
@ -214,9 +216,17 @@ type fsTestCase struct {
func checkFs(t *testing.T, fstype string, testCases []fsTestCase) { func checkFs(t *testing.T, fstype string, testCases []fsTestCase) {
for _, tc := range testCases { for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
t.Run("type", func(t *testing.T) {
if got := tc.fs.Type(); got != fstype { if got := tc.fs.Type(); got != fstype {
t.Errorf("Type: %q, want %q", got, fstype) t.Errorf("Type: %q, want %q", got, fstype)
} }
})
t.Run("valid", func(t *testing.T) {
if got := tc.fs.Valid(); got != tc.valid {
t.Errorf("Valid: %v, want %v", got, tc.valid)
}
})
t.Run("ops", func(t *testing.T) { t.Run("ops", func(t *testing.T) {
ops := new(container.Ops) ops := new(container.Ops)

View File

@ -27,9 +27,10 @@ type FSBind struct {
} }
func (b *FSBind) Type() string { return FilesystemBind } func (b *FSBind) Type() string { return FilesystemBind }
func (b *FSBind) Valid() bool { return b != nil && b.Src != nil }
func (b *FSBind) Target() *container.Absolute { func (b *FSBind) Target() *container.Absolute {
if b == nil || b.Src == nil { if !b.Valid() {
return nil return nil
} }
if b.Dst == nil { if b.Dst == nil {
@ -39,14 +40,14 @@ func (b *FSBind) Target() *container.Absolute {
} }
func (b *FSBind) Host() []*container.Absolute { func (b *FSBind) Host() []*container.Absolute {
if b == nil || b.Src == nil { if !b.Valid() {
return nil return nil
} }
return []*container.Absolute{b.Src} return []*container.Absolute{b.Src}
} }
func (b *FSBind) Apply(ops *container.Ops) { func (b *FSBind) Apply(ops *container.Ops) {
if b == nil || b.Src == nil { if !b.Valid() {
return return
} }
@ -69,7 +70,7 @@ func (b *FSBind) Apply(ops *container.Ops) {
func (b *FSBind) String() string { func (b *FSBind) String() string {
g := 4 g := 4
if b == nil || b.Src == nil { if !b.Valid() {
return "<invalid>" return "<invalid>"
} }

View File

@ -9,14 +9,14 @@ import (
func TestFSBind(t *testing.T) { func TestFSBind(t *testing.T) {
checkFs(t, "bind", []fsTestCase{ checkFs(t, "bind", []fsTestCase{
{"nil", (*hst.FSBind)(nil), nil, nil, nil, "<invalid>"}, {"nil", (*hst.FSBind)(nil), false, nil, nil, nil, "<invalid>"},
{"full", &hst.FSBind{ {"full", &hst.FSBind{
Dst: m("/dev"), Dst: m("/dev"),
Src: m("/mnt/dev"), Src: m("/mnt/dev"),
Optional: true, Optional: true,
Device: true, Device: true,
}, container.Ops{&container.BindMountOp{ }, true, container.Ops{&container.BindMountOp{
Source: m("/mnt/dev"), Source: m("/mnt/dev"),
Target: m("/dev"), Target: m("/dev"),
Flags: container.BindWritable | container.BindDevice | container.BindOptional, Flags: container.BindWritable | container.BindDevice | container.BindOptional,
@ -28,7 +28,7 @@ func TestFSBind(t *testing.T) {
Src: m("/mnt/dev"), Src: m("/mnt/dev"),
Write: true, Write: true,
Device: true, Device: true,
}, container.Ops{&container.BindMountOp{ }, true, container.Ops{&container.BindMountOp{
Source: m("/mnt/dev"), Source: m("/mnt/dev"),
Target: m("/dev"), Target: m("/dev"),
Flags: container.BindWritable | container.BindDevice, Flags: container.BindWritable | container.BindDevice,
@ -39,7 +39,7 @@ func TestFSBind(t *testing.T) {
Dst: m("/tmp"), Dst: m("/tmp"),
Src: m("/mnt/tmp"), Src: m("/mnt/tmp"),
Write: true, Write: true,
}, container.Ops{&container.BindMountOp{ }, true, container.Ops{&container.BindMountOp{
Source: m("/mnt/tmp"), Source: m("/mnt/tmp"),
Target: m("/tmp"), Target: m("/tmp"),
Flags: container.BindWritable, Flags: container.BindWritable,
@ -49,7 +49,7 @@ func TestFSBind(t *testing.T) {
{"full no flags", &hst.FSBind{ {"full no flags", &hst.FSBind{
Dst: m("/etc"), Dst: m("/etc"),
Src: m("/mnt/etc"), Src: m("/mnt/etc"),
}, container.Ops{&container.BindMountOp{ }, true, container.Ops{&container.BindMountOp{
Source: m("/mnt/etc"), Source: m("/mnt/etc"),
Target: m("/etc"), Target: m("/etc"),
}}, m("/etc"), ms("/mnt/etc"), }}, m("/etc"), ms("/mnt/etc"),
@ -57,7 +57,7 @@ func TestFSBind(t *testing.T) {
{"nil dst", &hst.FSBind{ {"nil dst", &hst.FSBind{
Src: m("/"), Src: m("/"),
}, container.Ops{&container.BindMountOp{ }, true, container.Ops{&container.BindMountOp{
Source: m("/"), Source: m("/"),
Target: m("/"), Target: m("/"),
}}, m("/"), ms("/"), }}, m("/"), ms("/"),

View File

@ -26,9 +26,10 @@ type FSEphemeral struct {
} }
func (e *FSEphemeral) Type() string { return FilesystemEphemeral } func (e *FSEphemeral) Type() string { return FilesystemEphemeral }
func (e *FSEphemeral) Valid() bool { return e != nil && e.Dst != nil }
func (e *FSEphemeral) Target() *container.Absolute { func (e *FSEphemeral) Target() *container.Absolute {
if e == nil { if !e.Valid() {
return nil return nil
} }
return e.Dst return e.Dst
@ -39,7 +40,7 @@ func (e *FSEphemeral) Host() []*container.Absolute { return nil }
const fsEphemeralDefaultPerm = os.FileMode(0755) const fsEphemeralDefaultPerm = os.FileMode(0755)
func (e *FSEphemeral) Apply(ops *container.Ops) { func (e *FSEphemeral) Apply(ops *container.Ops) {
if e == nil || e.Dst == nil { if !e.Valid() {
return return
} }
@ -61,7 +62,7 @@ func (e *FSEphemeral) Apply(ops *container.Ops) {
} }
func (e *FSEphemeral) String() string { func (e *FSEphemeral) String() string {
if e == nil || e.Dst == nil { if !e.Valid() {
return "<invalid>" return "<invalid>"
} }

View File

@ -10,14 +10,14 @@ import (
func TestFSEphemeral(t *testing.T) { func TestFSEphemeral(t *testing.T) {
checkFs(t, "ephemeral", []fsTestCase{ checkFs(t, "ephemeral", []fsTestCase{
{"nil", (*hst.FSEphemeral)(nil), nil, nil, nil, "<invalid>"}, {"nil", (*hst.FSEphemeral)(nil), false, nil, nil, nil, "<invalid>"},
{"full", &hst.FSEphemeral{ {"full", &hst.FSEphemeral{
Dst: m("/run/user/65534"), Dst: m("/run/user/65534"),
Write: true, Write: true,
Size: 1 << 10, Size: 1 << 10,
Perm: 0700, Perm: 0700,
}, container.Ops{&container.MountTmpfsOp{ }, true, container.Ops{&container.MountTmpfsOp{
FSName: "ephemeral", FSName: "ephemeral",
Path: m("/run/user/65534"), Path: m("/run/user/65534"),
Flags: syscall.MS_NOSUID | syscall.MS_NODEV, Flags: syscall.MS_NOSUID | syscall.MS_NODEV,
@ -26,7 +26,7 @@ func TestFSEphemeral(t *testing.T) {
}}, m("/run/user/65534"), nil, }}, m("/run/user/65534"), nil,
"w+ephemeral(-rwx------):/run/user/65534"}, "w+ephemeral(-rwx------):/run/user/65534"},
{"cover ro", &hst.FSEphemeral{Dst: m("/run/nscd")}, {"cover ro", &hst.FSEphemeral{Dst: m("/run/nscd")}, true,
container.Ops{&container.MountTmpfsOp{ container.Ops{&container.MountTmpfsOp{
FSName: "readonly", FSName: "readonly",
Path: m("/run/nscd"), Path: m("/run/nscd"),
@ -39,7 +39,7 @@ func TestFSEphemeral(t *testing.T) {
Dst: hst.AbsTmp, Dst: hst.AbsTmp,
Write: true, Write: true,
Size: -1, Size: -1,
}, container.Ops{&container.MountTmpfsOp{ }, true, container.Ops{&container.MountTmpfsOp{
FSName: "ephemeral", FSName: "ephemeral",
Path: hst.AbsTmp, Path: hst.AbsTmp,
Flags: syscall.MS_NOSUID | syscall.MS_NODEV, Flags: syscall.MS_NOSUID | syscall.MS_NODEV,