Skip to content

Commit

Permalink
test package constants in controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh-11 committed Jul 1, 2024
1 parent 84d4f43 commit 7e9a0f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
28 changes: 12 additions & 16 deletions controllers/cluster/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ import (
const (
testdataPath = "testdata"
maxConcurrentReconcilesProber = 1
workerOneName = "worker-1"
workerTwoName = "worker-2"
nodeDiskPressure = "DiskPressure"
nodeMemoryPressure = "MemoryPressure"
)

var (
Expand Down Expand Up @@ -141,7 +137,7 @@ func testProberDedicatedEnvTest(t *testing.T) {
func testReconciliationAfterAPIServerIsDown(ctx context.Context, t *testing.T, testEnv *envtest.Environment, _ client.Client, reconciler *Reconciler, _ manager.Manager, cancelFn context.CancelFunc) {
var err error
g := NewWithT(t)
cluster, _, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, _, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
cancelFn()
err = testEnv.ControlPlane.APIServer.Stop()
Expand Down Expand Up @@ -185,14 +181,14 @@ func testProberSharedEnvTest(t *testing.T) {
}

func testShootWorkerNodeConditions(g *WithT, crClient client.Client, reconciler *Reconciler) {
workerNodeConditions := map[string][]string{workerOneName: {nodeDiskPressure, nodeMemoryPressure}}
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).WithWorkerNodeConditions(workerNodeConditions).Build()
workerNodeConditions := map[string][]string{testutil.Worker1Name: {testutil.NodeConditionDiskPressure, testutil.NodeConditionMemoryPressure}}
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).WithWorkerNodeConditions(workerNodeConditions).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
proberShouldBePresent(g, reconciler, cluster, defaultKCMNodeMonitorGracePeriod, expectedWorkerNodeConditions)
// update the workers
updatedWorkers := testutil.CreateWorkers([]string{workerOneName, workerTwoName}, workerNodeConditions)
updatedWorkers := testutil.CreateWorkers([]string{testutil.Worker1Name, testutil.Worker2Name}, workerNodeConditions)
shoot.Spec.Provider.Workers = updatedWorkers
cluster.Spec.Shoot = runtime.RawExtension{
Object: shoot,
Expand Down Expand Up @@ -235,7 +231,7 @@ func updateShootHibernationSpec(g *WithT, crClient client.Client, cluster *garde
}

func testShootHibernation(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
Expand Down Expand Up @@ -263,7 +259,7 @@ func updateShootHibernationStatus(g *WithT, crClient client.Client, cluster *gar
}

func testInvalidShootInClusterSpec(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, _, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, _, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
cluster.Spec.Shoot.Object = nil
cluster.Spec.Shoot.Raw = []byte(`{"apiVersion": 8}`)
Expand All @@ -285,7 +281,7 @@ func updateShootDeletionTimeStamp(g *WithT, crClient client.Client, cluster *gar
}

func testProberShouldBeRemovedIfDeletionTimeStampIsSet(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
Expand Down Expand Up @@ -318,7 +314,7 @@ func testShootCreationNotComplete(g *WithT, crClient client.Client, reconciler *
}

for _, testCase := range testCases {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).WithNodeMonitorGracePeriod(shootKCMNodeMonitorGracePeriod).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).WithNodeMonitorGracePeriod(shootKCMNodeMonitorGracePeriod).Build()
g.Expect(err).ToNot(HaveOccurred())
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeCreate, testCase.lastOpState)
createCluster(g, crClient, cluster)
Expand All @@ -333,7 +329,7 @@ func testShootCreationNotComplete(g *WithT, crClient client.Client, reconciler *
}

func testShootIsMigrating(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeMigrate, "")
Expand All @@ -343,7 +339,7 @@ func testShootIsMigrating(g *WithT, crClient client.Client, reconciler *Reconcil
}

func testShootRestoringIsNotComplete(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
createCluster(g, crClient, cluster)
expectedWorkerNodeConditions := util.GetEffectiveNodeConditionsForWorkers(shoot)
Expand All @@ -360,7 +356,7 @@ func testShootRestoringIsNotComplete(g *WithT, crClient client.Client, reconcile
}

func testLastOperationIsRestoreAndSuccessful(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeRestore, gardencorev1beta1.LastOperationStateSucceeded)
createCluster(g, crClient, cluster)
Expand All @@ -370,7 +366,7 @@ func testLastOperationIsRestoreAndSuccessful(g *WithT, crClient client.Client, r
}

func testLastOperationIsShootReconciliation(g *WithT, crClient client.Client, reconciler *Reconciler) {
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{workerOneName}).Build()
cluster, shoot, err := testutil.NewClusterBuilder().WithWorkerNames([]string{testutil.Worker1Name}).Build()
g.Expect(err).ToNot(HaveOccurred())
setShootLastOperationStatus(cluster, shoot, gardencorev1beta1.LastOperationTypeReconcile, "")
createCluster(g, crClient, cluster)
Expand Down
30 changes: 16 additions & 14 deletions controllers/cluster/clusterpredicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: Apache-2.0

//go:build !kind_tests

package cluster

import (
Expand All @@ -24,17 +26,17 @@ func TestCreateAndDeletePredicateFunc(t *testing.T) {
workerNames []string
expectedResult bool
}{
{"test: create predicate func for cluster having workers", testCreatePredicateFunc, []string{workerOneName, workerTwoName}, true},
{"test: create predicate func for cluster having workers", testCreatePredicateFunc, []string{test.Worker1Name, test.Worker2Name}, true},
{"test: create predicate func for cluster having no workers", testCreatePredicateFunc, nil, false},
{"test: delete predicate func for cluster having workers", testDeletePredicateFunc, []string{workerOneName, workerTwoName}, true},
{"test: delete predicate func for cluster having workers", testDeletePredicateFunc, []string{test.Worker1Name, test.Worker2Name}, true},
{"test: delete predicate func for cluster having no workers", testDeletePredicateFunc, nil, false},
}

for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
for _, entry := range tests {
t.Run(entry.title, func(t *testing.T) {
g := NewWithT(t)
predicateResult := test.run(g, test.workerNames)
g.Expect(predicateResult).To(Equal(test.expectedResult))
predicateResult := entry.run(g, entry.workerNames)
g.Expect(predicateResult).To(Equal(entry.expectedResult))
})
}
}
Expand Down Expand Up @@ -63,17 +65,17 @@ func TestUpdatePredicateFunc(t *testing.T) {
expectedResult bool
}{
{"test: both old and new cluster do not have workers", nil, nil, false},
{"test: old cluster has no workers and new cluster has workers", nil, []string{workerOneName}, true},
{"test: old cluster has workers and new cluster do not have workers", []string{workerOneName, workerTwoName}, nil, true},
{"test: both old and new cluster have workers and there is no change", []string{workerOneName}, []string{workerOneName}, true},
{"test: both old and new cluster have workers and are different in number", []string{workerOneName}, []string{workerOneName, workerTwoName}, true},
{"test: old cluster has no workers and new cluster has workers", nil, []string{test.Worker1Name}, true},
{"test: old cluster has workers and new cluster do not have workers", []string{test.Worker1Name, test.Worker2Name}, nil, true},
{"test: both old and new cluster have workers and there is no change", []string{test.Worker1Name}, []string{test.Worker1Name}, true},
{"test: both old and new cluster have workers and are different in number", []string{test.Worker1Name}, []string{test.Worker1Name, test.Worker2Name}, true},
}

for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
for _, entry := range tests {
t.Run(entry.title, func(t *testing.T) {
g := NewWithT(t)
predicateResult := testUpdatePredicateFunc(g, test.oldWorkerNames, test.newWorkerNames)
g.Expect(predicateResult).To(Equal(test.expectedResult))
predicateResult := testUpdatePredicateFunc(g, entry.oldWorkerNames, entry.newWorkerNames)
g.Expect(predicateResult).To(Equal(entry.expectedResult))
})
}
}
Expand Down

0 comments on commit 7e9a0f4

Please sign in to comment.