@@ -3,15 +3,14 @@ package cluster
3
3
import (
4
4
"context"
5
5
"errors"
6
- "reflect"
7
- "runtime/debug"
8
6
"testing"
9
7
10
8
hubv1alpha1 "github.com/kubeslice/apis/pkg/controller/v1alpha1"
11
9
"github.com/kubeslice/kubeslice-monitoring/pkg/metrics"
12
10
kubeslicev1beta1 "github.com/kubeslice/worker-operator/api/v1beta1"
13
11
utilmock "github.com/kubeslice/worker-operator/pkg/mocks"
14
12
"github.com/prometheus/client_golang/prometheus"
13
+ "github.com/stretchr/testify/assert"
15
14
"github.com/stretchr/testify/mock"
16
15
batchv1 "k8s.io/api/batch/v1"
17
16
corev1 "k8s.io/api/core/v1"
@@ -87,9 +86,7 @@ func TestGetOperatorClusterRole(t *testing.T) {
87
86
mock .IsType (& rbacv1.ClusterRole {}),
88
87
).Return (nil )
89
88
_ , err := reconciler .getOperatorClusterRole (ctx )
90
- if expected .err != err {
91
- t .Error ("Expected error:" , expected .err , " but got " , err )
92
- }
89
+ assert .ErrorIs (t , expected .err , err )
93
90
}
94
91
95
92
func TestCreateDeregisterJobPositiveScenarios (t * testing.T ) {
@@ -194,9 +191,7 @@ func TestCreateDeregisterJobPositiveScenarios(t *testing.T) {
194
191
mock .IsType ([]k8sclient.CreateOption (nil )),
195
192
).Return (nil )
196
193
err := reconciler .createDeregisterJob (ctx , testClusterObj )
197
- if expected .err != err {
198
- t .Error ("Expected error:" , expected .err , " but got " , err )
199
- }
194
+ assert .ErrorIs (t , expected .err , err )
200
195
}
201
196
202
197
func TestReconcilerFailToUpdateClusterRegistrationStatus (t * testing.T ) {
@@ -237,9 +232,7 @@ func TestReconcilerFailToUpdateClusterRegistrationStatus(t *testing.T) {
237
232
).Return (errors .New ("error updating status of deregistration on the controller" ))
238
233
239
234
err := reconciler .createDeregisterJob (ctx , testClusterObj )
240
- if expected .errMsg != err .Error () {
241
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
242
- }
235
+ assert .Equal (t , expected .errMsg , err .Error ())
243
236
}
244
237
245
238
func TestReconcilerFailToCreateServiceAccount (t * testing.T ) {
@@ -295,9 +288,7 @@ func TestReconcilerFailToCreateServiceAccount(t *testing.T) {
295
288
).Return (errors .New ("unable to create service account" ))
296
289
297
290
err := reconciler .createDeregisterJob (ctx , testClusterObj )
298
- if expected .errMsg != err .Error () {
299
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
300
- }
291
+ assert .Equal (t , expected .errMsg , err .Error ())
301
292
}
302
293
303
294
func TestReconcilerFailToFetchOperatorClusterRole (t * testing.T ) {
@@ -363,9 +354,7 @@ func TestReconcilerFailToFetchOperatorClusterRole(t *testing.T) {
363
354
).Return (errors .New ("unable to fetch operator clusterrole" ))
364
355
365
356
err := reconciler .createDeregisterJob (ctx , testClusterObj )
366
- if expected .errMsg != err .Error () {
367
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
368
- }
357
+ assert .Equal (t , expected .errMsg , err .Error ())
369
358
}
370
359
371
360
func TestReconcilerFailToCreateClusterRole (t * testing.T ) {
@@ -441,9 +430,7 @@ func TestReconcilerFailToCreateClusterRole(t *testing.T) {
441
430
).Return (errors .New ("unable to create cluster role" ))
442
431
443
432
err := reconciler .createDeregisterJob (ctx , testClusterObj )
444
- if expected .errMsg != err .Error () {
445
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
446
- }
433
+ assert .Equal (t , expected .errMsg , err .Error ())
447
434
}
448
435
449
436
func TestReconcilerFailToCreateClusterRoleBinding (t * testing.T ) {
@@ -524,9 +511,7 @@ func TestReconcilerFailToCreateClusterRoleBinding(t *testing.T) {
524
511
).Return (errors .New ("unable to create cluster rolebinding" ))
525
512
526
513
err := reconciler .createDeregisterJob (ctx , testClusterObj )
527
- if expected .errMsg != err .Error () {
528
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
529
- }
514
+ assert .Equal (t , expected .errMsg , err .Error ())
530
515
}
531
516
532
517
func TestReconcilerFailToCreateConfigmap (t * testing.T ) {
@@ -621,9 +606,7 @@ func TestReconcilerFailToCreateConfigmap(t *testing.T) {
621
606
mock .IsType ([]k8sclient.CreateOption (nil )),
622
607
).Return (errors .New ("Unable to create configmap" ))
623
608
err := reconciler .createDeregisterJob (ctx , testClusterObj )
624
- if expected .errMsg != err .Error () {
625
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
626
- }
609
+ assert .Equal (t , expected .errMsg , err .Error ())
627
610
}
628
611
629
612
func TestReconcilerFailToDeleteJob (t * testing.T ) {
@@ -729,9 +712,7 @@ func TestReconcilerFailToDeleteJob(t *testing.T) {
729
712
).Return (errors .New ("Unable to delete deregister job" ))
730
713
731
714
err := reconciler .createDeregisterJob (ctx , testClusterObj )
732
- if expected .errMsg != err .Error () {
733
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
734
- }
715
+ assert .Equal (t , expected .errMsg , err .Error ())
735
716
}
736
717
737
718
func TestReconcilerFailToCreateDeregisterJob (t * testing.T ) {
@@ -842,64 +823,42 @@ func TestReconcilerFailToCreateDeregisterJob(t *testing.T) {
842
823
).Return (errors .New ("Unable to create deregister job" ))
843
824
844
825
err := reconciler .createDeregisterJob (ctx , testClusterObj )
845
- if expected .errMsg != err .Error () {
846
- t .Error ("Expected error:" , expected .errMsg , " but got " , err )
847
- }
826
+ assert .Equal (t , expected .errMsg , err .Error ())
848
827
}
849
828
850
829
func TestGetConfigmapScriptData (t * testing.T ) {
851
830
data , err := getCleanupScript ()
852
- AssertNoError (t , err )
853
- if len (data ) == 0 {
854
- t .Fatalf ("unable to get configmap data" )
855
- }
831
+ assert .NoError (t , err )
832
+ assert .NotZero (t , len (data ), "unable to get configmap data" )
856
833
}
857
834
858
835
func TestConstructJobForClusterDeregister (t * testing.T ) {
859
836
job := constructJobForClusterDeregister ()
860
- AssertEqual (t , job .Name , deregisterJobName )
861
- AssertEqual (t , job .Namespace , ControlPlaneNamespace )
837
+ assert . Equal (t , job .Name , deregisterJobName )
838
+ assert . Equal (t , job .Namespace , ControlPlaneNamespace )
862
839
}
863
840
864
841
func TestConstructServiceAccount (t * testing.T ) {
865
842
sa := constructServiceAccount ()
866
- AssertEqual (t , sa .Name , serviceAccountName )
867
- AssertEqual (t , sa .Namespace , ControlPlaneNamespace )
843
+ assert . Equal (t , sa .Name , serviceAccountName )
844
+ assert . Equal (t , sa .Namespace , ControlPlaneNamespace )
868
845
}
869
846
870
847
func TestConstructClusterRole (t * testing.T ) {
871
848
cr := constructClusterRole (testOperatorClusterRole , "random-uid" )
872
- isEqual := reflect .DeepEqual (cr .Rules , testOperatorClusterRole .Rules )
873
- if ! isEqual {
874
- t .Fatalf ("got invalid data in clusterrole Rules: got -- %q want -- %q" , & cr .Rules [0 ], & testOperatorClusterRole .Rules [0 ])
875
- }
849
+ assert .Equal (t , cr .Rules , testOperatorClusterRole .Rules )
876
850
}
877
851
878
852
func TestConstructClusterRoleBinding (t * testing.T ) {
879
853
crb := constructClusterRoleBinding ("random-uid" )
880
- AssertEqual (t , crb .Name , clusterRoleBindingName )
881
- AssertEqual (t , crb .RoleRef , testClusterRoleRef )
882
- AssertEqual (t , len ( crb .Subjects ) , 1 )
883
- AssertEqual (t , crb .Subjects [0 ], testClusterRoleBindingSubject [0 ])
854
+ assert . Equal (t , crb .Name , clusterRoleBindingName )
855
+ assert . Equal (t , crb .RoleRef , testClusterRoleRef )
856
+ assert . Len (t , crb .Subjects , 1 )
857
+ assert . Equal (t , crb .Subjects [0 ], testClusterRoleBindingSubject [0 ])
884
858
}
885
859
886
860
func TestConstructConfigMap (t * testing.T ) {
887
861
data := "this is the data."
888
862
cm := constructConfigMap (data )
889
- AssertEqual (t , cm .Data ["kubeslice-cleanup.sh" ], data )
890
- }
891
-
892
- func AssertEqual (t * testing.T , actual interface {}, expected interface {}) {
893
- t .Helper ()
894
- if actual != expected {
895
- t .Log ("expected --" , expected , "actual --" , actual )
896
- t .Fail ()
897
- }
898
- }
899
-
900
- func AssertNoError (t * testing.T , err error ) {
901
- t .Helper ()
902
- if err != nil {
903
- t .Errorf ("Expected No Error but got %s, Stack:\n %s" , err , string (debug .Stack ()))
904
- }
863
+ assert .Equal (t , cm .Data ["kubeslice-cleanup.sh" ], data )
905
864
}
0 commit comments