Skip to content

Commit

Permalink
Test for datacenterPods() functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Sep 5, 2024
1 parent f04541b commit 5c78d45
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pkg/reconciliation/reconcile_racks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2437,9 +2437,9 @@ func (rc *ReconciliationContext) datacenterPods() []*corev1.Pod {
dcPods := FilterPodListByLabels(rc.clusterPods, dcSelector)

if rc.Datacenter.Status.MetadataVersion < 1 && rc.Datacenter.Status.DatacenterName != nil && *rc.Datacenter.Status.DatacenterName == rc.Datacenter.Spec.DatacenterName {
rc.ReqLogger.Info("Fetching with the old metadata version also")
rc.ReqLogger.Info("Fetching datacenter pods with the old metadata version labels")
dcSelector[api.DatacenterLabel] = api.CleanLabelValue(rc.Datacenter.Spec.DatacenterName)
rc.dcPods = append(rc.dcPods, FilterPodListByLabels(rc.clusterPods, dcSelector)...)
dcPods = append(dcPods, FilterPodListByLabels(rc.clusterPods, dcSelector)...)
}

return dcPods
Expand Down
96 changes: 95 additions & 1 deletion pkg/reconciliation/reconcile_racks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package reconciliation
import (
"context"
"fmt"
"github.com/pkg/errors"
"io"
"net/http"
"reflect"
Expand All @@ -16,6 +15,8 @@ import (
"testing"
"time"

"github.com/pkg/errors"

"k8s.io/utils/ptr"

api "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
Expand Down Expand Up @@ -2780,3 +2781,96 @@ func TestDatacenterStatus(t *testing.T) {
assert.NoError(err)
assert.Equal(float64(0), val)
}

func TestDatacenterPods(t *testing.T) {
rc, _, cleanupMockScr := setupTest()
defer cleanupMockScr()
assert := assert.New(t)

desiredStatefulSet, err := newStatefulSetForCassandraDatacenter(
nil,
"default",
rc.Datacenter,
3)
assert.NoErrorf(err, "error occurred creating statefulset")

desiredStatefulSet.Status.ReadyReplicas = *desiredStatefulSet.Spec.Replicas

trackObjects := []runtime.Object{
desiredStatefulSet,
rc.Datacenter,
}

mockPods := mockReadyPodsForStatefulSet(desiredStatefulSet, rc.Datacenter.Spec.ClusterName, rc.Datacenter.Name)
for idx := range mockPods {
mp := mockPods[idx]
trackObjects = append(trackObjects, mp)
}

rc.Client = fake.NewClientBuilder().WithStatusSubresource(rc.Datacenter).WithRuntimeObjects(trackObjects...).Build()

nextRack := &RackInformation{}
nextRack.RackName = "default"
nextRack.NodeCount = 1
nextRack.SeedCount = 1

rackInfo := []*RackInformation{nextRack}

rc.desiredRackInformation = rackInfo
rc.statefulSets = make([]*appsv1.StatefulSet, len(rackInfo))

rc.clusterPods = mockPods
assert.Equal(int(*desiredStatefulSet.Spec.Replicas), len(rc.datacenterPods()))
}

func TestDatacenterPodsOldLabels(t *testing.T) {
rc, _, cleanupMockScr := setupTest()
defer cleanupMockScr()
assert := assert.New(t)

// We fake the process a bit to get old style naming and labels
rc.Datacenter.Name = "overrideMe"

desiredStatefulSet, err := newStatefulSetForCassandraDatacenter(
nil,
"default",
rc.Datacenter,
3)
assert.NoErrorf(err, "error occurred creating statefulset")

desiredStatefulSet.Status.ReadyReplicas = *desiredStatefulSet.Spec.Replicas

trackObjects := []runtime.Object{
desiredStatefulSet,
rc.Datacenter,
}

mockPods := mockReadyPodsForStatefulSet(desiredStatefulSet, rc.Datacenter.Spec.ClusterName, rc.Datacenter.Name)
for idx := range mockPods {
mp := mockPods[idx]
trackObjects = append(trackObjects, mp)
}

rc.Client = fake.NewClientBuilder().WithStatusSubresource(rc.Datacenter).WithRuntimeObjects(trackObjects...).Build()

nextRack := &RackInformation{}
nextRack.RackName = "default"
nextRack.NodeCount = 1
nextRack.SeedCount = 1

rackInfo := []*RackInformation{nextRack}

rc.desiredRackInformation = rackInfo
rc.statefulSets = make([]*appsv1.StatefulSet, len(rackInfo))

rc.clusterPods = mockPods

// Lets modify the Datacenter names and set the status like it used to be in some older versions
rc.Datacenter.Spec.DatacenterName = "overrideMe"
rc.Datacenter.Name = "dc1"
rc.Datacenter.Status.DatacenterName = ptr.To[string]("overrideMe")
rc.Datacenter.Status.MetadataVersion = 0

// We should still find the pods
assert.Equal(int(*desiredStatefulSet.Spec.Replicas), len(rc.datacenterPods()))
}
2 changes: 1 addition & 1 deletion tests/upgrade_operator/upgrade_operator_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe(testName, func() {
k := kubectl.ApplyFiles(dcYaml)
ns.ExecAndLog(step, k)

ns.WaitForDatacenterReady(dcName)
ns.WaitForDatacenterOperatorProgress(dcName, "Ready", 1800)

// Get UID of the cluster pod
step = "get Cassandra pods UID"
Expand Down

0 comments on commit 5c78d45

Please sign in to comment.