github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration/internal/network/network.go (about)

     1  package network
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/client"
     9  	"gotest.tools/assert"
    10  )
    11  
    12  func createNetwork(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
    13  	config := types.NetworkCreate{}
    14  
    15  	for _, op := range ops {
    16  		op(&config)
    17  	}
    18  
    19  	n, err := client.NetworkCreate(ctx, name, config)
    20  	return n.ID, err
    21  }
    22  
    23  // Create creates a network with the specified options
    24  func Create(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
    25  	return createNetwork(ctx, client, name, ops...)
    26  }
    27  
    28  // CreateNoError creates a network with the specified options and verifies there were no errors
    29  // nolint: golint
    30  func CreateNoError(t *testing.T, ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) string { // nolint: golint
    31  	t.Helper()
    32  
    33  	name, err := createNetwork(ctx, client, name, ops...)
    34  	assert.NilError(t, err)
    35  	return name
    36  }