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