Skip to content

Commit

Permalink
[JENKINS-74933] Allow ConfigurationContext to use a specific mergeStr…
Browse files Browse the repository at this point in the history
…ategy (#2602)

Co-authored-by: Tim Jacomb <[email protected]>
  • Loading branch information
jgreffe and timja authored Dec 2, 2024
1 parent addaa05 commit 004d553
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ public class ConfigurationContext implements ConfiguratorRegistry {
private transient SecretSourceResolver secretSourceResolver;

public ConfigurationContext(ConfiguratorRegistry registry) {
this(registry, null);
}

public ConfigurationContext(ConfiguratorRegistry registry, String mergeStrategy) {
this.registry = registry;
String prop = getPropertyOrEnv(CASC_YAML_MAX_ALIASES_ENV, CASC_YAML_MAX_ALIASES_PROPERTY);
yamlMaxAliasesForCollections = NumberUtils.toInt(prop, 50);
prop = getPropertyOrEnv(CASC_YAML_CODE_POINT_LIMIT_ENV, CASC_YAML_CODE_POINT_LIMIT_PROPERTY);
yamlCodePointLimit = NumberUtils.toInt(prop, 3) * 1024 * 1024;
secretSourceResolver = new SecretSourceResolver(this);
mergeStrategy = getPropertyOrEnv(CASC_MERGE_STRATEGY_ENV, CASC_MERGE_STRATEGY_PROPERTY);
this.mergeStrategy = mergeStrategy != null
? mergeStrategy
: getPropertyOrEnv(CASC_MERGE_STRATEGY_ENV, CASC_MERGE_STRATEGY_PROPERTY);
}

private String getPropertyOrEnv(String envKey, String proKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.jenkins.plugins.casc.yaml;

import static io.jenkins.plugins.casc.ConfigurationContext.CASC_MERGE_STRATEGY_ENV;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThrows;

import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorException;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.jvnet.hudson.test.JenkinsSessionRule;
import org.jvnet.hudson.test.TestExtension;
import org.yaml.snakeyaml.nodes.Node;

public class CustomMergeStrategyTest {

@Rule
public JenkinsSessionRule session = new JenkinsSessionRule();

@Rule
public final EnvironmentVariables environment = new EnvironmentVariables();

@Test
public void customMergeStrategy_withoutEnvironment() throws Throwable {
session.then(r -> {
assertThat(System.getenv(CASC_MERGE_STRATEGY_ENV), nullValue());
customMergeStrategy();
});
}

@Test
public void customMergeStrategy_withEnvironment() throws Throwable {
environment.set(CASC_MERGE_STRATEGY_ENV, "override");
session.then(r -> {
assertThat(System.getenv(CASC_MERGE_STRATEGY_ENV), is("override"));
customMergeStrategy();
});
}

private void customMergeStrategy() {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext customContext = new ConfigurationContext(registry, "custom-test");
String normalSource = getClass().getResource("normal.yml").toExternalForm();
String overwriteSource = getClass().getResource("overwrite.yml").toExternalForm();

List<YamlSource> list = List.of(YamlSource.of(normalSource), YamlSource.of(overwriteSource));

ConfiguratorException exception =
assertThrows(ConfiguratorException.class, () -> YamlUtils.merge(list, customContext));
assertThat(exception.getMessage(), is("custom-test merge strategy"));
}

@TestExtension
public static class CustomTestMergeStrategy implements MergeStrategy {
@Override
public void merge(Node firstNode, Node secondNode, String source) throws ConfiguratorException {
throw new ConfiguratorException("custom-test merge strategy");
}

@Override
public String getName() {
return "custom-test";
}
}
}

0 comments on commit 004d553

Please sign in to comment.