-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for GetManifestObjects
Signed-off-by: killianmuldoon <[email protected]>
- Loading branch information
1 parent
fd7a346
commit 1906c76
Showing
4 changed files
with
208 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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"}}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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, | ||
}, | ||
)) | ||
} | ||
} | ||
|
||
}) | ||
} | ||
} |