openshift / cluster-api-provider-libvirt

Apache License 2.0
36 stars 56 forks source link

Fix unit test for controller #250

Open praveenkumar opened 1 year ago

praveenkumar commented 1 year ago

With #249 the actuator tests are failing like below and this issue is to track so we can fix it later in time. As of now priority is to update latest machine api and vendoring part.

$ make test
 go test -race -cover ./cmd/... ./pkg/cloud/...
?       github.com/openshift/cluster-api-provider-libvirt/cmd/manager   [no test files]
E0131 15:06:10.408695   14100 actuator.go:107] Machine error: error getting machineProviderConfig from spec: no Value in ProviderConfig
E0131 15:06:10.411129   14100 actuator.go:107] Machine error: error creating libvirt client: error creating libvirt client
E0131 15:06:10.412790   14100 actuator.go:107] Machine error: error creating volume error
E0131 15:06:10.412874   14100 actuator.go:51] libvirt-actuator-testing-machine: error creating libvirt machine: error creating volume error
E0131 15:06:10.414281   14100 actuator.go:107] Machine error: error creating domain error
E0131 15:06:10.414341   14100 actuator.go:51] libvirt-actuator-testing-machine: error creating libvirt machine: error creating domain error
E0131 15:06:10.415850   14100 actuator.go:107] Machine error: error looking up libvirt machine error
E0131 15:06:10.415920   14100 actuator.go:51] libvirt-actuator-testing-machine: error creating libvirt machine: error looking up libvirt machine error
E0131 15:06:10.420175   14100 actuator.go:107] Machine error: error getting machineProviderConfig from spec: no Value in ProviderConfig
E0131 15:06:10.421666   14100 actuator.go:107] Machine error: error creating libvirt client: error creating libvirt client
E0131 15:06:10.423274   14100 actuator.go:107] Machine error: error checking for domain existence: error
E0131 15:06:10.426872   14100 actuator.go:107] Machine error: error deleting "libvirt-actuator-testing-machine" domain error
E0131 15:06:10.429808   14100 actuator.go:107] Machine error: error deleting "libvirt-actuator-testing-machine" volume error
--- FAIL: TestMachineEvents (0.03s)
    --- FAIL: TestMachineEvents/Create_machine_event_failed_(invalid_configuration) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.Close() /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:193
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Create_machine_event_failed_(error_creating_libvirt_client) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: missing call(s) to *mock.MockClient.Close() /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:193
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Delete_machine_event_failed_(invalid_configuration) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: missing call(s) to *mock.MockClient.Close() /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:193
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Delete_machine_event_failed_(error_creating_libvirt_client) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.Close() /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:193
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Delete_machine_event_failed_(error_getting_domain) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Delete_machine_event_failed_(error_deleting_domain) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Delete_machine_event_failed_(error_deleting_volume) (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: aborting test due to missing call(s)
    --- FAIL: TestMachineEvents/Delete_machine_event_succeeds (0.00s)
        controller.go:137: missing call(s) to *mock.MockClient.GetDHCPLeasesByNetwork(is anything) /go/src/github.com/openshift/cluster-api-provider-libvirt/pkg/cloud/libvirt/actuators/machine/actuator_test.go:198
        controller.go:137: aborting test due to missing call(s) 
praveenkumar commented 1 year ago

I tested #251 with cluster-bot release image and it works, I will also add the nightly or ec bits which have this commit if someone wants to test it

cfergeau commented 1 year ago

It's a bug in the unit test which became apparent after upgrading go-mock to its latest version. The test was missing:

diff --git a/pkg/cloud/libvirt/actuators/machine/actuator_test.go b/pkg/cloud/libvirt/actuators/machine/actuator_test.go
index a0ca8290..7a5771c0 100644
--- a/pkg/cloud/libvirt/actuators/machine/actuator_test.go
+++ b/pkg/cloud/libvirt/actuators/machine/actuator_test.go
@@ -213,6 +213,7 @@ func TestMachineEvents(t *testing.T) {
                        default:
                                t.Errorf("Expected %q event, got none", tc.event)
                        }
+                       mockCtrl.Finish()
                })
        }
 }

It was only seeming to succeed because Finish() was never called. When upgrading go-mock from 1.3.x to 1.6.0, we get this commit https://github.com/golang/mock/commit/6d816de489c18a7e9a8fbd2aa5bb2dd75f2bbc86 which automatically calls Finish() at the end of the test and makes it obvious that the test is broken.

cfergeau commented 1 year ago
diff --git a/pkg/cloud/libvirt/actuators/machine/actuator_test.go b/pkg/cloud/libvirt/actuators/machine/actuator_test.go
index fa89e42e..72502883 100644
--- a/pkg/cloud/libvirt/actuators/machine/actuator_test.go
+++ b/pkg/cloud/libvirt/actuators/machine/actuator_test.go
@@ -190,12 +190,12 @@ func TestMachineEvents(t *testing.T) {
                                },
                        }

-                       mockLibvirtClient.EXPECT().Close()
+                       mockLibvirtClient.EXPECT().Close().AnyTimes()
                        mockLibvirtClient.EXPECT().CreateVolume(gomock.Any()).Return(tc.createVolumeErr).AnyTimes()
                        mockLibvirtClient.EXPECT().DeleteVolume(gomock.Any()).Return(tc.deleteVolumeErr).AnyTimes()
                        mockLibvirtClient.EXPECT().CreateDomain(context.TODO(), gomock.Any()).Return(tc.createDomainErr).AnyTimes()
                        mockLibvirtClient.EXPECT().DeleteDomain(gomock.Any()).Return(tc.deleteDomainErr).AnyTimes()
-                       mockLibvirtClient.EXPECT().GetDHCPLeasesByNetwork(gomock.Any())
+                       mockLibvirtClient.EXPECT().GetDHCPLeasesByNetwork(gomock.Any()).AnyTimes()
                        mockLibvirtClient.EXPECT().LookupDomainByName(gomock.Any()).Return(tc.lookupDomainOutput, tc.lookupDomainErr).AnyTimes()
                        mockLibvirtClient.EXPECT().DomainExists(gomock.Any()).Return(tc.domainExists, tc.domainExistsErr).AnyTimes()

This allows the test to succeed but I'm unconvinced it's a great fix, this basically says "the mock interface expects all these methods to be called 0 or more times", this is not testing a lot. These expectations should probably be set per test-case rather than globally.

openshift-bot commented 1 year ago

Issues go stale after 90d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close. Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle stale

openshift-bot commented 1 year ago

Stale issues rot after 30d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity. Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle rotten /remove-lifecycle stale

openshift-bot commented 1 year ago

Rotten issues close after 30d of inactivity.

Reopen the issue by commenting /reopen. Mark the issue as fresh by commenting /remove-lifecycle rotten. Exclude this issue from closing again by commenting /lifecycle frozen.

/close

openshift-ci[bot] commented 1 year ago

@openshift-bot: Closing this issue.

In response to [this](https://github.com/openshift/cluster-api-provider-libvirt/issues/250#issuecomment-1626372904): >Rotten issues close after 30d of inactivity. > >Reopen the issue by commenting `/reopen`. >Mark the issue as fresh by commenting `/remove-lifecycle rotten`. >Exclude this issue from closing again by commenting `/lifecycle frozen`. > >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
praveenkumar commented 1 year ago

/reopen

This is still not fixed.

praveenkumar commented 1 year ago

/lifecycle frozen

openshift-ci[bot] commented 1 year ago

@praveenkumar: Reopened this issue.

In response to [this](https://github.com/openshift/cluster-api-provider-libvirt/issues/250#issuecomment-1628552035): >/reopen > >This is still not fixed. Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.