github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/integration/network/delete_test.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/integration/util/request"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func containsNetwork(nws []types.NetworkResource, nw types.NetworkCreateResponse) bool {
    14  	for _, n := range nws {
    15  		if n.ID == nw.ID {
    16  			return true
    17  		}
    18  	}
    19  	return false
    20  }
    21  
    22  // createAmbiguousNetworks creates three networks, of which the second network
    23  // uses a prefix of the first network's ID as name. The third network uses the
    24  // first network's ID as name.
    25  //
    26  // After successful creation, properties of all three networks is returned
    27  func createAmbiguousNetworks(t *testing.T) (types.NetworkCreateResponse, types.NetworkCreateResponse, types.NetworkCreateResponse) {
    28  	client := request.NewAPIClient(t)
    29  	ctx := context.Background()
    30  
    31  	testNet, err := client.NetworkCreate(ctx, "testNet", types.NetworkCreate{})
    32  	require.NoError(t, err)
    33  	idPrefixNet, err := client.NetworkCreate(ctx, testNet.ID[:12], types.NetworkCreate{})
    34  	require.NoError(t, err)
    35  	fullIDNet, err := client.NetworkCreate(ctx, testNet.ID, types.NetworkCreate{})
    36  	require.NoError(t, err)
    37  
    38  	nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
    39  	require.NoError(t, err)
    40  
    41  	assert.Equal(t, true, containsNetwork(nws, testNet), "failed to create network testNet")
    42  	assert.Equal(t, true, containsNetwork(nws, idPrefixNet), "failed to create network idPrefixNet")
    43  	assert.Equal(t, true, containsNetwork(nws, fullIDNet), "failed to create network fullIDNet")
    44  	return testNet, idPrefixNet, fullIDNet
    45  }
    46  
    47  // TestDockerNetworkDeletePreferID tests that if a network with a name
    48  // equal to another network's ID exists, the Network with the given
    49  // ID is removed, and not the network with the given name.
    50  func TestDockerNetworkDeletePreferID(t *testing.T) {
    51  	defer setupTest(t)()
    52  	client := request.NewAPIClient(t)
    53  	ctx := context.Background()
    54  	testNet, idPrefixNet, fullIDNet := createAmbiguousNetworks(t)
    55  
    56  	// Delete the network using a prefix of the first network's ID as name.
    57  	// This should the network name with the id-prefix, not the original network.
    58  	err := client.NetworkRemove(ctx, testNet.ID[:12])
    59  	require.NoError(t, err)
    60  
    61  	// Delete the network using networkID. This should remove the original
    62  	// network, not the network with the name equal to the networkID
    63  	err = client.NetworkRemove(ctx, testNet.ID)
    64  	require.NoError(t, err)
    65  
    66  	// networks "testNet" and "idPrefixNet" should be removed, but "fullIDNet" should still exist
    67  	nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
    68  	require.NoError(t, err)
    69  	assert.Equal(t, false, containsNetwork(nws, testNet), "Network testNet not removed")
    70  	assert.Equal(t, false, containsNetwork(nws, idPrefixNet), "Network idPrefixNet not removed")
    71  	assert.Equal(t, true, containsNetwork(nws, fullIDNet), "Network fullIDNet not found")
    72  }