From fcc2e806f93c740d1e9f8c53323a91672f352a5e Mon Sep 17 00:00:00 2001 From: Pranav-b-7 Date: Mon, 22 Jan 2024 13:09:23 +0530 Subject: [PATCH] Create bake task --- .../orca/bakery/tasks/CreateBakeTask.groovy | 3 + .../orca/bakery/tasks/CreateBakeTaskTest.java | 120 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 orca-bakery/src/test/java/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTaskTest.java diff --git a/orca-bakery/src/main/groovy/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTask.groovy b/orca-bakery/src/main/groovy/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTask.groovy index f725aca9e05..2b62a15a311 100644 --- a/orca-bakery/src/main/groovy/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTask.groovy +++ b/orca-bakery/src/main/groovy/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTask.groovy @@ -73,8 +73,11 @@ class CreateBakeTask implements RetryableTask { if (!bakerySelector) { throw new UnsupportedOperationException("You have not enabled baking for this orca instance. Set bakery.enabled: true") } + log.info("bakery selector : "+bakerySelector) def bakery = bakerySelector.select(stage) + log.info("bakery : "+bakery) + log.info("bakeryService : {}", bakery.service) // If application exists, we should pass the owner of the application as the user to the bakery try { if (front50Service != null) { diff --git a/orca-bakery/src/test/java/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTaskTest.java b/orca-bakery/src/test/java/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTaskTest.java new file mode 100644 index 00000000000..dbbc2de8c13 --- /dev/null +++ b/orca-bakery/src/test/java/com/netflix/spinnaker/orca/bakery/tasks/CreateBakeTaskTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2024 Netflix, 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.bakery.tasks; + +import static org.mockito.Mockito.*; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.netflix.spinnaker.kork.web.selector.v2.SelectableService; +import com.netflix.spinnaker.orca.api.pipeline.models.ExecutionType; +import com.netflix.spinnaker.orca.api.pipeline.models.PipelineExecution; +import com.netflix.spinnaker.orca.api.pipeline.models.StageExecution; +import com.netflix.spinnaker.orca.bakery.BakerySelector; +import com.netflix.spinnaker.orca.bakery.api.BakeRequest; +import com.netflix.spinnaker.orca.bakery.api.BakeStatus; +import com.netflix.spinnaker.orca.bakery.api.BakeryService; +import com.netflix.spinnaker.orca.bakery.config.BakeryConfigurationProperties; +import com.netflix.spinnaker.orca.front50.Front50Service; +import com.netflix.spinnaker.orca.pipeline.model.PipelineExecutionImpl; +import com.netflix.spinnaker.orca.pipeline.model.StageExecutionImpl; +import com.netflix.spinnaker.orca.pipeline.util.ArtifactUtils; +import java.util.Collections; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; +import retrofit.RetrofitError; +import retrofit.client.Response; + +public class CreateBakeTaskTest { + + private StageExecution stageExecution = new StageExecutionImpl(); + + private String application = "testapp"; + + private BakerySelector bakerySelector = null; + + private BakeryService bakeryService = mock(BakeryService.class); + + private CreateBakeTask createBakeTask = new CreateBakeTask(); + + private Front50Service front50Service = mock(Front50Service.class); + + private BakeStatus runningStatus = new BakeStatus(); + + private ArtifactUtils artifactUtils = mock(ArtifactUtils.class); + + PipelineExecution pipelineExecution = + new PipelineExecutionImpl(ExecutionType.PIPELINE, application); + + @BeforeEach + public void setup() { + + stageExecution.setExecution(pipelineExecution); + runningStatus.setId(UUID.randomUUID().toString()); + runningStatus.setState(BakeStatus.State.RUNNING); + + BakeryConfigurationProperties bakeryConfigurationProperties = + new BakeryConfigurationProperties(); + bakeryConfigurationProperties.setExtractBuildDetails(true); + bakeryConfigurationProperties.setSelectorEnabled(true); + bakeryConfigurationProperties.setAllowMissingPackageInstallation(false); + bakeryConfigurationProperties.setRoscoApisEnabled(false); + + bakerySelector = + spy( + new BakerySelector( + bakeryService, bakeryConfigurationProperties, (bakerySvc) -> bakeryService)); + } + + @Test + void testWarningLogsWhenSpinnakerHttpExceptionOccurs() { + + var url = "https://front50service.com/v2/applications/" + application; + + Response response = + new Response( + url, + HttpStatus.BAD_REQUEST.value(), + HttpStatus.BAD_REQUEST.name(), + Collections.emptyList(), + null); + RetrofitError httpError = RetrofitError.httpError(url, response, null, null); + + when(front50Service.get(application)).thenThrow(httpError); + + SelectableService.SelectedService selectedBakeryService = + new SelectableService.SelectedService( + bakeryService, Collections.EMPTY_MAP, Collections.emptyList()); + + createBakeTask.setBakerySelector(bakerySelector); + createBakeTask.setArtifactUtils(artifactUtils); + createBakeTask.setMapper(new ObjectMapper()); + + when(bakerySelector.select(stageExecution)).thenReturn(selectedBakeryService); + System.out.println("selected bakery service in tests : " + selectedBakeryService); + System.out.println("bakery selector in tests : " + bakerySelector); + System.out.println("bakery service in tests : " + bakeryService); + when(artifactUtils.getAllArtifacts(pipelineExecution)).thenReturn(Collections.emptyList()); + when(bakeryService.createBake(anyString(), any(BakeRequest.class), anyString())) + .thenReturn(runningStatus); + + // createBakeTask.execute(stageExecution); + + } +}