Skip to content

Commit

Permalink
Add additional unit-tests for Conditions
Browse files Browse the repository at this point in the history
Make sure each condition is defined (as best we can)
Enhancements to operator-framework#91

Signed-off-by: Todd Short <tshort@redhat.com>
  • Loading branch information
tmshort committed Jan 17, 2023
1 parent 081c4b0 commit b0b532a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
9 changes: 8 additions & 1 deletion api/v1alpha1/operator_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ type OperatorSpec struct {
}

const (
// TODO(user): add more Types
// TODO(user): add more Types, here and into GetTypes()
TypeReady = "Ready"

// TODO(user): add more Reasons
ReasonNotImplemented = "NotImplemented"
)

func GetTypes() []string {
// TODO(user): add Types from above
return []string{
TypeReady,
}
}

// OperatorStatus defines the observed state of Operator
type OperatorStatus struct {
// +patchMergeKey=type
Expand Down
4 changes: 2 additions & 2 deletions controllers/operator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ func (r *OperatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
compareOp := reconciledOp.DeepCopy()
existingOp.Status, compareOp.Status = operatorsv1alpha1.OperatorStatus{}, operatorsv1alpha1.OperatorStatus{}
existingOp.Finalizers, compareOp.Finalizers = []string{}, []string{}
specDiffers := !equality.Semantic.DeepEqual(existingOp, compareOp)
unexpectedFieldsChanged := !equality.Semantic.DeepEqual(existingOp, compareOp)

if updateStatus {
if updateErr := r.Status().Update(ctx, reconciledOp); updateErr != nil {
return res, utilerrors.NewAggregate([]error{reconcileErr, updateErr})
}
}

if specDiffers {
if unexpectedFieldsChanged {
panic("spec or metadata changed by reconciler")
}

Expand Down
21 changes: 19 additions & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ var _ = Describe("Reconcile Test", func() {
err = k8sClient.Delete(ctx, operator)
Expect(err).To(Not(HaveOccurred()))
})
It("has a Condition created", func() {
It("has all Conditions created", func() {
getOperator := &operatorsv1alpha1.Operator{}

err = k8sClient.Get(ctx, client.ObjectKey{
Expand All @@ -138,7 +138,24 @@ var _ = Describe("Reconcile Test", func() {
// There should always be a "Ready" condition, regardless of Status.
conds := getOperator.Status.Conditions
Expect(conds).To(Not(BeEmpty()))
Expect(conds).To(ContainElement(HaveField("Type", operatorsv1alpha1.TypeReady)))
types := operatorsv1alpha1.GetTypes()
Expect(conds).To(HaveLen(len(types)))
for _, t := range types {
Expect(conds).To(ContainElement(HaveField("Type", t)))
}
})
It("has matching gnerations in Conditions", func() {
getOperator := &operatorsv1alpha1.Operator{}

err = k8sClient.Get(ctx, client.ObjectKey{
Name: opName,
}, getOperator)
Expect(err).To(Not(HaveOccurred()))

// The ObservedGeneration should match the resource generation
for _, c := range getOperator.Status.Conditions {
Expect(c).To(HaveField("ObservedGeneration", getOperator.GetGeneration()))
}
})
})
})

0 comments on commit b0b532a

Please sign in to comment.