From 541d4144dcd98867b78409b997a0b1e82e6566da Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 13 Nov 2023 13:58:51 +0200 Subject: [PATCH] Testing: map binary names to HCN types. Signed-off-by: Nashwan Azhari --- plugins/nat/nat_windows_test.go | 9 +-- plugins/sdnbridge/sdnbridge_windows_test.go | 9 +-- plugins/sdnoverlay/sdnoverlay_windows_test.go | 9 +-- test/utilities/connectivity_testing.go | 61 ++++++++++++++----- 4 files changed, 62 insertions(+), 26 deletions(-) diff --git a/plugins/nat/nat_windows_test.go b/plugins/nat/nat_windows_test.go index 7fe24d36..c7893fa7 100644 --- a/plugins/nat/nat_windows_test.go +++ b/plugins/nat/nat_windows_test.go @@ -5,22 +5,23 @@ import ( "testing" "github.com/Microsoft/hcsshim/hcn" + "github.com/Microsoft/windows-container-networking/cni" util "github.com/Microsoft/windows-container-networking/test/utilities" ) var testDualStack bool var imageToUse string -func CreateNatTestNetwork() *hcn.HostComputeNetwork { +func CreateNatTestNetwork(t *testing.T) *hcn.HostComputeNetwork { ipams := util.GetDefaultIpams() - return util.CreateTestNetwork("natNet", "Nat", ipams, false) + return util.CreateTestNetwork(t, "natNet", cni.NatPluginName, ipams, false) } func TestNatCmdAdd(t *testing.T) { // t.Skip("Nat test is disabled for now.") testDualStack = (os.Getenv("TestDualStack") == "1") imageToUse = os.Getenv("ImageToUse") - testNetwork := CreateNatTestNetwork() - pt := util.MakeTestStruct(t, testNetwork, "nat", false, false, "", testDualStack, imageToUse) + testNetwork := CreateNatTestNetwork(t) + pt := util.MakeTestStruct(t, testNetwork, false, false, "", testDualStack, imageToUse) pt.RunAll(t) } diff --git a/plugins/sdnbridge/sdnbridge_windows_test.go b/plugins/sdnbridge/sdnbridge_windows_test.go index fb8b81b7..65a06f5a 100644 --- a/plugins/sdnbridge/sdnbridge_windows_test.go +++ b/plugins/sdnbridge/sdnbridge_windows_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/Microsoft/hcsshim/hcn" + "github.com/Microsoft/windows-container-networking/cni" util "github.com/Microsoft/windows-container-networking/test/utilities" "os" @@ -12,20 +13,20 @@ import ( var testDualStack bool var imageToUse string -func CreateBridgeTestNetwork() *hcn.HostComputeNetwork { +func CreateBridgeTestNetwork(t *testing.T) *hcn.HostComputeNetwork { ipams := util.GetDefaultIpams() if testDualStack { ipams = append(ipams, util.GetDefaultIpv6Ipams()...) } - return util.CreateTestNetwork("bridgeNet", "L2Bridge", ipams, true) + return util.CreateTestNetwork(t, "bridgeNet", cni.SdnBridgePluginName, ipams, true) } func TestBridgeCmdAdd(t *testing.T) { // t.Skip("Bridge test is disabled for now.") testDualStack = (os.Getenv("TestDualStack") == "1") imageToUse = os.Getenv("ImageToUse") - testNetwork := CreateBridgeTestNetwork() - pt := util.MakeTestStruct(t, testNetwork, "L2Bridge", true, true, "", testDualStack, imageToUse) + testNetwork := CreateBridgeTestNetwork(t) + pt := util.MakeTestStruct(t, testNetwork, true, true, "", testDualStack, imageToUse) pt.Ipv6Url = os.Getenv("Ipv6UrlToUse") pt.RunAll(t) } diff --git a/plugins/sdnoverlay/sdnoverlay_windows_test.go b/plugins/sdnoverlay/sdnoverlay_windows_test.go index eb5ab374..36691c29 100644 --- a/plugins/sdnoverlay/sdnoverlay_windows_test.go +++ b/plugins/sdnoverlay/sdnoverlay_windows_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/Microsoft/hcsshim/hcn" + "github.com/Microsoft/windows-container-networking/cni" util "github.com/Microsoft/windows-container-networking/test/utilities" ) @@ -28,17 +29,17 @@ func GetVsidPol() []json.RawMessage { return []json.RawMessage{vsidPolRaw} } -func CreateOverlayTestNetwork() *hcn.HostComputeNetwork { +func CreateOverlayTestNetwork(t *testing.T) *hcn.HostComputeNetwork { ipams := util.GetDefaultIpams() ipams[0].Subnets[0].Policies = GetVsidPol() - return util.CreateTestNetwork("overlayNet", "Overlay", ipams, true) + return util.CreateTestNetwork(t, "overlayNet", cni.SdnOverlayPluginName, ipams, true) } func TestOverlayCmdAdd(t *testing.T) { // t.Skip("Overlay test is disabled for now.") testDualStack = (os.Getenv("TestDualStack") == "1") imageToUse = os.Getenv("ImageToUse") - testNetwork := CreateOverlayTestNetwork() - pt := util.MakeTestStruct(t, testNetwork, "Overlay", true, false, "", testDualStack, imageToUse) + testNetwork := CreateOverlayTestNetwork(t) + pt := util.MakeTestStruct(t, testNetwork, true, false, "", testDualStack, imageToUse) pt.RunAll(t) } diff --git a/test/utilities/connectivity_testing.go b/test/utilities/connectivity_testing.go index 3896ad0c..d435d2c6 100644 --- a/test/utilities/connectivity_testing.go +++ b/test/utilities/connectivity_testing.go @@ -88,32 +88,52 @@ func CreateNetConfIpam(cidr string) cni.IpamConfig { return testIpam } -func CreateNetworkConf(cniVersion string, name string, pluginType string, - dns *cniTypes.DNS, addArgs []cni.KVP, gatewayPrefix string) *cni.NetworkConfig { +func CreateNetworkConf(t *testing.T, cniVersion string, name string, pluginType hcn.NetworkType, + dns *cniTypes.DNS, addArgs []cni.KVP, gatewayPrefix string) (*cni.NetworkConfig, error) { + + mappedType, err := cni.MapCniTypeToHcnType(pluginType) + if err != nil { + return nil, err + } + + if pluginType != mappedType { + t.Logf("WARN: supplied network plugin type %q was mapped to HCN network type %q", pluginType, mappedType) + } + netConf := cni.NetworkConfig{ CniVersion: cniVersion, Name: name, Ipam: CreateNetConfIpam(gatewayPrefix), - Type: pluginType, + Type: mappedType, DNS: *dns, AdditionalArgs: addArgs, } - return &netConf + return &netConf, nil } func CreateDualStackNetworkConf( + t *testing.T, cniVersion string, name string, - pluginType string, + pluginType hcn.NetworkType, dns *cniTypes.DNS, addArgs []cni.KVP, gatewayPrefixv4 string, - gatewayPrefixv6 string) *cni.NetworkConfig { + gatewayPrefixv6 string) (*cni.NetworkConfig, error) { + + mappedType, err := cni.MapCniTypeToHcnType(pluginType) + if err != nil { + return nil, err + } + + if pluginType != mappedType { + t.Logf("WARN: supplied network plugin type %q was mapped to HCN network type %q", pluginType, mappedType) + } netConf := cni.NetworkConfig{ CniVersion: cniVersion, Name: name, - Type: pluginType, + Type: mappedType, DNS: *dns, AdditionalArgs: addArgs, } @@ -139,7 +159,7 @@ func CreateDualStackNetworkConf( netConf.AdditionalRoutes = append(netConf.AdditionalRoutes, testRoute) - return &netConf + return &netConf, nil } func GetDefaultIpams() []hcn.Ipam { @@ -289,14 +309,24 @@ func CreateGatewayEp(t *testing.T, networkId string, ipAddress string, ipv6Adres return nil } -func CreateTestNetwork(name string, netType string, ipams []hcn.Ipam, tryGetNetAdapter bool) *hcn.HostComputeNetwork { +func CreateTestNetwork(t *testing.T, name string, netType string, ipams []hcn.Ipam, tryGetNetAdapter bool) *hcn.HostComputeNetwork { + hcnNetType := hcn.NetworkType(netType) + mappedType, err := cni.MapCniTypeToHcnType(hcnNetType) + if err != nil { + t.Errorf("Failed to map testing network type %q to HCN network type: %v", netType, err) + } + + if mappedType != hcnNetType { + t.Logf("WARN: testing network type %q was mapped to HCN network type %q", netType, mappedType) + } + network := &hcn.HostComputeNetwork{ SchemaVersion: hcn.SchemaVersion{ Major: 2, Minor: 0, }, Name: name, - Type: hcn.NetworkType(netType), + Type: mappedType, Ipams: ipams, } @@ -312,7 +342,6 @@ func CreateTestNetwork(name string, netType string, ipams []hcn.Ipam, tryGetNetA func MakeTestStruct( t *testing.T, testNetwork *hcn.HostComputeNetwork, - pluginType string, epPols bool, needGW bool, cid string, @@ -352,11 +381,15 @@ func MakeTestStruct( var netConf *cni.NetworkConfig if !testDualStack { - netConf = CreateNetworkConf(defaultCniVersion, testNetwork.Name, pluginType, dns, addArgs, netConfPrefix) + if netConf, err = CreateNetworkConf(t, defaultCniVersion, testNetwork.Name, testNetwork.Type, dns, addArgs, netConfPrefix); err != nil { + t.Errorf("Failed to create testing network config: %v", err) + return nil + } } else { - netConfPrefixv6 := "fd00::101/64" - netConf = CreateDualStackNetworkConf(defaultCniVersion, testNetwork.Name, pluginType, dns, addArgs, netConfPrefix, netConfPrefixv6) + if netConf, err = CreateDualStackNetworkConf(t, defaultCniVersion, testNetwork.Name, testNetwork.Type, dns, addArgs, netConfPrefix, netConfPrefixv6); err != nil { + return nil + } } netJson, _ := json.Marshal(netConf)