Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xhd2015 opened this issue Mar 31, 2024 · 11 comments
Open
Labels
proposal Proposal new ideas

Comments

@xhd2015
Copy link
Owner

xhd2015 commented Mar 31, 2024

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 xhd2015 added the proposal Proposal new ideas label Mar 31, 2024
@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 1, 2024

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

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 1, 2024

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
Copy link
Owner Author

xhd2015 commented Apr 1, 2024

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

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

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 1, 2024

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

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 1, 2024

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
Copy link
Owner Author

xhd2015 commented Apr 1, 2024

Solution to first problem:

  • inherit parent goroutine's interceptors

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

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 2, 2024

  • inherit parent goroutine's interceptors

This has been supported 5b757ae

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 2, 2024

Replaced all gomonkey patches in iotexproject/iotex-core: iotexproject/iotex-core#4219

Update: TencentBlueKing/bk-bcs TencentBlueKing/bk-bcs#3099

  • fixed an issue: go code on the system stack can't defer, see 2861a46

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 2, 2024

@xhd2015
Copy link
Owner Author

xhd2015 commented Apr 17, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposal new ideas
Projects
None yet
Development

No branches or pull requests

1 participant