-
Notifications
You must be signed in to change notification settings - Fork 749
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 10 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/GlobalNodePropertiesWithEnvaVarsTest.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 GlobalNodePropertiesWithEnvaVarsTest { | ||
|
|
||
| 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)); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
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,18 @@ | ||
| jenkins: | ||
| globalNodeProperties: | ||
| - diskSpaceMonitor: | ||
| freeDiskSpaceThreshold: "1GiB" | ||
| freeDiskSpaceWarningThreshold: "2GiB" | ||
| freeTempSpaceThreshold: "1GiB" | ||
| freeTempSpaceWarningThreshold: "2GiB" | ||
| - envVars: | ||
| env: | ||
| - key: FOO | ||
| value: BAR | ||
| - key: FOO2 | ||
| value: "" | ||
| - key: FOO3 | ||
| - 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" | ||
| - key: "FOO3" | ||
|
PereBueno marked this conversation as resolved.
Outdated
|
||
| - 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" |
9 changes: 9 additions & 0 deletions
9
...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,9 @@ | ||
| - envVars: | ||
| env: | ||
| - key: "FOO" | ||
| value: "BAR" | ||
| - key: "FOO2" | ||
|
PereBueno marked this conversation as resolved.
|
||
| - toolLocation: | ||
| locations: | ||
| - home: "git-home" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" | ||
65 changes: 65 additions & 0 deletions
65
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,65 @@ | ||
| package io.jenkins.plugins.casc.core; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import hudson.Extension; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty.Entry; | ||
| 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; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @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 { | ||
| Mapping mapping = c.asMapping(); | ||
| List<Entry> variables = getVarsAsList(mapping, context); | ||
| return new EnvironmentVariablesNodeProperty(variables); | ||
| } | ||
|
|
||
| private List<Entry> getVarsAsList(Mapping m, ConfigurationContext context) { | ||
| List<Entry> result = new ArrayList<>(); | ||
| if (m.get("env") != null) { | ||
| result = m.get("env").asSequence().stream() | ||
| .map(pair -> { | ||
| if (pair.asMapping().get("key") == null) { | ||
| return null; | ||
| } | ||
|
PereBueno marked this conversation as resolved.
Outdated
|
||
| final String key = | ||
| pair.asMapping().get("key").asScalar().getValue(); | ||
| final String value = pair.asMapping().get("value") == null | ||
| ? "" | ||
|
PereBueno marked this conversation as resolved.
Outdated
|
||
| : context.getSecretSourceResolver() | ||
| .resolve(pair.asMapping() | ||
| .get("value") | ||
| .asScalar() | ||
| .getValue()); | ||
|
|
||
| return new Entry(key, value); | ||
| }) | ||
| .toList(); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This (
FOO3) just looks like a user error to me ⇒ the bundle should be rejected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UI allows to save an empty env var (regardless why would someone want that), so theoretically configuration as code should allow creating that configuration too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am talking about
FOO3(null) notFOO2(empty).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the point in allowing importing the exported yaml.
Empty vars will still be exported with no value, so it's either allowing keys without a value when importing or changing the export (which was discussed here).
I think we all agree that would be the optimal solution, but when testing that it impacted exports from several plugins, so that's not what we're trying to cover with this change
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the point is that
is invalid and should not be allowed (unless it works right now and this is just testing something pre-existing)
a null env var value is invalid.
an empty string env var uncommon, but I guess
so ok:
not ok:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not just limit the behavioral change to this setting, leaving other misbehaving plugins for another day?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks do-able from a quick look, something similar to this on
CNodeand then allow setting the value when you construct it.Not sure how easy it would be to integrate when using databoundconfigurator though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying that, with no luck for the moment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(my link was missing added now)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated be00ec1:
Will update PR description accordingly