Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): fix the mismatches in the return types of APIs in gate and orca #1883

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright 2025 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.gate.service;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.netflix.spinnaker.kork.common.Header.REQUEST_ID;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import com.netflix.spinnaker.gate.Main;
import com.netflix.spinnaker.gate.health.DownstreamServicesHealthIndicator;
import com.netflix.spinnaker.gate.services.internal.ClouddriverService;
import com.netflix.spinnaker.gate.services.internal.ClouddriverServiceSelector;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import retrofit2.mock.Calls;

@SpringBootTest(classes = Main.class)
@TestPropertySource(
properties = {
"spring.config.location=classpath:gate-test.yml",
"services.front50.applicationRefreshInitialDelayMs=3600000"
})
public class PipelineServiceTest {
private MockMvc webAppMockMvc;

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

@Autowired private WebApplicationContext webApplicationContext;

@Autowired ObjectMapper objectMapper;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this in these tests?

Copy link
Contributor Author

@kirangodishala kirangodishala Mar 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean autowiring of ObjectMapper? It's not. I will update. But if you are asking whether ObjectMapper is needed at all, there is a usage in the test to create the response body string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asking about the FilterRegistrationBean

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it turns out that it is needed to silence warnings about e.g. missing X-SPINNAKER-USER, X-SPINNAKER-ACCOUNTS warnings...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed some commits to silence those warnings, and some others.

* This takes X-SPINNAKER-* headers from requests to gate and puts them in the MDC. This is
* enabled when gate runs normally (by GateConfig), but needs explicit mention to function in
* these tests.
*/
@Autowired
@Qualifier("authenticatedRequestFilter")
private FilterRegistrationBean filterRegistrationBean;

@MockBean ClouddriverServiceSelector clouddriverServiceSelector;

@MockBean ClouddriverService clouddriverService;

/** To prevent periodic calls to service's /health endpoints */
@MockBean DownstreamServicesHealthIndicator downstreamServicesHealthIndicator;

private static final String SUBMITTED_REQUEST_ID = "submitted-request-id";
private static final String PIPELINE_EXECUTION_ID = "my-pipeline-execution-id";

@DynamicPropertySource
static void registerUrls(DynamicPropertyRegistry registry) {
// Configure wiremock's random ports into gate
System.out.println("wiremock orca url: " + wmOrca.baseUrl());
registry.add("services.orca.base-url", wmOrca::baseUrl);
}

@BeforeEach
void init(TestInfo testInfo) {
System.out.println("--------------- Test " + testInfo.getDisplayName());

webAppMockMvc =
webAppContextSetup(webApplicationContext)
.addFilters(filterRegistrationBean.getFilter())
.build();

// Keep the background thread that refreshes the applications cache in
// ApplicationService happy.
when(clouddriverServiceSelector.select()).thenReturn(clouddriverService);
when(clouddriverService.getAllApplicationsUnrestricted(anyBoolean()))
.thenReturn(Calls.response(Collections.emptyList()));
}

@Test
void invokeDeletePipelineExecution() throws Exception {
wmOrca.stubFor(
WireMock.get(urlEqualTo("/pipelines/" + PIPELINE_EXECUTION_ID))
.willReturn(
aResponse()
.withStatus(200)
.withBody(objectMapper.writeValueAsString(Map.of("foo", "bar")))));

// simulate Orca response to the delete request
wmOrca.stubFor(
WireMock.delete(urlEqualTo("/pipelines/" + PIPELINE_EXECUTION_ID))
.willReturn(aResponse().withStatus(200)));

// TODO: Fix the response type of gate's delete pipeline execution endpoint
// which is supposed to be void instead of Map as Orca doesn't return anything.
webAppMockMvc
.perform(
delete("/pipelines/" + PIPELINE_EXECUTION_ID)
.header(REQUEST_ID.getHeader(), SUBMITTED_REQUEST_ID))
.andDo(print())
.andExpect(status().is5xxServerError())
.andExpect(
status()
.reason(
"Failed to process response body: No content to map due to end-of-input\n"
+ " at [Source: (okhttp3.ResponseBody$BomAwareReader); line: 1, column: 0]"))
.andExpect(header().string(REQUEST_ID.getHeader(), SUBMITTED_REQUEST_ID));
}
}