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