Skip to content

Commit

Permalink
chore: add tests to ipoib network state
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Rolland <frolland@nvidia.com>
  • Loading branch information
rollandf committed Feb 26, 2024
1 parent 342bd59 commit 8771f7f
Showing 1 changed file with 139 additions and 32 deletions.
171 changes: 139 additions & 32 deletions pkg/state/state_ipoib_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,166 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package state
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"
. "github.com/onsi/gomega"

mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1"
"github.com/Mellanox/network-operator/pkg/render"
"github.com/Mellanox/network-operator/pkg/testing/mocks"
"github.com/Mellanox/network-operator/pkg/utils"
"github.com/Mellanox/network-operator/pkg/state"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var _ = Describe("IPoIBNetwork Network state rendering tests", func() {

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

BeforeEach(func() {
scheme := runtime.NewScheme()
Expect(mellanoxv1alpha1.AddToScheme(scheme)).NotTo(HaveOccurred())
Expect(netattdefv1.AddToScheme(scheme)).NotTo(HaveOccurred())
client = fake.NewClientBuilder().WithScheme(scheme).Build()
manifestDir := "../../manifests/state-ipoib-network"
s, err := state.NewStateIPoIBNetwork(client, manifestDir)
Expect(err).NotTo(HaveOccurred())
ipoibState = s
catalog = getTestCatalog()
})

Context("IPoIBNetwork Network state", func() {
It("Should Render NetworkAttachmentDefinition", func() {
client := mocks.ControllerRuntimeClient{}
manifestBaseDir := "../../manifests/state-ipoib-network"

files, err := utils.GetFilesWithSuffix(manifestBaseDir, render.ManifestFileSuffix...)
name := "ipoib"
cr := getIPoIBNetwork()
err := client.Create(context.Background(), cr)
Expect(err).NotTo(HaveOccurred())
renderer := render.NewRenderer(files)
status, err := ipoibState.Sync(context.Background(), cr, catalog)
Expect(err).NotTo(HaveOccurred())
Expect(status).To(BeEquivalentTo(state.SyncStateReady))

stateName := "state-ipoib-network"
ipoibState := stateIPoIBNetwork{
stateSkel: stateSkel{
name: stateName,
description: "IPoIBNetwork net-attach-def CR deployed in cluster",
client: &client,
renderer: renderer,
},
}
By("Verify NetworkAttachmentDefinition")
nad := &netattdefv1.NetworkAttachmentDefinition{}
err = client.Get(context.Background(), types.NamespacedName{Namespace: testNamespace, Name: name}, nad)
Expect(err).NotTo(HaveOccurred())
cfg, err := getNADConfig(nad.Spec.Config)
Expect(err).NotTo(HaveOccurred())
Expect(cfg.Type).To(Equal("ipoib"))
expectedNad := getExpectedIPoIBNAD(name, "{}")
Expect(nad.Spec).To(BeEquivalentTo(expectedNad.Spec))
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal(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()
cr.Spec.IPAM = 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, err := getNADConfig(nad.Spec.Config)
Expect(err).NotTo(HaveOccurred())
Expect(ipoibState.Name()).To(Equal(stateName))
Expect(cfg.Type).To(Equal("ipoib"))
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal(testNamespace))
expectedNad := getExpectedIPoIBNAD(name, ipam)
Expect(nad.Spec).To(BeEquivalentTo(expectedNad.Spec))
})
It("Should Render NetworkAttachmentDefinition with default namespace", func() {
name := "ipoib"
cr := getIPoIBNetwork()
cr.Spec.NetworkNamespace = ""
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))

namespace := "namespace"
name := "ibs3"
ipam := "fakeIPAM"
spec := &mellanoxv1alpha1.IPoIBNetworkSpec{}
spec.NetworkNamespace = namespace
spec.Master = name
spec.IPAM = ipam
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"))
})
})
Context("Verify Sync flows", func() {
It("Should recreate NetworkAttachmentDefinition with different namespace", func() {
name := "ipoib"
cr := getIPoIBNetwork()
cr.Spec.NetworkNamespace = ""
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))

cr := &mellanoxv1alpha1.IPoIBNetwork{}
cr.Name = name
cr.Spec = *spec
objs, err := ipoibState.getManifestObjects(cr, testLogger)
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"))

By("Update network namespace")
cr = &mellanoxv1alpha1.IPoIBNetwork{}
err = client.Get(context.Background(), types.NamespacedName{Name: name}, cr)
Expect(err).NotTo(HaveOccurred())
cr.Spec.NetworkNamespace = testNamespace
err = client.Update(context.Background(), cr)
Expect(err).NotTo(HaveOccurred())
Expect(len(objs)).To(Equal(1))

checkRenderedNetAttachDef(objs[0], namespace, name, ipam)
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)
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, "{}")
Expect(nad.Spec).To(BeEquivalentTo(expectedNad.Spec))
Expect(nad.Name).To(Equal(name))
Expect(nad.Namespace).To(Equal(testNamespace))
})
})
})

func getExpectedIPoIBNAD(testName, 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() *mellanoxv1alpha1.IPoIBNetwork {
cr := &mellanoxv1alpha1.IPoIBNetwork{
Spec: mellanoxv1alpha1.IPoIBNetworkSpec{
NetworkNamespace: testNamespace,
Master: testMaster,
},
}
cr.Name = "ipoib"
return cr
}

0 comments on commit 8771f7f

Please sign in to comment.