daemon_linux.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package daemon // import "github.com/docker/docker/testutil/daemon"
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "golang.org/x/sys/unix"
  9. "gotest.tools/v3/assert"
  10. )
  11. func cleanupNetworkNamespace(t testing.TB, d *Daemon) {
  12. t.Helper()
  13. // Cleanup network namespaces in the exec root of this
  14. // daemon because this exec root is specific to this
  15. // daemon instance and has no chance of getting
  16. // cleaned up when a new daemon is instantiated with a
  17. // new exec root.
  18. filepath.WalkDir(filepath.Join(d.execRoot, "netns"), func(path string, _ os.DirEntry, _ error) error {
  19. if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
  20. t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
  21. }
  22. os.Remove(path)
  23. return nil
  24. })
  25. }
  26. // CgroupNamespace returns the cgroup namespace the daemon is running in
  27. func (d *Daemon) CgroupNamespace(t testing.TB) string {
  28. link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid()))
  29. assert.NilError(t, err)
  30. return strings.TrimSpace(link)
  31. }