Skip to content

Commit

Permalink
fix(): broken UTs for cluster and VPN
Browse files Browse the repository at this point in the history
Signed-off-by: gourishkb <[email protected]>
  • Loading branch information
gourishkb committed Feb 16, 2024
1 parent 64d8377 commit 6bbe30c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 20 deletions.
45 changes: 45 additions & 0 deletions pkg/hub/controllers/cluster/deregister_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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{}),
Expand Down
105 changes: 85 additions & 20 deletions pkg/hub/controllers/vpnkeyrotation/reconciler_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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"}),
Expand Down Expand Up @@ -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"}),
Expand All @@ -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"}),
Expand All @@ -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"}),
Expand Down Expand Up @@ -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"}),
Expand Down Expand Up @@ -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"}),
Expand Down Expand Up @@ -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}),
Expand Down Expand Up @@ -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}),
Expand Down Expand Up @@ -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}),
Expand Down Expand Up @@ -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"}),
Expand Down Expand Up @@ -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{}),
Expand Down Expand Up @@ -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")

Expand Down

0 comments on commit 6bbe30c

Please sign in to comment.