Skip to content

Commit

Permalink
chore(orca): half-way un-shit pipeline builder for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Aug 29, 2017
1 parent daee6bd commit 5555874
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@

package com.netflix.spinnaker.orca.pipeline;

import java.io.IOException;
import java.io.Serializable;
import java.time.Clock;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.spinnaker.orca.pipeline.model.Execution.AuthenticationDetails;
import com.netflix.spinnaker.orca.pipeline.model.Orchestration;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Serializable;
import java.time.Clock;
import java.util.Map;

import static com.netflix.spinnaker.orca.pipeline.model.Execution.ExecutionEngine.v3;
import static java.lang.String.format;

Expand All @@ -52,10 +50,7 @@ public OrchestrationLauncher(
protected Orchestration parse(String configJson) throws IOException {
@SuppressWarnings("unchecked")
Map<String, Serializable> config = objectMapper.readValue(configJson, Map.class);
Orchestration orchestration = new Orchestration();
if (config.containsKey("application")) {
orchestration.setApplication(getString(config, "application"));
}
Orchestration orchestration = new Orchestration(getString(config, "application"));
if (config.containsKey("name")) {
orchestration.setDescription(getString(config, "name"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public PipelineLauncher(ObjectMapper objectMapper,
// TODO: can we not just annotate the class properly to avoid all this?
Map<String, Serializable> config = objectMapper.readValue(configJson, Map.class);
return registry
.map(Pipeline::builder)
.orElseGet(Pipeline::builder)
.withApplication(getString(config, "application"))
.map(it -> Pipeline.builder(getString(config, "application"), it))
.orElseGet(() -> Pipeline.builder(getString(config, "application")))
.withName(getString(config, "name"))
.withPipelineConfigId(getString(config, "id"))
.withTrigger((Map<String, Object>) config.get("trigger"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@

public abstract class Execution<T extends Execution<T>> implements Serializable {

private String id = UUID.randomUUID().toString();
protected Execution(String application) {
this(UUID.randomUUID().toString(), application);
}

protected Execution(String id, String application) {
this.id = id;
this.application = application;
}

private String id;

public @Nonnull String getId() {
return id;
Expand All @@ -58,7 +67,7 @@ public void setApplication(@Nonnull String application) {
return name;
}

public void setName(@Nonnull String name) {
public void setName(@Nullable String name) {
this.name = name;
}

Expand Down Expand Up @@ -145,16 +154,12 @@ public void setContext(@Nonnull Map<String, Object> context) {
this.context = context;
}

private List<Stage<T>> stages = new ArrayList<>();
private final List<Stage<T>> stages = new ArrayList<>();

public @Nonnull List<Stage<T>> getStages() {
return stages;
}

public void setStages(@Nonnull List<Stage<T>> stages) {
this.stages = stages;
}

private Long startTime;

public @Nullable Long getStartTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
import javax.annotation.Nullable;

public class Orchestration extends Execution<Orchestration> {

public Orchestration(String application) {
super(application);
}

public Orchestration(String id, String application) {
super(id, application);
}

private String description;

public @Nullable String getDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@

public class Pipeline extends Execution<Pipeline> {

public Pipeline(String application) {
super(application);
}

public Pipeline(String id, String application) {
super(id, application);
}

private String pipelineConfigId;

public @Nullable String getPipelineConfigId() {
Expand Down Expand Up @@ -55,11 +63,11 @@ public void setPipelineConfigId(@Nullable String pipelineConfigId) {
return initialConfig;
}

public static PipelineBuilder builder(Registry registry) {
return new PipelineBuilder(registry);
public static PipelineBuilder builder(String application, Registry registry) {
return new PipelineBuilder(application, registry);
}

public static PipelineBuilder builder() {
return new PipelineBuilder();
public static PipelineBuilder builder(String application) {
return new PipelineBuilder(application);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ import static com.netflix.spinnaker.orca.pipeline.model.Execution.DEFAULT_EXECUT
@CompileStatic
class PipelineBuilder {

private final Pipeline pipeline = new Pipeline()
private final Pipeline pipeline
private final AtomicInteger nextRefid = new AtomicInteger(1)

PipelineBuilder(Registry registry) {
PipelineBuilder(String application, Registry registry) {
this(application)
pipeline.context = new AlertOnAccessMap<Pipeline>(pipeline, registry)
}

PipelineBuilder() {}
PipelineBuilder(String application) {
pipeline = new Pipeline(application)
}

PipelineBuilder withTrigger(Map<String, Object> trigger = [:]) {
pipeline.trigger.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,7 @@ class JedisExecutionRepository implements ExecutionRepository {
stageIds = results[1] ?: (map.stageIndex ?: "").tokenize(",")
}

def execution = type.newInstance()
execution.id = id
execution.application = map.application
def execution = type.newInstance(id, map.application)
execution.context.putAll(map.context ? mapper.readValue(map.context, Map) : [:])
execution.canceled = Boolean.parseBoolean(map.canceled)
execution.canceledBy = map.canceledBy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package com.netflix.spinnaker.orca.pipeline.model

import spock.lang.Specification
import static com.netflix.spinnaker.orca.pipeline.model.SyntheticStageOwner.STAGE_AFTER
import static java.lang.System.currentTimeMillis
import static com.netflix.spinnaker.orca.test.model.ExecutionBuilder.pipeline
import static com.netflix.spinnaker.orca.test.model.ExecutionBuilder.stage

class StageContextSpec extends Specification {

Expand All @@ -28,49 +29,48 @@ class StageContextSpec extends Specification {
context.baz = "global-baz"
context.qux = "global-qux"
context.fnord = "global-fnord"
stage {
refId = "1"
outputs.foo = "root-foo"
outputs.bar = "root-bar"
outputs.baz = "root-baz"
outputs.qux = "root-qux"
}
stage {
refId = "2"
requisiteStageRefIds = ["1"]
outputs.foo = "ancestor-foo"
outputs.bar = "ancestor-bar"
outputs.baz = "ancestor-baz"
}
stage {
refId = "3"
requisiteStageRefIds = ["2"]
outputs.foo = "parent-foo"
outputs.bar = "parent-bar"
}
stage {
refId = "3>1"
parentStageId = execution.stageByRef("3").id
syntheticStageOwner = STAGE_AFTER
context.foo = "current-foo"
}
stage {
refId = "4"
outputs.covfefe = "unrelated-covfefe"
}
stage {
refId = "3>2"
requisiteStageRefIds = ["3>1"]
parentStageId = execution.stageByRef("3").id
syntheticStageOwner = STAGE_AFTER
outputs.covfefe = "downstream-covfefe"
}
}
.stage {
refId = "1"
outputs.foo = "root-foo"
outputs.bar = "root-bar"
outputs.baz = "root-baz"
outputs.qux = "root-qux"
}
.stage {
refId = "2"
requisiteStageRefIds = ["1"]
outputs.foo = "ancestor-foo"
outputs.bar = "ancestor-bar"
outputs.baz = "ancestor-baz"
}
.stage {
refId = "3"
requisiteStageRefIds = ["2"]
outputs.foo = "parent-foo"
outputs.bar = "parent-bar"
}
.stage {
refId = "3>1"
parentStageId = execution.stageByRef("3").id
syntheticStageOwner = STAGE_AFTER
context.foo = "child-foo"
}
.stage {
refId = "4"
outputs.covfefe = "unrelated-covfefe"
}
.stage {
refId = "3>2"
requisiteStageRefIds = ["3>1"]
parentStageId = execution.stageByRef("3").id
syntheticStageOwner = STAGE_AFTER
outputs.covfefe = "downstream-covfefe"
}
.build()

def "a stage's own context takes priority"() {
expect:
pipeline.stageByRef("3>1").context.foo == "child-foo"
pipeline.stageByRef("3>1").context.foo == "current-foo"
}

def "parent takes priority over ancestor"() {
Expand All @@ -97,40 +97,4 @@ class StageContextSpec extends Specification {
expect:
pipeline.stageByRef("3>1").context.fnord == "global-fnord"
}

private static PipelineInit pipeline(
@DelegatesTo(Pipeline) Closure init = {}) {
def pipeline = new Pipeline()
pipeline.id = UUID.randomUUID().toString()
pipeline.buildTime = currentTimeMillis()

def builder = new PipelineInit(pipeline)
init.delegate = pipeline
init()

return builder
}

private static class PipelineInit {
final @Delegate Pipeline pipeline

PipelineInit(Pipeline pipeline) {
this.pipeline = pipeline
}

PipelineInit stage(@DelegatesTo(Stage) Closure init) {
def stage = new Stage<Pipeline>()
stage.execution = pipeline
pipeline.stages.add(stage)

init.delegate = stage
init()

return this
}

Pipeline build() {
return pipeline
}
}
}
Loading

0 comments on commit 5555874

Please sign in to comment.