delete_test.go 3.4 KB

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