From 1906c76687195ec30ad23cc4a02268dcce39014d Mon Sep 17 00:00:00 2001 From: killianmuldoon Date: Fri, 16 Feb 2024 16:43:40 +0000 Subject: [PATCH] Add unit tests for GetManifestObjects Signed-off-by: killianmuldoon --- .../030-nic-feature-discovery-ds.yaml | 6 +- pkg/state/state_nic_feature_discovery.go | 2 +- pkg/state/state_nic_feature_discovery_test.go | 112 ++++++++++++++++++ pkg/state/state_nv_ipam_cni_test.go | 92 ++++++++++++++ 4 files changed, 208 insertions(+), 4 deletions(-) create mode 100644 pkg/state/state_nic_feature_discovery_test.go create mode 100644 pkg/state/state_nv_ipam_cni_test.go diff --git a/manifests/state-nic-feature-discovery/030-nic-feature-discovery-ds.yaml b/manifests/state-nic-feature-discovery/030-nic-feature-discovery-ds.yaml index a6a6479b5..045e89d0e 100644 --- a/manifests/state-nic-feature-discovery/030-nic-feature-discovery-ds.yaml +++ b/manifests/state-nic-feature-discovery/030-nic-feature-discovery-ds.yaml @@ -39,9 +39,9 @@ spec: serviceAccountName: nic-feature-discovery {{- end }} tolerations: - {{- if .Tolerations }} - {{- .Tolerations | yaml | nindent 8 }} - {{- end }} + {{- if .Tolerations }} + {{- .Tolerations | yaml | nindent 8 }} + {{- end }} - key: nvidia.com/gpu operator: Exists effect: NoSchedule diff --git a/pkg/state/state_nic_feature_discovery.go b/pkg/state/state_nic_feature_discovery.go index 0fec967fe..f9c78beb4 100644 --- a/pkg/state/state_nic_feature_discovery.go +++ b/pkg/state/state_nic_feature_discovery.go @@ -129,7 +129,7 @@ func (s *stateNICFeatureDiscovery) Sync( return syncState, nil } -// Get a map of source kinds that should be watched for the state keyed by the source kind name +// GetWatchSources returns a map of source kinds that should be watched for the state keyed by the source kind name func (s *stateNICFeatureDiscovery) GetWatchSources() map[string]client.Object { wr := make(map[string]client.Object) wr["DaemonSet"] = &appsv1.DaemonSet{} diff --git a/pkg/state/state_nic_feature_discovery_test.go b/pkg/state/state_nic_feature_discovery_test.go new file mode 100644 index 000000000..b83afc0bc --- /dev/null +++ b/pkg/state/state_nic_feature_discovery_test.go @@ -0,0 +1,112 @@ +package state + +import ( + mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1" + "github.com/Mellanox/network-operator/pkg/render" + "github.com/Mellanox/network-operator/pkg/utils" + . "github.com/onsi/gomega" + "golang.org/x/net/context" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/log" + "testing" +) + +func Test_stateNICFeatureDiscovery_GetManifestObjects(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + logger := log.FromContext(ctx) + manifestDir := "../../manifests/state-nic-feature-discovery" + files, err := utils.GetFilesWithSuffix(manifestDir, render.ManifestFileSuffix...) + g.Expect(err).ToNot(HaveOccurred()) + renderer := render.NewRenderer(files) + cr := nicClusterPolicyWithBaseFields.DeepCopy() + cr.Spec.NicFeatureDiscovery = &mellanoxv1alpha1.NICFeatureDiscoverySpec{ + ImageSpec: mellanoxv1alpha1.ImageSpec{ + Image: "image-one", + Repository: "repository", + Version: "five", + ImagePullSecrets: []string{"secret-one", "secret-two"}, + ContainerResources: []mellanoxv1alpha1.ResourceRequirements{ + { + Name: "first-resource", + Limits: map[corev1.ResourceName]resource.Quantity{}, + Requests: map[corev1.ResourceName]resource.Quantity{ + "resource-one": {Format: "format-one"}}, + }, + }, + }, + } + + tests := []struct { + name string + catalog InfoCatalog + want []*unstructured.Unstructured + }{ + { + name: "With Tolerations, Node Affinity, ContainerResources and ImagePullSecrets", + }, + } + for _, tt := range tests { + client := fake.NewClientBuilder().Build() + t.Run(tt.name, func(t *testing.T) { + s := &stateNICFeatureDiscovery{ + stateSkel: stateSkel{ + name: "state-nic-feature-discovery", + description: "nic-feature-discovery deployed in the cluster", + client: client, + renderer: renderer, + }, + } + got, err := s.GetManifestObjects(ctx, cr, getDummyCatalog(), logger) + g.Expect(err).NotTo(HaveOccurred()) + for i := range got { + if got[i].GetKind() == "DaemonSet" { + ds := &appsv1.DaemonSet{} + err = runtime.DefaultUnstructuredConverter. + FromUnstructured(got[i].UnstructuredContent(), ds) + g.Expect(ds.Spec.Template.Spec.ImagePullSecrets).To(ConsistOf( + corev1.LocalObjectReference{Name: "secret-one"}, + corev1.LocalObjectReference{Name: "secret-two"}), + ) + g.Expect(ds.Spec.Template.Spec.Tolerations).To(ConsistOf( + corev1.Toleration{Key: "first-taint"}, + corev1.Toleration{ + Key: "nvidia.com/gpu", + Operator: "Exists", + Value: "", + Effect: "NoSchedule", + TolerationSeconds: nil, + }, + )) + } + } + + }) + } +} + +var nicClusterPolicyWithBaseFields = &mellanoxv1alpha1.NicClusterPolicy{ + Spec: mellanoxv1alpha1.NicClusterPolicySpec{ + NodeAffinity: &corev1.NodeAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{ + NodeSelectorTerms: []corev1.NodeSelectorTerm{ + { + MatchExpressions: []corev1.NodeSelectorRequirement{{ + Key: "node-label", + Operator: corev1.NodeSelectorOpIn, + Values: []string{"labels"}, + }, + }, + }, + }, + }, + PreferredDuringSchedulingIgnoredDuringExecution: nil, + }, + Tolerations: []corev1.Toleration{{Key: "first-taint"}}, + }, +} diff --git a/pkg/state/state_nv_ipam_cni_test.go b/pkg/state/state_nv_ipam_cni_test.go new file mode 100644 index 000000000..32e8ef767 --- /dev/null +++ b/pkg/state/state_nv_ipam_cni_test.go @@ -0,0 +1,92 @@ +package state + +import ( + "context" + mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1" + "github.com/Mellanox/network-operator/pkg/render" + "github.com/Mellanox/network-operator/pkg/utils" + . "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/log" + "testing" +) + +func Test_stateNVIPAMCNI_GetManifestObjects(t *testing.T) { + g := NewWithT(t) + ctx := context.Background() + logger := log.FromContext(ctx) + manifestDir := "../../manifests/state-nv-ipam-cni" + files, err := utils.GetFilesWithSuffix(manifestDir, render.ManifestFileSuffix...) + g.Expect(err).ToNot(HaveOccurred()) + renderer := render.NewRenderer(files) + cr := nicClusterPolicyWithBaseFields.DeepCopy() + cr.Spec.NvIpam = &mellanoxv1alpha1.NVIPAMSpec{ + EnableWebhook: true, + ImageSpec: mellanoxv1alpha1.ImageSpec{ + Image: "image-one", + Repository: "repository", + Version: "five", + ImagePullSecrets: []string{"secret-one", "secret-two"}, + ContainerResources: []mellanoxv1alpha1.ResourceRequirements{ + { + Name: "first-resource", + Limits: map[corev1.ResourceName]resource.Quantity{}, + Requests: map[corev1.ResourceName]resource.Quantity{ + "resource-one": {Format: "format-one"}}, + }, + }, + }, + } + + tests := []struct { + name string + catalog InfoCatalog + want []*unstructured.Unstructured + }{ + { + name: "With Tolerations, Node Affinity, ContainerResources and ImagePullSecrets", + }, + } + for _, tt := range tests { + client := fake.NewClientBuilder().Build() + t.Run(tt.name, func(t *testing.T) { + s := &stateNVIPAMCNI{ + stateSkel: stateSkel{ + name: "state-nic-feature-discovery", + description: "nic-feature-discovery deployed in the cluster", + client: client, + renderer: renderer, + }, + } + got, err := s.GetManifestObjects(ctx, cr, getDummyCatalog(), logger) + g.Expect(err).NotTo(HaveOccurred()) + for i := range got { + if got[i].GetKind() == "DaemonSet" { + ds := &appsv1.DaemonSet{} + err = runtime.DefaultUnstructuredConverter. + FromUnstructured(got[i].UnstructuredContent(), ds) + g.Expect(ds.Spec.Template.Spec.ImagePullSecrets).To(ConsistOf( + corev1.LocalObjectReference{Name: "secret-one"}, + corev1.LocalObjectReference{Name: "secret-two"}), + ) + g.Expect(ds.Spec.Template.Spec.Tolerations).To(ConsistOf( + corev1.Toleration{Key: "first-taint"}, + corev1.Toleration{ + Key: "nvidia.com/gpu", + Operator: "Exists", + Value: "", + Effect: "NoSchedule", + TolerationSeconds: nil, + }, + )) + } + } + + }) + } +}