Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willdavsmith committed Sep 26, 2023
1 parent c407c9e commit e4a017e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions pkg/recipes/driver/bicep.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,16 @@ func (d *bicepDriver) Delete(ctx context.Context, opts DeleteOptions) error {
return nil
}

for attempt := 1; attempt <= d.options.DeleteRetryCount; attempt++ {
allowedAttempts := d.options.DeleteRetryCount + 1
var err error
for attempt := 1; attempt <= allowedAttempts; attempt++ {
logger.WithValues("attempt", attempt)
ctx := logr.NewContext(groupCtx, logger)
logger.V(ucplog.LevelDebug).Info("beginning attempt")

err := d.ResourceClient.Delete(ctx, id)
err = d.ResourceClient.Delete(ctx, id)
if err != nil {
if attempt < d.options.DeleteRetryCount {
if attempt <= allowedAttempts {
logger.V(ucplog.LevelInfo).Error(err, "attempt failed", "delay", d.options.DeleteRetryDelaySeconds)
time.Sleep(time.Duration(d.options.DeleteRetryDelaySeconds) * time.Second)
continue
Expand All @@ -219,8 +221,8 @@ func (d *bicepDriver) Delete(ctx context.Context, opts DeleteOptions) error {
return nil
}

err := fmt.Errorf("failed to delete resource after %d attempts", d.options.DeleteRetryCount)
return recipes.NewRecipeError(recipes.RecipeDeletionFailed, err.Error(), "", recipes.GetRecipeErrorDetails(err))
deletionErr := fmt.Errorf("failed to delete resource after %d attempt(s), last error: %s", allowedAttempts, err.Error())
return recipes.NewRecipeError(recipes.RecipeDeletionFailed, deletionErr.Error(), "", recipes.GetRecipeErrorDetails(deletionErr))
})
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/recipes/driver/bicep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func setupDeleteInputs(t *testing.T) (bicepDriver, *processors.MockResourceClien
driver := bicepDriver{
ResourceClient: client,
options: BicepOptions{
DeleteRetryCount: 1,
DeleteRetryCount: 0,
DeleteRetryDelaySeconds: 1,
},
}
Expand Down Expand Up @@ -418,7 +418,7 @@ func Test_Bicep_Delete_Error(t *testing.T) {
recipeError := recipes.RecipeError{
ErrorDetails: v1.ErrorDetails{
Code: recipes.RecipeDeletionFailed,
Message: fmt.Sprintf("could not find API version for type %q, no supported API versions", outputResources[0].GetResourceType().Type),
Message: fmt.Sprintf("failed to delete resource after 1 attempt(s), last error: could not find API version for type %q, no supported API versions", outputResources[0].GetResourceType().Type),
},
}
client.EXPECT().
Expand Down Expand Up @@ -528,6 +528,8 @@ func Test_GetGCOutputResources_NoDiff(t *testing.T) {
func Test_Bicep_Delete_Success_AfterRetry(t *testing.T) {
ctx := testcontext.New(t)
driver, client := setupDeleteInputs(t)
driver.options.DeleteRetryCount = 1

outputResources := []rpv1.OutputResource{
{
ID: resources_kubernetes.IDFromParts(
Expand All @@ -543,7 +545,7 @@ func Test_Bicep_Delete_Success_AfterRetry(t *testing.T) {
gomock.InOrder(
client.EXPECT().
Delete(gomock.Any(), "/planes/kubernetes/local/namespaces/recipe-app/providers/core/Deployment/redis").
Return(fmt.Errorf("could not find API version for type %q, no supported API versions", outputResources[0].GetResourceType().Type)),
Return(fmt.Errorf("failed to delete resource after 1 attempt(s), last error: could not find API version for type %q, no supported API versions", outputResources[0].GetResourceType().Type)),
client.EXPECT().
Delete(gomock.Any(), "/planes/kubernetes/local/namespaces/recipe-app/providers/core/Deployment/redis").
Return(nil),
Expand Down

0 comments on commit e4a017e

Please sign in to comment.