-
Notifications
You must be signed in to change notification settings - Fork 486
Fix TestReconcileBatchJob #2350
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
Changes from 9 commits
2e204c1
13e39b3
6b6e249
ac0c04e
539ca6e
f0d5fd7
4a52a18
b4caa68
5cb9b2b
317c40d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,7 +21,6 @@ import ( | |||||
"testing" | ||||||
"time" | ||||||
|
||||||
"github.com/golang/mock/gomock" | ||||||
"github.com/onsi/gomega" | ||||||
"github.com/prometheus/client_golang/prometheus" | ||||||
"github.com/spf13/viper" | ||||||
|
@@ -43,7 +42,6 @@ import ( | |||||
"github.com/kubeflow/katib/pkg/controller.v1beta1/consts" | ||||||
trialutil "github.com/kubeflow/katib/pkg/controller.v1beta1/trial/util" | ||||||
"github.com/kubeflow/katib/pkg/controller.v1beta1/util" | ||||||
managerclientmock "github.com/kubeflow/katib/pkg/mock/v1beta1/trial/managerclient" | ||||||
) | ||||||
|
||||||
const ( | ||||||
|
@@ -58,7 +56,7 @@ var trialKey = types.NamespacedName{Name: trialName, Namespace: namespace} | |||||
var batchJobKey = types.NamespacedName{Name: batchJobName, Namespace: namespace} | ||||||
|
||||||
func init() { | ||||||
logf.SetLogger(zap.New()) | ||||||
logf.SetLogger(zap.New(zap.UseDevMode(true))) | ||||||
} | ||||||
|
||||||
func TestAdd(t *testing.T) { | ||||||
|
@@ -89,9 +87,7 @@ func TestAdd(t *testing.T) { | |||||
func TestReconcileBatchJob(t *testing.T) { | ||||||
g := gomega.NewGomegaWithT(t) | ||||||
|
||||||
mockCtrl := gomock.NewController(t) | ||||||
defer mockCtrl.Finish() | ||||||
mockManagerClient := managerclientmock.NewMockManagerClient(mockCtrl) | ||||||
mockMC := &mockManagerClient{} | ||||||
|
||||||
// Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a | ||||||
// channel when it is finished. | ||||||
|
@@ -102,7 +98,7 @@ func TestReconcileBatchJob(t *testing.T) { | |||||
r := &ReconcileTrial{ | ||||||
Client: mgr.GetClient(), | ||||||
scheme: mgr.GetScheme(), | ||||||
ManagerClient: mockManagerClient, | ||||||
ManagerClient: mockMC, | ||||||
recorder: mgr.GetEventRecorderFor(ControllerName), | ||||||
collector: trialutil.NewTrialsCollector(mgr.GetCache(), prometheus.NewRegistry()), | ||||||
} | ||||||
|
@@ -179,137 +175,146 @@ func TestReconcileBatchJob(t *testing.T) { | |||||
}, | ||||||
} | ||||||
|
||||||
mockManagerClient.EXPECT().GetTrialObservationLog(gomock.Any()).Return(observationLogAvailable, nil).Times(1) | ||||||
mockManagerClient.EXPECT().GetTrialObservationLog(gomock.Any()).Return(observationLogUnavailable, nil).MinTimes(1) | ||||||
mockManagerClient.EXPECT().DeleteTrialObservationLog(gomock.Any()).Return(nil, nil).AnyTimes() | ||||||
t.Run(`Trial run with "Failed" BatchJob.`, func(t *testing.T) { | ||||||
g := gomega.NewGomegaWithT(t) | ||||||
mockMC.msg = observationLogUnavailable | ||||||
|
mockMC.msg = observationLogUnavailable | |
mockManagerClient.EXPECT().GetTrialObservationLog(gomock.Any()).Return(observationLogAvailable, nil).AnyTimes() |
Instead of this original mock client, can we reuse the autogenerated mock clients like this?
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.
We need WithOverridableExpectations here, but github.com/golang/mock/mock does not support this feature.
Or "Each test should create a new Controller".
In my opinion, gomock is not flexible enough, and it complicates a simple thing for this scenario.
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.
Finally, I found another way.
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.
@forsaken628 I opened the issue to replace gomock with go.uber.com/gomock here: #2356
As a separate PR, could you work on the issue?
Uh oh!
There was an error while loading. Please reload this page.
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.
TBH, we should reimplement these tests as ginkgo BDD style, but that is out of scope of this PR.
So, I leave such refactoring in the future.