package netnsutils
import "github.com/dotcloud/docker/internal/testutils/netnsutils"
Index ¶
- func AssertSocketSameNetNS(t testing.TB, conn syscall.Conn)
- func SetupTestOSContext(t *testing.T) func()
- type OSContext
Functions ¶
func AssertSocketSameNetNS ¶
AssertSocketSameNetNS makes a best-effort attempt to assert that conn is in the same network namespace as the current goroutine's thread.
func SetupTestOSContext ¶
SetupTestOSContext joins the current goroutine to a new network namespace, and returns its associated teardown function.
Example usage:
defer SetupTestOSContext(t)()
Types ¶
type OSContext ¶
type OSContext struct {
// contains filtered or unexported fields
}
OSContext is a handle to a test OS context.
func SetupTestOSContextEx ¶
SetupTestOSContextEx joins the current goroutine to a new network namespace.
Compared to SetupTestOSContext, this function allows goroutines to be spawned which are associated with the same OS context via the returned OSContext value.
Example usage:
c := SetupTestOSContext(t) defer c.Cleanup(t)
func (*OSContext) Cleanup ¶
Cleanup tears down the OS context. It must be called from the same goroutine as the SetupTestOSContextEx call which returned c.
Explicit cleanup is required as (*testing.T).Cleanup() makes no guarantees about which goroutine the cleanup functions are invoked on.
func (*OSContext) Go ¶
Go starts running fn in a new goroutine inside the test OS context.
func (*OSContext) Set ¶
Set sets the OS context of the calling goroutine to c and returns a teardown function to restore the calling goroutine's OS context and release resources. The teardown function accepts an optional Logger argument.
This is a lower-level interface which is less ergonomic than c.Go() but more composable with other goroutine-spawning utilities such as sync.WaitGroup or golang.org/x/sync/errgroup.Group.
Example usage:
func TestFoo(t *testing.T) { osctx := testutils.SetupTestOSContextEx(t) defer osctx.Cleanup(t) var eg errgroup.Group eg.Go(func() error { teardown, err := osctx.Set() if err != nil { return err } defer teardown(t) // ... }) if err := eg.Wait(); err != nil { t.Fatalf("%+v", err) } }
Source Files ¶
context_unix.go sanity_linux.go
- Version
- v28.1.1+incompatible (latest)
- Published
- Apr 18, 2025
- Platform
- linux/amd64
- Imports
- 12 packages
- Last checked
- 7 hours ago –
Tools for package owners.