Skip to content

Commit

Permalink
replace monkey with gomonkey
Browse files Browse the repository at this point in the history
  • Loading branch information
samanhappy committed Aug 12, 2024
1 parent 052c266 commit f5dfdb7
Show file tree
Hide file tree
Showing 51 changed files with 215 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-13]
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion conf/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/notify"
"github.com/megaease/easeprobe/notify/discord"
"github.com/megaease/easeprobe/notify/email"
Expand Down
2 changes: 1 addition & 1 deletion conf/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"reflect"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/monkey"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"gopkg.in/natefinch/lumberjack.v2"
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"path/filepath"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"

"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/Knetic/govaluate"
"github.com/megaease/easeprobe/monkey"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion eval/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/megaease/easeprobe/monkey"
"github.com/stretchr/testify/assert"
"golang.org/x/net/html"
)
Expand Down
2 changes: 1 addition & 1 deletion global/easeprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/megaease/easeprobe/monkey"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion global/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/megaease/easeprobe/monkey"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.21
toolchain go1.21.6

require (
bou.ke/monkey v1.0.2
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/agiledragon/gomonkey/v2 v2.11.0
github.com/antchfx/htmlquery v1.3.2
github.com/antchfx/jsonquery v1.3.5
github.com/antchfx/xmlquery v1.4.1
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/Knetic/govaluate v3.0.0+incompatible h1:7o6+MAPhYTCF0+fdvoz1xDedhRb4f6s9Tn1Tt7/WTEg=
github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
github.com/a8m/envsubst v1.4.2 h1:4yWIHXOLEJHQEFd4UjrWDrYeYlV7ncFWJOCBRLOZHQg=
github.com/a8m/envsubst v1.4.2/go.mod h1:MVUTQNGQ3tsjOOtKCNd+fl8RzhsXcDvvAEzkhGtlsbY=
github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U=
github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/assert/v2 v2.3.0/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/participle/v2 v2.1.1 h1:hrjKESvSqGHzRb4yW1ciisFJ4p3MGYih6icjJvbsmV8=
Expand Down Expand Up @@ -77,6 +77,7 @@ github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
Expand All @@ -92,6 +93,7 @@ github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHW
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -152,6 +154,8 @@ github.com/segmentio/kafka-go v0.4.47 h1:IqziR4pA3vrZq7YdRxaT3w1/5fvIH5qpCwstUan
github.com/segmentio/kafka-go v0.4.47/go.mod h1:HjF6XbOKh0Pjlkr5GVZxt6CsjjwnmhVOfURM5KMd8qg=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down Expand Up @@ -200,6 +204,7 @@ golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pB
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand Down Expand Up @@ -244,6 +249,7 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
Expand Down
2 changes: 1 addition & 1 deletion metric/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package metric
import (
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/monkey"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
)
Expand Down
80 changes: 80 additions & 0 deletions monkey/monkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package monkey

import (
"reflect"

"github.com/agiledragon/gomonkey/v2"
)

var patchesMap = make(map[string]*gomonkey.Patches)

// Patch replaces a function with another
func Patch(target, replacement interface{}) *gomonkey.Patches {
key := reflect.TypeOf(target).String()
existingPatches, ok := patchesMap[key]
if ok {
existingPatches.Reset()
}
patches := gomonkey.ApplyFunc(target, replacement)
patchesMap[key] = patches
return patches
}

// Unpatch unpatch a patch
func Unpatch(target interface{}) bool {
patches, ok := patchesMap[reflect.TypeOf(target).String()]
if !ok {
return false
}
patches.Reset()
delete(patchesMap, reflect.TypeOf(target).String())
return true
}

// PatchInstanceMethod replaces an instance method methodName for the type target with replacement
func PatchInstanceMethod(target reflect.Type, methodName string, replacement interface{}) *gomonkey.Patches {
key := target.String() + methodName
existingPatches, ok := patchesMap[key]
if ok {
existingPatches.Reset()
}
patches := gomonkey.ApplyMethod(target, methodName, replacement)
patchesMap[key] = patches
return patches
}

// UnpatchInstanceMethod unpatch a patch
func UnpatchInstanceMethod(target reflect.Type, methodName string) bool {
patches, ok := patchesMap[target.String()+methodName]
if !ok {
return false
}
patches.Reset()
delete(patchesMap, target.String()+methodName)
return true
}

// UnpatchAll unpatch all patches
func UnpatchAll() {
for _, patches := range patchesMap {
patches.Reset()
}
patchesMap = make(map[string]*gomonkey.Patches)
}
77 changes: 77 additions & 0 deletions monkey/monkey_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package monkey

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

type MyStruct struct{}

func (s *MyStruct) Method() string {
return "original"
}

func TestPatch(t *testing.T) {
originalFunc := func() string { return "original" }
replacementFunc := func() string { return "replacement" }

patch := Patch(originalFunc, replacementFunc)
defer patch.Reset()

assert.Equal(t, "replacement", originalFunc())
}

func TestUnpatch(t *testing.T) {
originalFunc := func() string { return "original" }
replacementFunc := func() string { return "replacement" }

Patch(originalFunc, replacementFunc)
Unpatch(originalFunc)

assert.Equal(t, "original", originalFunc())
}

func TestPatchInstanceMethod(t *testing.T) {
PatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method", func(*MyStruct) string { return "replacement" })
defer UnpatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method")

assert.Equal(t, "replacement", (&MyStruct{}).Method())
}

func TestUnpatchInstanceMethod(t *testing.T) {
PatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method", func(*MyStruct) string { return "replacement" })
UnpatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method")

assert.Equal(t, "original", (&MyStruct{}).Method())
}

func TestUnpatchAll(t *testing.T) {
originalFunc := func() string { return "original" }
replacementFunc := func() string { return "replacement" }

Patch(originalFunc, replacementFunc)
PatchInstanceMethod(reflect.TypeOf(&MyStruct{}), "Method", func(*MyStruct) string { return "replacement" })
UnpatchAll()

assert.Equal(t, "original", originalFunc())
assert.Equal(t, "original", (&MyStruct{}).Method())
}
2 changes: 1 addition & 1 deletion notify/aws/sns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"reflect"
"testing"

"bou.ke/monkey"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion notify/dingtalk/dingtalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"strings"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion notify/discord/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"testing"
"time"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/probe"
"github.com/megaease/easeprobe/probe/base"
"github.com/sirupsen/logrus"
Expand Down
2 changes: 1 addition & 1 deletion notify/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"reflect"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
"gopkg.in/gomail.v2"
Expand Down
2 changes: 1 addition & 1 deletion notify/lark/lark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"strings"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion notify/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"os"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion notify/log/log_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"log/syslog"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/stretchr/testify/assert"
)

Expand Down
2 changes: 1 addition & 1 deletion notify/ringcentral/ringcentral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"strings"
"testing"

"bou.ke/monkey"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/monkey"
"github.com/megaease/easeprobe/report"
"github.com/stretchr/testify/assert"
)
Expand Down
Loading

0 comments on commit f5dfdb7

Please sign in to comment.