xhd2015 / xgo

All-in-one go testing library
MIT License
330 stars 19 forks source link

Proposal: actively replacing gomonkey and mockery in open source projects #18

Open xhd2015 opened 6 months ago

xhd2015 commented 6 months ago

Since gomonkey and mockery both aim to provide mock for go, each in different ways. And their method should be considered outdated as xgo has evovled.

Thus, I propose we find active projects(has commits in last 2 months) that are still using these two library, finding how their tests are written with the help of the 2 library, and try to create a PR to replace them with xgo, demonstrating that xgo is a better choice, even the choice of the state of art.

xhd2015 commented 6 months ago

That may include providing a base image which includes xgo, which can be used in github workflows to automating testing.

xhd2015 commented 6 months ago

gomonkey usages: https://github.com/kubeedge/kubeedge/blob/master/cloud/cmd/cloudcore/app/server_test.go#:~:text=gomonkey.NewPatches https://github.com/kubeedge/kubeedge/blob/master/edge/pkg/metamanager/metaserver/kubernetes/storage/storage_test.go#:~:text=gomonkey.NewPatches

Running test without any setup gives the following error:

$ go test -timeout 30s -run ^TestNegotiateTunnelPort$ github.com/kubeedge/kubeedge/cloud/cmd/cloudcore/app

--- FAIL: TestNegotiateTunnelPort (0.03s)
panic: permission denied [recovered]
    panic: permission denied

goroutine 173 [running]:
testing.tRunner.func1.2({0x101acd880, 0x1031a1968})
    /Users/xhd2015/installed/go1.21.7/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
    /Users/xhd2015/installed/go1.21.7/src/testing/testing.go:1548 +0x397
panic({0x101acd880?, 0x1031a1968?})
    /Users/xhd2015/installed/go1.21.7/src/runtime/panic.go:914 +0x21f
github.com/agiledragon/gomonkey.modifyBinary(0x101590500, {0xc00081f6dc, 0xc, 0x10191b620?})
    /Users/xhd2015/Projects/xhd2015/kubeedge/vendor/github.com/agiledragon/gomonkey/modify_binary_darwin.go:11 +0x116

This is a valid case that can be replaced by xgo.

xhd2015 commented 6 months ago

Example: https://github.com/traas-stack/chaosmeta

Total: 19 patches using gomonkey out of 74 cases, can all be rewritten using xgo

xhd2015 commented 6 months ago

Example: https://github.com/brokercap/Bifrost Total: 43 patches using gomonkey out of 275 cases

xhd2015 commented 6 months ago

Example: https://github.com/traas-stack/chaosmeta

Total: 19 patches using gomonkey out of 74 cases, can all be rewritten using xgo

Update: so far two major blockers: 1.When a goroutine started, all mock are lost, causing failures 2.When mock from init, the target function is not yet registered, so mock failed

xhd2015 commented 6 months ago

Solution to first problem:

Solution to second problem: defer these mock/trap calls during init, defer them at the end of all init.

xhd2015 commented 6 months ago
  • inherit parent goroutine's interceptors

This has been supported https://github.com/xhd2015/xgo/commit/5b757aef0696c3e82b44ac96d340880e33c80648

xhd2015 commented 6 months ago

Next:

Heavy use:

xhd2015 commented 6 months ago

Replaced all gomonkey patches in iotexproject/iotex-core: https://github.com/iotexproject/iotex-core/pull/4219

Update: TencentBlueKing/bk-bcs https://github.com/TencentBlueKing/bk-bcs/pull/3099

xhd2015 commented 6 months ago
xhd2015 commented 5 months ago

Adding: