|
| 1 | +/* |
| 2 | + * Copyright 2024 OpsMx, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.netflix.spinnaker.echo.pipelinetriggers.postprocessors; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import com.netflix.spinnaker.echo.jackson.EchoObjectMapper; |
| 22 | +import com.netflix.spinnaker.echo.model.Pipeline; |
| 23 | +import com.netflix.spinnaker.echo.model.Trigger; |
| 24 | +import com.netflix.spinnaker.kork.artifacts.model.Artifact; |
| 25 | +import com.netflix.spinnaker.kork.artifacts.model.ExpectedArtifact; |
| 26 | +import com.netflix.spinnaker.kork.expressions.config.ExpressionProperties; |
| 27 | +import java.util.List; |
| 28 | +import java.util.concurrent.atomic.AtomicInteger; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 32 | +import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +import org.springframework.test.context.ContextConfiguration; |
| 34 | +import org.springframework.test.context.TestPropertySource; |
| 35 | + |
| 36 | +@ContextConfiguration(classes = ExpectedArtifactExpressionLengthTest.class) |
| 37 | +@SpringBootTest |
| 38 | +@EnableConfigurationProperties(ExpressionProperties.class) |
| 39 | +@TestPropertySource(properties = {"expression.max-expression-length=11000"}) |
| 40 | +class ExpectedArtifactExpressionLengthTest { |
| 41 | + @Autowired private ExpressionProperties expressionProperties; |
| 42 | + |
| 43 | + @Test |
| 44 | + void customExpressionLength() { |
| 45 | + String expression = String.format("%s", repeat("T", 10900)); |
| 46 | + |
| 47 | + ExpectedArtifactExpressionEvaluationPostProcessor artifactPostProcessor = |
| 48 | + new ExpectedArtifactExpressionEvaluationPostProcessor( |
| 49 | + EchoObjectMapper.getInstance(), expressionProperties); |
| 50 | + |
| 51 | + Trigger trigger = |
| 52 | + Trigger.builder() |
| 53 | + .enabled(true) |
| 54 | + .type("jenkins") |
| 55 | + .master("master") |
| 56 | + .job(expression) |
| 57 | + .buildNumber(100) |
| 58 | + .build(); |
| 59 | + |
| 60 | + ExpectedArtifact artifact = |
| 61 | + ExpectedArtifact.builder() |
| 62 | + .matchArtifact( |
| 63 | + Artifact.builder() |
| 64 | + .name( |
| 65 | + "group:artifact:${trigger['job'] == '" |
| 66 | + + expression |
| 67 | + + "' ? 'expr-worked' : 'expr-not-worked'}") |
| 68 | + .version("${trigger['buildNumber']}") |
| 69 | + .type("maven/file") |
| 70 | + .build()) |
| 71 | + .id("testId") |
| 72 | + .build(); |
| 73 | + |
| 74 | + Pipeline inputPipeline = |
| 75 | + Pipeline.builder() |
| 76 | + .application("application") |
| 77 | + .name("name") |
| 78 | + .id(Integer.toString(new AtomicInteger(1).getAndIncrement())) |
| 79 | + .trigger(trigger) |
| 80 | + .expectedArtifacts(List.of(artifact)) |
| 81 | + .build() |
| 82 | + .withTrigger(trigger); |
| 83 | + |
| 84 | + Pipeline outputPipeline = artifactPostProcessor.processPipeline(inputPipeline); |
| 85 | + |
| 86 | + Artifact evaluatedArtifact = outputPipeline.getExpectedArtifacts().get(0).getMatchArtifact(); |
| 87 | + |
| 88 | + assertTrue(evaluatedArtifact.getName().equalsIgnoreCase("group:artifact:expr-worked")); |
| 89 | + } |
| 90 | + |
| 91 | + private String repeat(String str, int count) { |
| 92 | + String res = ""; |
| 93 | + for (int i = 0; i < count; i++) { |
| 94 | + res += str; |
| 95 | + } |
| 96 | + return res; |
| 97 | + } |
| 98 | +} |
0 commit comments