Skip to content

Commit

Permalink
fix(gcb): process spel in gcb build definition artifact (#3246)
Browse files Browse the repository at this point in the history
  • Loading branch information
maggieneterval committed Oct 23, 2019
1 parent 154eb6c commit e6391e8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.netflix.spinnaker.orca.igor.tasks;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.kork.core.RetrySupport;
import com.netflix.spinnaker.orca.ExecutionStatus;
Expand All @@ -28,6 +27,7 @@
import com.netflix.spinnaker.orca.igor.model.GoogleCloudBuildStageDefinition;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import com.netflix.spinnaker.orca.pipeline.util.ArtifactResolver;
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Nonnull;
Expand All @@ -43,8 +43,8 @@ public class StartGoogleCloudBuildTask implements Task {
private final IgorService igorService;
private final OortService oortService;
private final ArtifactResolver artifactResolver;
private final ContextParameterProcessor contextParameterProcessor;

private final ObjectMapper objectMapper = new ObjectMapper();
private final RetrySupport retrySupport = new RetrySupport();
private static final ThreadLocal<Yaml> yamlParser =
ThreadLocal.withInitial(() -> new Yaml(new SafeConstructor()));
Expand Down Expand Up @@ -90,18 +90,22 @@ private Map<String, Object> getBuildDefinitionFromArtifact(
throw new IllegalArgumentException("No manifest artifact account was specified.");
}

return retrySupport.retry(
() -> {
try {
Response buildText = oortService.fetchArtifact(buildDefinitionArtifact);
Object result = yamlParser.get().load(buildText.getBody().in());
return (Map<String, Object>) result;
} catch (IOException e) {
throw new RuntimeException(e);
}
},
10,
200,
false);
Map<String, Object> buildDefinition =
retrySupport.retry(
() -> {
try {
Response buildText = oortService.fetchArtifact(buildDefinitionArtifact);
Object result = yamlParser.get().load(buildText.getBody().in());
return (Map<String, Object>) result;
} catch (IOException e) {
throw new RuntimeException(e);
}
},
10,
200,
false);

return contextParameterProcessor.process(
buildDefinition, contextParameterProcessor.buildExecutionContext(stage), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,46 @@ import com.netflix.spinnaker.orca.igor.model.GoogleCloudBuild
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.util.ArtifactResolver
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor
import retrofit.client.Response
import retrofit.mime.TypedString
import spock.lang.Specification
import spock.lang.Subject

class StartGoogleCloudBuildTaskSpec extends Specification {
def ACCOUNT = "my-account"
def RAW_BUILD = [
steps: [
[
args: [
"bin/echo",
'${#currentStage()["name"].toString()}'
],
name: "debian"
]
]
]
def BUILD = [
steps: []
steps: [
[
args: [
"bin/echo",
"My GCB Stage"
],
name: "debian"
]
]
]
def objectMapper = new ObjectMapper()

Execution execution = Mock(Execution)
IgorService igorService = Mock(IgorService)
OortService oortService = Mock(OortService)
ArtifactResolver artifactResolver = Mock(ArtifactResolver)
ContextParameterProcessor contextParameterProcessor = Mock(ContextParameterProcessor)

@Subject
StartGoogleCloudBuildTask task = new StartGoogleCloudBuildTask(igorService, oortService, artifactResolver)
StartGoogleCloudBuildTask task = new StartGoogleCloudBuildTask(igorService, oortService, artifactResolver, contextParameterProcessor)

def "starts a build defined inline"() {
given:
Expand Down Expand Up @@ -74,15 +95,17 @@ class StartGoogleCloudBuildTaskSpec extends Specification {
buildDefinitionSource: "artifact",
buildDefinitionArtifact: [
artifact: artifact
]
],
name: "My GCB Stage"
])

when:
TaskResult result = task.execute(stage)

then:
artifactResolver.getBoundArtifactForStage(stage, null, artifact) >> artifact
oortService.fetchArtifact(artifact) >> new Response("", 200, "", Collections.emptyList(), new TypedString(objectMapper.writeValueAsString(BUILD)))
oortService.fetchArtifact(artifact) >> new Response("", 200, "", Collections.emptyList(), new TypedString(objectMapper.writeValueAsString(RAW_BUILD)))
1 * contextParameterProcessor.process(RAW_BUILD, _, _) >> BUILD

1 * igorService.createGoogleCloudBuild(ACCOUNT, BUILD) >> igorResponse
result.context.buildInfo == igorResponse
Expand Down

0 comments on commit e6391e8

Please sign in to comment.