From b360521954ff8b432d3b5fcf76c4997d19a39b51 Mon Sep 17 00:00:00 2001 From: Ian King'ori <50120072+iankingori@users.noreply.github.com> Date: Mon, 2 Oct 2023 04:46:17 -0700 Subject: [PATCH] Enable Unit Tests (#94) * fix: separate config name from config type to allow creation of multiple networks * tests: enable nat plugin tests * allow icmp requests on job runners * set nano server 2022 for windows server 2022 runner * enable sdn bridge tests * update network type to L2Bridge * fix: restore network plugin setup steps * enable sdn overlay tests * limit parallel test execution to 1 --- .github/workflows/ci.yml | 7 ++ Makefile | 2 +- cni/cni.go | 2 +- plugins/nat/nat_windows_test.go | 2 +- plugins/sdnbridge/sdnbridge_windows_test.go | 4 +- plugins/sdnoverlay/sdnoverlay_windows_test.go | 4 +- test/container/container_windows.go | 18 ++--- test/utilities/connectivity_testing.go | 2 +- test/utilities/container_testing.go | 2 +- test/utilities/plugin_testing.go | 33 ++++++--- test/utilities/testing_windows.go | 70 ++++++++++--------- 11 files changed, 86 insertions(+), 60 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00deb993..d394fa1b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -57,7 +57,14 @@ jobs: go install ./... working-directory: src/github.com/Microsoft/windows-container-networking + - name: Enable ICMP V4 + shell: pwsh + run: | + netsh advFirewall Firewall add rule name="Enable ICMP Protocal" protocol=icmpv4:8,any dir=in action=allow + - name: Test + env: + ImageToUse: ${{ matrix.os == 'windows-2022' && 'mcr.microsoft.com/windows/nanoserver:ltsc2022' || '' }} run: | mingw32-make.exe test working-directory: src/github.com/Microsoft/windows-container-networking diff --git a/Makefile b/Makefile index 1c71e84e..c4a62e1a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ CNIFILES = \ GOCMD=go GOLOCALENV=GO111MODULE=on GOARCH=amd64 GOOS=windows GOBUILD=$(GOLOCALENV) $(GOCMD) build -v -mod=vendor -GOTEST=$(GOLOCALENV) $(GOCMD) test -v -mod=vendor +GOTEST=$(GOLOCALENV) $(GOCMD) test -v -p 1 -mod=vendor CNI_NET_DIR = plugins OUTPUTDIR = out diff --git a/cni/cni.go b/cni/cni.go index 032b130c..497edc4e 100644 --- a/cni/cni.go +++ b/cni/cni.go @@ -246,7 +246,7 @@ func (config *NetworkConfig) GetNetworkInfo(podNamespace string) (ninfo *network ninfo = &network.NetworkInfo{ ID: config.Name, Name: config.Name, - Type: network.NetworkType(config.Name), + Type: network.NetworkType(config.Type), Subnets: subnets, InterfaceName: "", DNS: dnsSettings, diff --git a/plugins/nat/nat_windows_test.go b/plugins/nat/nat_windows_test.go index 89ca2418..7fe24d36 100644 --- a/plugins/nat/nat_windows_test.go +++ b/plugins/nat/nat_windows_test.go @@ -17,7 +17,7 @@ func CreateNatTestNetwork() *hcn.HostComputeNetwork { } func TestNatCmdAdd(t *testing.T) { - t.Skip("Nat test is disabled for now.") + // t.Skip("Nat test is disabled for now.") testDualStack = (os.Getenv("TestDualStack") == "1") imageToUse = os.Getenv("ImageToUse") testNetwork := CreateNatTestNetwork() diff --git a/plugins/sdnbridge/sdnbridge_windows_test.go b/plugins/sdnbridge/sdnbridge_windows_test.go index 0cdbc3b2..fb8b81b7 100644 --- a/plugins/sdnbridge/sdnbridge_windows_test.go +++ b/plugins/sdnbridge/sdnbridge_windows_test.go @@ -21,11 +21,11 @@ func CreateBridgeTestNetwork() *hcn.HostComputeNetwork { } func TestBridgeCmdAdd(t *testing.T) { - t.Skip("Bridge test is disabled for now.") + // t.Skip("Bridge test is disabled for now.") testDualStack = (os.Getenv("TestDualStack") == "1") imageToUse = os.Getenv("ImageToUse") testNetwork := CreateBridgeTestNetwork() - pt := util.MakeTestStruct(t, testNetwork, "sdnbridge", true, true, "", testDualStack, imageToUse) + pt := util.MakeTestStruct(t, testNetwork, "L2Bridge", 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 a98c783b..eb5ab374 100644 --- a/plugins/sdnoverlay/sdnoverlay_windows_test.go +++ b/plugins/sdnoverlay/sdnoverlay_windows_test.go @@ -35,10 +35,10 @@ func CreateOverlayTestNetwork() *hcn.HostComputeNetwork { } func TestOverlayCmdAdd(t *testing.T) { - t.Skip("Overlay test is disabled for now.") + // t.Skip("Overlay test is disabled for now.") testDualStack = (os.Getenv("TestDualStack") == "1") imageToUse = os.Getenv("ImageToUse") testNetwork := CreateOverlayTestNetwork() - pt := util.MakeTestStruct(t, testNetwork, "sdnoverlay", true, false, "", testDualStack, imageToUse) + pt := util.MakeTestStruct(t, testNetwork, "Overlay", true, false, "", testDualStack, imageToUse) pt.RunAll(t) } diff --git a/test/container/container_windows.go b/test/container/container_windows.go index 7f463c01..478c1ee5 100644 --- a/test/container/container_windows.go +++ b/test/container/container_windows.go @@ -5,14 +5,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/Microsoft/go-winio/vhd" - "github.com/Microsoft/hcsshim" - runhcs "github.com/Microsoft/hcsshim/pkg/go-runhcs" - "github.com/Microsoft/hcsshim/test/functional/utilities" - runc "github.com/containerd/go-runc" - "github.com/opencontainers/runtime-tools/generate" - "github.com/pkg/errors" - "golang.org/x/sync/errgroup" "io" "io/ioutil" "os" @@ -21,6 +13,15 @@ import ( "strconv" "strings" "testing" + + "github.com/Microsoft/go-winio/vhd" + "github.com/Microsoft/hcsshim" + runhcs "github.com/Microsoft/hcsshim/pkg/go-runhcs" + testutilities "github.com/Microsoft/hcsshim/test/functional/utilities" + runc "github.com/containerd/go-runc" + "github.com/opencontainers/runtime-tools/generate" + "github.com/pkg/errors" + "golang.org/x/sync/errgroup" ) const ( @@ -32,7 +33,6 @@ const ( func PingTest(c hcsshim.Container, ip string, ipv6 bool) error { var pingCommand string - if !ipv6 { pingCommand = fmt.Sprintf("ping -w 8000 -n 4 %s", ip) } else { diff --git a/test/utilities/connectivity_testing.go b/test/utilities/connectivity_testing.go index 1af67b51..612711a4 100644 --- a/test/utilities/connectivity_testing.go +++ b/test/utilities/connectivity_testing.go @@ -25,7 +25,7 @@ const ( func getDefaultDns() *cniTypes.DNS { defaultDns := cniTypes.DNS{ // Nameservers: []string{"8.8.8.8", "11.0.0.10"}, - Nameservers: []string{"10.50.10.50"}, + Nameservers: []string{"10.50.10.50", "8.8.8.8"}, Search: []string{"svc.cluster.local", "svc.cluster.local"}, } return &defaultDns diff --git a/test/utilities/container_testing.go b/test/utilities/container_testing.go index a9310758..cdb6ad11 100644 --- a/test/utilities/container_testing.go +++ b/test/utilities/container_testing.go @@ -5,7 +5,7 @@ import ( "github.com/Microsoft/hcsshim" "github.com/Microsoft/hcsshim/hcn" - "github.com/Microsoft/windows-container-networking/test/container" + contTest "github.com/Microsoft/windows-container-networking/test/container" ) const ( diff --git a/test/utilities/plugin_testing.go b/test/utilities/plugin_testing.go index 29a8ea57..9599f59e 100644 --- a/test/utilities/plugin_testing.go +++ b/test/utilities/plugin_testing.go @@ -4,12 +4,13 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/Microsoft/hcsshim/hcn" - "github.com/Microsoft/windows-container-networking/cni" - cniSkel "github.com/containernetworking/cni/pkg/skel" "net" "strings" "testing" + + "github.com/Microsoft/hcsshim/hcn" + "github.com/Microsoft/windows-container-networking/cni" + cniSkel "github.com/containernetworking/cni/pkg/skel" ) type PluginUnitTest struct { @@ -178,10 +179,14 @@ func (pt *PluginUnitTest) verifyAddEndpointProperties(t *testing.T, ci *Containe } func (pt *PluginUnitTest) verifyAddNamespaceProperties(t *testing.T, ci *ContainerInfo) { - EpNamespace := string(ci.Namespace.Resources[0].Data) - if !strings.Contains(EpNamespace, strings.ToUpper(ci.Endpoint.Id)) { - t.Errorf("Namespace does not contain a reference to endpoint.") + if len(ci.Namespace.Resources) > 0 { + EpNamespace := string(ci.Namespace.Resources[0].Data) + if strings.Contains(EpNamespace, strings.ToUpper(ci.Endpoint.Id)) { + return + } } + + t.Errorf("Namespace does not contain a reference to endpoint.") } func (pt *PluginUnitTest) verifyDelNamespaceProperties(t *testing.T, ci *ContainerInfo) { @@ -292,9 +297,11 @@ func (pt *PluginUnitTest) RunBasicConnectivityTest(t *testing.T, numContainers i var err error if !pt.DualStack { - err = ctList[0].RunContainerConnectivityTest( - t, pt.HostIp.String(), ctx.Endpoint.IpConfigurations[0].IpAddress, - false, "", "", "") + if ctx.Endpoint != nil { + err = ctList[0].RunContainerConnectivityTest( + t, pt.HostIp.String(), ctx.Endpoint.IpConfigurations[0].IpAddress, + false, "", "", "") + } } else { var ipv4addr string var ipv6addr string @@ -331,6 +338,14 @@ func (pt *PluginUnitTest) RunBasicConnectivityTest(t *testing.T, numContainers i } func (pt *PluginUnitTest) RunAll(t *testing.T) { + if err := pt.Setup(t); err != nil { + t.Errorf("Failed to set up test case for %v: %s", pt.CniCmdArgs, err) + } + defer func() { + if err := pt.Teardown(t); err != nil { + t.Logf("WARN: failed to tear down test case for %v: %s", pt.CniCmdArgs, err) + } + }() pt.RunUnitTest(t) pt.RunBasicConnectivityTest(t, 2) } diff --git a/test/utilities/testing_windows.go b/test/utilities/testing_windows.go index 85e2bae5..94148b99 100644 --- a/test/utilities/testing_windows.go +++ b/test/utilities/testing_windows.go @@ -4,15 +4,16 @@ import ( "encoding/json" "errors" "fmt" + "net" + "github.com/Microsoft/hcsshim/hcn" "github.com/Microsoft/windows-container-networking/cni" "github.com/Microsoft/windows-container-networking/common" "github.com/Microsoft/windows-container-networking/common/core" cniSkel "github.com/containernetworking/cni/pkg/skel" - "net" ) -const Interface = "Ethernet" +var Interface string func GetDefaultInterface(getipv6 bool) (*net.Interface, *net.IP, *net.IP, error) { var foundv4 bool @@ -23,41 +24,44 @@ func GetDefaultInterface(getipv6 bool) (*net.Interface, *net.IP, *net.IP, error) foundInterface := net.Interface{} ifaces, _ := net.Interfaces() - for _, i := range ifaces { - if i.Name == Interface { - foundInterface = i - foundv4 = false - foundv6 = false - - addrs, _ := i.Addrs() - for _, addr := range addrs { - ipTemp, _, _ := net.ParseCIDR(addr.String()) - if ipTemp.To4() != nil { - - if !foundv4 { - foundIp = &ipTemp - foundv4 = true - } - - } else { - if getipv6 && - !foundv6 && - !ipTemp.IsLinkLocalUnicast() && - !ipTemp.IsLoopback() { - - foundIpv6 = &ipTemp - foundv6 = true - } - } - - if foundv4 && foundv6 { - break - } + + if len(ifaces) == 0 { + return nil, nil, nil, fmt.Errorf("failed to find any network interfaces, unable to proceed with tests") + } + + i := ifaces[0] // use highest priority network + foundInterface = i + Interface = i.Name + foundv4 = false + foundv6 = false + + addrs, _ := i.Addrs() + for _, addr := range addrs { + ipTemp, _, _ := net.ParseCIDR(addr.String()) + if ipTemp.To4() != nil { + + if !foundv4 { + foundIp = &ipTemp + foundv4 = true } + + } else { + if getipv6 && + !foundv6 && + !ipTemp.IsLinkLocalUnicast() && + !ipTemp.IsLoopback() { + + foundIpv6 = &ipTemp + foundv6 = true + } + } + + if foundv4 && foundv6 { + break } } if foundIp == nil { - return nil, nil, nil, fmt.Errorf("Failed to find interface %s, unable to proceed with tests", Interface) + return nil, nil, nil, fmt.Errorf("failed to find interface %s, unable to proceed with tests", Interface) } return &foundInterface, foundIp, foundIpv6, nil }