Skip to content

Commit

Permalink
Fix flaky e2e test due to mac address re-used
Browse files Browse the repository at this point in the history
The mac addresses used during the e2e tests are now generated during the
tests instead of being hard-coded.
The hard-coded mac address was causing flacky tests probably due to the
kernel refusing a new macvlan interface to be created with an already
existing mac address.
  • Loading branch information
LionelJouin authored and maiqueb committed Jan 22, 2024
1 parent 29fe72a commit 4e491f1
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package e2e

import (
"crypto/rand"
"encoding/json"
"fmt"
"net"
"os"
"testing"
"time"
Expand Down Expand Up @@ -153,17 +155,22 @@ var _ = Describe("Multus dynamic networks controller", func() {
})

Context("a network with IPAM", func() {
var macAddress string

const (
ifaceToAddWithIPAM = "ens202"
ipAddressToAdd = "10.10.10.111"
ipamNetworkToAdd = "tenant-network-ipam"
netmaskLen = 24
)

macAddress := "02:03:04:05:06:07"

BeforeEach(func() {
_, err := clients.AddNetAttachDef(macvlanNetworkWitStaticIPAM(ipamNetworkToAdd, namespace, lowerDeviceName()))
var err error
hardwareAddr, err := generateMacAddress()
Expect(err).NotTo(HaveOccurred())
macAddress = hardwareAddr.String()

_, err = clients.AddNetAttachDef(macvlanNetworkWitStaticIPAM(ipamNetworkToAdd, namespace, lowerDeviceName()))
Expect(err).NotTo(HaveOccurred())
Expect(clients.AddNetworkToPod(pod, &nettypes.NetworkSelectionElement{
Name: ipamNetworkToAdd,
Expand Down Expand Up @@ -207,10 +214,17 @@ var _ = Describe("Multus dynamic networks controller", func() {
})

Context("a provisioned pod featuring *only* the cluster's default network", func() {
var pod *corev1.Pod
var (
pod *corev1.Pod
desiredMACAddr string
)

BeforeEach(func() {
var err error
hardwareAddr, err := generateMacAddress()
Expect(err).NotTo(HaveOccurred())
desiredMACAddr = hardwareAddr.String()

pod, err = clients.ProvisionPod(
podName,
namespace,
Expand All @@ -226,8 +240,7 @@ var _ = Describe("Multus dynamic networks controller", func() {

It("manages to add a new interface to a running pod", func() {
const (
desiredMACAddr = "02:03:04:05:06:07"
ifaceToAdd = "ens58"
ifaceToAdd = "ens58"
)

Expect(clients.AddNetworkToPod(pod, &nettypes.NetworkSelectionElement{
Expand All @@ -249,12 +262,12 @@ var _ = Describe("Multus dynamic networks controller", func() {
Context("a provisioned pod whose network selection elements do not feature the interface name", func() {
const (
ifaceToAdd = "ens58"
macAddress = "02:03:04:05:06:07"
)

var (
pod *corev1.Pod
initialPodsNetworkStatus []nettypes.NetworkStatus
macAddress string
)

runtimePodNetworkStatus := func() []nettypes.NetworkStatus {
Expand All @@ -270,6 +283,10 @@ var _ = Describe("Multus dynamic networks controller", func() {

BeforeEach(func() {
var err error
hardwareAddr, err := generateMacAddress()
Expect(err).NotTo(HaveOccurred())
macAddress = hardwareAddr.String()

pod, err = clients.ProvisionPod(
podName,
namespace,
Expand Down Expand Up @@ -326,6 +343,19 @@ var _ = Describe("Multus dynamic networks controller", func() {
})
})

// https://stackoverflow.com/questions/21018729/generate-mac-address-in-go
func generateMacAddress() (net.HardwareAddr, error) {
buf := make([]byte, 6)
_, err := rand.Read(buf)
if err != nil {
return nil, err
}

buf[0] = (buf[0] | 2) & 0xfe // Set local bit, ensure unicast addres

return buf, nil
}

func clusterConfig() (*rest.Config, error) {
const kubeconfig = "KUBECONFIG"

Expand Down

0 comments on commit 4e491f1

Please sign in to comment.