Skip to content

Commit

Permalink
test(clouddriver): demonstrate the behavior of the task WaitOnJobComp…
Browse files Browse the repository at this point in the history
…letion

A set of test cases added to verify the upcoming changes when Front50 service is configured with SpinnakerRetrofitErrorHandler.

These test cases verifies the behavior of the task when different kinds RetrofitErrors are thrown
  • Loading branch information
Pranav-b-7 committed Jan 12, 2024
1 parent 70f2702 commit e0eefc0
Showing 1 changed file with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.netflix.spinnaker.orca.TestUtils.getResource;
import static com.netflix.spinnaker.orca.TestUtils.getResourceAsStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -53,7 +54,11 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.http.HttpStatus;
import retrofit.RetrofitError;
import retrofit.client.Response;
import retrofit.converter.ConversionException;
import retrofit.converter.JacksonConverter;
import retrofit.mime.TypedByteArray;

public final class WaitOnJobCompletionTest {
Expand Down Expand Up @@ -528,6 +533,115 @@ void testPropertyFileContentsErrorHandlingForRunJobFailures() throws IOException
+ " Logs: fatal error"));
}

@Test
void testTaskThrowsRetrofitHttpError() {

var app = "atest";
var url = "https://front50service.com/v2/applications/" + app;

Response mockResponse =
new Response(
url,
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.name(),
Collections.emptyList(),
new TypedByteArray("application/json", "{ \"jobState\": \"Failed\"}".getBytes()));

RetrofitError httpError =
RetrofitError.httpError(url, mockResponse, new JacksonConverter(), null);

when(mockFront50Service.get(app)).thenThrow(httpError);

StageExecutionImpl myStage =
createStageWithContextWithoutExecutionApplication(
ImmutableMap.of(
"deploy.jobs", ImmutableMap.of("test", ImmutableList.of("atest-btest-ctest"))));

assertThatThrownBy(() -> task.execute(myStage))
.isExactlyInstanceOf(RetrofitError.class)
.hasMessage(HttpStatus.BAD_REQUEST.value() + " " + HttpStatus.BAD_REQUEST.name())
.hasCause(httpError.getCause());
}

@Test
void testTaskThrowsRetrofitConversionError() {

var app = "atest";
var url = "https://front50service.com/v2/applications/" + app;
Response mockResponse =
new Response(
url,
HttpStatus.BAD_REQUEST.value(),
HttpStatus.BAD_REQUEST.name(),
Collections.emptyList(),
new TypedByteArray("application/json", "{ \"jobState\": \"Failed\"}".getBytes()));

RetrofitError conversionError =
RetrofitError.conversionError(
url,
mockResponse,
new JacksonConverter(),
null,
new ConversionException("Failed to convert http error response body"));

when(mockFront50Service.get(app)).thenThrow(conversionError);

StageExecutionImpl myStage =
createStageWithContextWithoutExecutionApplication(
ImmutableMap.of(
"deploy.jobs", ImmutableMap.of("test", ImmutableList.of("atest-btest-ctest"))));

assertThatThrownBy(() -> task.execute(myStage))
.isExactlyInstanceOf(RetrofitError.class)
.hasMessage("Failed to convert http error response body");
}

@Test
void testTaskThrowsRetrofitNetworkError() {

var app = "atest";
var url = "https://front50service.com/v2/applications/" + app;

RetrofitError networkError =
RetrofitError.networkError(
url, new IOException("Failed to connect to the host : front50.service.com"));

when(mockFront50Service.get(app)).thenThrow(networkError);

StageExecutionImpl myStage =
createStageWithContextWithoutExecutionApplication(
ImmutableMap.of(
"deploy.jobs", ImmutableMap.of("test", ImmutableList.of("atest-btest-ctest"))));

assertThatThrownBy(() -> task.execute(myStage))
.isExactlyInstanceOf(RetrofitError.class)
.hasMessage("Failed to connect to the host : front50.service.com")
.hasCause(networkError.getCause());
}

@Test
void testTaskThrowsRetrofitUnexpectedError() {

var app = "atest";
var url = "https://front50service.com/v2/applications/" + app;

RetrofitError unexpectedError =
RetrofitError.unexpectedError(
url, new IOException("Something went wrong, Please try again later"));

when(mockFront50Service.get(app)).thenThrow(unexpectedError);

StageExecutionImpl myStage =
createStageWithContextWithoutExecutionApplication(
ImmutableMap.of(
"deploy.jobs", ImmutableMap.of("test", ImmutableList.of("atest-btest-ctest"))));

assertThatThrownBy(() -> task.execute(myStage))
.isExactlyInstanceOf(RetrofitError.class)
.hasMessage("Something went wrong, Please try again later")
.hasCause(unexpectedError.getCause());
}

private StageExecutionImpl createStageWithContext(Map<String, ?> context) {
return new StageExecutionImpl(
new PipelineExecutionImpl(ExecutionType.PIPELINE, "test-app"),
Expand Down

0 comments on commit e0eefc0

Please sign in to comment.