Skip to content

Commit

Permalink
test(mine): verify network error case in RegisterCanaryTask
Browse files Browse the repository at this point in the history
These tests ensures the upcoming changes on MineService with SpinnakerRetrofitErrorHandler, will not modify/break the fix made in the commit: 4dc0667.
  • Loading branch information
Pranav-b-7 committed Mar 23, 2024
1 parent 4dc0667 commit 8b678f8
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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.http.HttpHeaders;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
Expand Down Expand Up @@ -108,6 +109,10 @@ private static void simulateFault(String url, String body, HttpStatus httpStatus
.withBody(body)));
}

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

@Test
public void verifyRegisterCanaryThrowsHttpError() throws Exception {

Expand Down Expand Up @@ -157,6 +162,38 @@ public void verifyRegisterCanaryThrowsHttpError() throws Exception {
errorResponseBody));
}

@Test
public void verifyRegisterCanaryThrowsNetworkError() throws Exception {

// simulate network error
simulateFault("/registerCanary", Fault.CONNECTION_RESET_BY_PEER);

var canaryObject = (LinkedHashMap) deployCanaryStage.getContext().get("canary");
canaryObject.put("application", "foo");

var canaryConfig = (LinkedHashMap) canaryObject.get("canaryConfig");
canaryConfig.put("name", deployCanaryStage.getExecution().getId());
canaryConfig.put("application", "foo");

// Format the canary data as per error log message
var canary =
Objects.toString(deployCanaryStage.getContext().get("canary"))
.replace("{", "[")
.replace("}", "]")
.replace("=", ":");

String errorResponseBody = "[status:null, errorKind:NETWORK]";

assertThatThrownBy(() -> registerCanaryTask.execute(deployCanaryStage))
.hasMessage(
String.format(
"Unable to register canary (executionId: %s, stageId: %s canary: %s), response: %s",
deployCanaryStage.getExecution().getId(),
deployCanaryStage.getId(),
canary,
errorResponseBody));
}

/*
* Populate Register canary stage execution test data,
* {@link LinkedHashMap} is used to maintain the insertion order , so the assertions will be much simpler
Expand Down

0 comments on commit 8b678f8

Please sign in to comment.