diff --git a/internal/controller/ansibleRun/ansibleRun_test.go b/internal/controller/ansibleRun/ansibleRun_test.go index 689a9a1..d63c83c 100644 --- a/internal/controller/ansibleRun/ansibleRun_test.go +++ b/internal/controller/ansibleRun/ansibleRun_test.go @@ -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" @@ -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 @@ -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{