Skip to content

Commit

Permalink
test(keel): Test network error for KeelService API
Browse files Browse the repository at this point in the history
This commit covers the test to verify the exception handling of SpinnakerNetworkException in ImportDeliveryConfigTask

By this the all kinds of Spinnaker*Exception is covered and verified with the new changes.
  • Loading branch information
Pranav-b-7 committed Feb 6, 2024
1 parent 0bfa99b commit 5cff4f7
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.http.Fault;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerRetrofitErrorHandler;
Expand Down Expand Up @@ -117,6 +118,10 @@ private static void simulateFault(String url, String body, HttpStatus httpStatus
.willReturn(aResponse().withStatus(httpStatus.value()).withBody(body)));
}

private static void simulateFault(String url, Fault fault) {
wireMock.givenThat(WireMock.post(urlPathEqualTo(url)).willReturn(aResponse().withFault(fault)));
}

@Test
public void testTaskResultWhenErrorBodyIsEmpty() {

Expand Down Expand Up @@ -207,4 +212,44 @@ public void testTaskResultWhenHttp5xxErrorIsThrown() {

assertThat(result).isEqualTo(running);
}

@Test
public void testTaskResultWhenAPIFailsWithNetworkError() {

var mockResponseBody =
Map.of(
"name",
"keeldemo-manifest",
"application",
"keeldemo",
"artifacts",
Collections.emptySet(),
"environments",
Collections.emptySet());

contextMap.put("attempt", (Integer) contextMap.get("attempt") + 1);
contextMap.put(
"errorFromLastAttempt",
String.format(
"Network error talking to downstream service, attempt 1 of %s: Connection reset: Connection reset",
contextMap.get("maxRetries")));

TaskResult running = TaskResult.builder(ExecutionStatus.RUNNING).context(contextMap).build();

// Simulate network failure
simulateFault("/delivery-configs/", Fault.CONNECTION_RESET_BY_PEER);

when(scmService.getDeliveryConfigManifest(
(String) contextMap.get("repoType"),
(String) contextMap.get("projectKey"),
(String) contextMap.get("repositorySlug"),
(String) contextMap.get("directory"),
(String) contextMap.get("manifest"),
(String) contextMap.get("ref")))
.thenReturn(mockResponseBody);

var result = importDeliveryConfigTask.execute(stage);

assertThat(result).isEqualTo(running);
}
}

0 comments on commit 5cff4f7

Please sign in to comment.