File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\test\src\test\java\hudson\slaves\EnvironmentVariableNodePropertyTest.java.html

1 package hudson.slaves;
2
3 import hudson.model.FreeStyleBuild;
4 import hudson.model.FreeStyleProject;
5 import hudson.model.Node;
6 import hudson.model.ParametersDefinitionProperty;
7 import hudson.model.Result;
8 import hudson.model.StringParameterDefinition;
9 import hudson.slaves.EnvironmentVariablesNodeProperty.Entry;
10
11 import java.io.IOException;
12 import java.util.Collections;
13 import java.util.Map;
14
15 import junit.framework.Assert;
16
17 import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
18 import org.jvnet.hudson.test.HudsonTestCase;
19
20 import com.gargoylesoftware.htmlunit.html.HtmlForm;
21 import com.gargoylesoftware.htmlunit.html.HtmlPage;
22
23 /**
24 * This class tests that environment variables from node properties are applied,
25 * and that the priority is maintained: parameters > slave node properties >
26 * master node properties
27 */
28 public class EnvironmentVariableNodePropertyTest extends HudsonTestCase {
29
30 private DumbSlave slave;
31 private FreeStyleProject project;
32
33 /**
34 * Slave properties are available
35 */
36 public void testSlavePropertyOnSlave() throws Exception {
37 setVariables(slave, new Entry("KEY", "slaveValue"));
38 Map<String, String> envVars = executeBuild(slave);
39 Assert.assertEquals("slaveValue", envVars.get("KEY"));
40 }
41
42 /**
43 * Master properties are available
44 */
45 public void testMasterPropertyOnMaster() throws Exception {
46 jenkins.getGlobalNodeProperties().replaceBy(
47 Collections.singleton(new EnvironmentVariablesNodeProperty(
48 new Entry("KEY", "masterValue"))));
49
50 Map<String, String> envVars = executeBuild(jenkins);
51
52 Assert.assertEquals("masterValue", envVars.get("KEY"));
53 }
54
55 /**
56 * Both slave and master properties are available, but slave properties have priority
57 */
58 public void testSlaveAndMasterPropertyOnSlave() throws Exception {
59 jenkins.getGlobalNodeProperties().replaceBy(
60 Collections.singleton(new EnvironmentVariablesNodeProperty(
61 new Entry("KEY", "masterValue"))));
62 setVariables(slave, new Entry("KEY", "slaveValue"));
63
64 Map<String, String> envVars = executeBuild(slave);
65
66 Assert.assertEquals("slaveValue", envVars.get("KEY"));
67 }
68
69 /**
70 * Slave and master properties and parameters are available.
71 * Priority: parameters > slave > master
72 * @throws Exception
73 */
74 public void testSlaveAndMasterPropertyAndParameterOnSlave()
75 throws Exception {
76 ParametersDefinitionProperty pdp = new ParametersDefinitionProperty(
77 new StringParameterDefinition("KEY", "parameterValue"));
78 project.addProperty(pdp);
79
80 setVariables(jenkins, new Entry("KEY", "masterValue"));
81 setVariables(slave, new Entry("KEY", "slaveValue"));
82
83 Map<String, String> envVars = executeBuild(slave);
84
85 Assert.assertEquals("parameterValue", envVars.get("KEY"));
86 }
87
88 public void testVariableResolving() throws Exception {
89 jenkins.getGlobalNodeProperties().replaceBy(
90 Collections.singleton(new EnvironmentVariablesNodeProperty(
91 new Entry("KEY1", "value"), new Entry("KEY2", "$KEY1"))));
92 Map<String,String> envVars = executeBuild(jenkins);
93 Assert.assertEquals("value", envVars.get("KEY1"));
94 Assert.assertEquals("value", envVars.get("KEY2"));
95 }
96
97 public void testFormRoundTripForMaster() throws Exception {
98 jenkins.getGlobalNodeProperties().replaceBy(
99 Collections.singleton(new EnvironmentVariablesNodeProperty(
100 new Entry("KEY", "value"))));
101
102 WebClient webClient = new WebClient();
103 HtmlPage page = webClient.getPage(jenkins, "configure");
104 HtmlForm form = page.getFormByName("config");
105 submit(form);
106
107 Assert.assertEquals(1, jenkins.getGlobalNodeProperties().toList().size());
108
109 EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class);
110 Assert.assertEquals(1, prop.getEnvVars().size());
111 Assert.assertEquals("value", prop.getEnvVars().get("KEY"));
112 }
113
114 public void testFormRoundTripForSlave() throws Exception {
115 setVariables(slave, new Entry("KEY", "value"));
116
117 WebClient webClient = new WebClient();
118 HtmlPage page = webClient.getPage(slave, "configure");
119 HtmlForm form = page.getFormByName("config");
120 submit(form);
121
122 Assert.assertEquals(1, slave.getNodeProperties().toList().size());
123
124 EnvironmentVariablesNodeProperty prop = slave.getNodeProperties().get(EnvironmentVariablesNodeProperty.class);
125 Assert.assertEquals(1, prop.getEnvVars().size());
126 Assert.assertEquals("value", prop.getEnvVars().get("KEY"));
127 }
128
129 // //////////////////////// setup //////////////////////////////////////////
130
131 public void setUp() throws Exception {
132 super.setUp();
133 slave = createSlave();
134 project = createFreeStyleProject();
135 }
136
137 // ////////////////////// helper methods /////////////////////////////////
138
139 private void setVariables(Node node, Entry... entries) throws IOException {
140 node.getNodeProperties().replaceBy(
141 Collections.singleton(new EnvironmentVariablesNodeProperty(
142 entries)));
143
144 }
145
146 /**
147 * Launches project on this node, waits for the result, and returns the environment that is used
148 */
149 private Map<String, String> executeBuild(Node node) throws Exception {
150 CaptureEnvironmentBuilder builder = new CaptureEnvironmentBuilder();
151
152 project.getBuildersList().add(builder);
153 project.setAssignedLabel(node.getSelfLabel());
154
155 // use a timeout so we don't wait infinitely in case of failure
156 FreeStyleBuild build = project.scheduleBuild2(0).get(/*10, TimeUnit.SECONDS*/);
157
158 System.out.println(build.getLog());
159 Assert.assertEquals(Result.SUCCESS, build.getResult());
160
161 return builder.getEnvVars();
162 }
163
164 }
165