@@ -23,7 +23,7 @@ import (
2323 "strconv"
2424 "testing"
2525
26- . "bou.ke/monkey "
26+ "github.com/agiledragon/gomonkey/v2 "
2727 "github.com/stretchr/testify/assert"
2828 corev1 "k8s.io/api/core/v1"
2929 "k8s.io/apimachinery/pkg/api/resource"
@@ -408,18 +408,18 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
408408 },
409409 },
410410 }
411- guard := PatchInstanceMethod (reflect .TypeOf (cluster ), "GetLabels" , func (* MysqlCluster ) labels.Set {
411+ guard := gomonkey . ApplyMethod (reflect .TypeOf (cluster ), "GetLabels" , func (* MysqlCluster ) labels.Set {
412412 return nil
413413 })
414- guard1 := Patch (resource .MustParse , func (_ string ) resource.Quantity {
414+ guard1 := gomonkey . ApplyFunc (resource .MustParse , func (_ string ) resource.Quantity {
415415 return resource.Quantity {}
416416 })
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 {
418418 return nil
419419 })
420- defer guard .Unpatch ()
421- defer guard1 .Unpatch ()
422- defer guard2 .Unpatch ()
420+ defer guard .Reset ()
421+ defer guard1 .Reset ()
422+ defer guard2 .Reset ()
423423 result , err := testCase .EnsureVolumeClaimTemplates (& scheme )
424424 assert .Equal (t , want , result )
425425 assert .Nil (t , err )
@@ -435,10 +435,10 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
435435 testCase := MysqlCluster {
436436 & testMysql ,
437437 }
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 {
439439 return nil
440440 })
441- defer guard .Unpatch ()
441+ defer guard .Reset ()
442442 result , err := testCase .EnsureVolumeClaimTemplates (& scheme )
443443
444444 assert .Equal (t , & storageClass , result [0 ].Spec .StorageClassName )
@@ -453,10 +453,10 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
453453 testCase := MysqlCluster {
454454 & testMysql ,
455455 }
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 {
457457 return fmt .Errorf ("test" )
458458 })
459- defer guard .Unpatch ()
459+ defer guard .Reset ()
460460 result , err := testCase .EnsureVolumeClaimTemplates (& scheme )
461461 want := fmt .Errorf ("failed setting controller reference: test" )
462462 assert .Nil (t , result )
@@ -531,10 +531,10 @@ func TestEnsureMysqlConf(t *testing.T) {
531531 }
532532 // cpu 1 corev1s,memory 1 gb,innodb_buffer_pool_size 600 mb
533533 {
534- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
534+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
535535 return uint64 (600 * mb ), nil
536536 })
537- defer guard .Unpatch ()
537+ defer guard .Reset ()
538538
539539 testMysqlCase := testMysql
540540 testMysqlCase .Spec .MysqlOpts .MysqlConf ["innodb_buffer_pool_size" ] = strconv .FormatUint (uint64 (600 * mb ), 10 )
@@ -549,10 +549,10 @@ func TestEnsureMysqlConf(t *testing.T) {
549549 }
550550 // cpu 1 corev1s,memory 2 gb,innodb_buffer_pool_size 1.7 gb
551551 {
552- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
552+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
553553 return uint64 (1700 * mb ), nil
554554 })
555- defer guard .Unpatch ()
555+ defer guard .Reset ()
556556
557557 memoryCase := resource .NewQuantity (2 * gb , resource .BinarySI )
558558 testMysqlCase := testMysql
@@ -569,10 +569,10 @@ func TestEnsureMysqlConf(t *testing.T) {
569569 }
570570 // cpu 1 corev1s,memory 2 gb,innodb_buffer_pool_size 1.7 gb, sizeToBytes error
571571 {
572- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
572+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
573573 return uint64 (1700 * mb ), fmt .Errorf ("error" )
574574 })
575- defer guard .Unpatch ()
575+ defer guard .Reset ()
576576 memoryCase := resource .NewQuantity (2 * gb , resource .BinarySI )
577577 testMysqlCase := testMysql
578578 testMysqlCase .Spec .MysqlOpts .Resources .Requests ["memory" ] = * memoryCase
@@ -588,10 +588,10 @@ func TestEnsureMysqlConf(t *testing.T) {
588588 }
589589 // cpu 8 corev1s,memory 16 gb,innodb_buffer_pool_size 2 gb
590590 {
591- guard := Patch (sizeToBytes , func (s string ) (uint64 , error ) {
591+ guard := gomonkey . ApplyFunc (sizeToBytes , func (s string ) (uint64 , error ) {
592592 return uint64 (2 * gb ), nil
593593 })
594- defer guard .Unpatch ()
594+ defer guard .Reset ()
595595
596596 memoryCase := resource .NewQuantity (16 * gb , resource .BinarySI )
597597 limitCpucorev1sCase := resource .NewQuantity (4 , resource .DecimalSI )
@@ -650,10 +650,10 @@ func TestSizeToBytes(t *testing.T) {
650650 }
651651 // it will return the result of ParseUint() when the parameter without unit
652652 {
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 ) {
654654 return uint64 (666 ), nil
655655 })
656- defer guard .Unpatch ()
656+ defer guard .Reset ()
657657
658658 testCase := "1000"
659659 want := uint64 (666 )
@@ -663,10 +663,10 @@ func TestSizeToBytes(t *testing.T) {
663663 }
664664 // ParseUint error
665665 {
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 ) {
667667 return uint64 (777 ), fmt .Errorf ("error" )
668668 })
669- defer guard .Unpatch ()
669+ defer guard .Reset ()
670670
671671 testCase := "1000k"
672672 want := uint64 (0 )
@@ -679,21 +679,21 @@ func TestSizeToBytes(t *testing.T) {
679679func TestGetPrefixFromEnv (t * testing.T ) {
680680 // Prefix is empty.
681681 {
682- guard := Patch (os .Getenv , func (key string ) string {
682+ patch := gomonkey . ApplyFunc (os .Getenv , func (key string ) string {
683683 return ""
684684 })
685- defer guard . Unpatch ()
685+ defer patch . Reset ()
686686
687687 want := ""
688688 result := GetPrefixFromEnv ()
689689 assert .Equal (t , want , result )
690690 }
691691 // Prefix is not empty.
692692 {
693- guard := Patch (os .Getenv , func (key string ) string {
693+ patch := gomonkey . ApplyFunc (os .Getenv , func (key string ) string {
694694 return "docker.io"
695695 })
696- defer guard . Unpatch ()
696+ defer patch . Reset ()
697697
698698 want := "docker.io/"
699699 result := GetPrefixFromEnv ()
0 commit comments