helpers.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // +build !windows
  2. package network
  3. import (
  4. "context"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/client"
  10. "github.com/docker/docker/pkg/parsers/kernel"
  11. "gotest.tools/assert/cmp"
  12. "gotest.tools/icmd"
  13. )
  14. // CreateMasterDummy creates a dummy network interface
  15. func CreateMasterDummy(t *testing.T, master string) {
  16. // ip link add <dummy_name> type dummy
  17. icmd.RunCommand("ip", "link", "add", master, "type", "dummy").Assert(t, icmd.Success)
  18. icmd.RunCommand("ip", "link", "set", master, "up").Assert(t, icmd.Success)
  19. }
  20. // CreateVlanInterface creates a vlan network interface
  21. func CreateVlanInterface(t *testing.T, master, slave, id string) {
  22. // ip link add link <master> name <master>.<VID> type vlan id <VID>
  23. icmd.RunCommand("ip", "link", "add", "link", master, "name", slave, "type", "vlan", "id", id).Assert(t, icmd.Success)
  24. // ip link set <sub_interface_name> up
  25. icmd.RunCommand("ip", "link", "set", slave, "up").Assert(t, icmd.Success)
  26. }
  27. // DeleteInterface deletes a network interface
  28. func DeleteInterface(t *testing.T, ifName string) {
  29. icmd.RunCommand("ip", "link", "delete", ifName).Assert(t, icmd.Success)
  30. icmd.RunCommand("iptables", "-t", "nat", "--flush").Assert(t, icmd.Success)
  31. icmd.RunCommand("iptables", "--flush").Assert(t, icmd.Success)
  32. }
  33. // LinkExists verifies that a link exists
  34. func LinkExists(t *testing.T, master string) {
  35. // verify the specified link exists, ip link show <link_name>
  36. icmd.RunCommand("ip", "link", "show", master).Assert(t, icmd.Success)
  37. }
  38. // IsNetworkAvailable provides a comparison to check if a docker network is available
  39. func IsNetworkAvailable(c client.NetworkAPIClient, name string) cmp.Comparison {
  40. return func() cmp.Result {
  41. networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
  42. if err != nil {
  43. return cmp.ResultFromError(err)
  44. }
  45. for _, network := range networks {
  46. if network.Name == name {
  47. return cmp.ResultSuccess
  48. }
  49. }
  50. return cmp.ResultFailure(fmt.Sprintf("could not find network %s", name))
  51. }
  52. }
  53. // IsNetworkNotAvailable provides a comparison to check if a docker network is not available
  54. func IsNetworkNotAvailable(c client.NetworkAPIClient, name string) cmp.Comparison {
  55. return func() cmp.Result {
  56. networks, err := c.NetworkList(context.Background(), types.NetworkListOptions{})
  57. if err != nil {
  58. return cmp.ResultFromError(err)
  59. }
  60. for _, network := range networks {
  61. if network.Name == name {
  62. return cmp.ResultFailure(fmt.Sprintf("network %s is still present", name))
  63. }
  64. }
  65. return cmp.ResultSuccess
  66. }
  67. }
  68. // CheckKernelMajorVersionGreaterOrEqualThen returns whether the kernel version is greater or equal than the one provided
  69. func CheckKernelMajorVersionGreaterOrEqualThen(kernelVersion int, majorVersion int) bool {
  70. kv, err := kernel.GetKernelVersion()
  71. if err != nil {
  72. return false
  73. }
  74. if kv.Kernel < kernelVersion || (kv.Kernel == kernelVersion && kv.Major < majorVersion) {
  75. return false
  76. }
  77. return true
  78. }
  79. // IsUserNamespace returns whether the user namespace remapping is enabled
  80. func IsUserNamespace() bool {
  81. root := os.Getenv("DOCKER_REMAP_ROOT")
  82. return root != ""
  83. }