Port testutil/daemon to FreeBSD

testutil/daemon uses a generic unix implementation that assumes that
the host OS supports cgroups & network namespaces, which is not the
case for FreeBSD.

This change adds a FreeBSD-specific implementation for
`testutil/daemon`, namely for `cleanupNetworkNamespace` and
`CgroupNamespace` functions.

Signed-off-by: Artem Khramov <akhramov@pm.me>
This commit is contained in:
Artem Khramov 2021-10-02 09:01:43 +03:00
parent 306fa44b7c
commit 8f1b2a0fd3
No known key found for this signature in database
GPG Key ID: 6CB5147BFFBCC525
3 changed files with 55 additions and 30 deletions

View File

@ -0,0 +1,18 @@
//go:build freebsd
// +build freebsd
package daemon // import "github.com/docker/docker/testutil/daemon"
import (
"testing"
"gotest.tools/v3/assert"
)
func cleanupNetworkNamespace(_ testing.TB, _ *Daemon) {}
// CgroupNamespace returns the cgroup namespace the daemon is running in
func (d *Daemon) CgroupNamespace(t testing.TB) string {
assert.Assert(t, false, "cgroup namespaces are not supported on FreeBSD")
return ""
}

View File

@ -0,0 +1,37 @@
package daemon // import "github.com/docker/docker/testutil/daemon"
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"golang.org/x/sys/unix"
"gotest.tools/v3/assert"
)
func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
t.Helper()
// Cleanup network namespaces in the exec root of this
// daemon because this exec root is specific to this
// daemon instance and has no chance of getting
// cleaned up when a new daemon is instantiated with a
// new exec root.
netnsPath := filepath.Join(d.execRoot, "netns")
filepath.Walk(netnsPath, func(path string, info os.FileInfo, err error) error {
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
}
os.Remove(path)
return nil
})
}
// CgroupNamespace returns the cgroup namespace the daemon is running in
func (d *Daemon) CgroupNamespace(t testing.TB) string {
link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid()))
assert.NilError(t, err)
return strings.TrimSpace(link)
}

View File

@ -4,17 +4,12 @@
package daemon // import "github.com/docker/docker/testutil/daemon"
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"testing"
"github.com/moby/sys/mount"
"golang.org/x/sys/unix"
"gotest.tools/v3/assert"
)
// cleanupMount unmounts the daemon root directory, or logs a message if
@ -26,31 +21,6 @@ func cleanupMount(t testing.TB, d *Daemon) {
}
}
func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
t.Helper()
// Cleanup network namespaces in the exec root of this
// daemon because this exec root is specific to this
// daemon instance and has no chance of getting
// cleaned up when a new daemon is instantiated with a
// new exec root.
netnsPath := filepath.Join(d.execRoot, "netns")
filepath.Walk(netnsPath, func(path string, info os.FileInfo, err error) error {
if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
}
os.Remove(path)
return nil
})
}
// CgroupNamespace returns the cgroup namespace the daemon is running in
func (d *Daemon) CgroupNamespace(t testing.TB) string {
link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid()))
assert.NilError(t, err)
return strings.TrimSpace(link)
}
// SignalDaemonDump sends a signal to the daemon to write a dump file
func SignalDaemonDump(pid int) {
unix.Kill(pid, unix.SIGQUIT)