-
Notifications
You must be signed in to change notification settings - Fork 189
CCO-837: Replace deprecated golang/mock with go.uber.org/mock #1069
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: openshift/cloud-credential-operator
Length of output: 252
🌐 Web query:
go.uber.org/mock gomock NewController testing.T auto cleanup Finish idempotent documentation💡 Result:
In GoMock (including the version at go.uber.org/mock), the gomock.NewController(t) function automatically handles cleanup for you when provided with a *testing.T, starting from Go 1.14 [1][2][3]. Key details regarding this functionality: Automatic Cleanup: When you pass a *testing.T (or any type implementing the Cleanup method) to NewController, the library registers a cleanup function using t.Cleanup [1][2]. This function triggers ctrl.Finish automatically at the end of the test, removing the need to manually call defer ctrl.Finish [1][2][3]. Idempotency and Multiple Calls: The Finish method itself is not inherently idempotent [1][2]. Internally, it uses a flag (ctrl.finished) to track state [1][2]. If Finish is called more than once, it will trigger a fatal error (via T.Fatalf) to notify you of the misuse, unless it is being handled by the automatic cleanup mechanism which manages the state safely [1][2]. Best Practice: Because of the automatic cleanup provided by Go 1.14+, calling ctrl.Finish manually is now considered unnecessary and is often marked as deprecated [4]. You should rely on the automatic invocation handled by the controller via t.Cleanup to ensure test expectations are verified correctly without manual intervention [3].
Citations:
🏁 Script executed:
Repository: openshift/cloud-credential-operator
Length of output: 1265
🌐 Web query:
go.uber.org/mock gomock NewController finish cleanup t.Cleanup documentation💡 Result:
In Gomock, specifically when using
go.uber.org/mock/gomock, you generally do not need to explicitly callctrl.Finish()if you are using Go 1.14 or later and passing*testing.T(or anytesting.TBimplementation that supportsCleanup) togomock.NewController(t)[1][2][3]. When you provide a*testing.TtoNewController, the library checks if the provided reporter implements aCleanupmethod [1][4]. If it does,gomockregisters its internalfinishmethod witht.Cleanup()[1]. This ensures thatFinish()—which validates that all expected mock calls occurred—is automatically executed when the test completes, regardless of whether the test passes or fails (or panics) [1][2]. If you are using an older version of Go (pre-1.14) or a test reporter that does not supportCleanup, you must continue to manually calldefer ctrl.Finish()to ensure your mock expectations are verified [2][5]. Key points to remember: 1. Automatic Cleanup: Only occurs iftpassed toNewControllersupportsCleanup(added in Go 1.14) [1][2]. 2. Idempotency:ctrl.Finish()is designed to be idempotent; calling it multiple times will not cause issues, as the controller tracks whether it has already finished [1][4]. 3. Best Practice: While automatic cleanup reduces boilerplate, deferringFinish()remains a safe and compatible pattern across all versions of Go and Gomock [2][6].Citations:
🏁 Script executed:
Repository: openshift/cloud-credential-operator
Length of output: 822
🌐 Web query:
site:github.com/go.uber.org/mock gomock/controller.go Finish idempotent t.Cleanup source💡 Result:
In
go.uber.org/mock/gomock,Controller.Finish()is not idempotent and should only be called once. If you pass*testing.TtoNewController, it registers at.Cleanupthat callsFinish()automatically after the test and subtests complete, so you usually should not callctrl.Finish()yourself. (pkg.go.dev)If you want, I can also point you to the exact
controller.gologic in the repo.Citations:
🌐 Web query:
"go.uber.org/mock/gomock" Finish idempotent Cleanup source controller.go💡 Result:
The query appears to conflate two distinct concepts in Go development: the gomock library (now maintained as go.uber.org/mock) and Kubernetes controller patterns for idempotent cleanup. 1. gomock and Controller.Finish: In the gomock library (go.uber.org/mock), the Controller.Finish method is used to verify that all expected method calls occurred [1][2]. Historically, it was not idempotent and required explicit calling [2][3]. However, since Go 1.14, if you pass a *testing.T to gomock.NewController(t), the library automatically registers a cleanup function using t.Cleanup to call Finish for you [2][3]. This removes the need for manual invocation in standard tests [3]. 2. Kubernetes Controller Idempotent Cleanup: In the context of Kubernetes controllers (e.g., source-controller.go, often built with controller-runtime), "idempotent cleanup" typically refers to the implementation of Finalizers [4][5]. When a resource is marked for deletion (DeletionTimestamp is non-zero), the controller must perform cleanup tasks (like removing external cloud resources) in an idempotent manner so that the logic can safely run multiple times without side effects [4]. Once the cleanup is confirmed successful, the finalizer is removed from the resource to allow Kubernetes to complete the deletion [4][5]. The phrase "Finish idempotent Cleanup source controller.go" likely combines a search for gomock's Finish mechanism with the common requirement to write idempotent cleanup logic within a Kubernetes source-controller.
Citations:
Finishautomatically golang/mock#407🌐 Web query:
go.uber.org/mock README Finish defer ctrl.Finish safe double call t.Cleanup💡 Result:
When using
go.uber.org/mock(or the originalgolang/mock), callingctrl.Finish()is used to verify that all expectations set on a mock controller have been met [1][2]. For Go 1.14 and later, when you pass a*testing.T(or anytesting.TB) togomock.NewController(t), the library automatically registers at.Cleanup()function to callctrl.Finish()[1][3][4]. BecauseFinish()is designed to be safe to call multiple times, this automatic cleanup eliminates the need for an explicitdefer ctrl.Finish()in your test code [1][3][4]. If you callctrl.Finish()manually and it is also called byt.Cleanup, the implementation handles the double call safely [1][3][4]. While some earlier development efforts inuber-go/mockattempted to markctrl.Finish()as deprecated to encourage this automatic behavior, community feedback noted that explicit calls are still necessary for certain specific test frameworks, so the function remains available for use [5][6].Citations:
Finishautomatically golang/mock#407🌐 Web query:
go.uber.org/mock v0.6.0 controller.go Finish called multiple times safe idempotent💡 Result:
The Controller.Finish method in go.uber.org/mock v0.6.0 is not idempotent [1]. If it is called more than once, the library will trigger a failure [2][1]. Specifically, the implementation in controller.go checks the internal finished state; if Finish has already been called, it uses the testing reporter to call t.Fatalf, which aborts the test with the error message: "Controller.Finish was called more than once. It has to be called exactly once." [2] To avoid this, you should rely on the automatic cleanup provided by the library [1]. When you pass a *testing.T (or a compatible TestReporter) into gomock.NewController, the controller registers a cleanup function that automatically invokes Finish when the test and its subtests complete [3][1]. Consequently, explicit calls to Finish are unnecessary in modern Go tests [3][1].
Citations:
🌐 Web query:
site:github.com/uber-go/mock controller.go "finish" "Cleanup" "finished"💡 Result:
The phrase "Cleanup finished" does not appear to be a standard or recognizable log message associated with controller.go in prominent Go libraries like gomock or Kubernetes controller-runtime. In the context of the uber-go/mock (gomock) library, there is a prominent function named Controller.Finish defined in controller.go [1]. This function is used in testing to verify that all expected mock calls have been satisfied [2][3]. Regarding "Cleanup": - Modern Go (since 1.14) provides t.Cleanup in the testing package, which allows tests to register cleanup functions that run automatically when the test finishes [2]. - In gomock, the Controller.Finish method was marked as deprecated because it is often unnecessary to call manually when t.Cleanup is used to automatically handle cleanup, although it remains available for specific test framework requirements [2][3]. If you are seeing a specific log message "Cleanup finished" in a file named controller.go, it is likely custom logging code within your specific project or a library not standard to common Go infrastructure, rather than a built-in message from standard Go mocking or controller libraries. You should check the source code for that string to identify which logger or function is outputting it.
Citations:
Controller.Finishbreaks certain test frameworks uber-go/mock#81Remove the explicit
Finish()callsgomock.NewController(t)already registers cleanup viat.Cleanup, so these extra finalizers can tripController.Finish was called more than oncein these tests.pkg/azure/actuator_test.go#L556-L557,pkg/azure/actuator_test.go#L682-L702pkg/cmd/provisioning/gcp/create_service_accounts_test.go#L190-L191pkg/operator/secretannotator/gcp/reconciler_test.go#L218-L219📍 Affects 3 files
pkg/azure/actuator_test.go#L34-L34(this comment)pkg/cmd/provisioning/gcp/create_service_accounts_test.go#L12-L12pkg/operator/secretannotator/gcp/reconciler_test.go#L25-L25🤖 Prompt for AI Agents
Source: MCP tools