github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/drivers/windows/windows_test.go (about)

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