

GitHub - bouk/monkey: Monkey patching in Go
source link: https://github.com/bouk/monkey
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Go monkeypatching 

Actual arbitrary monkeypatching for Go. Yes really.
Read this blogpost for an explanation on how it works: https://bou.ke/blog/monkey-patching-in-go/
I thought that monkeypatching in Go is impossible?
It's not possible through regular language constructs, but we can always bend computers to our will! Monkey implements monkeypatching by rewriting the running executable at runtime and inserting a jump to the function you want called instead. This is as unsafe as it sounds and I don't recommend anyone do it outside of a testing environment.
Make sure you read the notes at the bottom of the README if you intend to use this library.
Using monkey
Monkey's API is very simple and straightfoward. Call monkey.Patch(<target function>, <replacement function>)
to replace a function. For example:
package main import ( "fmt" "os" "strings" "bou.ke/monkey" ) func main() { monkey.Patch(fmt.Println, func(a ...interface{}) (n int, err error) { s := make([]interface{}, len(a)) for i, v := range a { s[i] = strings.Replace(fmt.Sprint(v), "hell", "*bleep*", -1) } return fmt.Fprintln(os.Stdout, s...) }) fmt.Println("what the hell?") // what the *bleep*? }
You can then call monkey.Unpatch(<target function>)
to unpatch the method again. The replacement function can be any function value, whether it's anonymous, bound or otherwise.
If you want to patch an instance method you need to use monkey.PatchInstanceMethod(<type>, <name>, <replacement>)
. You get the type by using reflect.TypeOf
, and your replacement function simply takes the instance as the first argument. To disable all network connections, you can do as follows for example:
package main import ( "fmt" "net" "net/http" "reflect" "bou.ke/monkey" ) func main() { var d *net.Dialer // Has to be a pointer to because `Dial` has a pointer receiver monkey.PatchInstanceMethod(reflect.TypeOf(d), "Dial", func(_ *net.Dialer, _, _ string) (net.Conn, error) { return nil, fmt.Errorf("no dialing allowed") }) _, err := http.Get("http://google.com") fmt.Println(err) // Get http://google.com: no dialing allowed }
Note that patching the method for just one instance is currently not possible, PatchInstanceMethod
will patch it for all instances. Don't bother trying monkey.Patch(instance.Method, replacement)
, it won't work. monkey.UnpatchInstanceMethod(<type>, <name>)
will undo PatchInstanceMethod
.
If you want to remove all currently applied monkeypatches simply call monkey.UnpatchAll
. This could be useful in a test teardown function.
If you want to call the original function from within the replacement you need to use a monkey.PatchGuard
. A patchguard allows you to easily remove and restore the patch so you can call the original function. For example:
package main import ( "fmt" "net/http" "reflect" "strings" "bou.ke/monkey" ) func main() { var guard *monkey.PatchGuard guard = monkey.PatchInstanceMethod(reflect.TypeOf(http.DefaultClient), "Get", func(c *http.Client, url string) (*http.Response, error) { guard.Unpatch() defer guard.Restore() if !strings.HasPrefix(url, "https://") { return nil, fmt.Errorf("only https requests allowed") } return c.Get(url) }) _, err := http.Get("http://google.com") fmt.Println(err) // only https requests allowed resp, err := http.Get("https://google.com") fmt.Println(resp.Status, err) // 200 OK <nil> }
Notes
- Monkey sometimes fails to patch a function if inlining is enabled. Try running your tests with inlining disabled, for example:
go test -gcflags=-l
. The same command line argument can also be used for build. - Monkey won't work on some security-oriented operating system that don't allow memory pages to be both write and execute at the same time. With the current approach there's not really a reliable fix for this.
- Monkey is not threadsafe. Or any kind of safe.
- I've tested monkey on OSX 10.10.2 and Ubuntu 14.04. It should work on any unix-based x86 or x86-64 system.
© Bouke van der Bijl
Recommend
-
49
Errors are red, Outputs are white, File descriptor injection is bold, Let me tell you about LD_PRELOAD . Red errors are importa...
-
15
Many people think that monkey patching is something that is restricted to dynamic languages like Ruby and Python. That is not true however, as computers are just dumb machines and we can always make them do what we want!...
-
8
Why I am not using Masochism for my master-slave setups and why monkey-patching isn’t the only solution I got a message this morning from Gregg at Ruby5 asking why...
-
14
extension methods In some programming languages, there is a style of “monkey-patching” classes in order to add convenience methods. For example, let’s say that we’re using Ruby on Rails, and we...
-
18
Here are (semi)random notes, trying to outline a system of views on controversional “monkey-patching” topic—the views I myself consider sane and reasonable. It may seem other way for you, but it could be a start for rational discussion....
-
11
February 18, 2021 Fix .NET Dependencies With Monkey Patching ...
-
10
Monkey patching in NodeJS Reading Time: 3 minutes Hey guys, I am back with another blog in my series of blogs on mocking i...
-
11
README.md An open source kernel extensi...
-
7
Using GitHub Release for patching Monorepo npm package Jul 29 ・4 min read
-
3
A Tasty Treats Podcast for...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK