github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration/network/delete_test.go (about)

     1  package network // import "github.com/Prakhar-Agarwal-byte/moby/integration/network"
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/Prakhar-Agarwal-byte/moby/api/types"
     8  	"github.com/Prakhar-Agarwal-byte/moby/api/types/versions"
     9  	dclient "github.com/Prakhar-Agarwal-byte/moby/client"
    10  	"github.com/Prakhar-Agarwal-byte/moby/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  	ctx := setupTest(t)
    48  	client := testEnv.APIClient()
    49  
    50  	netName := "testnetwork_" + t.Name()
    51  	network.CreateNoError(ctx, t, client, netName)
    52  	assert.Check(t, IsNetworkAvailable(ctx, client, netName))
    53  
    54  	// delete the network and make sure it is deleted
    55  	err := client.NetworkRemove(ctx, netName)
    56  	assert.NilError(t, err)
    57  	assert.Check(t, IsNetworkNotAvailable(ctx, client, netName))
    58  }
    59  
    60  // TestDockerNetworkDeletePreferID tests that if a network with a name
    61  // equal to another network's ID exists, the Network with the given
    62  // ID is removed, and not the network with the given name.
    63  func TestDockerNetworkDeletePreferID(t *testing.T) {
    64  	skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.34"), "broken in earlier versions")
    65  	skip.If(t, testEnv.DaemonInfo.OSType == "windows",
    66  		"FIXME. Windows doesn't run DinD and uses networks shared between control daemon and daemon under test")
    67  
    68  	ctx := setupTest(t)
    69  	client := testEnv.APIClient()
    70  
    71  	testNet, idPrefixNet, fullIDNet := createAmbiguousNetworks(ctx, t, client)
    72  
    73  	// Delete the network using a prefix of the first network's ID as name.
    74  	// This should the network name with the id-prefix, not the original network.
    75  	err := client.NetworkRemove(ctx, testNet[:12])
    76  	assert.NilError(t, err)
    77  
    78  	// Delete the network using networkID. This should remove the original
    79  	// network, not the network with the name equal to the networkID
    80  	err = client.NetworkRemove(ctx, testNet)
    81  	assert.NilError(t, err)
    82  
    83  	// networks "testNet" and "idPrefixNet" should be removed, but "fullIDNet" should still exist
    84  	nws, err := client.NetworkList(ctx, types.NetworkListOptions{})
    85  	assert.NilError(t, err)
    86  	assert.Check(t, is.Equal(false, containsNetwork(nws, testNet)), "Network testNet not removed")
    87  	assert.Check(t, is.Equal(false, containsNetwork(nws, idPrefixNet)), "Network idPrefixNet not removed")
    88  	assert.Check(t, is.Equal(true, containsNetwork(nws, fullIDNet)), "Network fullIDNet not found")
    89  }