helpers.go 2.5 KB

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