@@ -23,7 +23,7 @@ import (
23
23
"strconv"
24
24
"testing"
25
25
26
- . "bou.ke/monkey "
26
+ "github.com/agiledragon/gomonkey/v2 "
27
27
"github.com/stretchr/testify/assert"
28
28
corev1 "k8s.io/api/core/v1"
29
29
"k8s.io/apimachinery/pkg/api/resource"
@@ -408,18 +408,18 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
408
408
},
409
409
},
410
410
}
411
- guard := PatchInstanceMethod (reflect .TypeOf (cluster ), "GetLabels" , func (* MysqlCluster ) labels.Set {
411
+ guard := gomonkey . ApplyMethod (reflect .TypeOf (cluster ), "GetLabels" , func (* MysqlCluster ) labels.Set {
412
412
return nil
413
413
})
414
- guard1 := Patch (resource .MustParse , func (_ string ) resource.Quantity {
414
+ guard1 := gomonkey . ApplyFunc (resource .MustParse , func (_ string ) resource.Quantity {
415
415
return resource.Quantity {}
416
416
})
417
- guard2 := Patch (controllerutil .SetControllerReference , func (_ metav1.Object , _ metav1.Object , _ * runtime.Scheme ) error {
417
+ guard2 := gomonkey . ApplyFunc (controllerutil .SetControllerReference , func (_ metav1.Object , _ metav1.Object , _ * runtime.Scheme ) error {
418
418
return nil
419
419
})
420
- defer guard .Unpatch ()
421
- defer guard1 .Unpatch ()
422
- defer guard2 .Unpatch ()
420
+ defer guard .Reset ()
421
+ defer guard1 .Reset ()
422
+ defer guard2 .Reset ()
423
423
result , err := testCase .EnsureVolumeClaimTemplates (& scheme )
424
424
assert .Equal (t , want , result )
425
425
assert .Nil (t , err )
@@ -435,10 +435,10 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
435
435
testCase := MysqlCluster {
436
436
& testMysql ,
437
437
}
438
- guard := Patch (controllerutil .SetControllerReference , func (_ metav1.Object , _ metav1.Object , _ * runtime.Scheme ) error {
438
+ guard := gomonkey . ApplyFunc (controllerutil .SetControllerReference , func (_ metav1.Object , _ metav1.Object , _ * runtime.Scheme ) error {
439
439
return nil
440
440
})
441
- defer guard .Unpatch ()
441
+ defer guard .Reset ()
442
442
result , err := testCase .EnsureVolumeClaimTemplates (& scheme )
443
443
444
444
assert .Equal (t , & storageClass , result [0 ].Spec .StorageClassName )
@@ -453,10 +453,10 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
453
453
testCase := MysqlCluster {
454
454
& testMysql ,
455
455
}
456
- guard := Patch (controllerutil .SetControllerReference , func (_ metav1.Object , _ metav1.Object , _ * runtime.Scheme ) error {
456
+ guard := gomonkey . ApplyFunc (controllerutil .SetControllerReference , func (_ metav1.Object , _ metav1.Object , _ * runtime.Scheme ) error {
457
457
return fmt .Errorf ("test" )
458
458
})
459
- defer guard .Unpatch ()
459
+ defer guard .Reset ()
460
460
result , err := testCase .EnsureVolumeClaimTemplates (& scheme )
461
461
want := fmt .Errorf ("failed setting controller reference: test" )
462
462
assert .Nil (t , result )
@@ -531,10 +531,10 @@ func TestEnsureMysqlConf(t *testing.T) {
531
531
}
532
532
// cpu 1 corev1s,memory 1 gb,innodb_buffer_pool_size 600 mb
533
533
{
534
- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
534
+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
535
535
return uint64 (600 * mb ), nil
536
536
})
537
- defer guard .Unpatch ()
537
+ defer guard .Reset ()
538
538
539
539
testMysqlCase := testMysql
540
540
testMysqlCase .Spec .MysqlOpts .MysqlConf ["innodb_buffer_pool_size" ] = strconv .FormatUint (uint64 (600 * mb ), 10 )
@@ -549,10 +549,10 @@ func TestEnsureMysqlConf(t *testing.T) {
549
549
}
550
550
// cpu 1 corev1s,memory 2 gb,innodb_buffer_pool_size 1.7 gb
551
551
{
552
- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
552
+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
553
553
return uint64 (1700 * mb ), nil
554
554
})
555
- defer guard .Unpatch ()
555
+ defer guard .Reset ()
556
556
557
557
memoryCase := resource .NewQuantity (2 * gb , resource .BinarySI )
558
558
testMysqlCase := testMysql
@@ -569,10 +569,10 @@ func TestEnsureMysqlConf(t *testing.T) {
569
569
}
570
570
// cpu 1 corev1s,memory 2 gb,innodb_buffer_pool_size 1.7 gb, sizeToBytes error
571
571
{
572
- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
572
+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
573
573
return uint64 (1700 * mb ), fmt .Errorf ("error" )
574
574
})
575
- defer guard .Unpatch ()
575
+ defer guard .Reset ()
576
576
memoryCase := resource .NewQuantity (2 * gb , resource .BinarySI )
577
577
testMysqlCase := testMysql
578
578
testMysqlCase .Spec .MysqlOpts .Resources .Requests ["memory" ] = * memoryCase
@@ -588,10 +588,10 @@ func TestEnsureMysqlConf(t *testing.T) {
588
588
}
589
589
// cpu 8 corev1s,memory 16 gb,innodb_buffer_pool_size 2 gb
590
590
{
591
- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
591
+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
592
592
return uint64 (2 * gb ), nil
593
593
})
594
- defer guard .Unpatch ()
594
+ defer guard .Reset ()
595
595
596
596
memoryCase := resource .NewQuantity (16 * gb , resource .BinarySI )
597
597
limitCpucorev1sCase := resource .NewQuantity (4 , resource .DecimalSI )
@@ -650,10 +650,10 @@ func TestSizeToBytes(t *testing.T) {
650
650
}
651
651
// it will return the result of ParseUint() when the parameter without unit
652
652
{
653
- guard := Patch (strconv .ParseUint , func (s string , base int , bitSize int ) (uint64 , error ) {
653
+ guard := gomonkey . ApplyFunc (strconv .ParseUint , func (s string , base int , bitSize int ) (uint64 , error ) {
654
654
return uint64 (666 ), nil
655
655
})
656
- defer guard .Unpatch ()
656
+ defer guard .Reset ()
657
657
658
658
testCase := "1000"
659
659
want := uint64 (666 )
@@ -663,10 +663,10 @@ func TestSizeToBytes(t *testing.T) {
663
663
}
664
664
// ParseUint error
665
665
{
666
- guard := Patch (strconv .ParseUint , func (s string , base int , bitSize int ) (uint64 , error ) {
666
+ guard := gomonkey . ApplyFunc (strconv .ParseUint , func (s string , base int , bitSize int ) (uint64 , error ) {
667
667
return uint64 (777 ), fmt .Errorf ("error" )
668
668
})
669
- defer guard .Unpatch ()
669
+ defer guard .Reset ()
670
670
671
671
testCase := "1000k"
672
672
want := uint64 (0 )
@@ -679,21 +679,21 @@ func TestSizeToBytes(t *testing.T) {
679
679
func TestGetPrefixFromEnv (t * testing.T ) {
680
680
// Prefix is empty.
681
681
{
682
- guard := Patch (os .Getenv , func (key string ) string {
682
+ patch := gomonkey . ApplyFunc (os .Getenv , func (key string ) string {
683
683
return ""
684
684
})
685
- defer guard . Unpatch ()
685
+ defer patch . Reset ()
686
686
687
687
want := ""
688
688
result := GetPrefixFromEnv ()
689
689
assert .Equal (t , want , result )
690
690
}
691
691
// Prefix is not empty.
692
692
{
693
- guard := Patch (os .Getenv , func (key string ) string {
693
+ patch := gomonkey . ApplyFunc (os .Getenv , func (key string ) string {
694
694
return "docker.io"
695
695
})
696
- defer guard . Unpatch ()
696
+ defer patch . Reset ()
697
697
698
698
want := "docker.io/"
699
699
result := GetPrefixFromEnv ()
0 commit comments