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 19, 2024
1 parent f981a63 commit f81789f
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 32 deletions.
27 changes: 27 additions & 0 deletions pkg/state/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package state_test

import (
"encoding/json"

clustertype_mocks "github.com/Mellanox/network-operator/pkg/clustertype/mocks"
"github.com/Mellanox/network-operator/pkg/state"
"github.com/Mellanox/network-operator/pkg/staticconfig"
Expand All @@ -33,3 +35,28 @@ func getTestCatalog() state.InfoCatalog {
catalog.Add(state.InfoTypeClusterType, &clusterTypeProvider)
return catalog
}

type ipam struct {
Type string `json:"type"`
Range string `json:"range"`
Exclude []string `json:"exclude"`
}

type nadConfig struct {
CNIVersion string `json:"cniVersion"`
Name string `json:"name"`
Type string `json:"type"`
Master string `json:"master"`
Mode string `json:"mode"`
MTU int `json:"mtu"`
IPAM ipam `json:"ipam"`
}

func getNADConfig(jsonData string) (*nadConfig, error) {
config := &nadConfig{}
err := json.Unmarshal([]byte(jsonData), &config)
if err != nil {
return nil, err
}
return config, nil
}
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 f81789f

Please sign in to comment.