Skip to content

Commit 6b9b322

Browse files
committed
tests: adjust tests
* remove replicas value from the Normalize unit-tests * check in the e2e tests that the number of replicas equals to the number of control-plane nodes. * renforce integration tests. Signed-off-by: Talor Itzhak <titzhak@redhat.com>
1 parent 860724c commit 6b9b322

4 files changed

Lines changed: 56 additions & 17 deletions

File tree

api/v1/numaresourcesscheduler_defaults_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
6262
ScoringStrategy: &ScoringStrategyParams{
6363
Type: scoringStrategyType,
6464
},
65-
Replicas: ptr.To[int32](defaultReplicas),
6665
},
6766
},
6867
{
@@ -83,7 +82,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
8382
ScoringStrategy: &ScoringStrategyParams{
8483
Type: scoringStrategyType,
8584
},
86-
Replicas: ptr.To[int32](defaultReplicas),
8785
},
8886
},
8987
{
@@ -107,7 +105,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
107105
ScoringStrategy: &ScoringStrategyParams{
108106
Type: scoringStrategyType,
109107
},
110-
Replicas: ptr.To[int32](defaultReplicas),
111108
},
112109
},
113110
{
@@ -132,7 +129,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
132129
ScoringStrategy: &ScoringStrategyParams{
133130
Type: scoringStrategyType,
134131
},
135-
Replicas: ptr.To[int32](defaultReplicas),
136132
},
137133
},
138134
{
@@ -157,7 +153,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
157153
ScoringStrategy: &ScoringStrategyParams{
158154
Type: scoringStrategyType,
159155
},
160-
Replicas: ptr.To[int32](defaultReplicas),
161156
},
162157
},
163158
{
@@ -180,7 +175,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
180175
SchedulerInformer: &schedInformer,
181176
CacheResyncDetection: &cacheResyncDetection,
182177
ScoringStrategy: &scoringStrategyCustom,
183-
Replicas: ptr.To[int32](defaultReplicas),
184178
},
185179
},
186180
{
@@ -274,7 +268,6 @@ func TestSetDefaults_NUMAResourcesSchedulerSpec(t *testing.T) {
274268
ScoringStrategy: &ScoringStrategyParams{
275269
Type: scoringStrategyType,
276270
},
277-
Replicas: ptr.To[int32](defaultReplicas),
278271
},
279272
},
280273
{

api/v1/numaresourcesscheduler_normalize_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
6262
ScoringStrategy: &ScoringStrategyParams{
6363
Type: scoringStrategyType,
6464
},
65-
Replicas: ptr.To[int32](defaultReplicas),
6665
},
6766
},
6867
{
@@ -83,7 +82,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
8382
ScoringStrategy: &ScoringStrategyParams{
8483
Type: scoringStrategyType,
8584
},
86-
Replicas: ptr.To[int32](defaultReplicas),
8785
},
8886
},
8987
{
@@ -107,7 +105,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
107105
ScoringStrategy: &ScoringStrategyParams{
108106
Type: scoringStrategyType,
109107
},
110-
Replicas: ptr.To[int32](defaultReplicas),
111108
},
112109
},
113110
{
@@ -132,7 +129,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
132129
ScoringStrategy: &ScoringStrategyParams{
133130
Type: scoringStrategyType,
134131
},
135-
Replicas: ptr.To[int32](defaultReplicas),
136132
},
137133
},
138134
{
@@ -157,7 +153,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
157153
ScoringStrategy: &ScoringStrategyParams{
158154
Type: scoringStrategyType,
159155
},
160-
Replicas: ptr.To[int32](defaultReplicas),
161156
},
162157
},
163158
{
@@ -180,7 +175,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
180175
SchedulerInformer: &schedInformer,
181176
CacheResyncDetection: &cacheResyncDetection,
182177
ScoringStrategy: &scoringStrategyCustom,
183-
Replicas: ptr.To[int32](defaultReplicas),
184178
},
185179
},
186180
{
@@ -274,7 +268,6 @@ func TestNUMAResourcesSchedulerSpecNormalize(t *testing.T) {
274268
ScoringStrategy: &ScoringStrategyParams{
275269
Type: scoringStrategyType,
276270
},
277-
Replicas: ptr.To[int32](defaultReplicas),
278271
},
279272
},
280273
{

internal/controller/numaresourcesscheduler_controller_test.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controller
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"time"
2223

2324
"github.com/onsi/ginkgo/v2"
@@ -33,6 +34,7 @@ import (
3334
"k8s.io/apimachinery/pkg/runtime"
3435
"k8s.io/client-go/kubernetes/scheme"
3536
"k8s.io/klog/v2"
37+
"k8s.io/utils/ptr"
3638
"sigs.k8s.io/controller-runtime/pkg/client"
3739
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3840
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -93,11 +95,14 @@ var _ = ginkgo.Describe("Test NUMAResourcesScheduler Reconcile", func() {
9395
ginkgo.Context("with correct NRS CR", func() {
9496
var nrs *nropv1.NUMAResourcesScheduler
9597
var reconciler *NUMAResourcesSchedulerReconciler
98+
numOfMasters := 3
9699

97100
ginkgo.BeforeEach(func() {
98101
var err error
99102
nrs = testobjs.NewNUMAResourcesScheduler("numaresourcesscheduler", "some/url:latest", testSchedulerName, 11*time.Second)
100-
reconciler, err = NewFakeNUMAResourcesSchedulerReconciler(nrs)
103+
initObjects := []runtime.Object{nrs}
104+
initObjects = append(initObjects, fakeNodes(numOfMasters, 3)...)
105+
reconciler, err = NewFakeNUMAResourcesSchedulerReconciler(initObjects...)
101106
gomega.Expect(err).ToNot(gomega.HaveOccurred())
102107
})
103108

@@ -585,10 +590,20 @@ var _ = ginkgo.Describe("Test NUMAResourcesScheduler Reconcile", func() {
585590
})
586591

587592
ginkgo.It("should set the leader election resource parameters by default", func() {
593+
nrs := nrs.DeepCopy()
594+
nrs.Spec.Replicas = ptr.To(int32(1))
595+
gomega.Eventually(reconciler.Client.Update).WithArguments(context.TODO(), nrs).WithPolling(30 * time.Second).WithTimeout(5 * time.Minute).Should(gomega.Succeed())
596+
597+
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: client.ObjectKeyFromObject(nrs)})
598+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
599+
expectLeaderElectParams(reconciler.Client, false, testNamespace, nrosched.LeaderElectionResourceName)
600+
})
601+
602+
ginkgo.It("should set the leader election resource parameters to true default", func() {
588603
key := client.ObjectKeyFromObject(nrs)
589604
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: key})
590605
gomega.Expect(err).ToNot(gomega.HaveOccurred())
591-
expectLeaderElectParams(reconciler.Client, false, testNamespace, nrosched.LeaderElectionResourceName)
606+
expectLeaderElectParams(reconciler.Client, true, testNamespace, nrosched.LeaderElectionResourceName)
592607
})
593608

594609
ginkgo.DescribeTable("should set the leader election resource parameters depending on replica count", func(replicas int32, expectedEnabled bool) {
@@ -605,6 +620,16 @@ var _ = ginkgo.Describe("Test NUMAResourcesScheduler Reconcile", func() {
605620
ginkgo.Entry("replicas=1", int32(1), false),
606621
ginkgo.Entry("replicas=3", int32(3), true),
607622
)
623+
624+
ginkgo.It("should detect replicas number by default when spec.Replicas is unset", func() {
625+
key := client.ObjectKeyFromObject(nrs)
626+
_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: key})
627+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
628+
629+
dp := &appsv1.Deployment{}
630+
gomega.Expect(reconciler.Client.Get(context.TODO(), client.ObjectKey{Namespace: testNamespace, Name: "secondary-scheduler"}, dp)).To(gomega.Succeed())
631+
gomega.Expect(*dp.Spec.Replicas).To(gomega.Equal(int32(numOfMasters)), "number of replicas is different than number of control-planes nodes; want=%d got=%d", numOfMasters, *dp.Spec.Replicas)
632+
})
608633
})
609634
})
610635

@@ -695,3 +720,28 @@ func expectLeaderElectParams(cli client.Client, enabled bool, resourceNamespace,
695720
gomega.Expect(cfg.LeaderElection.ResourceNamespace).To(gomega.Equal(resourceNamespace))
696721
gomega.Expect(cfg.LeaderElection.ResourceName).To(gomega.Equal(resourceName))
697722
}
723+
724+
func fakeNodes(numOfMasters, numOfWorkers int) []runtime.Object {
725+
var nodes []runtime.Object
726+
for i := range numOfMasters {
727+
nodes = append(nodes, &corev1.Node{
728+
ObjectMeta: metav1.ObjectMeta{
729+
Name: fmt.Sprintf("master-node-%d", i+1),
730+
Labels: map[string]string{
731+
"node-role.kubernetes.io/control-plane": "",
732+
},
733+
},
734+
})
735+
}
736+
for i := range numOfWorkers {
737+
nodes = append(nodes, &corev1.Node{
738+
ObjectMeta: metav1.ObjectMeta{
739+
Name: fmt.Sprintf("worker-node-%d", i+1),
740+
Labels: map[string]string{
741+
"node-role.kubernetes.io/worker": "",
742+
},
743+
},
744+
})
745+
}
746+
return nodes
747+
}

test/e2e/sched/install/install_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package install
1919
import (
2020
"context"
2121
"fmt"
22-
"github.com/openshift-kni/numaresources-operator/test/e2e/label"
2322
"time"
2423

2524
appsv1 "k8s.io/api/apps/v1"
@@ -37,6 +36,7 @@ import (
3736
"github.com/openshift-kni/numaresources-operator/internal/podlist"
3837
"github.com/openshift-kni/numaresources-operator/pkg/objectnames"
3938
"github.com/openshift-kni/numaresources-operator/pkg/status"
39+
"github.com/openshift-kni/numaresources-operator/test/e2e/label"
4040
e2eclient "github.com/openshift-kni/numaresources-operator/test/internal/clients"
4141
"github.com/openshift-kni/numaresources-operator/test/internal/crds"
4242
"github.com/openshift-kni/numaresources-operator/test/internal/objects"
@@ -112,6 +112,9 @@ var _ = Describe("[Scheduler] install", func() {
112112
By("checking the NumaResourcesScheduler CRD is deployed")
113113
_, err = crds.GetByName(e2eclient.Client, crds.CrdNROSName)
114114
Expect(err).NotTo(HaveOccurred())
115+
116+
By("checking deployment has number of replicas equal to number of control plane nodes")
117+
Expect(*deployment.Spec.Replicas).To(Equal(int32(len(nodeList))), "wrong number of replicas configured for the deployment; want=%d got=%d", int32(len(nodeList)), *deployment.Spec.Replicas)
115118
})
116119
})
117120
})

0 commit comments

Comments
 (0)