Skip to content

Commit 901eb1d

Browse files
committed
MGMT-23548: Ensure tests w/ gomock controllers call Finish()
1 parent 99890ee commit 901eb1d

18 files changed

+79
-12
lines changed

CLAUDE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,18 @@ The project has three main test categories:
368368
- Upstream: [assisted-test-infra](https://github.com/openshift/assisted-test-infra)
369369
- Downstream: QE maintained tests
370370

371+
### Test Patterns
372+
373+
#### Mock Testing with Gomock
374+
375+
All tests using gomock controllers MUST call `ctrl.Finish()` to verify mock expectations.
376+
Without `ctrl.Finish()`, gomock EXPECT() calls with constraints are not verified:
377+
- `.Times(N)` - Expected call count won't be checked
378+
- `.MaxTimes(0)` - Unexpected calls won't be caught
379+
- `.Do()` / `.DoAndReturn()` - Mock behavior runs without verification
380+
381+
Each context that creates a controller needs its own `AfterEach` with `ctrl.Finish()`.
382+
371383
### Environment Variables
372384
373385
#### Test Execution

internal/cluster/common_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ var _ = Describe("update_cluster_state", func() {
8282
})
8383
AfterEach(func() {
8484
common.DeleteTestDB(db, dbName)
85+
ctrl.Finish()
8586
})
8687

8788
})

internal/controller/controllers/clusterdeployments_controller_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ var _ = Describe("processMirrorRegistryConfig", func() {
418418

419419
Expect(err.Error()).To(Equal("failed to load value of registries.conf into toml tree; incorrectly formatted toml: (6, 13): cannot have two dots in one float"))
420420
})
421+
422+
AfterEach(func() {
423+
mockCtrl.Finish()
424+
})
421425
})
422426

423427
var _ = Describe("cluster reconcile", func() {
@@ -2050,6 +2054,10 @@ var _ = Describe("cluster reconcile", func() {
20502054
Expect(err).To(BeNil())
20512055
Expect(result).To(Equal(ctrl.Result{}))
20522056
})
2057+
2058+
AfterEach(func() {
2059+
mockCtrl.Finish()
2060+
})
20532061
})
20542062

20552063
Context("cluster installation", func() {
@@ -5199,6 +5207,10 @@ var _ = Describe("unbindAgents", func() {
51995207
err = c.Get(ctx, types.NamespacedName{Name: "test-agent2", Namespace: testNamespace}, agent)
52005208
Expect(k8serrors.IsNotFound(err)).To(BeTrue())
52015209
})
5210+
5211+
AfterEach(func() {
5212+
mockCtrl.Finish()
5213+
})
52025214
})
52035215

52045216
var _ = Describe("day2 cluster", func() {

internal/host/hostcommands/container_image_availability_cmd_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ var _ = Describe("container_image_availability_cmd", func() {
107107

108108
AfterEach(func() {
109109
common.DeleteTestDB(db, dbName)
110+
ctrl.Finish()
110111
})
111112
})
112113

@@ -173,4 +174,8 @@ var _ = Describe("get images", func() {
173174
Expect(images[0]).To(ContainSubstring("registry.mirror.example.com"))
174175
Expect(images[0]).NotTo(ContainSubstring("quay.io"))
175176
})
177+
178+
AfterEach(func() {
179+
ctrl.Finish()
180+
})
176181
})

internal/host/hostcommands/disk_performance_cmd_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,6 @@ var _ = Describe("disk_performance", func() {
7272
common.DeleteTestDB(db, dbName)
7373
stepReply = nil
7474
stepErr = nil
75+
ctrl.Finish()
7576
})
7677
})

internal/host/hostcommands/domain_name_resolution_cmd_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ var _ = Describe("domainNameResolution", func() {
8181
It("Missing cluster name", func() {
8282
cluster = common.Cluster{Cluster: models.Cluster{ID: &clusterID, BaseDNSDomain: baseDNSDomain}}
8383
Expect(db.Create(&cluster).Error).ShouldNot(HaveOccurred())
84-
mockVersions.EXPECT().GetReleaseImage(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&models.ReleaseImage{URL: swag.String("quay.io/release")}, nil)
8584
stepReply, stepErr = dCmd.GetSteps(ctx, &host)
8685
Expect(stepReply).To(BeNil())
8786
Expect(stepErr).To(HaveOccurred())
@@ -90,7 +89,6 @@ var _ = Describe("domainNameResolution", func() {
9089
It("Missing domain base name", func() {
9190
cluster = common.Cluster{Cluster: models.Cluster{ID: &clusterID, Name: name}}
9291
Expect(db.Create(&cluster).Error).ShouldNot(HaveOccurred())
93-
mockVersions.EXPECT().GetReleaseImage(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&models.ReleaseImage{URL: swag.String("quay.io/release")}, nil)
9492
stepReply, stepErr = dCmd.GetSteps(ctx, &host)
9593
Expect(stepReply).To(BeNil())
9694
Expect(stepErr).To(HaveOccurred())
@@ -100,5 +98,6 @@ var _ = Describe("domainNameResolution", func() {
10098
common.DeleteTestDB(db, dbName)
10199
stepReply = nil
102100
stepErr = nil
101+
ctrl.Finish()
103102
})
104103
})

internal/host/hostutil/update_host_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@ var _ = Describe("update_host_state", func() {
102102

103103
AfterEach(func() {
104104
common.DeleteTestDB(db, dbName)
105+
ctrl.Finish()
105106
})
106107
})

internal/host/refresh_status_preprocessor_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,9 @@ var _ = Describe("Cluster Refresh Status Preprocessor", func() {
6161
},
6262
}, nil)
6363
mockOperatorManager = operators.NewMockAPI(ctrl)
64-
mockOperatorManager.EXPECT().ValidateHost(gomock.Any(), gomock.Any(), gomock.Any())
6564
mockProviderRegistry = registry.NewMockProviderRegistry(ctrl)
6665
mockS3WrapperAPI = s3wrapper.NewMockAPI(ctrl)
6766
mockVersions = versions.NewMockHandler(ctrl)
68-
mockOperatorManager.EXPECT().ValidateCluster(ctx, gomock.Any())
6967
db, dbName = common.PrepareTestDB()
7068
validatorConfig := &hardware.ValidatorCfg{}
7169
disabledHostValidations := make(map[string]struct{})
@@ -82,6 +80,7 @@ var _ = Describe("Cluster Refresh Status Preprocessor", func() {
8280

8381
AfterEach(func() {
8482
common.DeleteTestDB(db, dbName)
83+
ctrl.Finish()
8584
})
8685

8786
createHost := func() models.Host {

internal/installercache/installercache_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ var _ = Describe("release event", func() {
6262
).Times(1)
6363
Expect(r.Cleanup(ctx)).To(Succeed())
6464
})
65+
66+
AfterEach(func() {
67+
ctrl.Finish()
68+
})
6569
})
6670

6771
var _ = Describe("installer cache", func() {
@@ -103,6 +107,7 @@ var _ = Describe("installer cache", func() {
103107

104108
AfterEach(func() {
105109
os.RemoveAll(cacheDir)
110+
ctrl.Finish()
106111
})
107112

108113
expectEventsSent := func() {

internal/metrics/directory_usage_collector_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ var _ = Describe("Collection on scrape", func() {
4747

4848
AfterEach(func() {
4949
server.Close()
50+
ctrl.Finish()
5051
})
5152

5253
var expectMetricValue = func(metric string, value string) {

0 commit comments

Comments
 (0)