helpers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //go:build !windows
  2. package network
  3. import (
  4. "context"
  5. "fmt"
  6. "testing"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/client"
  9. "github.com/docker/docker/testutil"
  10. "gotest.tools/v3/assert/cmp"
  11. "gotest.tools/v3/icmd"
  12. )
  13. // CreateMasterDummy creates a dummy network interface
  14. func CreateMasterDummy(ctx context.Context, t *testing.T, master string) {
  15. // ip link add <dummy_name> type dummy
  16. testutil.RunCommand(ctx, "ip", "link", "add", master, "type", "dummy").Assert(t, icmd.Success)
  17. testutil.RunCommand(ctx, "ip", "link", "set", master, "up").Assert(t, icmd.Success)
  18. }
  19. // CreateVlanInterface creates a vlan network interface
  20. func CreateVlanInterface(ctx context.Context, t *testing.T, master, slave, id string) {
  21. // ip link add link <master> name <master>.<VID> type vlan id <VID>
  22. testutil.RunCommand(ctx, "ip", "link", "add", "link", master, "name", slave, "type", "vlan", "id", id).Assert(t, icmd.Success)
  23. // ip link set <sub_interface_name> up
  24. testutil.RunCommand(ctx, "ip", "link", "set", slave, "up").Assert(t, icmd.Success)
  25. }
  26. // DeleteInterface deletes a network interface
  27. func DeleteInterface(ctx context.Context, t *testing.T, ifName string) {
  28. testutil.RunCommand(ctx, "ip", "link", "delete", ifName).Assert(t, icmd.Success)
  29. testutil.RunCommand(ctx, "iptables", "-t", "nat", "--flush").Assert(t, icmd.Success)
  30. testutil.RunCommand(ctx, "iptables", "--flush").Assert(t, icmd.Success)
  31. }
  32. // LinkExists verifies that a link exists
  33. func LinkExists(ctx context.Context, t *testing.T, master string) {
  34. // verify the specified link exists, ip link show <link_name>
  35. testutil.RunCommand(ctx, "ip", "link", "show", master).Assert(t, icmd.Success)
  36. }
  37. // IsNetworkAvailable provides a comparison to check if a docker network is available
  38. func IsNetworkAvailable(ctx context.Context, c client.NetworkAPIClient, name string) cmp.Comparison {
  39. return func() cmp.Result {
  40. networks, err := c.NetworkList(ctx, types.NetworkListOptions{})
  41. if err != nil {
  42. return cmp.ResultFromError(err)
  43. }
  44. for _, network := range networks {
  45. if network.Name == name {
  46. return cmp.ResultSuccess
  47. }
  48. }
  49. return cmp.ResultFailure(fmt.Sprintf("could not find network %s", name))
  50. }
  51. }
  52. // IsNetworkNotAvailable provides a comparison to check if a docker network is not available
  53. func IsNetworkNotAvailable(ctx context.Context, c client.NetworkAPIClient, name string) cmp.Comparison {
  54. return func() cmp.Result {
  55. networks, err := c.NetworkList(ctx, types.NetworkListOptions{})
  56. if err != nil {
  57. return cmp.ResultFromError(err)
  58. }
  59. for _, network := range networks {
  60. if network.Name == name {
  61. return cmp.ResultFailure(fmt.Sprintf("network %s is still present", name))
  62. }
  63. }
  64. return cmp.ResultSuccess
  65. }
  66. }