bridge_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package network
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "time"
  7. networktypes "github.com/docker/docker/api/types/network"
  8. "github.com/docker/docker/api/types/versions"
  9. ctr "github.com/docker/docker/integration/internal/container"
  10. "github.com/docker/docker/integration/internal/network"
  11. "gotest.tools/v3/assert"
  12. "gotest.tools/v3/skip"
  13. )
  14. func TestCreateWithMultiNetworks(t *testing.T) {
  15. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  16. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.44"), "requires API v1.44")
  17. ctx := setupTest(t)
  18. apiClient := testEnv.APIClient()
  19. network.CreateNoError(ctx, t, apiClient, "testnet1")
  20. defer network.RemoveNoError(ctx, t, apiClient, "testnet1")
  21. network.CreateNoError(ctx, t, apiClient, "testnet2")
  22. defer network.RemoveNoError(ctx, t, apiClient, "testnet2")
  23. attachCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
  24. defer cancel()
  25. res := ctr.RunAttach(attachCtx, t, apiClient,
  26. ctr.WithCmd("ip", "-o", "-4", "addr", "show"),
  27. ctr.WithNetworkMode("testnet1"),
  28. ctr.WithEndpointSettings("testnet1", &networktypes.EndpointSettings{}),
  29. ctr.WithEndpointSettings("testnet2", &networktypes.EndpointSettings{}))
  30. assert.Equal(t, res.ExitCode, 0)
  31. assert.Equal(t, res.Stderr.String(), "")
  32. // Only interfaces with an IPv4 address are printed by iproute2 when flag -4 is specified. Here, we should have two
  33. // interfaces for testnet1 and testnet2, plus lo.
  34. ifacesWithAddress := strings.Count(res.Stdout.String(), "\n")
  35. assert.Equal(t, ifacesWithAddress, 3)
  36. }