From 6bbe30c386d9c774e289229a996e9a2791041f1f Mon Sep 17 00:00:00 2001 From: gourishkb <104021126+gourishkb@users.noreply.github.com> Date: Fri, 16 Feb 2024 14:39:11 +0530 Subject: [PATCH] fix(): broken UTs for cluster and VPN Signed-off-by: gourishkb <104021126+gourishkb@users.noreply.github.com> --- .../cluster/deregister_unit_test.go | 45 ++++++++ .../vpnkeyrotation/reconciler_unit_test.go | 105 ++++++++++++++---- 2 files changed, 130 insertions(+), 20 deletions(-) diff --git a/pkg/hub/controllers/cluster/deregister_unit_test.go b/pkg/hub/controllers/cluster/deregister_unit_test.go index f01c01eaf..dde36f003 100644 --- a/pkg/hub/controllers/cluster/deregister_unit_test.go +++ b/pkg/hub/controllers/cluster/deregister_unit_test.go @@ -123,6 +123,11 @@ func TestCreateDeregisterJobPositiveScenarios(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -225,6 +230,11 @@ func TestReconcilerFailToUpdateClusterRegistrationStatus(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(errors.New("error updating status of deregistration on the controller")) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(errors.New("error updating status of deregistration on the controller")) err := reconciler.createDeregisterJob(ctx, testClusterObj) if expected.errMsg != err.Error() { @@ -263,6 +273,11 @@ func TestReconcilerFailToCreateServiceAccount(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -316,6 +331,11 @@ func TestReconcilerFailToFetchOperatorClusterRole(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -379,6 +399,11 @@ func TestReconcilerFailToCreateClusterRole(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -452,6 +477,11 @@ func TestReconcilerFailToCreateClusterRoleBinding(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -530,6 +560,11 @@ func TestReconcilerFailToCreateConfigmap(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -622,6 +657,11 @@ func TestReconcilerFailToDeleteJob(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), @@ -725,6 +765,11 @@ func TestReconcilerFailToCreateDeregisterJob(t *testing.T) { mock.IsType(&hubv1alpha1.Cluster{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.Cluster{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&kubeslicev1beta1.SliceList{}), diff --git a/pkg/hub/controllers/vpnkeyrotation/reconciler_unit_test.go b/pkg/hub/controllers/vpnkeyrotation/reconciler_unit_test.go index 5a5fa3b1a..4a5b35c53 100644 --- a/pkg/hub/controllers/vpnkeyrotation/reconciler_unit_test.go +++ b/pkg/hub/controllers/vpnkeyrotation/reconciler_unit_test.go @@ -54,26 +54,26 @@ var testVPNKeyRotationObject = &hubv1alpha1.VpnKeyRotation{ Status: hubv1alpha1.VpnKeyRotationStatus{}, } -var testVPNKeyRotationObjectWithInProgressStatus = &hubv1alpha1.VpnKeyRotation{ - ObjectMeta: metav1.ObjectMeta{ - Name: testVPNKeyRotationName, - Namespace: testProjectNamespace, - }, - Spec: hubv1alpha1.VpnKeyRotationSpec{ - ClusterGatewayMapping: map[string][]string{ - "cluster1": {"fire-worker-2-worker-1", "fire-worker-2-worker-3", "fire-worker-2-worker-4"}, - }, - CertificateCreationTime: &metav1.Time{Time: time.Now()}, - }, - Status: hubv1alpha1.VpnKeyRotationStatus{ - CurrentRotationState: map[string]hubv1alpha1.StatusOfKeyRotation{ - "fire-worker-2-worker-1": { - Status: hubv1alpha1.InProgress, - LastUpdatedTimestamp: metav1.Time{Time: time.Now()}, - }, - }, - }, -} +// var testVPNKeyRotationObjectWithInProgressStatus = &hubv1alpha1.VpnKeyRotation{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: testVPNKeyRotationName, +// Namespace: testProjectNamespace, +// }, +// Spec: hubv1alpha1.VpnKeyRotationSpec{ +// ClusterGatewayMapping: map[string][]string{ +// "cluster1": {"fire-worker-2-worker-1", "fire-worker-2-worker-3", "fire-worker-2-worker-4"}, +// }, +// CertificateCreationTime: &metav1.Time{Time: time.Now()}, +// }, +// Status: hubv1alpha1.VpnKeyRotationStatus{ +// CurrentRotationState: map[string]hubv1alpha1.StatusOfKeyRotation{ +// "fire-worker-2-worker-1": { +// Status: hubv1alpha1.InProgress, +// LastUpdatedTimestamp: metav1.Time{Time: time.Now()}, +// }, +// }, +// }, +// } var testVPNKeyRotationObjectWithIntervalTest = &hubv1alpha1.VpnKeyRotation{ ObjectMeta: metav1.ObjectMeta{ @@ -323,6 +323,11 @@ func TestReconcilerVPNRotationReconcilerCompletionAsSuccess(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Create", mock.IsType(ctx), mock.IsType(&corev1.Event{}), @@ -397,6 +402,11 @@ func TestReconcilerVPNRotationReconcilerIntervalTestNegative(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1-1"}), @@ -476,6 +486,11 @@ func TestReconcilerVPNRotationReconcilerIntervalTest(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1-0"}), @@ -491,6 +506,11 @@ func TestReconcilerVPNRotationReconcilerIntervalTest(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1"}), @@ -517,6 +537,11 @@ func TestReconcilerVPNRotationReconcilerIntervalTest(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1"}), @@ -602,6 +627,11 @@ func TestReconcilerVPNRotationReconcilerTriggerFSM(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1-0"}), @@ -643,6 +673,11 @@ func TestReconcilerVPNRotationReconcilerTriggerFSM(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1"}), @@ -760,6 +795,11 @@ func TestReconcilerSecretCreationError(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1-0", Namespace: ControlPlaneNamespace}), @@ -879,6 +919,11 @@ func TestReconcilerControllerSecretNotFound(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1-0", Namespace: ControlPlaneNamespace}), @@ -990,6 +1035,11 @@ func TestReconcilerSecretCreationAndRequeue(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1-0", Namespace: ControlPlaneNamespace}), @@ -1046,6 +1096,11 @@ func TestReconcilerSecretCreationAndRequeue(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("Get", mock.IsType(ctx), mock.IsType(types.NamespacedName{Name: "fire-worker-2-worker-1"}), @@ -1111,6 +1166,11 @@ func TestReconcileRotationSyncingClusterAttach(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) client.On("List", mock.IsType(ctx), mock.IsType(&corev1.PodList{}), @@ -1212,6 +1272,11 @@ func TestReconcileRotationSyncingClusterDetach(t *testing.T) { mock.IsType(&hubv1alpha1.VpnKeyRotation{}), mock.IsType([]k8sclient.UpdateOption(nil)), ).Return(nil) + client.StatusMock.On("Update", + mock.IsType(ctx), + mock.IsType(&hubv1alpha1.VpnKeyRotation{}), + mock.IsType([]k8sclient.SubResourceUpdateOption(nil)), + ).Return(nil) os.Setenv("CLUSTER_NAME", "worker-2")