Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose issue66 via unit test #84

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion pkg/controller/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

cni100 "github.com/containernetworking/cni/pkg/types/100"
Expand Down Expand Up @@ -202,10 +202,72 @@ var _ = Describe("Dynamic Attachment controller", func() {
}).Should(BeEmpty())
})
})

When("an update to an existing attachment occurs", func() {
DescribeTable("nothing happens", func(updatedNetworkSelectionElement nad.NetworkSelectionElement) {
Expect(updatePodNetworkSelectionElements(k8sClient, pod, updatedNetworkSelectionElement)).To(Succeed())

_, err := k8sClient.CoreV1().Pods(namespace).UpdateStatus(
context.TODO(),
pod,
metav1.UpdateOptions{})
Expect(err).NotTo(HaveOccurred())

Consistently(func() ([]nad.NetworkStatus, error) {
updatedPod, err := k8sClient.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
return nil, err
}
status, err := networkStatus(updatedPod.Annotations)
if err != nil {
return nil, err
}
return status, nil
}).Should(ConsistOf(ifaceStatus(namespace, networkName, "net0", "")))
},
Entry("when the MAC address is updated", nad.NetworkSelectionElement{
Name: networkName,
Namespace: namespace,
InterfaceRequest: "net0",
MacRequest: "07:06:05:04:03:02",
}),
XEntry("when the iface name is updated", nad.NetworkSelectionElement{
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong; the original attachment does feature the interface name.

We need to somehow have a pod with a provisioned attachment that does not specify the interface name on the network selection elements.

Name: networkName,
Namespace: namespace,
InterfaceRequest: "newiface",
}, Label("issue#66")),
)
})
})
})
})

func updatePodNetworkSelectionElements(k8sClient k8sclient.Interface, pod *corev1.Pod, newAttachments ...nad.NetworkSelectionElement) error {
currentPodAttachments, err := networkSelectionElements(pod.Annotations, pod.GetNamespace())
if err != nil {
return err
} else if len(currentPodAttachments) == 0 {
return fmt.Errorf("no attachments ")
}

for i := range currentPodAttachments[1:] {
newAttachments = append(newAttachments, *currentPodAttachments[i])
}

serializedAttachments, err := json.Marshal(newAttachments)
if err != nil {
return err
}

pod.Annotations[nad.NetworkAttachmentAnnot] = string(serializedAttachments)
_, err = k8sClient.CoreV1().Pods(pod.GetNamespace()).UpdateStatus(
context.TODO(),
pod,
metav1.UpdateOptions{})

return err
}

func networkConfig(cmd, ifaceName, mac string) fakemultusclient.NetworkConfig {
const cniVersion = "1.0.0"
return fakemultusclient.NetworkConfig{
Expand Down