forked from geoffbullen/build-pipeline-plugin
-
Notifications
You must be signed in to change notification settings - Fork 160
/
BuildUtilTest.java
221 lines (179 loc) · 9.45 KB
/
BuildUtilTest.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* The MIT License
*
* Copyright (c) 2011, Centrumsystems Pty Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
package au.com.centrumsystems.hudson.plugin.util;
import hudson.model.FreeStyleBuild;
import hudson.model.AbstractBuild;
import hudson.model.Cause.UserCause;
import hudson.model.FreeStyleProject;
import hudson.model.Hudson;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.model.StringParameterValue;
import hudson.tasks.BuildTrigger;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hudson.test.HudsonTestCase;
import org.jvnet.hudson.test.RunLoadCounter;
import java.util.concurrent.Callable;
public class BuildUtilTest extends HudsonTestCase {
@Override
@Before
public void setUp() throws Exception {
super.setUp();
}
@Test
public void testGetDownstreamBuildWithLimit() throws Exception {
int max_upstream_depth = 3;
int total_proj2_builds = 10;
System.setProperty(BuildUtil.class.getCanonicalName() + ".MAX_DOWNSTREAM_DEPTH", String.valueOf(max_upstream_depth));
final FreeStyleProject proj1 = createFreeStyleProject();
final FreeStyleProject proj2 = createFreeStyleProject();
final FreeStyleBuild freeStyleBuild = proj1.scheduleBuild2(0).get();
assertBuildStatusSuccess(freeStyleBuild);
proj1.getPublishersList().add(new BuildTrigger(proj2.getName(), true));
jenkins.rebuildDependencyGraph();
// Schedule a build 10 times
for (int i = 0; i < total_proj2_builds; i++) {
assertBuildStatusSuccess(proj1.scheduleBuild2(0));
waitUntilNoActivity();
}
assertEquals("Proj2 does not have the correct number of builds.", total_proj2_builds + 1, proj2.getNextBuildNumber());
RunLoadCounter.prepare(proj2);
assertNull(RunLoadCounter.assertMaxLoads(proj2, max_upstream_depth + 2, new Callable<AbstractBuild<?, ?>>() {
@Override
public AbstractBuild<?, ?> call() throws Exception {
return BuildUtil.getDownstreamBuild(proj2, freeStyleBuild);
}
}));
}
@Test
public void testGetDownstreamBuild() throws Exception {
final String proj1 = "Proj1";
final String proj2 = "Proj2";
final String proj3 = "Proj3";
FreeStyleProject project1, project2, project3;
FreeStyleBuild build1, build2, build3;
final FreeStyleProject project4 = null;
final FreeStyleBuild build4 = null;
// Create test projects and associated builders
project1 = createFreeStyleProject(proj1);
project2 = createFreeStyleProject(proj2);
project3 = createFreeStyleProject(proj3);
// Add project2 as a post build action: build other project
project1.getPublishersList().add(new BuildTrigger(proj2, true));
project2.getPublishersList().add(new BuildTrigger(proj3, true));
// Important; we must do this step to ensure that the dependency graphs are updated
Hudson.getInstance().rebuildDependencyGraph();
// Build project1, upon completion project2 will be built
build1 = buildAndAssertSuccess(project1);
// When all building is complete retrieve the last build from project2
waitUntilNoActivity();
build2 = project2.getLastBuild();
build3 = project3.getLastBuild();
AbstractBuild<?, ?> nextBuild = BuildUtil.getDownstreamBuild(project2, build1);
assertEquals("The next build should be " + proj1 + build2.number, build2, nextBuild);
nextBuild = BuildUtil.getDownstreamBuild(project3, nextBuild);
assertEquals("The next build should be " + proj1 + build3.number, build3, nextBuild);
nextBuild = BuildUtil.getDownstreamBuild(project4, build4);
assertNull(nextBuild);
}
@Test
public void testGetAllBuildParametersAction() throws Exception {
final String proj1 = "Proj1";
final String proj2 = "Proj2";
final String key1 = "testKey";
final String key2 = "testKey2";
final String value1 = "testValue";
final String value2 = "testValue2";
final String value3 = "testValue3";
FreeStyleProject project1, project2;
FreeStyleBuild build1;
// Create test projects and associated builders
project1 = createFreeStyleProject(proj1);
project2 = createFreeStyleProject(proj2);
// Add a String parameter
project1.addProperty((new ParametersDefinitionProperty(new StringParameterDefinition(key1, value1))));
project1.addProperty((new ParametersDefinitionProperty(new StringParameterDefinition(key2, value3))));
project2.addProperty((new ParametersDefinitionProperty(new StringParameterDefinition(key1, value2))));
// Add project2 as a post build action: build other project
project1.getPublishersList().add(new BuildTrigger(proj2, true));
// Important; we must do this step to ensure that the dependency graphs are updated
Hudson.getInstance().rebuildDependencyGraph();
// Build project1, upon completion project2 will be built
// build1 = buildAndAssertSuccess(project1);
build1 = project1.scheduleBuild2(0, new UserCause(),
new ParametersAction(new StringParameterValue(key1, value1), new StringParameterValue(key2, value3))).get();
// When all building is complete retrieve the last build from project2
waitUntilNoActivity();
final ParametersAction params = (ParametersAction) BuildUtil.getAllBuildParametersAction(build1, project2);
assertEquals(((StringParameterValue) params.getParameter(key1)).value, value2);
assertEquals(((StringParameterValue) params.getParameter(key2)).value, value3);
}
@Test
public void testGetBuildParametersAction() throws Exception {
final String proj1 = "Proj1";
final String key1 = "testKey";
final String key2 = "testKey2";
final String value1 = "testValue";
final String value3 = "testValue3";
FreeStyleProject project1;
FreeStyleBuild build1;
final FreeStyleBuild build2 = null;
// Create test projects and associated builders
project1 = createFreeStyleProject(proj1);
// Add a String parameter
project1.addProperty((new ParametersDefinitionProperty(new StringParameterDefinition(key1, value1))));
project1.addProperty((new ParametersDefinitionProperty(new StringParameterDefinition(key2, value3))));
// Important; we must do this step to ensure that the dependency graphs are updated
Hudson.getInstance().rebuildDependencyGraph();
// Build project1 with the two StringParameterValues
build1 = project1.scheduleBuild2(0, new UserCause(),
new ParametersAction(new StringParameterValue(key1, value1), new StringParameterValue(key2, value3))).get();
waitUntilNoActivity();
ParametersAction params = BuildUtil.getBuildParametersAction(build1);
assertEquals(((StringParameterValue) params.getParameter(key1)).value, value1);
assertEquals(((StringParameterValue) params.getParameter(key2)).value, value3);
params = BuildUtil.getBuildParametersAction(build2);
assertNull(params);
}
@Test
public void testMergeParameters() throws Exception {
final String key1 = "testKey";
final String key2 = "testKey2";
final String value1 = "testValue";
final String value2 = "testValue2";
final String value3 = "testValue3";
ParametersAction baseParams = new ParametersAction(new StringParameterValue(key1, value1), new StringParameterValue(key2, value3));
ParametersAction extraParams = new ParametersAction(new StringParameterValue(key2, value2));
ParametersAction params = BuildUtil.mergeParameters(baseParams, extraParams);
assertEquals(((StringParameterValue) params.getParameter(key1)).value, value1);
assertEquals(((StringParameterValue) params.getParameter(key2)).value, value2);
baseParams = null;
extraParams = null;
params = BuildUtil.mergeParameters(baseParams, extraParams);
assertEquals(params.getParameters().size(), 0);
}
}