delete_test.go 3.7 KB

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