|
| 1 | +package com.netflix.spinnaker.clouddriver.safety; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; |
| 6 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 7 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 12 | +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; |
| 13 | +import com.netflix.spectator.api.NoopRegistry; |
| 14 | +import com.netflix.spinnaker.clouddriver.config.RetrofitConfig; |
| 15 | +import com.netflix.spinnaker.clouddriver.core.services.Front50Service; |
| 16 | +import com.netflix.spinnaker.clouddriver.model.NoopClusterProvider; |
| 17 | +import com.netflix.spinnaker.config.DefaultServiceClientProvider; |
| 18 | +import com.netflix.spinnaker.config.okhttp3.DefaultOkHttpClientBuilderProvider; |
| 19 | +import com.netflix.spinnaker.config.okhttp3.OkHttpClientProvider; |
| 20 | +import com.netflix.spinnaker.kork.dynamicconfig.DynamicConfigService; |
| 21 | +import com.netflix.spinnaker.kork.retrofit.Retrofit2ServiceFactory; |
| 22 | +import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException; |
| 23 | +import com.netflix.spinnaker.moniker.Moniker; |
| 24 | +import com.netflix.spinnaker.okhttp.OkHttpClientConfigurationProperties; |
| 25 | +import com.netflix.spinnaker.okhttp.SpinnakerRequestHeaderInterceptor; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Optional; |
| 29 | +import okhttp3.OkHttpClient; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.boot.test.context.SpringBootTest; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.Configuration; |
| 37 | +import org.springframework.http.HttpStatus; |
| 38 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 39 | +import org.springframework.test.context.DynamicPropertySource; |
| 40 | + |
| 41 | +@SpringBootTest( |
| 42 | + classes = { |
| 43 | + RetrofitConfig.class, |
| 44 | + OkHttpClientProvider.class, |
| 45 | + TrafficGuardTest.TestConfig.class, |
| 46 | + DefaultServiceClientProvider.class, |
| 47 | + Retrofit2ServiceFactory.class, |
| 48 | + DefaultOkHttpClientBuilderProvider.class, |
| 49 | + OkHttpClient.class, |
| 50 | + OkHttpClientConfigurationProperties.class, |
| 51 | + ObjectMapper.class |
| 52 | + }) |
| 53 | +public class TrafficGuardTest { |
| 54 | + private final String APP_NAME = "testApp"; |
| 55 | + |
| 56 | + @RegisterExtension |
| 57 | + static WireMockExtension wmFront50 = |
| 58 | + WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build(); |
| 59 | + |
| 60 | + @Autowired ObjectMapper objectMapper; |
| 61 | + |
| 62 | + @Autowired Front50Service front50Service; |
| 63 | + |
| 64 | + @Autowired SpinnakerRequestHeaderInterceptor requestHeaderInterceptor; |
| 65 | + |
| 66 | + TrafficGuard trafficGuard; |
| 67 | + |
| 68 | + @DynamicPropertySource |
| 69 | + static void registerUrls(DynamicPropertyRegistry registry) { |
| 70 | + // Configure wiremock's random ports into clouddriver |
| 71 | + System.out.println("wiremock front50 url: " + wmFront50.baseUrl()); |
| 72 | + registry.add("services.front50.base-url", wmFront50::baseUrl); |
| 73 | + } |
| 74 | + |
| 75 | + @BeforeEach |
| 76 | + void init() throws JsonProcessingException { |
| 77 | + wmFront50.stubFor( |
| 78 | + WireMock.get(urlMatching("/v2/applications/" + APP_NAME)) |
| 79 | + .willReturn( |
| 80 | + aResponse() |
| 81 | + .withStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()) |
| 82 | + .withBody( |
| 83 | + objectMapper.writeValueAsString( |
| 84 | + Map.of("error", "Internal Server Error"))))); |
| 85 | + |
| 86 | + trafficGuard = |
| 87 | + new TrafficGuard( |
| 88 | + List.of(new NoopClusterProvider()), |
| 89 | + Optional.of(front50Service), |
| 90 | + new NoopRegistry(), |
| 91 | + new DynamicConfigService.NoopDynamicConfig()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void verifyErrorHandlingCallAdapterFactory_with_500_exception() { |
| 96 | + Moniker moniker = Moniker.builder().app(APP_NAME).build(); |
| 97 | + Exception ex = null; |
| 98 | + |
| 99 | + try { |
| 100 | + trafficGuard.hasDisableLock(moniker, "acc1", "location1"); |
| 101 | + } catch (Exception e) { |
| 102 | + ex = e; |
| 103 | + } |
| 104 | + |
| 105 | + assert ex instanceof SpinnakerHttpException; |
| 106 | + |
| 107 | + wmFront50.verify(getRequestedFor(urlPathEqualTo("/v2/applications/" + APP_NAME))); |
| 108 | + } |
| 109 | + |
| 110 | + @Configuration |
| 111 | + static class TestConfig { |
| 112 | + |
| 113 | + @Bean |
| 114 | + SpinnakerRequestHeaderInterceptor spinnakerRequestHeaderInterceptor() { |
| 115 | + return new SpinnakerRequestHeaderInterceptor(false); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments