Skip to content

Commit

Permalink
test(keel): demonstrate behavior of ImportDeliveryConfigTask
Browse files Browse the repository at this point in the history
Test cases added to verify the upcoming changes when KeelService is configured with SpinnakerRetrofitErrorHandler.

These tests verifies the behaviour of the method handleRetryableFailures() when 4xx with empty error body and 5xx http erros are thrown
  • Loading branch information
Pranav-b-7 committed Feb 1, 2024
1 parent a587515 commit efe8aee
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
2 changes: 2 additions & 0 deletions orca-keel/orca-keel.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
testImplementation("org.codehaus.groovy:groovy")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-params")
testImplementation("com.github.tomakehurst:wiremock-jre8-standalone")
testImplementation("org.mockito:mockito-junit-jupiter")
}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* Copyright 2024 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.keel.task;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType.PIPELINE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties;
import com.netflix.spinnaker.okhttp.SpinnakerRequestInterceptor;
import com.netflix.spinnaker.orca.KeelService;
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.ExecutionType;
import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution;
import com.netflix.spinnaker.orca.igor.ScmService;
import com.netflix.spinnaker.orca.pipeline.model.PipelineExecutionImpl;
import com.netflix.spinnaker.orca.pipeline.model.StageExecutionImpl;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.HttpStatus;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.JacksonConverter;

public class ImportDeliveryConfigTaskTest {

private static KeelService keelService;
private static ScmService scmService;

private static ObjectMapper objectMapper = new ObjectMapper();

private StageExecution stage;

private ImportDeliveryConfigTask importDeliveryConfigTask;

private Map<String, Object> contextMap = new LinkedHashMap<>();

@BeforeAll
static void setupOnce(WireMockRuntimeInfo wmRuntimeInfo) {
OkClient okClient = new OkClient();
RestAdapter.LogLevel retrofitLogLevel = RestAdapter.LogLevel.NONE;

keelService =
new RestAdapter.Builder()
.setRequestInterceptor(
new SpinnakerRequestInterceptor(new OkHttpClientConfigurationProperties()))
.setEndpoint(wmRuntimeInfo.getHttpBaseUrl())
.setClient(okClient)
.setLogLevel(retrofitLogLevel)
.setConverter(new JacksonConverter(objectMapper))
.build()
.create(KeelService.class);

scmService = mock(ScmService.class);
}

@BeforeEach
public void setup() {
importDeliveryConfigTask = new ImportDeliveryConfigTask(keelService, scmService, objectMapper);

PipelineExecutionImpl pipeline = new PipelineExecutionImpl(PIPELINE, "keeldemo");
contextMap.put("repoType", "stash");
contextMap.put("projectKey", "SPKR");
contextMap.put("repositorySlug", "keeldemo");
contextMap.put("directory", ".");
contextMap.put("manifest", "spinnaker.yml");
contextMap.put("ref", "refs/heads/master");
contextMap.put("attempt", 1);
contextMap.put("maxRetries", 5);

stage = new StageExecutionImpl(pipeline, ExecutionType.PIPELINE.toString(), contextMap);
}

@RegisterExtension
static WireMockExtension wireMock =
WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build();

private static void simulateFault(String url, String body, HttpStatus httpStatus) {
wireMock.givenThat(
WireMock.post(urlPathEqualTo(url))
.willReturn(aResponse().withStatus(httpStatus.value()).withBody(body)));
}

@Test
public void testTaskResultWhenErrorBodyIsEmpty() {

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

String expectedMessage =
String.format(
"Non-retryable HTTP response %s received from downstream service: %s",
HttpStatus.BAD_REQUEST.value(),
"HTTP 400 " + wireMock.baseUrl() + "/delivery-configs/: 400 Bad Request");

var errorMap = new HashMap<>();
errorMap.put("message", expectedMessage);

TaskResult terminal =
TaskResult.builder(ExecutionStatus.TERMINAL).context(Map.of("error", errorMap)).build();

// Simulate any 4xx http error with empty error response body
String emptyBody = "";
simulateFault("/delivery-configs/", emptyBody, HttpStatus.BAD_REQUEST);

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(terminal);
}

@Test
public void testTaskResultWhenHttp5xxErrorIsThrown() {

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",
"Retryable HTTP response 500 received from downstream service: HTTP 500 "
+ wireMock.baseUrl()
+ "/delivery-configs/: 500 Server Error");

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

// Simulate any 5xx http error with empty error response body
String emptyBody = "";
simulateFault("/delivery-configs/", emptyBody, HttpStatus.INTERNAL_SERVER_ERROR);

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 efe8aee

Please sign in to comment.