Skip to content

Commit 62535bf

Browse files
authored
Merge pull request #485 from runkecheng/feature_gomonkey
mysqlcluster: Use gomonkey instead of Monkey. #483
2 parents 3d0df14 + 1883bdb commit 62535bf

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ module github.com/radondb/radondb-mysql-kubernetes
33
go 1.16
44

55
require (
6-
bou.ke/monkey v1.0.2
6+
github.com/agiledragon/gomonkey/v2 v2.2.0
77
github.com/blang/semver v3.5.1+incompatible
88
github.com/go-ini/ini v1.62.0
99
github.com/go-logr/logr v0.4.0
1010
github.com/go-sql-driver/mysql v1.6.0
1111
github.com/go-test/deep v1.0.7
12-
github.com/golang/glog v1.0.0
1312
github.com/iancoleman/strcase v0.0.0-20190422225806-e506e3ef7365
1413
github.com/imdario/mergo v0.3.12
1514
github.com/onsi/ginkgo/v2 v2.0.0

go.sum

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
2-
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
31
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
42
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
53
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
@@ -48,6 +46,8 @@ github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMo
4846
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4947
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
5048
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
49+
github.com/agiledragon/gomonkey/v2 v2.2.0 h1:QJWqpdEhGV/JJy70sZ/LDnhbSlMrqHAWHcNOjz1kyuI=
50+
github.com/agiledragon/gomonkey/v2 v2.2.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
5151
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5252
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
5353
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@@ -99,7 +99,6 @@ github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8
9999
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
100100
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
101101
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
102-
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153 h1:yUdfgN0XgIJw7foRItutHYUIhlcKzcSf5vDpdhQAKTc=
103102
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
104103
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
105104
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
@@ -152,8 +151,6 @@ github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zV
152151
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
153152
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
154153
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
155-
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
156-
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
157154
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
158155
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
159156
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -300,7 +297,6 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
300297
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
301298
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
302299
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
303-
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
304300
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
305301
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635/go.mod h1:FBS0z0QWA44HXygs7VXDUOGoN/1TV3RuWkLO04am3wc=
306302
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=

mysqlcluster/mysqlcluster_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
679679
func 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

Comments
 (0)