-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(artifacts): allow multiple artifacts in find artifact from execu…
…tion stage
- Loading branch information
1 parent
5b5464e
commit 674b0d6
Showing
3 changed files
with
182 additions
and
61 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
.../netflix/spinnaker/orca/clouddriver/tasks/artifacts/FindArtifactFromExecutionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2019 Google, 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.clouddriver.tasks.artifacts; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact; | ||
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository.ExecutionCriteria; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class FindArtifactFromExecutionContext extends HashMap<String, Object> { | ||
private final ExecutionOptions executionOptions; | ||
private final List<ExpectedArtifact> expectedArtifacts; | ||
private final String pipeline; | ||
|
||
// There does not seem to be a way to auto-generate a constructor using our current version of | ||
// Lombok (1.16.20) that | ||
// Jackson can use to deserialize. | ||
public FindArtifactFromExecutionContext( | ||
@JsonProperty("executionOptions") ExecutionOptions executionOptions, | ||
@JsonProperty("expectedArtifact") ExpectedArtifact expectedArtifact, | ||
@JsonProperty("expectedArtifacts") List<ExpectedArtifact> expectedArtifacts, | ||
@JsonProperty("pipeline") String pipeline) { | ||
this.executionOptions = executionOptions; | ||
// Previously, this stage accepted only one expected artifact | ||
this.expectedArtifacts = | ||
Optional.ofNullable(expectedArtifacts).orElse(Collections.singletonList(expectedArtifact)); | ||
this.pipeline = pipeline; | ||
} | ||
|
||
@Data | ||
static class ExecutionOptions { | ||
// Accept either 'succeeded' or 'successful' in the stage config. The front-end sets | ||
// 'successful', but due to a bug | ||
// this class was only looking for 'succeeded'. Fix this by accepting 'successful' but to avoid | ||
// breaking anyone who | ||
// discovered this bug and manually edited their stage to set 'succeeded', continue to accept | ||
// 'succeeded'. | ||
boolean succeeded; | ||
boolean successful; | ||
|
||
boolean terminal; | ||
boolean running; | ||
|
||
ExecutionCriteria toCriteria() { | ||
List<String> statuses = new ArrayList<>(); | ||
if (succeeded || successful) { | ||
statuses.add("SUCCEEDED"); | ||
} | ||
|
||
if (terminal) { | ||
statuses.add("TERMINAL"); | ||
} | ||
|
||
if (running) { | ||
statuses.add("RUNNING"); | ||
} | ||
|
||
return new ExecutionCriteria().setStatuses(statuses); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...roovy/com/netflix/spinnaker/orca/tasks/artifacts/FindArtifactFromExecutionTaskSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2019 Google, 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.tasks.artifacts | ||
|
||
import com.netflix.spinnaker.kork.artifacts.model.Artifact | ||
import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact | ||
import com.netflix.spinnaker.orca.TaskResult | ||
import com.netflix.spinnaker.orca.clouddriver.tasks.artifacts.FindArtifactFromExecutionTask | ||
import com.netflix.spinnaker.orca.pipeline.model.Execution | ||
import com.netflix.spinnaker.orca.pipeline.model.Stage | ||
import com.netflix.spinnaker.orca.pipeline.persistence.ExecutionRepository | ||
import com.netflix.spinnaker.orca.pipeline.util.ArtifactResolver | ||
import spock.lang.Specification | ||
import spock.lang.Subject | ||
|
||
class FindArtifactFromExecutionTaskSpec extends Specification { | ||
def PIPELINE = "my pipeline" | ||
def EXECUTION_CRITERIA = new ExecutionRepository.ExecutionCriteria().setStatuses(Collections.singletonList("SUCCEEDED")) | ||
def ARTIFACT_A = new Artifact(type: "kubernetes/replicaSet") | ||
def ARTIFACT_B = new Artifact(type: "kubernetes/configMap") | ||
|
||
ArtifactResolver artifactResolver = Mock(ArtifactResolver) | ||
Execution execution = Mock(Execution) | ||
|
||
@Subject | ||
FindArtifactFromExecutionTask task = new FindArtifactFromExecutionTask(artifactResolver) | ||
|
||
def "finds a single artifact"() { | ||
given: | ||
def expectedArtifacts = [new ExpectedArtifact(matchArtifact: ARTIFACT_A)] | ||
def pipelineArtifacts = [ARTIFACT_A, ARTIFACT_B] | ||
Set resolvedArtifacts = [ARTIFACT_A] | ||
def stage = new Stage(execution, "findArtifactFromExecution", [ | ||
executionOptions: [ | ||
succeeded: true | ||
], | ||
expectedArtifacts: expectedArtifacts, | ||
pipeline: PIPELINE | ||
]) | ||
|
||
when: | ||
TaskResult result = task.execute(stage) | ||
|
||
then: | ||
1 * artifactResolver.getArtifactsForPipelineId(PIPELINE, EXECUTION_CRITERIA) >> pipelineArtifacts | ||
1 * artifactResolver.resolveExpectedArtifacts(expectedArtifacts, pipelineArtifacts, null, false) >> resolvedArtifacts | ||
result.context.resolvedExpectedArtifacts == expectedArtifacts | ||
result.context.artifacts == resolvedArtifacts | ||
} | ||
|
||
def "finds multiple artifacts"() { | ||
given: | ||
def expectedArtifacts = [new ExpectedArtifact(matchArtifact: ARTIFACT_A), new ExpectedArtifact(matchArtifact: ARTIFACT_B)] | ||
def pipelineArtifacts = [ARTIFACT_A, ARTIFACT_B] | ||
Set resolvedArtifacts = [ARTIFACT_A, ARTIFACT_B] | ||
def stage = new Stage(execution, "findArtifactFromExecution", [ | ||
executionOptions: [ | ||
succeeded: true | ||
], | ||
expectedArtifacts: expectedArtifacts, | ||
pipeline: PIPELINE | ||
]) | ||
|
||
when: | ||
TaskResult result = task.execute(stage) | ||
|
||
then: | ||
1 * artifactResolver.getArtifactsForPipelineId(PIPELINE, EXECUTION_CRITERIA) >> pipelineArtifacts | ||
1 * artifactResolver.resolveExpectedArtifacts(expectedArtifacts, pipelineArtifacts, null, false) >> resolvedArtifacts | ||
result.context.resolvedExpectedArtifacts == expectedArtifacts | ||
result.context.artifacts == resolvedArtifacts | ||
} | ||
} |