github.com/moby/docker@v26.1.3+incompatible/integration/network/delete_test.go (about)

     1  package network // import "github.com/docker/docker/integration/network"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	dclient "github.com/docker/docker/client"
     9  	"github.com/docker/docker/integration/internal/network"
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  	"gotest.tools/v3/skip"
    13  )
    14  
    15  func containsNetwork(nws []types.NetworkResource, networkID string) bool {
    16  	for _, n := range nws {
    17  		if n.ID == networkID {
    18  			return true
    19  		}
    20  	}
    21  	return false
    22  }
    23  
    24  // createAmbiguousNetworks creates three networks, of which the second network
    25  // uses a prefix of the first network's ID as name. The third network uses the
    26  // first network's ID as name.
    27  //
    28  // After successful creation, properties of all three networks is returned
    29  func createAmbiguousNetworks(ctx context.Context, t *testing.T, client dclient.APIClient) (string, string, string) {
    30  	testNet := network.CreateNoError(ctx, t, client, "testNet")
    31  	idPrefixNet := network.CreateNoError(ctx, t, client, testNet[:12])
    32  	fullIDNet := network.CreateNoError(ctx, t, client, testNet)
    33  
    34  	nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
    35  	assert.NilError(t, err)
    36  
    37  	assert.Check(t, is.Equal(true, containsNetwork(nws, testNet)), "failed to create network testNet")
    38  	assert.Check(t, is.Equal(true, containsNetwork(nws, idPrefixNet)), "failed to create network idPrefixNet")
    39  	assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "failed to create network fullIDNet")
    40  	return testNet, idPrefixNet, fullIDNet
    41  }
    42  
    43  // TestNetworkCreateDelete tests creation and deletion of a network.
    44  func TestNetworkCreateDelete(t *testing.T) {
    45  	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
    46  	ctx := setupTest(t)
    47  	client := testEnv.APIClient()
    48  
    49  	netName := "testnetwork_" + t.Name()
    50  	network.CreateNoError(ctx, t, client, netName)
    51  	assert.Check(t, IsNetworkAvailable(ctx, client, netName))
    52  
    53  	// delete the network and make sure it is deleted
    54  	err := client.NetworkRemove(ctx, netName)
    55  	assert.NilError(t, err)
    56  	assert.Check(t, IsNetworkNotAvailable(ctx, client, netName))
    57  }
    58  
    59  // TestDockerNetworkDeletePreferID tests that if a network with a name
    60  // equal to another network's ID exists, the Network with the given
    61  // ID is removed, and not the network with the given name.
    62  func TestDockerNetworkDeletePreferID(t *testing.T) {
    63  	skip.If(t, testEnv.DaemonInfo.OSType == "windows",
    64  		"FIXME. Windows doesn't run DinD and uses networks shared between control daemon and daemon under test")
    65  
    66  	ctx := setupTest(t)
    67  	client := testEnv.APIClient()
    68  
    69  	testNet, idPrefixNet, fullIDNet := createAmbiguousNetworks(ctx, t, client)
    70  
    71  	// Delete the network using a prefix of the first network's ID as name.
    72  	// This should the network name with the id-prefix, not the original network.
    73  	err := client.NetworkRemove(ctx, testNet[:12])
    74  	assert.NilError(t, err)
    75  
    76  	// Delete the network using networkID. This should remove the original
    77  	// network, not the network with the name equal to the networkID
    78  	err = client.NetworkRemove(ctx, testNet)
    79  	assert.NilError(t, err)
    80  
    81  	// networks "testNet" and "idPrefixNet" should be removed, but "fullIDNet" should still exist
    82  	nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
    83  	assert.NilError(t, err)
    84  	assert.Check(t, is.Equal(false, containsNetwork(nws, testNet)), "Network testNet not removed")
    85  	assert.Check(t, is.Equal(false, containsNetwork(nws, idPrefixNet)), "Network idPrefixNet not removed")
    86  	assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "Network fullIDNet not found")
    87  }