github.com/kaisenlinux/docker@v0.0.0-20230510090727-ea55db55fac7/engine/integration/network/helpers.go (about)

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