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 5eba662
Show file tree
Hide file tree
Showing 4 changed files with 192 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
116 changes: 116 additions & 0 deletions pkg/state/state_nic_feature_discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
2024 NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package state

import (
"testing"

appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"

. "github.com/onsi/gomega"
"golang.org/x/net/context"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/log"

mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1"
"github.com/Mellanox/network-operator/pkg/render"
"github.com/Mellanox/network-operator/pkg/utils"
)

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)
skel := stateSkel{client: fake.NewClientBuilder().Build(), renderer: renderer}

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"}},
},
},
},
}
t.Run("test GetObjectManifests", func(t *testing.T) {
s := &stateNICFeatureDiscovery{skel}
got, err := s.GetManifestObjects(ctx, cr, getDummyCatalog(), logger)
g.Expect(err).NotTo(HaveOccurred())
for i := range got {
if got[i].GetKind() == "DaemonSet" {
assertCommonDaemonSetFields(g, got[i])
}
}
})
}

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"}},
},
}

func assertCommonDaemonSetFields(g Gomega, u *unstructured.Unstructured) {
ds := &appsv1.DaemonSet{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), ds)
g.Expect(err).ToNot(HaveOccurred())
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,
},
))
}
72 changes: 72 additions & 0 deletions pkg/state/state_nv_ipam_cni_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
2024 NVIDIA CORPORATION & AFFILIATES
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package state

import (
"context"
"testing"

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

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"sigs.k8s.io/controller-runtime/pkg/log"

mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1"
"github.com/Mellanox/network-operator/pkg/render"
"github.com/Mellanox/network-operator/pkg/utils"
)

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)
skel := stateSkel{client: fake.NewClientBuilder().Build(), renderer: renderer}
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"}},
},
},
},
}
t.Run("test GetObjectManifests", func(t *testing.T) {
s := &stateNVIPAMCNI{skel}
got, err := s.GetManifestObjects(ctx, cr, getDummyCatalog(), logger)
g.Expect(err).NotTo(HaveOccurred())
for i := range got {
if got[i].GetKind() == "DaemonSet" {
assertCommonDaemonSetFields(g, got[i])
}
}
})
}

0 comments on commit 5eba662

Please sign in to comment.