Skip to content

Commit 92a425e

Browse files
committed
Add test that unmanages resource if there's AWS error
1 parent 5c50ea9 commit 92a425e

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

pkg/runtime/reconciler_test.go

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222
"time"
2323

24+
"github.com/aws/smithy-go"
2425
"github.com/stretchr/testify/assert"
2526
"github.com/stretchr/testify/mock"
2627
"github.com/stretchr/testify/require"
@@ -218,6 +219,63 @@ func TestReconcilerCreate_BackoffRetries(t *testing.T) {
218219
rm.AssertNumberOfCalls(t, "ReadOne", 6)
219220
}
220221

222+
type awsError struct {
223+
smithy.APIError
224+
}
225+
226+
func (err awsError) Error() string {
227+
return "mock error"
228+
}
229+
230+
func TestReconcilerCreate_UnmanageResourceOnAWSErrors(t *testing.T) {
231+
require := require.New(t)
232+
233+
ctx := context.TODO()
234+
arn := ackv1alpha1.AWSResourceName("mybook-arn")
235+
236+
desired, desiredRTObj, _ := resourceMocks()
237+
desired.On("ReplaceConditions", []*ackv1alpha1.Condition{}).Return()
238+
239+
ids := &ackmocks.AWSResourceIdentifiers{}
240+
ids.On("ARN").Return(&arn)
241+
242+
latest, latestRTObj, _ := resourceMocks()
243+
latest.On("Identifiers").Return(ids)
244+
245+
latest.On("Conditions").Return([]*ackv1alpha1.Condition{})
246+
latest.On(
247+
"ReplaceConditions",
248+
mock.AnythingOfType("[]*v1alpha1.Condition"),
249+
).Return()
250+
251+
rm := &ackmocks.AWSResourceManager{}
252+
rm.On("ResolveReferences", ctx, nil, desired).Return(
253+
desired, false, nil,
254+
).Times(2)
255+
rm.On("ClearResolvedReferences", desired).Return(desired)
256+
rm.On("ClearResolvedReferences", latest).Return(latest)
257+
rm.On("ReadOne", ctx, desired).Return(
258+
latest, ackerr.NotFound,
259+
).Once()
260+
rm.On("Create", ctx, desired).Return(
261+
latest, awsError{},
262+
)
263+
rm.On("IsSynced", ctx, latest).Return(false, nil)
264+
rmf, rd := managedResourceManagerFactoryMocks(desired, latest)
265+
rd.On("IsManaged", desired).Return(true)
266+
rd.On("MarkUnmanaged", desired)
267+
rd.On("ResourceFromRuntimeObject", desiredRTObj).Return(desired)
268+
rd.On("Delta", desired, desired).Return(ackcompare.NewDelta())
269+
270+
r, kc, scmd := reconcilerMocks(rmf)
271+
rm.On("EnsureTags", ctx, desired, scmd).Return(nil)
272+
// Use specific matcher for WithoutCancel context instead of mock.Anything
273+
kc.On("Patch", withoutCancelContextMatcher, latestRTObj, mock.AnythingOfType("*client.mergeFromPatch")).Return(nil)
274+
_, err := r.Sync(ctx, rm, desired)
275+
require.NotNil(err)
276+
rm.AssertNumberOfCalls(t, "ReadOne", 1)
277+
}
278+
221279
func TestReconcilerReadOnlyResource(t *testing.T) {
222280
require := require.New(t)
223281

@@ -1237,7 +1295,7 @@ func TestReconcilerHandleReconcilerError_PatchStatus_Latest(t *testing.T) {
12371295

12381296
_, err := r.HandleReconcileError(ctx, desired, latest, nil)
12391297
require.Nil(err)
1240-
statusWriter.AssertCalled(t, "Patch", withoutCancelContextMatcher, latestRTObj, mock.AnythingOfType("*client.mergeFromPatch"))
1298+
statusWriter.AssertCalled(t, "Patch", withoutCancelContextMatcher, latestRTObj, mock.AnythingOfType("*client.mergeFromPatch"))
12411299
// The HandleReconcilerError function never updates spec or metadata, so
12421300
// even though there is a change to the annotations we expect no call to
12431301
// patch the spec/metadata...

0 commit comments

Comments
 (0)