From 8f3521db0690c2ed2980f00a73a93fc85c39cf39 Mon Sep 17 00:00:00 2001 From: Pranav-b-7 Date: Tue, 6 Feb 2024 22:05:08 +0530 Subject: [PATCH] test(keel): Test network error for KeelService API 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. --- .../task/ImportDeliveryConfigTaskTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/orca-keel/src/test/java/com/netflix/spinnaker/orca/keel/task/ImportDeliveryConfigTaskTest.java b/orca-keel/src/test/java/com/netflix/spinnaker/orca/keel/task/ImportDeliveryConfigTaskTest.java index bb4e5876254..a932fac6243 100644 --- a/orca-keel/src/test/java/com/netflix/spinnaker/orca/keel/task/ImportDeliveryConfigTaskTest.java +++ b/orca-keel/src/test/java/com/netflix/spinnaker/orca/keel/task/ImportDeliveryConfigTaskTest.java @@ -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; @@ -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() { @@ -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); + } }