github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/libnetwork/drivers/windows/windows_test.go (about)

     1  //go:build windows
     2  // +build windows
     3  
     4  package windows
     5  
     6  import (
     7  	"net"
     8  	"testing"
     9  
    10  	"github.com/docker/libnetwork/driverapi"
    11  	"github.com/docker/libnetwork/netlabel"
    12  	"github.com/docker/libnetwork/types"
    13  )
    14  
    15  func testNetwork(networkType string, t *testing.T) {
    16  	d := newDriver(networkType)
    17  	bnw, _ := types.ParseCIDR("172.16.0.0/24")
    18  	br, _ := types.ParseCIDR("172.16.0.1/16")
    19  
    20  	netOption := make(map[string]interface{})
    21  	networkOptions := map[string]string{
    22  		NetworkName: "TestNetwork",
    23  	}
    24  
    25  	netOption[netlabel.GenericData] = networkOptions
    26  	ipdList := []driverapi.IPAMData{
    27  		{
    28  			Pool:    bnw,
    29  			Gateway: br,
    30  		},
    31  	}
    32  
    33  	err := d.CreateNetwork("dummy", netOption, nil, ipdList, nil)
    34  	if err != nil {
    35  		t.Fatalf("Failed to create bridge: %v", err)
    36  	}
    37  	defer func() {
    38  		err = d.DeleteNetwork("dummy")
    39  		if err != nil {
    40  			t.Fatalf("Failed to create bridge: %v", err)
    41  		}
    42  	}()
    43  
    44  	epOptions := make(map[string]interface{})
    45  	te := &testEndpoint{}
    46  	err = d.CreateEndpoint("dummy", "ep1", te.Interface(), epOptions)
    47  	if err != nil {
    48  		t.Fatalf("Failed to create an endpoint : %s", err.Error())
    49  	}
    50  
    51  	err = d.DeleteEndpoint("dummy", "ep1")
    52  	if err != nil {
    53  		t.Fatalf("Failed to delete an endpoint : %s", err.Error())
    54  	}
    55  }
    56  
    57  func TestNAT(t *testing.T) {
    58  	testNetwork("nat", t)
    59  }
    60  
    61  func TestTransparent(t *testing.T) {
    62  	testNetwork("transparent", t)
    63  }
    64  
    65  type testEndpoint struct {
    66  	t                     *testing.T
    67  	src                   string
    68  	dst                   string
    69  	address               string
    70  	macAddress            string
    71  	gateway               string
    72  	disableGatewayService bool
    73  }
    74  
    75  func (test *testEndpoint) Interface() driverapi.InterfaceInfo {
    76  	return test
    77  }
    78  
    79  func (test *testEndpoint) Address() *net.IPNet {
    80  	if test.address == "" {
    81  		return nil
    82  	}
    83  	nw, _ := types.ParseCIDR(test.address)
    84  	return nw
    85  }
    86  
    87  func (test *testEndpoint) AddressIPv6() *net.IPNet {
    88  	return nil
    89  }
    90  
    91  func (test *testEndpoint) MacAddress() net.HardwareAddr {
    92  	if test.macAddress == "" {
    93  		return nil
    94  	}
    95  	mac, _ := net.ParseMAC(test.macAddress)
    96  	return mac
    97  }
    98  
    99  func (test *testEndpoint) SetMacAddress(mac net.HardwareAddr) error {
   100  	if test.macAddress != "" {
   101  		return types.ForbiddenErrorf("endpoint interface MAC address present (%s). Cannot be modified with %s.", test.macAddress, mac)
   102  	}
   103  
   104  	if mac == nil {
   105  		return types.BadRequestErrorf("tried to set nil MAC address to endpoint interface")
   106  	}
   107  	test.macAddress = mac.String()
   108  	return nil
   109  }
   110  
   111  func (test *testEndpoint) SetIPAddress(address *net.IPNet) error {
   112  	if address.IP == nil {
   113  		return types.BadRequestErrorf("tried to set nil IP address to endpoint interface")
   114  	}
   115  
   116  	test.address = address.String()
   117  	return nil
   118  }
   119  
   120  func (test *testEndpoint) InterfaceName() driverapi.InterfaceNameInfo {
   121  	return test
   122  }
   123  
   124  func (test *testEndpoint) SetGateway(ipv4 net.IP) error {
   125  	return nil
   126  }
   127  
   128  func (test *testEndpoint) SetGatewayIPv6(ipv6 net.IP) error {
   129  	return nil
   130  }
   131  
   132  func (test *testEndpoint) SetNames(src string, dst string) error {
   133  	return nil
   134  }
   135  
   136  func (test *testEndpoint) AddStaticRoute(destination *net.IPNet, routeType int, nextHop net.IP) error {
   137  	return nil
   138  }
   139  
   140  func (test *testEndpoint) DisableGatewayService() {
   141  	test.disableGatewayService = true
   142  }