@@ -3,9 +3,11 @@ package cassandracluster
33import (
44 "context"
55 "fmt"
6+ "strconv"
67 "strings"
78 "testing"
89
10+ api "github.com/cscetbon/casskop/api/v2"
911 "github.com/jarcoal/httpmock"
1012 "github.com/stretchr/testify/assert"
1113)
@@ -33,6 +35,31 @@ func registerJolokiaOperationJoiningNodes(host podName, numberOfJoiningNodes int
3335 "status": 200}` , stringOfSlice (joiningNodes ))))
3436}
3537
38+ func simulateNewPodsReady (t * testing.T , rcc * CassandraClusterReconciler , stfsName string , dc api.DC , scaleFrom , scaleTo int ) {
39+ assert := assert .New (t )
40+
41+ sts , err := rcc .GetStatefulSet (ctx , rcc .cc .Namespace , stfsName )
42+ assert .NoError (err , "get sts" )
43+
44+ //Now simulate sts to be ready for CassKop
45+ sts .Status .Replicas = * sts .Spec .Replicas
46+ sts .Status .ReadyReplicas = * sts .Spec .Replicas
47+ err = rcc .Client .Status ().Update (ctx , sts )
48+ assert .NoError (err , "update sts status" )
49+
50+ // create new fake Pods (consequence of scale-out) so action may finish
51+ podTemplate := fakePodTemplate (rcc .cc , dc .Name , dc .Rack [0 ].Name )
52+ for i := scaleFrom ; i < scaleTo ; i ++ {
53+ pod := podTemplate .DeepCopy ()
54+ pod .Name = sts .Name + "-" + strconv .Itoa (i )
55+ pod .Spec .Hostname = pod .Name
56+ pod .Spec .Subdomain = rcc .cc .Name
57+ if err = rcc .CreatePod (ctx , pod ); err != nil {
58+ t .Fatalf ("can't create pod: (%v)" , err )
59+ }
60+ }
61+ }
62+
3663func TestAddTwoNodes (t * testing.T ) {
3764 overrideDelayWaitWithNoDelay ()
3865 defer restoreDefaultDelayWait ()
@@ -78,4 +105,30 @@ func TestAddTwoNodes(t *testing.T) {
78105 assert .GreaterOrEqual (jolokiaCallsCount (firstPod ), 1 )
79106 assertStatefulsetReplicas (ctx , t , rcc , expectedReplicas + 1 , cassandraCluster .Namespace , stfsName )
80107 }
108+
109+ assertClusterStatusPhase (assert , rcc , api .ClusterPhasePending )
110+ assertRackStatusPhase (assert , rcc , "dc1-rack1" , api .ClusterPhasePending )
111+ assertClusterStatusLastAction (assert , rcc , api .ActionScaleUp , api .StatusOngoing )
112+ assertRackStatusLastAction (assert , rcc , "dc1-rack1" , api .ActionScaleUp , api .StatusOngoing )
113+
114+ //Reconcile does not end the action even when sts and all new pods are ready, because there are joining nodes
115+ simulateNewPodsReady (t , rcc , stfsName , dc , 3 , 5 )
116+ registerJolokiaOperationJoiningNodes (firstPod , 1 )
117+ for reconcileIteration := 0 ; reconcileIteration <= 2 ; reconcileIteration ++ {
118+ reconcileValidation (t , rcc , * req )
119+ assert .GreaterOrEqual (jolokiaCallsCount (firstPod ), 1 )
120+ assertClusterStatusPhase (assert , rcc , api .ClusterPhasePending )
121+ assertRackStatusPhase (assert , rcc , "dc1-rack1" , api .ClusterPhaseRunning )
122+ assertClusterStatusLastAction (assert , rcc , api .ActionScaleUp , api .StatusOngoing )
123+ assertRackStatusLastAction (assert , rcc , "dc1-rack1" , api .ActionScaleUp , api .StatusOngoing )
124+ }
125+
126+ //Reconcile ends the action when sts and all new pods are ready and there are no joining nodes
127+ registerJolokiaOperationJoiningNodes (firstPod , 0 )
128+ reconcileValidation (t , rcc , * req )
129+ assert .GreaterOrEqual (jolokiaCallsCount (firstPod ), 1 )
130+ assertClusterStatusPhase (assert , rcc , api .ClusterPhaseRunning )
131+ assertRackStatusPhase (assert , rcc , "dc1-rack1" , api .ClusterPhaseRunning )
132+ assertClusterStatusLastAction (assert , rcc , api .ActionScaleUp , api .StatusDone )
133+ assertRackStatusLastAction (assert , rcc , "dc1-rack1" , api .ActionScaleUp , api .StatusDone )
81134}
0 commit comments