daemon_linux.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. netnsPath := filepath.Join(d.execRoot, "netns")
  19. filepath.Walk(netnsPath, func(path string, info os.FileInfo, err error) error {
  20. if err := unix.Unmount(path, unix.MNT_DETACH); err != nil && err != unix.EINVAL && err != unix.ENOENT {
  21. t.Logf("[%s] unmount of %s failed: %v", d.id, path, err)
  22. }
  23. os.Remove(path)
  24. return nil
  25. })
  26. }
  27. // CgroupNamespace returns the cgroup namespace the daemon is running in
  28. func (d *Daemon) CgroupNamespace(t testing.TB) string {
  29. link, err := os.Readlink(fmt.Sprintf("/proc/%d/ns/cgroup", d.Pid()))
  30. assert.NilError(t, err)
  31. return strings.TrimSpace(link)
  32. }