forked from jenkinsci/configuration-as-code-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalNodePropertiesWithEnvVarsTest.java
More file actions
64 lines (54 loc) · 2.77 KB
/
GlobalNodePropertiesWithEnvVarsTest.java
File metadata and controls
64 lines (54 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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));
}
}