Skip to content

Commit

Permalink
Add unit tests for GetManifestObjects
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <[email protected]>
  • Loading branch information
killianmuldoon committed Feb 16, 2024
1 parent fd7a346 commit 1906c76
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/state/state_nic_feature_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
112 changes: 112 additions & 0 deletions pkg/state/state_nic_feature_discovery_test.go
Original file line number Diff line number Diff line change
@@ -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.

Check failure on line 70 in pkg/state/state_nic_feature_discovery_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ineffectual assignment to err (ineffassign)
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"}},
},
}
92 changes: 92 additions & 0 deletions pkg/state/state_nv_ipam_cni_test.go
Original file line number Diff line number Diff line change
@@ -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.

Check failure on line 71 in pkg/state/state_nv_ipam_cni_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ineffectual assignment to err (ineffassign)
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,
},
))
}
}

})
}
}

0 comments on commit 1906c76

Please sign in to comment.