Skip to content

Commit

Permalink
fix(clouddriver): change exception handling in ClouddriverClearAltTab…
Browse files Browse the repository at this point in the history
…lespaceTask (#4597)

to check for Spinnaker*Exception since, as of #4528, CloudDriverCacheService
uses SpinnakerRetrofitErrorHandler
  • Loading branch information
Pranav-b-7 committed Nov 17, 2023
1 parent baacddb commit 42a9a1a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Collections.emptyList;

import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerServerException;
import com.netflix.spinnaker.orca.api.pipeline.Task;
import com.netflix.spinnaker.orca.api.pipeline.TaskResult;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
Expand All @@ -17,8 +18,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import retrofit.RetrofitError;
import retrofit.mime.TypedByteArray;

@Component
public class ClouddriverClearAltTablespaceTask implements Task {
Expand Down Expand Up @@ -48,17 +47,11 @@ public TaskResult execute(@Nonnull StageExecution stage) {
result.getOrDefault("tables", emptyList()));

return TaskResult.builder(ExecutionStatus.SUCCEEDED).context(result).build();
} catch (RetrofitError e) {
} catch (SpinnakerServerException e) {
Map<String, Object> output = new HashMap<>();
List<String> errors = new ArrayList<>();

if (e.getResponse() != null && e.getResponse().getBody() != null) {
String error = new String(((TypedByteArray) e.getResponse().getBody()).getBytes());
log.error("Failed clearing clouddriver table namespace: {}", error, e);
errors.add(error);
} else {
errors.add(e.getMessage());
}
errors.add(e.getMessage());
output.put("errors", errors);
return TaskResult.builder(ExecutionStatus.TERMINAL).context(output).build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2023 OpsMx, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.netflix.spinnaker.orca.clouddriver.tasks.cache;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerServerException;
import com.netflix.spinnaker.orca.api.pipeline.TaskResult;
import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionStatus;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.clouddriver.CloudDriverCacheService;
import com.netflix.spinnaker.orca.pipeline.model.StageExecutionImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class ClouddriverClearAltTablespaceTaskTest {

private CloudDriverCacheService cloudDriverCacheService;

private ClouddriverClearAltTablespaceTask clouddriverClearAltTablespaceTask;

@BeforeEach
public void setup() {
cloudDriverCacheService = mock(CloudDriverCacheService.class);
clouddriverClearAltTablespaceTask =
new ClouddriverClearAltTablespaceTask(cloudDriverCacheService);
}

@Test
void testSpinnakerServerExceptionHandling() {

String namespace = "test-namespace";
StageExecution stageExecution = new StageExecutionImpl();
Map<String, Object> context = new HashMap<>();
List<String> errors = new ArrayList<>();

context.put("namespace", namespace);
stageExecution.setContext(context);
Response mockResponse =
new Response(
"http://foo.com/admin/db/truncate/" + namespace,
400,
"bad-request",
Collections.emptyList(),
null);

RetrofitError retrofitError =
RetrofitError.httpError(
"https://foo.com/admin/db/truncate/" + namespace, mockResponse, null, null);
SpinnakerServerException spinnakerServerException = new SpinnakerServerException(retrofitError);
errors.add(spinnakerServerException.getMessage());

when(cloudDriverCacheService.clearNamespace(namespace)).thenThrow(spinnakerServerException);
TaskResult taskResult = clouddriverClearAltTablespaceTask.execute(stageExecution);

assertEquals(taskResult.getStatus(), ExecutionStatus.TERMINAL);
assertTrue(taskResult.getContext().containsKey("errors"));
assertEquals(errors.toString(), taskResult.getContext().get("errors").toString());
}
}

0 comments on commit 42a9a1a

Please sign in to comment.