Skip to content

Commit e69f894

Browse files
committed
test(perf): unit tests speed-up
1 parent dfbe0bf commit e69f894

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

controllers/cassandracluster/cassandra_status_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/cscetbon/casskop/controllers/common"
2323
"github.com/cscetbon/casskop/pkg/k8s"
24+
"github.com/jarcoal/httpmock"
2425
"k8s.io/apimachinery/pkg/runtime"
2526
"k8s.io/client-go/kubernetes/scheme"
2627
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -375,6 +376,10 @@ func assertRackStatus(assert *assert.Assertions, cc *api.CassandraCluster, dcRac
375376

376377
func TestCassandraClusterReconciler(t *testing.T) {
377378

379+
// tests speed-up
380+
httpmock.Activate()
381+
defer httpmock.DeactivateAndReset()
382+
378383
// Mock request to simulate Reconcile() being called on an event for a
379384
// watched resource .
380385
rcc, req := helperCreateCassandraCluster(context.TODO(), t, "cassandracluster-2DC.yaml")
@@ -393,6 +398,14 @@ func TestCassandraClusterReconciler(t *testing.T) {
393398

394399
func TestCassandraClusterReconcilerMultiNodePerRack(t *testing.T) {
395400

401+
// override delay wait to speed up tests
402+
overrideDelayWaitWithNoDelay()
403+
defer restoreDefaultDelayWait()
404+
405+
// tests speed-up
406+
httpmock.Activate()
407+
defer httpmock.DeactivateAndReset()
408+
396409
// Mock request to simulate Reconcile() being called on an event for a
397410
// watched resource .
398411
rcc, req := helperCreateCassandraCluster(context.TODO(), t, "cassandracluster-2DC-2racksEach-2nodesPerRack.yaml")
@@ -411,6 +424,11 @@ func TestCassandraClusterReconcilerMultiNodePerRack(t *testing.T) {
411424

412425
// test that we detect an addition of a configmap
413426
func TestUpdateStatusIfconfigMapHasChangedWithNoConfigMap(t *testing.T) {
427+
428+
// tests speed-up
429+
httpmock.Activate()
430+
defer httpmock.DeactivateAndReset()
431+
414432
// Mock request to simulate Reconcile() being called on an event for a
415433
// watched resource .
416434
rcc, req := helperCreateCassandraCluster(context.TODO(), t, "cassandracluster-2DC.yaml")
@@ -464,6 +482,11 @@ func TestUpdateStatusIfconfigMapHasChangedWithNoConfigMap(t *testing.T) {
464482

465483
// test that we detect a change in a configmap
466484
func TestUpdateStatusIfconfigMapHasChangedWithConfigMap(t *testing.T) {
485+
486+
// tests speed-up
487+
httpmock.Activate()
488+
defer httpmock.DeactivateAndReset()
489+
467490
// Mock request to simulate Reconcile() being called on an event for a
468491
// watched resource .
469492
rcc, req := helperCreateCassandraCluster(context.TODO(), t, "cassandracluster-2DC-configmap.yaml")
@@ -535,6 +558,11 @@ func TestUpdateStatusIfconfigMapHasChangedWithConfigMap(t *testing.T) {
535558

536559
// test that we detect a change in a the docker image
537560
func TestUpdateStatusIfDockerImageHasChanged(t *testing.T) {
561+
562+
// tests speed-up
563+
httpmock.Activate()
564+
defer httpmock.DeactivateAndReset()
565+
538566
// Mock request to simulate Reconcile() being called on an event for a
539567
// watched resource .
540568
rcc, req := helperCreateCassandraCluster(context.TODO(), t, "cassandracluster-2DC-configmap.yaml")
@@ -611,8 +639,12 @@ func overrideDelayWaitWithNoDelay() {
611639
delayWait = func() time.Duration {
612640
return 0
613641
}
642+
retryInterval = func() time.Duration {
643+
return time.Millisecond
644+
}
614645
}
615646

616647
func restoreDefaultDelayWait() {
617648
delayWait = defaultDelayWait
649+
retryInterval = defaultRetryInterval
618650
}

controllers/cassandracluster/decommission_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ func TestOneDecommission(t *testing.T) {
9898
overrideDelayWaitWithNoDelay()
9999
defer restoreDefaultDelayWait()
100100

101-
ctx := context.TODO()
102-
rcc, req := createCassandraClusterWithNoDisruption(t, "cassandracluster-1DC.yaml")
103-
104101
httpmock.Activate()
105102
defer httpmock.DeactivateAndReset()
106103
assert := assert.New(t)
107104

105+
ctx := context.TODO()
106+
rcc, req := createCassandraClusterWithNoDisruption(t, "cassandracluster-1DC.yaml")
107+
108108
assertCCStatusLastAction(assert, rcc, api.ClusterPhaseInitial, api.StatusDone)
109109
assertClusterStatusPhaseV2(assert, rcc, api.ClusterPhaseV2Running)
110110

controllers/cassandracluster/pod_operation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ func (rcc *CassandraClusterReconciler) finalizeOperation(ctx context.Context, er
675675
}
676676
logrus.WithFields(logrus.Fields{"cluster": cc.Name, "rack": dcRackName, "pod": pod.Name,
677677
"status": status}).Debug("Can't get new version of Cassandra Cluster. Will try again")
678-
time.Sleep(retryInterval)
678+
time.Sleep(retryInterval())
679679
}
680680
}
681681

@@ -830,7 +830,7 @@ func (rcc *CassandraClusterReconciler) runRemove(ctx context.Context, hostName s
830830
}
831831

832832
func (rcc *CassandraClusterReconciler) waitUntilPvcIsDeleted(ctx context.Context, namespace, pvcName string) error {
833-
err := wait.Poll(retryInterval, deletedPvcTimeout, func() (done bool, err error) {
833+
err := wait.Poll(retryInterval(), deletedPvcTimeout, func() (done bool, err error) {
834834
_, err = rcc.GetPVC(ctx, namespace, pvcName)
835835
if err != nil && apierrors.IsNotFound(err) {
836836
logrus.WithFields(logrus.Fields{"namespace": namespace,

controllers/cassandracluster/scaleup_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ func TestAddTwoNodes(t *testing.T) {
6565
overrideDelayWaitWithNoDelay()
6666
defer restoreDefaultDelayWait()
6767

68-
rcc, req := createCassandraClusterWithNoDisruption(t, "cassandracluster-1DC.yaml")
69-
7068
httpmock.Activate()
7169
defer httpmock.DeactivateAndReset()
7270
assert := assert.New(t)
7371

72+
rcc, req := createCassandraClusterWithNoDisruption(t, "cassandracluster-1DC.yaml")
73+
7474
assert.Equal(int32(3), rcc.cc.Spec.NodesPerRacks)
7575

7676
cassandraCluster := rcc.cc.DeepCopy()

controllers/cassandracluster/statefulset.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ import (
3939
)
4040

4141
var (
42-
retryInterval = time.Second
43-
timeout = time.Second * 5
42+
// visible for tests
43+
retryInterval = defaultRetryInterval
44+
45+
timeout = time.Second * 5
4446
)
4547

48+
func defaultRetryInterval() time.Duration {
49+
return time.Second
50+
}
51+
4652
// GetStatefulSet return the Statefulset name from the cluster in the namespace
4753
func (rcc *CassandraClusterReconciler) GetStatefulSet(ctx context.Context, namespace, name string) (*appsv1.StatefulSet, error) {
4854

@@ -89,7 +95,7 @@ func (rcc *CassandraClusterReconciler) UpdateStatefulSet(ctx context.Context, st
8995
return fmt.Errorf("failed to update cassandra statefulset: %cc", err)
9096
}
9197
//Check that the new revision of statefulset has been taken into account
92-
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
98+
err := wait.Poll(retryInterval(), timeout, func() (done bool, err error) {
9399
newSts, err := rcc.GetStatefulSet(ctx, statefulSet.Namespace, statefulSet.Name)
94100
if err != nil && !apierrors.IsNotFound(err) {
95101
return false, fmt.Errorf("failed to get cassandra statefulset: %cc", err)

0 commit comments

Comments
 (0)