github.com/rumpl/bof@v23.0.0-rc.2+incompatible/testutil/daemon/daemon_linux.go (about)

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