github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/testutils/context_unix.go (about)

     1  //go:build linux || freebsd
     2  // +build linux freebsd
     3  
     4  package testutils
     5  
     6  import (
     7  	"runtime"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/libnetwork/ns"
    11  	"github.com/vishvananda/netns"
    12  )
    13  
    14  // SetupTestOSContext joins a new network namespace, and returns its associated
    15  // teardown function.
    16  //
    17  // Example usage:
    18  //
    19  //	defer SetupTestOSContext(t)()
    20  func SetupTestOSContext(t *testing.T) func() {
    21  	origNS, err := netns.Get()
    22  	if err != nil {
    23  		t.Fatalf("Failed to open initial netns: %v", err)
    24  	}
    25  	restore := func() {
    26  		if err := netns.Set(origNS); err != nil {
    27  			t.Logf("Warning: failed to restore thread netns (%v)", err)
    28  		} else {
    29  			runtime.UnlockOSThread()
    30  		}
    31  
    32  		if err := origNS.Close(); err != nil {
    33  			t.Logf("Warning: netns closing failed (%v)", err)
    34  		}
    35  	}
    36  
    37  	runtime.LockOSThread()
    38  	newNS, err := netns.New()
    39  	if err != nil {
    40  		// netns.New() is not atomic: it could have encountered an error
    41  		// after unsharing the current thread's network namespace.
    42  		restore()
    43  		t.Fatalf("Failed to enter netns: %v", err)
    44  	}
    45  
    46  	// Since we are switching to a new test namespace make
    47  	// sure to re-initialize initNs context
    48  	ns.Init()
    49  
    50  	return func() {
    51  		if err := newNS.Close(); err != nil {
    52  			t.Logf("Warning: netns closing failed (%v)", err)
    53  		}
    54  		restore()
    55  		ns.Init()
    56  	}
    57  }