delete_test.go 3.2 KB

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