delete_test.go 2.8 KB

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