Skip to content

Commit

Permalink
add unit tests for retrying failed ObserveAndDelete runs
Browse files Browse the repository at this point in the history
Signed-off-by: Dasha Komsa <komsa.darya@gmail.com>
  • Loading branch information
d-honeybadger committed Feb 1, 2024
1 parent dd4143a commit 8fc237f
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions internal/controller/ansibleRun/ansibleRun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/spf13/afero"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -535,6 +536,26 @@ func TestObserve(t *testing.T) {
err error
}

testPlaybook := "fake playbook"
testRun := v1alpha1.AnsibleRun{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
v1.LastAppliedConfigAnnotation: fmt.Sprintf(`{"playbookInline":"%s"}`, testPlaybook),
},
},
Spec: v1alpha1.AnsibleRunSpec{
ForProvider: v1alpha1.AnsibleRunParameters{
PlaybookInline: &testPlaybook,
},
},
}

testRunWithReconcileSuccess := testRun.DeepCopy()
testRunWithReconcileSuccess.SetConditions(xpv1.ReconcileSuccess())

testRunWithReconcileError := testRun.DeepCopy()
testRunWithReconcileError.SetConditions(xpv1.ReconcileError(errors.New("fake error")))

cases := map[string]struct {
reason string
fields fields
Expand Down Expand Up @@ -583,6 +604,69 @@ func TestObserve(t *testing.T) {
err: fmt.Errorf("%s: %w", errGetAnsibleRun, errBoom),
},
},
"UnchangedWithObserveAndDeletePolicy": {
reason: "We should not run ansible when spec has not changed and last sync was successful",
fields: fields{
kube: &test.MockClient{
MockGet: test.NewMockGetFn(nil, func(obj client.Object) error {
obj = testRunWithReconcileSuccess
return nil
}),
MockUpdate: test.NewMockUpdateFn(nil),
},
runner: &MockRunner{
MockAnsibleRunPolicy: func() *ansible.RunPolicy {
return &ansible.RunPolicy{
Name: "ObserveAndDelete",
}
},
MockWriteExtraVar: func(extraVar map[string]interface{}) error {
return nil
},
MockRun: func() (*exec.Cmd, io.Reader, error) {
return nil, nil, fmt.Errorf("run should not have been called")
},
},
},
args: args{
mg: testRunWithReconcileSuccess,
},
want: want{
o: managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true},
},
},
"RetryFailedWithObserveAndDeletePolicy": {
reason: "We should run ansible when spec has not changed but last sync was unsuccessful",
fields: fields{
kube: &test.MockClient{
MockGet: test.NewMockGetFn(nil, func(obj client.Object) error {
obj = testRunWithReconcileError
return nil
}),
MockUpdate: test.NewMockUpdateFn(nil),
},
runner: &MockRunner{
MockAnsibleRunPolicy: func() *ansible.RunPolicy {
return &ansible.RunPolicy{
Name: "ObserveAndDelete",
}
},
MockWriteExtraVar: func(extraVar map[string]interface{}) error {
return nil
},
MockRun: func() (*exec.Cmd, io.Reader, error) {
cmd := exec.Command("ls")
return cmd, nil, cmd.Start()
},
},
},
args: args{
mg: testRunWithReconcileError,
},
want: want{
o: managed.ExternalObservation{ResourceExists: true, ResourceUpToDate: true},
},
},
"GetObservedErrorWhenCheckWhenObservePolicy": {
reason: "We should return any error we encounter getting observed resource",
fields: fields{
Expand Down

0 comments on commit 8fc237f

Please sign in to comment.