github.com/moby/docker@v26.1.3+incompatible/integration/network/helpers.go (about)

     1  //go:build !windows
     2  
     3  package network
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/docker/docker/api/types"
    11  	"github.com/docker/docker/client"
    12  	"github.com/docker/docker/testutil"
    13  	"gotest.tools/v3/assert/cmp"
    14  	"gotest.tools/v3/icmd"
    15  )
    16  
    17  // CreateMasterDummy creates a dummy network interface
    18  func CreateMasterDummy(ctx context.Context, t *testing.T, master string) {
    19  	// ip link add <dummy_name> type dummy
    20  	testutil.RunCommand(ctx, "ip", "link", "add", master, "type", "dummy").Assert(t, icmd.Success)
    21  	testutil.RunCommand(ctx, "ip", "link", "set", master, "up").Assert(t, icmd.Success)
    22  }
    23  
    24  // CreateVlanInterface creates a vlan network interface
    25  func CreateVlanInterface(ctx context.Context, t *testing.T, master, slave, id string) {
    26  	// ip link add link <master> name <master>.<VID> type vlan id <VID>
    27  	testutil.RunCommand(ctx, "ip", "link", "add", "link", master, "name", slave, "type", "vlan", "id", id).Assert(t, icmd.Success)
    28  	// ip link set <sub_interface_name> up
    29  	testutil.RunCommand(ctx, "ip", "link", "set", slave, "up").Assert(t, icmd.Success)
    30  }
    31  
    32  // DeleteInterface deletes a network interface
    33  func DeleteInterface(ctx context.Context, t *testing.T, ifName string) {
    34  	testutil.RunCommand(ctx, "ip", "link", "delete", ifName).Assert(t, icmd.Success)
    35  	testutil.RunCommand(ctx, "iptables", "-t", "nat", "--flush").Assert(t, icmd.Success)
    36  	testutil.RunCommand(ctx, "iptables", "--flush").Assert(t, icmd.Success)
    37  }
    38  
    39  // LinkExists verifies that a link exists
    40  func LinkExists(ctx context.Context, t *testing.T, master string) {
    41  	// verify the specified link exists, ip link show <link_name>
    42  	testutil.RunCommand(ctx, "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(ctx context.Context, c client.NetworkAPIClient, name string) cmp.Comparison {
    47  	return func() cmp.Result {
    48  		networks, err := c.NetworkList(ctx, 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(ctx context.Context, c client.NetworkAPIClient, name string) cmp.Comparison {
    63  	return func() cmp.Result {
    64  		networks, err := c.NetworkList(ctx, 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  }