-
Notifications
You must be signed in to change notification settings - Fork 748
Allow empty valued variables without ignoring enviroment vars substitution #2665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
695761b
Using configurator for global props
PereBueno 3f5d776
Accept EnvVars wihtout value
fcojfernandez be60c5a
Testing additional global props
PereBueno e7055ab
Removing not used describe
PereBueno d1c08d9
Switching to use DataBoundConstructor
PereBueno 4884ab3
Adding variable resolution
PereBueno 3309c95
Added export with vars test
PereBueno ea785f7
Merge branch 'master' into empty-vars-exporting
PereBueno 8ea9107
spotless
PereBueno 8a8c932
Merge branch 'empty-vars-exporting' of https://github.com/PereBueno/c…
PereBueno be00ec1
Exporting empty env vars
PereBueno 27986fc
fixed test
PereBueno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
64 changes: 64 additions & 0 deletions
64
integrations/src/test/java/io/jenkins/plugins/casc/GlobalNodePropertiesWithEnvVarsTest.java
This file contains hidden or 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,64 @@ | ||
| package io.jenkins.plugins.casc; | ||
|
|
||
| import static io.jenkins.plugins.casc.misc.Util.getJenkinsRoot; | ||
| import static io.jenkins.plugins.casc.misc.Util.toStringFromYamlFile; | ||
| import static io.jenkins.plugins.casc.misc.Util.toYamlString; | ||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.hasSize; | ||
|
|
||
| import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
| import hudson.slaves.NodeProperty; | ||
| import hudson.slaves.NodePropertyDescriptor; | ||
| import hudson.tools.ToolLocationNodeProperty; | ||
| import hudson.util.DescribableList; | ||
| import io.jenkins.plugins.casc.misc.ConfiguredWithCode; | ||
| import io.jenkins.plugins.casc.misc.Env; | ||
| import io.jenkins.plugins.casc.misc.EnvVarsRule; | ||
| import io.jenkins.plugins.casc.misc.Envs; | ||
| import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule; | ||
| import io.jenkins.plugins.casc.model.CNode; | ||
| import java.util.Map; | ||
| import org.hamcrest.core.Is; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.RuleChain; | ||
|
|
||
| public class GlobalNodePropertiesWithEnvVarsTest { | ||
|
|
||
| private JenkinsConfiguredWithCodeRule j = new JenkinsConfiguredWithCodeRule(); | ||
|
|
||
| @Rule | ||
| public RuleChain chain = RuleChain.outerRule(new EnvVarsRule()).around(j); | ||
|
|
||
| @Test | ||
| @ConfiguredWithCode("GlobalNodePropertiesWithEnvVarsTest.yml") | ||
| @Envs({@Env(name = "VALUE_1", value = "BAR"), @Env(name = "TEST_GIT_HOME", value = "git-home")}) | ||
| public void configureWithEnvVarsTest() { | ||
| DescribableList<NodeProperty<?>, NodePropertyDescriptor> nodeProperties = j.jenkins.getGlobalNodeProperties(); | ||
| Map<String, String> envVars = ((EnvironmentVariablesNodeProperty) | ||
| nodeProperties.get(EnvironmentVariablesNodeProperty.class)) | ||
| .getEnvVars(); | ||
|
|
||
| assertThat(envVars.size(), is(2)); | ||
| assertThat(envVars.get("FOO"), is("BAR")); | ||
| assertThat(envVars.get("FOO2"), is("")); | ||
|
|
||
| ToolLocationNodeProperty toolLocations = nodeProperties.get(ToolLocationNodeProperty.class); | ||
| assertThat(toolLocations.getLocations(), hasSize(1)); | ||
| assertThat(toolLocations.getLocations().get(0).getHome(), is("git-home")); | ||
| } | ||
|
|
||
| @Test | ||
| @ConfiguredWithCode("GlobalNodePropertiesWithEnvVarsTest.yml") | ||
| @Envs({@Env(name = "VALUE_1", value = "BAR"), @Env(name = "TEST_GIT_HOME", value = "git-home")}) | ||
| public void export() throws Exception { | ||
| ConfiguratorRegistry registry = ConfiguratorRegistry.get(); | ||
| ConfigurationContext context = new ConfigurationContext(registry); | ||
| CNode yourAttribute = getJenkinsRoot(context).get("globalNodeProperties"); | ||
|
|
||
| String exported = toYamlString(yourAttribute); | ||
| String expected = toStringFromYamlFile(this, "GlobalNodePropertiesWithEnvVarsTestExpected.yml"); | ||
| assertThat(exported, Is.is(expected)); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
integrations/src/test/resources/io/jenkins/plugins/casc/GlobalNodePropertiesTest.yml
This file contains hidden or 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 |
|---|---|---|
| @@ -1,6 +1,17 @@ | ||
| jenkins: | ||
| globalNodeProperties: | ||
| - diskSpaceMonitor: | ||
| freeDiskSpaceThreshold: "1GiB" | ||
| freeDiskSpaceWarningThreshold: "2GiB" | ||
| freeTempSpaceThreshold: "1GiB" | ||
| freeTempSpaceWarningThreshold: "2GiB" | ||
| - envVars: | ||
| env: | ||
| - key: FOO | ||
| value: BAR | ||
| - key: FOO2 | ||
| value: "" | ||
| - toolLocation: | ||
| locations: | ||
| - home: "/home/user/bin/git" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" |
11 changes: 11 additions & 0 deletions
11
integrations/src/test/resources/io/jenkins/plugins/casc/GlobalNodePropertiesTestExpected.yml
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| - diskSpaceMonitor: | ||
| freeDiskSpaceThreshold: "1GiB" | ||
| freeDiskSpaceWarningThreshold: "2GiB" | ||
| freeTempSpaceThreshold: "1GiB" | ||
| freeTempSpaceWarningThreshold: "2GiB" | ||
| - envVars: | ||
| env: | ||
| - key: "FOO" | ||
| value: "BAR" | ||
| - key: "FOO2" | ||
| value: "" | ||
| - toolLocation: | ||
| locations: | ||
| - home: "/home/user/bin/git" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" |
12 changes: 12 additions & 0 deletions
12
...ations/src/test/resources/io/jenkins/plugins/casc/GlobalNodePropertiesWithEnvVarsTest.yml
This file contains hidden or 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,12 @@ | ||
| jenkins: | ||
| globalNodeProperties: | ||
| - envVars: | ||
| env: | ||
| - key: FOO | ||
| value: ${VALUE_1} | ||
| - key: FOO2 | ||
| value: ${VALUE_2} | ||
| - toolLocation: | ||
| locations: | ||
| - home: "${TEST_GIT_HOME}" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" |
10 changes: 10 additions & 0 deletions
10
...rc/test/resources/io/jenkins/plugins/casc/GlobalNodePropertiesWithEnvVarsTestExpected.yml
This file contains hidden or 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,10 @@ | ||
| - envVars: | ||
| env: | ||
| - key: "FOO" | ||
| value: "BAR" | ||
| - key: "FOO2" | ||
| value: "" | ||
| - toolLocation: | ||
| locations: | ||
| - home: "git-home" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" | ||
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
plugin/src/main/java/io/jenkins/plugins/casc/core/GlobalNodePropertiesConfigurator.java
This file contains hidden or 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,57 @@ | ||
| package io.jenkins.plugins.casc.core; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import hudson.Extension; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
| import io.jenkins.plugins.casc.Attribute; | ||
| import io.jenkins.plugins.casc.ConfigurationContext; | ||
| import io.jenkins.plugins.casc.ConfiguratorException; | ||
| import io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator; | ||
| import io.jenkins.plugins.casc.model.CNode; | ||
| import io.jenkins.plugins.casc.model.Mapping; | ||
|
|
||
| @Extension | ||
| public class GlobalNodePropertiesConfigurator extends DataBoundConfigurator<EnvironmentVariablesNodeProperty> { | ||
|
|
||
| public GlobalNodePropertiesConfigurator() { | ||
| this(EnvironmentVariablesNodeProperty.class); | ||
| } | ||
|
|
||
| public GlobalNodePropertiesConfigurator(Class<?> clazz) { | ||
| super(EnvironmentVariablesNodeProperty.class); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public String getName() { | ||
| return "globalNodeProperties"; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public EnvironmentVariablesNodeProperty configure(CNode c, ConfigurationContext context) | ||
| throws ConfiguratorException { | ||
| return super.configure(c, context); | ||
| } | ||
|
|
||
| @Override | ||
| @CheckForNull | ||
| public CNode describe(EnvironmentVariablesNodeProperty instance, ConfigurationContext context) throws Exception { | ||
| Mapping mapping = new Mapping(); | ||
| for (Attribute attribute : getAttributes()) { | ||
| CNode value = attribute.describe(instance, context); | ||
| if (value != null) { | ||
| // Making sure empty variables are part of the export | ||
| value.asSequence().forEach(entry -> { | ||
| if (entry.asMapping().get("key") != null | ||
| && entry.asMapping().get("value") != null) { | ||
| entry.asMapping().get("value").asScalar().setPrintableWhenEmpty(true); | ||
| } | ||
| }); | ||
| mapping.put(attribute.getName(), value); | ||
| } | ||
| } | ||
| return mapping; | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.