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

test: replace gomonkey with xgo #3099

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xhd2015
Copy link

@xhd2015 xhd2015 commented Apr 2, 2024

This PR changes test cases that use gomonkey, replacing that with xgo.

The xgo, which is a native-go solution of monkey patching, have the following advantages compared to gomonkey:

  • Compatibility
    • xgo provides support through go1.17 to go1.22, for all OS and Arch, while gomonkey has limited support
  • Stability
    • xgo does not need to modify readonly area, so on MacOS developers does not need any extra setup
  • Concurrent Safety
    • xgo has built-in concurrent safety, patchings are isolated among goroutines, tests do not affect each other. While gomonkey is not concurrently safe
  • Simplicity
    • xgo provides a powerful API named Patch, which can smartly recognize the target to mock

With this introduction of xgo and removal of gomonkey, future tests can use xgo reliably.

The xgo project: https://github.com/xhd2015/xgo.

To run the tests, xgo needs to be installed:

# macOS and Linux (and WSL)
curl -fsSL https://github.com/xhd2015/xgo/raw/master/install.sh | bash

# windows
powershell -c "irm github.com/xhd2015/xgo/raw/master/install.ps1|iex"

Then run:

cd chaosmeta-inject-operator 
xgo test -v ./pkg/secret

Output:

=== RUN   TestInitProject
--- PASS: TestInitProject (0.18s)
=== RUN   TestGetSecret
--- PASS: TestGetSecret (0.00s)
=== RUN   TestGetMetadata
--- PASS: TestGetMetadata (0.00s)
=== RUN   TestListSecret
--- PASS: TestListSecret (0.00s)
=== RUN   TestCreatePutSecret
--- PASS: TestCreatePutSecret (0.00s)
=== RUN   TestDeleteSecret
--- PASS: TestDeleteSecret (0.00s)
PASS
ok      github.com/Tencent/bk-bcs/bcs-scenarios/bcs-gitops-vaultplugin-server/pkg/secret        3.235s

@tencentblueking-adm
Copy link

tencentblueking-adm commented Apr 2, 2024

CLA assistant check
All committers have signed the CLA.

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

Successfully merging this pull request may close these issues.

None yet

2 participants