github.com/rawahars/moby@v24.0.4+incompatible/libnetwork/testutils/context.go (about)

     1  package testutils
     2  
     3  import "testing"
     4  
     5  // Logger is used to log non-fatal messages during tests.
     6  type Logger interface {
     7  	Logf(format string, args ...any)
     8  }
     9  
    10  var _ Logger = (*testing.T)(nil)
    11  
    12  // SetupTestOSContext joins the current goroutine to a new network namespace,
    13  // and returns its associated teardown function.
    14  //
    15  // Example usage:
    16  //
    17  //	defer SetupTestOSContext(t)()
    18  func SetupTestOSContext(t *testing.T) func() {
    19  	c := SetupTestOSContextEx(t)
    20  	return func() { c.Cleanup(t) }
    21  }
    22  
    23  // Go starts running fn in a new goroutine inside the test OS context.
    24  func (c *OSContext) Go(t *testing.T, fn func()) {
    25  	t.Helper()
    26  	errCh := make(chan error, 1)
    27  	go func() {
    28  		teardown, err := c.Set()
    29  		if err != nil {
    30  			errCh <- err
    31  			return
    32  		}
    33  		defer teardown(t)
    34  		close(errCh)
    35  		fn()
    36  	}()
    37  
    38  	if err := <-errCh; err != nil {
    39  		t.Fatalf("%+v", err)
    40  	}
    41  }