Skip to content

Commit

Permalink
Tidy up testing
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Hallisey <[email protected]>
  • Loading branch information
rthallisey committed Feb 4, 2021
1 parent 7ef2736 commit 0b37e41
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 62 deletions.
53 changes: 26 additions & 27 deletions pkg/virt-launcher/virtwrap/network/podinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ func (b *BridgeBindMechanism) preparePodNetworkInterfaces(queueNumber uint32, la
return err
}

tapDeviceName := generateTapDeviceName(b.podInterfaceName)

if !b.vif.IPAMDisabled {
// Remove IP from POD interface
err := Handler.AddrDel(b.podNicLink, &b.vif.IP)
Expand All @@ -482,7 +484,6 @@ func (b *BridgeBindMechanism) preparePodNetworkInterfaces(queueNumber uint32, la
return err
}

tapDeviceName := generateTapDeviceName(b.podInterfaceName)
err := createAndBindTapToBridge(tapDeviceName, b.bridgeInterfaceName, queueNumber, launcherPID, int(b.vif.Mtu))
if err != nil {
log.Log.Reason(err).Errorf("failed to create tap device named %s", tapDeviceName)
Expand Down Expand Up @@ -642,41 +643,39 @@ func (b *BridgeBindMechanism) switchPodInterfaceWithDummy() error {
newPodInterfaceName := fmt.Sprintf("%s-nic", originalPodInterfaceName)
dummy := &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: originalPodInterfaceName}}

if b.podNicLink.Type() != "dummy" {
// Set arp_ignore=1 on the bridge interface to avoid
// the interface being seen by Duplicate Address Detection (DAD).
// Without this, some VMs will lose their ip address after a few
// minutes.
b.arpIgnore = true
// Set arp_ignore=1 on the bridge interface to avoid
// the interface being seen by Duplicate Address Detection (DAD).
// Without this, some VMs will lose their ip address after a few
// minutes.
b.arpIgnore = true

// Rename pod interface to free the original name for a new dummy interface
err := Handler.LinkSetName(b.podNicLink, newPodInterfaceName)
if err != nil {
log.Log.Reason(err).Errorf("failed to rename interface : %s", b.podInterfaceName)
return err
}
// Rename pod interface to free the original name for a new dummy interface
err := Handler.LinkSetName(b.podNicLink, newPodInterfaceName)
if err != nil {
log.Log.Reason(err).Errorf("failed to rename interface : %s", b.podInterfaceName)
return err
}

b.podInterfaceName = newPodInterfaceName
b.podNicLink, err = Handler.LinkByName(newPodInterfaceName)
if err != nil {
log.Log.Reason(err).Errorf("failed to get a link for interface: %s", b.podInterfaceName)
return err
}
b.podInterfaceName = newPodInterfaceName
b.podNicLink, err = Handler.LinkByName(newPodInterfaceName)
if err != nil {
log.Log.Reason(err).Errorf("failed to get a link for interface: %s", b.podInterfaceName)
return err
}

// Create a dummy interface named after the original interface
err = Handler.LinkAdd(dummy)
if err != nil {
log.Log.Reason(err).Errorf("failed to create dummy interface : %s", newPodInterfaceName)
return err
}
// Create a dummy interface named after the original interface
err = Handler.LinkAdd(dummy)
if err != nil {
log.Log.Reason(err).Errorf("failed to create dummy interface : %s", originalPodInterfaceName)
return err
}

// Replace original pod interface IP address to the dummy
// Since the dummy is not connected to anything, it should not affect networking
// Replace will add if ip doesn't exist or modify the ip
err := Handler.AddrReplace(dummy, &b.vif.IP)
err = Handler.AddrReplace(dummy, &b.vif.IP)
if err != nil {
log.Log.Reason(err).Errorf("failed to replace original IP address to dummy interface: %s", newPodInterfaceName)
log.Log.Reason(err).Errorf("failed to replace original IP address to dummy interface: %s", originalPodInterfaceName)
return err
}

Expand Down
71 changes: 37 additions & 34 deletions pkg/virt-launcher/virtwrap/network/podinterface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ import (
var _ = Describe("Pod Network", func() {
var mockNetwork *MockNetworkHandler
var ctrl *gomock.Controller
var dummy *netlink.Dummy
var dummySwap *netlink.Dummy
var primaryPodInterface *netlink.GenericLink
var primaryPodInterfaceAfterNameChange *netlink.GenericLink
var addrList []netlink.Addr
var newPodInterfaceName string
var routeList []netlink.Route
Expand Down Expand Up @@ -92,9 +93,10 @@ var _ = Describe("Pod Network", func() {
testMac := "12:34:56:78:9A:BC"
updateTestMac := "AF:B3:1F:78:2A:CA"
mtu = 1410
dummySwap = &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: primaryPodInterfaceName}}
dummy = &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Index: 1, MTU: mtu}}
newPodInterfaceName = fmt.Sprintf("%s-nic", primaryPodInterfaceName)
dummySwap = &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: primaryPodInterfaceName}}
primaryPodInterface = &netlink.GenericLink{LinkAttrs: netlink.LinkAttrs{Name: primaryPodInterfaceName, MTU: mtu}}
primaryPodInterfaceAfterNameChange = &netlink.GenericLink{LinkAttrs: netlink.LinkAttrs{Name: newPodInterfaceName}}
address := &net.IPNet{IP: net.IPv4(10, 35, 0, 6), Mask: net.CIDRMask(24, 32)}
gw := net.IPv4(10, 35, 0, 1)
fakeMac, _ = net.ParseMAC(testMac)
Expand Down Expand Up @@ -183,33 +185,35 @@ var _ = Describe("Pod Network", func() {
TestPodInterfaceIPBinding := func(vm *v1.VirtualMachineInstance, domain *api.Domain) {

//For Bridge tests
mockNetwork.EXPECT().LinkSetName(dummySwap, newPodInterfaceName).Return(nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummy, nil).Times(2)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummySwap, nil)
mockNetwork.EXPECT().LinkSetName(primaryPodInterface, newPodInterfaceName).Return(nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(primaryPodInterface, nil)
mockNetwork.EXPECT().LinkByName(newPodInterfaceName).Return(primaryPodInterfaceAfterNameChange, nil)
mockNetwork.EXPECT().LinkAdd(dummySwap).Return(nil)
mockNetwork.EXPECT().AddrReplace(dummySwap, &fakeAddr).Return(nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_V4).Return(addrList, nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().RouteList(dummy, netlink.FAMILY_V4).Return(routeList, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_V4).Return(addrList, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().RouteList(primaryPodInterface, netlink.FAMILY_V4).Return(routeList, nil)
mockNetwork.EXPECT().GetMacDetails(primaryPodInterfaceName).Return(fakeMac, nil)
mockNetwork.EXPECT().AddrDel(dummy, &fakeAddr).Return(nil)
mockNetwork.EXPECT().LinkSetDown(dummy).Return(nil)
mockNetwork.EXPECT().SetRandomMac(primaryPodInterfaceName).Return(updateFakeMac, nil)
mockNetwork.EXPECT().LinkSetUp(dummy).Return(nil)
mockNetwork.EXPECT().LinkSetLearningOff(dummy).Return(nil)
mockNetwork.EXPECT().AddrDel(primaryPodInterface, &fakeAddr).Return(nil)
mockNetwork.EXPECT().LinkSetDown(primaryPodInterface).Return(nil)
mockNetwork.EXPECT().SetRandomMac(newPodInterfaceName).Return(updateFakeMac, nil)
mockNetwork.EXPECT().LinkSetUp(primaryPodInterfaceAfterNameChange).Return(nil)
mockNetwork.EXPECT().LinkSetLearningOff(primaryPodInterfaceAfterNameChange).Return(nil)
mockNetwork.EXPECT().LinkAdd(bridgeTest).Return(nil)
mockNetwork.EXPECT().LinkByName(api.DefaultBridgeName).Return(bridgeTest, nil)
mockNetwork.EXPECT().LinkSetUp(bridgeTest).Return(nil)
mockNetwork.EXPECT().ParseAddr(fmt.Sprintf(bridgeFakeIP, 0)).Return(bridgeAddr, nil)
mockNetwork.EXPECT().LinkSetMaster(dummy, bridgeTest).Return(nil)
mockNetwork.EXPECT().LinkSetMaster(primaryPodInterfaceAfterNameChange, bridgeTest).Return(nil)
mockNetwork.EXPECT().AddrAdd(bridgeTest, bridgeAddr).Return(nil)
mockNetwork.EXPECT().StartDHCP(testNic, bridgeAddr, api.DefaultBridgeName, nil, true)
mockNetwork.EXPECT().CreateTapDevice(tapDeviceName, queueNumber, pid, mtu).Return(nil)
mockNetwork.EXPECT().BindTapDeviceToBridge(tapDeviceName, "k6t-eth0").Return(nil)
mockNetwork.EXPECT().DisableTXOffloadChecksum(bridgeTest.Name).Return(nil)
mockNetwork.EXPECT().ConfigureIpv4ArpIgnore().Return(nil)

// For masquerade tests
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummy, nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(primaryPodInterface, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().ParseAddr(masqueradeGwStr).Return(masqueradeGwAddr, nil)
mockNetwork.EXPECT().ParseAddr(masqueradeIpv6GwStr).Return(masqueradeIpv6GwAddr, nil)
mockNetwork.EXPECT().ParseAddr(masqueradeVmStr).Return(masqueradeVmAddr, nil)
Expand Down Expand Up @@ -314,22 +318,22 @@ var _ = Describe("Pod Network", func() {

api.NewDefaulter(runtime.GOARCH).SetObjectDefaults_Domain(domain)

mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummy, nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummy, nil)
mockNetwork.EXPECT().LinkSetDown(dummy).Return(nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(primaryPodInterface, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(primaryPodInterface, nil)
mockNetwork.EXPECT().LinkSetDown(primaryPodInterface).Return(nil)
mockNetwork.EXPECT().SetRandomMac(primaryPodInterfaceName).Return(updateFakeMac, nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_V4).Return(addrList, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_V4).Return(addrList, nil)
mockNetwork.EXPECT().LinkAdd(bridgeTest).Return(nil)
mockNetwork.EXPECT().LinkByName(api.DefaultBridgeName).Return(bridgeTest, nil)
mockNetwork.EXPECT().LinkSetUp(bridgeTest).Return(nil)
mockNetwork.EXPECT().LinkSetUp(dummy).Return(nil)
mockNetwork.EXPECT().LinkSetUp(primaryPodInterface).Return(nil)
mockNetwork.EXPECT().ParseAddr(fmt.Sprintf(bridgeFakeIP, 0)).Return(bridgeAddr, nil)
mockNetwork.EXPECT().AddrAdd(bridgeTest, bridgeAddr).Return(nil)
mockNetwork.EXPECT().RouteList(dummy, netlink.FAMILY_V4).Return(routeList, nil)
mockNetwork.EXPECT().RouteList(primaryPodInterface, netlink.FAMILY_V4).Return(routeList, nil)
mockNetwork.EXPECT().GetMacDetails(primaryPodInterfaceName).Return(fakeMac, nil)
mockNetwork.EXPECT().LinkSetMaster(dummy, bridgeTest).Return(nil)
mockNetwork.EXPECT().AddrDel(dummy, &fakeAddr).Return(errors.New("device is busy"))
mockNetwork.EXPECT().LinkSetMaster(primaryPodInterface, bridgeTest).Return(nil)
mockNetwork.EXPECT().AddrDel(primaryPodInterface, &fakeAddr).Return(errors.New("device is busy"))
mockNetwork.EXPECT().CreateTapDevice(tapDeviceName, queueNumber, pid, mtu).Return(nil)
mockNetwork.EXPECT().BindTapDeviceToBridge(tapDeviceName, "k6t-eth0").Return(nil)
mockNetwork.EXPECT().DisableTXOffloadChecksum(bridgeTest.Name).Return(nil)
Expand All @@ -342,16 +346,15 @@ var _ = Describe("Pod Network", func() {
Expect(ok).To(BeTrue(), "SetupPodNetworkPhase1 should return an error of type CriticalNetworkError")
})
It("should return an error if the MTU is out or range", func() {
dummy = &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Index: 1, MTU: 65536}}

primaryPodInterface = &netlink.GenericLink{LinkAttrs: netlink.LinkAttrs{Index: 1, MTU: 65536}}
domain := NewDomainWithBridgeInterface()
vm := newVMIBridgeInterface("testnamespace", "testVmName")

api.NewDefaulter(runtime.GOARCH).SetObjectDefaults_Domain(domain)

mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummy, nil).Times(2)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_V4).Return(addrList, nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(primaryPodInterface, nil).Times(2)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_V4).Return(addrList, nil)
mockNetwork.EXPECT().GetMacDetails(primaryPodInterfaceName).Return(fakeMac, nil)
mockNetwork.EXPECT().IsIpv4Primary().Return(true, nil).Times(1)

Expand Down Expand Up @@ -596,7 +599,7 @@ var _ = Describe("Pod Network", func() {

driver, err := getPhase2Binding(vmi, &vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, ifaceName)
mockNetwork.EXPECT().GetMacDetails(ifaceName).Return(fakeMac, nil)
mockNetwork.EXPECT().LinkByName(ifaceName).Return(dummy, nil)
mockNetwork.EXPECT().LinkByName(ifaceName).Return(primaryPodInterface, nil)
Expect(err).ToNot(HaveOccurred(), "should have identified the correct binding mechanism")
TestRunPlug(driver)
Expect(len(domain.Spec.Devices.Interfaces)).To(Equal(1), "should have a single interface")
Expand Down Expand Up @@ -807,8 +810,8 @@ var _ = Describe("Pod Network", func() {
Expect(err).ToNot(HaveOccurred())

iface := &v1.Interface{Name: "default", InterfaceBindingMethod: v1.InterfaceBindingMethod{Masquerade: &v1.InterfaceMasquerade{}}}
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(dummy, nil)
mockNetwork.EXPECT().AddrList(dummy, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().LinkByName(primaryPodInterfaceName).Return(primaryPodInterface, nil)
mockNetwork.EXPECT().AddrList(primaryPodInterface, netlink.FAMILY_ALL).Return(addrList, nil)
mockNetwork.EXPECT().IsIpv4Primary().Return(true, nil).Times(1)

err = setPodInterfaceCache(iface, primaryPodInterfaceName, uid)
Expand Down
1 change: 1 addition & 0 deletions tests/vmi_cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ var _ = Describe("[rfe_id:151][crit:high][vendor:[email protected]][level:compon

By("applying the hostname from meta-data")
Expect(libnet.WithIPv6(console.LoginToCirros)(vmi)).To(Succeed())

Expect(console.SafeExpectBatch(vmi, []expect.Batcher{
&expect.BSnd{S: "hostname\n"},
&expect.BExp{R: dns.SanitizeHostname(vmi)},
Expand Down
2 changes: 1 addition & 1 deletion tests/vmi_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ var _ = Describe("[rfe_id:273][crit:high][vendor:[email protected]][level:compon
By("restarting kubelet")
pod := renderPkillAllPod("kubelet")
pod.Spec.NodeName = nodeName
_, err = virtClient.CoreV1().Pods(tests.NamespaceTestDefault).Create(pod)
_, err = virtClient.CoreV1().Pods(tests.NamespaceTestDefault).Create(context.Background(), pod, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())

By("starting another VMI on the same node, to verify kubelet is running again")
Expand Down

0 comments on commit 0b37e41

Please sign in to comment.