From 66045ed721574768d8b566c31ac3d01efbfb855d Mon Sep 17 00:00:00 2001 From: Adam Jordens Date: Sat, 14 Oct 2017 23:35:19 -0700 Subject: [PATCH] Test cases for `id` and `name` support in #stage() --- .../expressions/ExpressionsSupport.java | 2 +- .../expressions/ExpressionsSupportSpec.groovy | 73 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 orca-core/src/test/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupportSpec.groovy diff --git a/orca-core/src/main/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java b/orca-core/src/main/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java index 09260d65159..ea918f86793 100644 --- a/orca-core/src/main/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java +++ b/orca-core/src/main/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupport.java @@ -252,7 +252,7 @@ static Map readProperties(String text) throws IOException { /** * Finds a Stage by id * @param obj #root.execution - * @param id the name of the stage to find + * @param id the name or id of the stage to find * @return a stage specified by id */ static Object stage(Object obj, String id) { diff --git a/orca-core/src/test/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupportSpec.groovy b/orca-core/src/test/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupportSpec.groovy new file mode 100644 index 00000000000..c7c3ed5caf2 --- /dev/null +++ b/orca-core/src/test/groovy/com/netflix/spinnaker/orca/pipeline/expressions/ExpressionsSupportSpec.groovy @@ -0,0 +1,73 @@ +/* + * Copyright 2017 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.pipeline.expressions + +import com.netflix.spinnaker.orca.ExecutionStatus +import spock.lang.Shared +import spock.lang.Specification +import spock.lang.Unroll + +import static com.netflix.spinnaker.orca.test.model.ExecutionBuilder.pipeline +import static com.netflix.spinnaker.orca.test.model.ExecutionBuilder.stage; + +class ExpressionsSupportSpec extends Specification { + @Shared + def pipeline = pipeline { + stage { + id = "1" + name = "My First Stage" + context = [ + "region": "us-east-1", + ] + } + + stage { + id = "2" + name = "My Second Stage" + context = [ + "region": "us-west-1", + ] + } + } + + @Unroll + def "stage() should match on #matchedAttribute"() { + expect: + ExpressionsSupport.stage(pipeline, stageCriteria).context.region == expectedRegion + + where: + stageCriteria || matchedAttribute || expectedRegion + "My Second Stage" || "name" || "us-west-1" + "1" || "id" || "us-east-1" + "2" || "id" || "us-west-1" + } + + def "stage() should raise exception if stage not found"() { + when: + ExpressionsSupport.stage(pipeline, "does_not_exist") + + then: + thrown(SpelHelperFunctionException) + + when: + ExpressionsSupport.stage("not_an_expression", "does_not_matter") + + then: + // raise exception when not passed an Execution + thrown(SpelHelperFunctionException) + } +}