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

1 package hudson.tasks;
2
3 import hudson.EnvVars;
4 import hudson.model.labels.LabelAtom;
5 import hudson.maven.MavenModuleSet;
6 import hudson.maven.MavenModuleSetBuild;
7 import hudson.model.AbstractBuild;
8 import hudson.model.FreeStyleBuild;
9 import hudson.model.FreeStyleProject;
10 import hudson.model.JDK;
11 import hudson.model.Result;
12 import hudson.slaves.DumbSlave;
13 import hudson.tasks.Ant.AntInstallation;
14 import hudson.tasks.Maven.MavenInstallation;
15
16 import org.apache.tools.ant.taskdefs.condition.Os;
17 import org.jvnet.hudson.test.ExtractResourceSCM;
18 import org.jvnet.hudson.test.HudsonTestCase;
19 import static hudson.tasks._ant.Messages.Ant_ExecutableNotFound;
20
21 public class EnvVarsInConfigTasksTest extends HudsonTestCase {
22 public static final String DUMMY_LOCATION_VARNAME = "TOOLS_DUMMY_LOCATION";
23
24 private DumbSlave slaveEnv = null;
25 private DumbSlave slaveRegular = null;
26
27 public void setUp() throws Exception {
28 super.setUp();
29
30 JDK defaultJDK = jenkins.getJDK(null);
31 JDK varJDK = new JDK("varJDK", withVariable(defaultJDK.getHome()));
32 jenkins.getJDKs().add(varJDK);
33
34 // Maven with a variable in its path
35 configureDefaultMaven();
36 MavenInstallation defaultMaven = jenkins.getDescriptorByType(Maven.DescriptorImpl.class).getInstallations()[0];
37 MavenInstallation varMaven = new MavenInstallation("varMaven",
38 withVariable(defaultMaven.getHome()), NO_PROPERTIES);
39 jenkins.getDescriptorByType(Maven.DescriptorImpl.class).setInstallations(varMaven);
40
41 // Ant with a variable in its path
42 AntInstallation ant = configureDefaultAnt();
43 AntInstallation antInstallation = new AntInstallation("varAnt",
44 withVariable(ant.getHome()),NO_PROPERTIES);
45 jenkins.getDescriptorByType(Ant.DescriptorImpl.class).setInstallations(antInstallation);
46
47 // create slaves
48 EnvVars additionalEnv = new EnvVars(DUMMY_LOCATION_VARNAME, "");
49 slaveEnv = createSlave(new LabelAtom("slaveEnv"), additionalEnv);
50 slaveRegular = createSlave(new LabelAtom("slaveRegular"));
51 }
52
53 private String withVariable(String s) {
54 return s + "${" + DUMMY_LOCATION_VARNAME + "}";
55 }
56
57 public void testFreeStyleShellOnSlave() throws Exception {
58 FreeStyleProject project = createFreeStyleProject();
59 if (Os.isFamily("dos")) {
60 project.getBuildersList().add(new BatchFile("echo %JAVA_HOME%"));
61 } else {
62 project.getBuildersList().add(new Shell("echo \"$JAVA_HOME\""));
63 }
64 project.setJDK(jenkins.getJDK("varJDK"));
65
66 // set appropriate SCM to get the necessary build files
67 project.setScm(new ExtractResourceSCM(getClass().getResource(
68 "/simple-projects.zip")));
69
70 // test the regular slave - variable not expanded
71 project.setAssignedLabel(slaveRegular.getSelfLabel());
72 FreeStyleBuild build = project.scheduleBuild2(0).get();
73 System.out.println(build.getDisplayName() + " completed");
74
75 assertBuildStatusSuccess(build);
76
77 String buildLogRegular = getBuildLog(build);
78 System.out.println(buildLogRegular);
79 assertTrue(buildLogRegular.contains(DUMMY_LOCATION_VARNAME));
80
81 // test the slave with prepared environment
82 project.setAssignedLabel(slaveEnv.getSelfLabel());
83 build = project.scheduleBuild2(0).get();
84 System.out.println(build.getDisplayName() + " completed");
85
86 assertBuildStatusSuccess(build);
87
88 // Check variable was expanded
89 String buildLogEnv = getBuildLog(build);
90 System.out.println(buildLogEnv);
91 assertFalse(buildLogEnv.contains(DUMMY_LOCATION_VARNAME));
92 }
93
94 public void testFreeStyleAntOnSlave() throws Exception {
95 if (jenkins.getDescriptorByType(Ant.DescriptorImpl.class).getInstallations().length == 0) {
96 System.out.println("Cannot do testFreeStyleAntOnSlave without ANT_HOME");
97 return;
98 }
99 FreeStyleProject project = createFreeStyleProject();
100 project.setJDK(jenkins.getJDK("varJDK"));
101 project.setScm(new ExtractResourceSCM(getClass().getResource(
102 "/simple-projects.zip")));
103
104 String buildFile = "build.xml${" + DUMMY_LOCATION_VARNAME + "}";
105 // we need additional escapes because bash itself expanding
106 project.getBuildersList().add(
107 new Ant("-Dtest.property=cor${" + DUMMY_LOCATION_VARNAME
108 + "}rect", "varAnt", "", buildFile, ""));
109
110 // test the regular slave - variable not expanded
111 project.setAssignedLabel(slaveRegular.getSelfLabel());
112 FreeStyleBuild build = project.scheduleBuild2(0).get();
113 System.out.println(build.getDisplayName() + " completed");
114
115 assertBuildStatus(Result.FAILURE, build);
116
117 String buildLogRegular = getBuildLog(build);
118 assertTrue(buildLogRegular.contains(Ant_ExecutableNotFound("varAnt")));
119
120 // test the slave with prepared environment
121 project.setAssignedLabel(slaveEnv.getSelfLabel());
122 build = project.scheduleBuild2(0).get();
123 System.out.println(build.getDisplayName() + " completed");
124
125 assertBuildStatusSuccess(build);
126
127 // Check variable was expanded
128 String buildLogEnv = getBuildLog(build);
129 System.out.println(buildLogEnv);
130 assertTrue(buildLogEnv.contains("Ant home: "));
131 assertTrue(buildLogEnv.contains("Test property: correct"));
132 assertFalse(buildLogEnv.matches("(?s)^.*Ant home: [^\\n\\r]*"
133 + DUMMY_LOCATION_VARNAME + ".*$"));
134 assertFalse(buildLogEnv.matches("(?s)^.*Test property: [^\\n\\r]*"
135 + DUMMY_LOCATION_VARNAME + ".*$"));
136 }
137
138 public void testFreeStyleMavenOnSlave() throws Exception {
139 FreeStyleProject project = createFreeStyleProject();
140 project.setJDK(jenkins.getJDK("varJDK"));
141 project.setScm(new ExtractResourceSCM(getClass().getResource(
142 "/simple-projects.zip")));
143
144 project.getBuildersList().add(
145 new Maven("test", "varMaven", "pom.xml${"
146 + DUMMY_LOCATION_VARNAME + "}", "", "",
147 false));
148
149 // test the regular slave - variable not expanded
150 project.setAssignedLabel(slaveRegular.getSelfLabel());
151 FreeStyleBuild build = project.scheduleBuild2(0).get();
152 System.out.println(build.getDisplayName() + " completed");
153
154 assertBuildStatus(Result.FAILURE, build);
155
156 String buildLogRegular = getBuildLog(build);
157 System.out.println(buildLogRegular);
158 assertTrue(buildLogRegular.contains(DUMMY_LOCATION_VARNAME));
159
160 // test the slave with prepared environment
161 project.setAssignedLabel(slaveEnv.getSelfLabel());
162 build = project.scheduleBuild2(0).get();
163 System.out.println(build.getDisplayName() + " completed");
164
165 assertBuildStatusSuccess(build);
166
167 // Check variable was expanded
168 String buildLogEnv = getBuildLog(build);
169 System.out.println(buildLogEnv);
170 assertFalse(buildLogEnv.contains(DUMMY_LOCATION_VARNAME));
171 }
172
173 public void testNativeMavenOnSlave() throws Exception {
174 MavenModuleSet project = createMavenProject();
175 project.setJDK(jenkins.getJDK("varJDK"));
176 project.setScm(new ExtractResourceSCM(getClass().getResource(
177 "/simple-projects.zip")));
178
179 project.setMaven("varMaven");
180 project.setGoals("clean${" + DUMMY_LOCATION_VARNAME + "}");
181
182 // test the regular slave - variable not expanded
183 project.setAssignedLabel(slaveRegular.getSelfLabel());
184 MavenModuleSetBuild build = project.scheduleBuild2(0).get();
185 System.out.println(build.getDisplayName() + " completed");
186
187 assertBuildStatus(Result.FAILURE, build);
188
189 String buildLogRegular = getBuildLog(build);
190 System.out.println(buildLogRegular);
191
192 // test the slave with prepared environment
193 project.setAssignedLabel(slaveEnv.getSelfLabel());
194 build = project.scheduleBuild2(0).get();
195 System.out.println(build.getDisplayName() + " completed");
196
197 assertBuildStatusSuccess(build);
198
199 // Check variable was expanded
200 String buildLogEnv = getBuildLog(build);
201 System.out.println(buildLogEnv);
202 assertFalse(buildLogEnv.contains(DUMMY_LOCATION_VARNAME));
203 }
204
205 @SuppressWarnings("deprecation") // it's okay to use it in tests
206 private String getBuildLog(AbstractBuild<?,?> build) throws Exception {
207 return build.getLog();
208 }
209 }
210