inspect_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/integration/internal/network"
  7. "github.com/docker/docker/integration/internal/swarm"
  8. "gotest.tools/v3/assert"
  9. "gotest.tools/v3/poll"
  10. "gotest.tools/v3/skip"
  11. )
  12. func TestInspectNetwork(t *testing.T) {
  13. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "FIXME")
  14. skip.If(t, testEnv.IsRootless, "rootless mode doesn't support Swarm-mode")
  15. defer setupTest(t)()
  16. d := swarm.NewSwarm(t, testEnv)
  17. defer d.Stop(t)
  18. c := d.NewClientT(t)
  19. defer c.Close()
  20. networkName := "Overlay" + t.Name()
  21. overlayID := network.CreateNoError(context.Background(), t, c, networkName,
  22. network.WithDriver("overlay"),
  23. network.WithCheckDuplicate(),
  24. )
  25. var instances uint64 = 2
  26. serviceName := "TestService" + t.Name()
  27. serviceID := swarm.CreateService(t, d,
  28. swarm.ServiceWithReplicas(instances),
  29. swarm.ServiceWithName(serviceName),
  30. swarm.ServiceWithNetwork(networkName),
  31. )
  32. poll.WaitOn(t, swarm.RunningTasksCount(c, serviceID, instances), swarm.ServicePoll)
  33. tests := []struct {
  34. name string
  35. network string
  36. opts types.NetworkInspectOptions
  37. }{
  38. {
  39. name: "full network id",
  40. network: overlayID,
  41. opts: types.NetworkInspectOptions{
  42. Verbose: true,
  43. },
  44. },
  45. {
  46. name: "partial network id",
  47. network: overlayID[0:11],
  48. opts: types.NetworkInspectOptions{
  49. Verbose: true,
  50. },
  51. },
  52. {
  53. name: "network name",
  54. network: networkName,
  55. opts: types.NetworkInspectOptions{
  56. Verbose: true,
  57. },
  58. },
  59. {
  60. name: "network name and swarm scope",
  61. network: networkName,
  62. opts: types.NetworkInspectOptions{
  63. Verbose: true,
  64. Scope: "swarm",
  65. },
  66. },
  67. }
  68. ctx := context.Background()
  69. for _, tc := range tests {
  70. tc := tc
  71. t.Run(tc.name, func(t *testing.T) {
  72. nw, err := c.NetworkInspect(ctx, tc.network, tc.opts)
  73. assert.NilError(t, err)
  74. if service, ok := nw.Services[serviceName]; ok {
  75. assert.Equal(t, len(service.Tasks), int(instances))
  76. }
  77. assert.Assert(t, nw.IPAM.Config != nil)
  78. for _, cfg := range nw.IPAM.Config {
  79. assert.Assert(t, cfg.Gateway != "")
  80. assert.Assert(t, cfg.Subnet != "")
  81. }
  82. })
  83. }
  84. // TODO find out why removing networks is needed; other tests fail if the network is not removed, even though they run on a new daemon.
  85. err := c.ServiceRemove(ctx, serviceID)
  86. assert.NilError(t, err)
  87. poll.WaitOn(t, swarm.NoTasksForService(ctx, c, serviceID), swarm.ServicePoll)
  88. err = c.NetworkRemove(ctx, overlayID)
  89. assert.NilError(t, err)
  90. poll.WaitOn(t, network.IsRemoved(ctx, c, overlayID), swarm.NetworkPoll)
  91. }