Skip to content

Commit

Permalink
chore: use new assert NAD func for ipoib unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Giese <[email protected]>
  • Loading branch information
tobiasgiese committed Jul 23, 2024
1 parent 9b63ba9 commit aa34560
Showing 1 changed file with 38 additions and 65 deletions.
103 changes: 38 additions & 65 deletions pkg/state/state_ipoib_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package state_test

import (
"context"
"fmt"

netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
. "github.com/onsi/ginkgo/v2"
Expand All @@ -37,12 +36,16 @@ import (

var _ = Describe("IPoIBNetwork Network state rendering tests", func() {
const (
testNamespace = "namespace"
testName = "ipo-ib"
testNamespace = "ipoib"
testType = "ipoib"
testMaster = "eth0"
)

var ipoibState state.State
var catalog state.InfoCatalog
var client client.Client
var expectedNadConfig nadConfig

BeforeEach(func() {
scheme := runtime.NewScheme()
Expand All @@ -54,90 +57,70 @@ var _ = Describe("IPoIBNetwork Network state rendering tests", func() {
Expect(err).NotTo(HaveOccurred())
ipoibState = s
catalog = getTestCatalog()
expectedNadConfig = defaultNADConfig(&nadConfig{
Name: testName,
Type: testType,
Master: testMaster,
IPAM: nadConfigIPAM{},
})
})

Context("IPoIBNetwork Network state", func() {
It("Should Render NetworkAttachmentDefinition", func() {
name := "ipoib"
cr := getIPoIBNetwork(testNamespace)
cr := getIPoIBNetwork(testName, testNamespace, testMaster)
err := client.Create(context.Background(), cr)
Expect(err).NotTo(HaveOccurred())
status, err := ipoibState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateReady))

By("Verify NetworkAttachmentDefinition")
nad := &netattdefv1.NetworkAttachmentDefinition{}
err = client.Get(context.Background(), types.NamespacedName{Namespace: testNamespace, Name: name}, nad)
Expect(err).NotTo(HaveOccurred())
cfg := getNADConfig(nad.Spec.Config)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Type).To(Equal("ipoib"))
expectedNad := getExpectedIPoIBNAD(name, cr.Spec.Master, "{}")
Expect(nad.Spec).To(BeEquivalentTo(expectedNad.Spec))
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal(testNamespace))
expectedNadConfig.IPAM = nadConfigIPAM{}
assertNetworkAttachmentDefinition(client, &expectedNadConfig, testName, testNamespace, "")
})
It("Should Render NetworkAttachmentDefinition with IPAM", func() {
ipam := `{"type":"whereabouts","range":"192.168.2.225/28","exclude":["192.168.2.229/30","192.168.2.236/32"]}`
name := "ipoib"
cr := getIPoIBNetwork(testNamespace)
cr.Spec.IPAM = ipam
ipam := nadConfigIPAM{
Type: "whereabouts",
Range: "192.168.2.225/28",
Exclude: []string{"192.168.2.229/30", "192.168.2.236/32"},
}
cr := getIPoIBNetwork(testName, testNamespace, testMaster)
cr.Spec.IPAM = getNADConfigIPAMJSON(ipam)
err := client.Create(context.Background(), cr)
Expect(err).NotTo(HaveOccurred())
status, err := ipoibState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateReady))

By("Verify NetworkAttachmentDefinition")
nad := &netattdefv1.NetworkAttachmentDefinition{}
err = client.Get(context.Background(), types.NamespacedName{Namespace: testNamespace, Name: name}, nad)
Expect(err).NotTo(HaveOccurred())
cfg := getNADConfig(nad.Spec.Config)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Type).To(Equal("ipoib"))
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal(testNamespace))
expectedNad := getExpectedIPoIBNAD(name, cr.Spec.Master, ipam)
Expect(nad.Spec).To(BeEquivalentTo(expectedNad.Spec))
expectedNadConfig.IPAM = ipam
assertNetworkAttachmentDefinition(client, &expectedNadConfig, testName, testNamespace, "")
})
It("Should Render NetworkAttachmentDefinition with default namespace", func() {
name := "ipoib"
cr := getIPoIBNetwork("")
cr := getIPoIBNetwork(testName, "", testMaster)
err := client.Create(context.Background(), cr)
Expect(err).NotTo(HaveOccurred())
status, err := ipoibState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateReady))

By("Verify NetworkAttachmentDefinition")
nad := &netattdefv1.NetworkAttachmentDefinition{}
err = client.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: name}, nad)
Expect(err).NotTo(HaveOccurred())
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal("default"))
expectedNadConfig.IPAM = nadConfigIPAM{}
assertNetworkAttachmentDefinition(client, &expectedNadConfig, testName, "default", "")
})
})
Context("Verify Sync flows", func() {
It("Should recreate NetworkAttachmentDefinition with different namespace", func() {
name := "ipoib"
cr := getIPoIBNetwork("")
cr := getIPoIBNetwork(testName, "", testMaster)
err := client.Create(context.Background(), cr)
Expect(err).NotTo(HaveOccurred())
status, err := ipoibState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateReady))

By("Verify NetworkAttachmentDefinition")
nad := &netattdefv1.NetworkAttachmentDefinition{}
err = client.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: name}, nad)
Expect(err).NotTo(HaveOccurred())
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal("default"))
expectedNadConfig.IPAM = nadConfigIPAM{}
assertNetworkAttachmentDefinition(client, &expectedNadConfig, testName, "default", "")

By("Update network namespace")
cr = &mellanoxv1alpha1.IPoIBNetwork{}
err = client.Get(context.Background(), types.NamespacedName{Name: name}, cr)
err = client.Get(context.Background(), types.NamespacedName{Name: testName}, cr)
Expect(err).NotTo(HaveOccurred())
cr.Spec.NetworkNamespace = testNamespace
err = client.Update(context.Background(), cr)
Expand All @@ -146,33 +129,23 @@ var _ = Describe("IPoIBNetwork Network state rendering tests", func() {
By("Sync")
_, err = ipoibState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
err = client.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: name}, nad)
nad := &netattdefv1.NetworkAttachmentDefinition{}
err = client.Get(context.Background(), types.NamespacedName{Namespace: "default", Name: testName}, nad)
Expect(errors.IsNotFound(err)).To(BeTrue())
err = client.Get(context.Background(), types.NamespacedName{Namespace: testNamespace, Name: name}, nad)
Expect(err).NotTo(HaveOccurred())
expectedNad := getExpectedIPoIBNAD(name, cr.Spec.Master, "{}")
Expect(nad.Spec).To(BeEquivalentTo(expectedNad.Spec))
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal(testNamespace))

assertNetworkAttachmentDefinition(client, &expectedNadConfig, testName, testNamespace, "")
})
})
})

func getExpectedIPoIBNAD(testName, testMaster, ipam string) *netattdefv1.NetworkAttachmentDefinition {
nad := &netattdefv1.NetworkAttachmentDefinition{}
cfg := fmt.Sprintf(`{ "cniVersion":"0.3.1", "name":%q, "type":"ipoib", "master": %q, "ipam":%s }`,
testName, testMaster, ipam)
nad.Spec.Config = cfg
return nad
}

func getIPoIBNetwork(testNamespace string) *mellanoxv1alpha1.IPoIBNetwork {
// nolint:unparam
func getIPoIBNetwork(testName, testNamespace, testMaster string) *mellanoxv1alpha1.IPoIBNetwork {
cr := &mellanoxv1alpha1.IPoIBNetwork{
Spec: mellanoxv1alpha1.IPoIBNetworkSpec{
NetworkNamespace: testNamespace,
Master: "eth0",
Master: testMaster,
},
}
cr.Name = "ipoib"
cr.Name = testName
return cr
}

0 comments on commit aa34560

Please sign in to comment.