Skip to content

Commit

Permalink
Add additional unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
  • Loading branch information
Rizwana777 committed Jul 29, 2024
1 parent ebe7814 commit 2e2e5ac
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion controllers/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,16 @@ var _ = Describe("updateStatusConditionOfRolloutManager tests", func() {
Expect(updateStatusConditionOfRolloutManager(ctx, rsr, &rolloutsManager, k8sClient, logger.FromContext(ctx))).To(Succeed())

Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&rolloutsManager), &rolloutsManager)).To(Succeed())

Expect(rolloutsManager.Status.Conditions).To(HaveLen(1))
Expect(rolloutsManager.Status.Conditions[0].Message).To(Equal(newCondition.Message))
Expect(rolloutsManager.Status.Conditions[0].Reason).To(Equal(newCondition.Reason))

// Verify whether an error condition is set when len(reason) > 1.
if len(reason) > 1 {
Expect(newCondition.Reason).To(Equal(rolloutsmanagerv1alpha1.RolloutManagerReasonErrorOccurred))
Expect(newCondition.Message).To(Equal("An internal error occurred"))
Expect(newCondition.Status).To(Equal(metav1.ConditionTrue))
}
},
Entry("should set condition on status"),
Entry("should return error when len(reason) > 1", "my reason 1", "my reason 2"))
Expand Down Expand Up @@ -435,6 +441,39 @@ var _ = Describe("insertOrUpdateConditionsInSlice tests", func() {
})
})

Context("when there is another unrelated condition in the list", func() {
It("should not remove the unrelated condition and return true", func() {
newCondition := metav1.Condition{
Type: rolloutsmanagerv1alpha1.RolloutManagerConditionType,
Status: metav1.ConditionTrue,
Reason: "test reason",
Message: "test message",
}
unrelatedCondition := metav1.Condition{
Type: "UnrelatedCondition",
Status: metav1.ConditionFalse,
Reason: "some reason",
Message: "some message",
}
existingConditions := []metav1.Condition{
unrelatedCondition,
}

changed, conditions := insertOrUpdateConditionsInSlice(newCondition, existingConditions)
Expect(changed).To(BeTrue())
Expect(conditions).To(HaveLen(2))

By("Check that the unrelated condition is still present")
Expect(conditions).To(ContainElement(unrelatedCondition))

By("Check that the new condition was added")
Expect(conditions[1].Type).To(Equal(newCondition.Type))
Expect(conditions[1].Status).To(Equal(newCondition.Status))
Expect(conditions[1].Reason).To(Equal(newCondition.Reason))
Expect(conditions[1].Message).To(Equal(newCondition.Message))
})
})

})

var _ = Describe("isMergable tests", func() {
Expand Down Expand Up @@ -513,6 +552,24 @@ var _ = Describe("setAdditionalRolloutsLabelsAndAnnotationsToObject tests", func
})
})

Context("and obj.Labels and obj.Annotations are are already set with different values", func() {
BeforeEach(func() {
obj.Labels = map[string]string{"key1": "oldValue"}
obj.Annotations = map[string]string{"annotation1": "oldValue"}

cr.Spec.AdditionalMetadata = &rolloutsmanagerv1alpha1.ResourceMetadata{
Labels: map[string]string{"key1": "newValue"},
Annotations: map[string]string{"annotation1": "newValue"},
}
})

It("should replace the existing values with the new values", func() {
setAdditionalRolloutsLabelsAndAnnotationsToObject(obj, cr)
Expect(obj.Labels).To(HaveKeyWithValue("key1", "newValue"))
Expect(obj.Annotations).To(HaveKeyWithValue("annotation1", "newValue"))
})
})

})
})

Expand Down

0 comments on commit 2e2e5ac

Please sign in to comment.