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