-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathOrganizationFolderTest.java
More file actions
420 lines (361 loc) · 19.3 KB
/
OrganizationFolderTest.java
File metadata and controls
420 lines (361 loc) · 19.3 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* The MIT License
*
* Copyright 2015 CloudBees, Inc.
*
* 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 jenkins.branch;
import com.cloudbees.hudson.plugins.folder.computed.ChildObserver;
import com.cloudbees.hudson.plugins.folder.computed.ComputedFolder;
import hudson.ExtensionList;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.ParameterDefinition;
import hudson.model.Result;
import hudson.model.StringParameterDefinition;
import hudson.model.TaskListener;
import hudson.model.View;
import hudson.scm.NullSCM;
import hudson.security.Permission;
import integration.harness.BasicMultiBranchProject;
import integration.harness.BasicMultiBranchProjectFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import jenkins.branch.harness.MultiBranchImpl;
import jenkins.scm.api.SCMNavigator;
import jenkins.scm.api.SCMSource;
import jenkins.scm.impl.SingleSCMNavigator;
import jenkins.scm.impl.SingleSCMSource;
import jenkins.scm.impl.mock.MockSCM;
import jenkins.scm.impl.mock.MockSCMController;
import jenkins.scm.impl.mock.MockSCMDiscoverBranches;
import jenkins.scm.impl.mock.MockSCMDiscoverChangeRequests;
import jenkins.scm.impl.mock.MockSCMDiscoverTags;
import jenkins.scm.impl.mock.MockSCMHead;
import jenkins.scm.impl.mock.MockSCMNavigator;
import org.acegisecurity.Authentication;
import org.acegisecurity.providers.TestingAuthenticationToken;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;
import static jenkins.branch.matchers.Extracting.extracting;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeThat;
import org.jvnet.hudson.test.LoggerRule;
public class OrganizationFolderTest {
public static class OrganizationFolderBranchProperty extends ParameterDefinitionBranchProperty {
@DataBoundConstructor
public OrganizationFolderBranchProperty() {
super();
}
@TestExtension
public static class DescriptorImpl extends BranchPropertyDescriptor {
@Override
protected boolean isApplicable(MultiBranchProjectDescriptor projectDescriptor) {
return projectDescriptor instanceof BasicMultiBranchProject.DescriptorImpl;
}
}
}
@Rule
public JenkinsRule r = new JenkinsRule();
@Rule
public LoggerRule logging = new LoggerRule().record(ComputedFolder.class, Level.FINE).record(OrganizationFolder.class, Level.FINE);
@Test
public void configRoundTrip() throws Exception {
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("stuff");
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
projectFactories.add(new MockFactory());
top.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy",
new MockSCM(c, "stuff", new MockSCMHead("master"), null))))
);
top = r.configRoundtrip(top);
List<SCMNavigator> navigators = top.getNavigators();
assertEquals(1, navigators.size());
assertEquals(SingleSCMNavigator.class, navigators.get(0).getClass());
assertEquals("stuff", ((SingleSCMNavigator) navigators.get(0)).getName());
projectFactories = top.getProjectFactories();
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItems(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class), ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
}
}
@Issue("JENKINS-48837")
@Test
public void verifyBranchPropertiesAppliedOnNewProjects() throws Exception {
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("stuff");
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
assertEquals(1, projectFactories.size());
assertEquals(MockFactory.class, projectFactories.get(0).getClass());
top.getNavigators().add(new SingleSCMNavigator("stuff",
Collections.<SCMSource>singletonList(new SingleSCMSource("stuffy",
new MockSCM(c, "stuff", new MockSCMHead("master"), null))))
);
OrganizationFolderBranchProperty instance = new OrganizationFolderBranchProperty();
instance.setParameterDefinitions(Collections.<ParameterDefinition>singletonList(
new StringParameterDefinition("PARAM_STR", "PARAM_DEFAULT_0812673", "The param")
));
top.setStrategy(new DefaultBranchPropertyStrategy(new BranchProperty[] { instance }));
top = r.configRoundtrip(top);
top.scheduleBuild(0);
r.waitUntilNoActivity();
// get the child project produced by the factory after scan
MultiBranchImpl prj = (MultiBranchImpl) top.getItem("stuff");
// verify new multibranch project have branch properties inherited from folder
assertThat(prj.getSources().get(0).getStrategy(), instanceOf(DefaultBranchPropertyStrategy.class));
DefaultBranchPropertyStrategy strategy = (DefaultBranchPropertyStrategy) prj.getSources().get(0).getStrategy();
assertThat(strategy.getProps().get(0), instanceOf(OrganizationFolderBranchProperty.class));
OrganizationFolderBranchProperty property = (OrganizationFolderBranchProperty) strategy.getProps().get(0);
assertThat(property.getParameterDefinitions(), contains(
allOf(
instanceOf(StringParameterDefinition.class),
hasProperty("name", is("PARAM_STR")),
hasProperty("defaultValue", is("PARAM_DEFAULT_0812673")),
hasProperty("description", is("The param"))
)
));
}
}
@Test
@Issue("JENKINS-31516")
public void indexChildrenOnOrganizationFolderIndex() throws Exception {
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("stuff");
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
top.getNavigators().add(new MockSCMNavigator(c, new MockSCMDiscoverBranches(), new MockSCMDiscoverTags(), new MockSCMDiscoverChangeRequests()));
assertThat("Top level has not been scanned", top.getItem("stuff"), nullValue());
top.scheduleBuild(0);
r.waitUntilNoActivity();
assertThat("Top level has been scanned", top.getItem("stuff"), notNullValue());
assertThat("We have run an index on the child item",
top.getItem("stuff").getComputation().getResult(), is(Result.SUCCESS)
);
}
}
@Issue("JENKINS-32782")
@Test
public void emptyViewEquality() throws Exception {
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
View emptyView = top.getPrimaryView();
assertEquals(BaseEmptyView.VIEW_NAME, emptyView.getViewName());
assertEquals(Messages.BaseEmptyView_displayName(), emptyView.getDisplayName());
assertEquals(emptyView, top.getPrimaryView());
assertTrue(emptyView.isDefault());
}
@Issue("JENKINS-34246")
@Test
public void deletedMarker() throws Exception {
assumeThat("TODO fails if jth.jenkins-war.path includes WorkflowMultiBranchProjectFactory since SingleSCMSource ignores SCMSourceCriteria",
ExtensionList.lookup(MultiBranchProjectFactoryDescriptor.class).stream().map(d -> d.clazz).toArray(),
arrayContainingInAnyOrder(MockFactory.class, BasicMultiBranchProjectFactory.class));
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
List<MultiBranchProjectFactory> projectFactories = top.getProjectFactories();
assertThat(projectFactories, extracting(f -> f.getDescriptor(), hasItem(ExtensionList.lookupSingleton(ConfigRoundTripDescriptor.class))));
top.getNavigators().add(new SingleSCMNavigator("stuff", Collections.<SCMSource>singletonList(new SingleSCMSource("id", "stuffy", new NullSCM()))));
top.scheduleBuild2(0).getFuture().get();
top.getComputation().writeWholeLogTo(System.out);
assertEquals(1, top.getItems().size());
r.waitUntilNoActivity();
MockFactory.live = false;
try {
top.scheduleBuild2(0).getFuture().get();
top.getComputation().writeWholeLogTo(System.out);
assertEquals(0, top.getItems().size());
} finally {
MockFactory.live = true;
}
}
/**
* When an OrganizationFolder is created and provided with no {@link MultiBranchProjectFactory} implementations,
* it should automatically add the enabled-by-default factories for the current Jenkins instance
* when {@link OrganizationFolder#onCreatedFromScratch()} is called.
* @throws IOException
*/
@Test
public void defaultFactoriesWhenNeeded() throws IOException {
// programmatically create an OrganizationFolder
OrganizationFolder newbie = new OrganizationFolder( r.jenkins, "newbie" );
// it should have no factories
assertEquals(
"A newly-created OrganizationFolder has no MultiBranchProjectFactories.",
0,
newbie.getProjectFactories().size() );
// these are all of the MultiBranchProjectFactory definitions.
ExtensionList<MultiBranchProjectFactoryDescriptor> factory_descriptors = r.jenkins.getExtensionList( MultiBranchProjectFactoryDescriptor.class );
// at some point after creating it, onCreatedFromScratch() would be called by Jenkins
newbie.onCreatedFromScratch();
// now, the folder should have all the default factories.
for( MultiBranchProjectFactoryDescriptor factory_descriptor : factory_descriptors ) {
if( factory_descriptor.newInstance() != null) {
// this factory is enabled by default, and should be present in the org folder
boolean default_factory_was_present = false;
for( MultiBranchProjectFactory org_factory : newbie.getProjectFactories() ) {
if( org_factory.getDescriptor().getClass().equals( factory_descriptor.getClass() ) ) {
// the factory that the org contained was one of the enabled-by-default factories.
default_factory_was_present = true;
break;
}
}
assertTrue(
"After #onCreatedFromScratch(), the enabled-by-default MultiBranchProjectFactory [" + factory_descriptor.getDisplayName()+ "] is present in an OrganizationFolder created with no initial factories.",
default_factory_was_present );
}
}
// additionally, all of the Org Folder's factories should be enabled-by-default factories.
for( MultiBranchProjectFactory org_factory : newbie.getProjectFactories() ) {
boolean org_factory_is_default_factory = false;
for( MultiBranchProjectFactoryDescriptor factory_descriptor : factory_descriptors ) {
if( ( factory_descriptor.newInstance() != null ) &&
( org_factory.getDescriptor().getClass().equals( factory_descriptor.getClass() ) ) )
{
// this descriptor was enabled by default AND it matches the org's factory descriptor
org_factory_is_default_factory = true;
break;
}
}
assertTrue(
"After #onCreatedFromScratch() with no initial project factories, the OrganizationFolder MultiBranchProjectFactory [" + org_factory.getDescriptor().getDisplayName()+ "] is one of the enabled-by-default factories.",
org_factory_is_default_factory );
}
}
/**
* When an OrganizationFolder is created and provided with at least one {@link MultiBranchProjectFactory},
* it should not add any other factories when {@link OrganizationFolder#onCreatedFromScratch()} is called.
*/
@Test
public void noDefaultFactoriesWhenNotNeeded() {
// programmatically create an OrganizationFolder
OrganizationFolder newbie = new OrganizationFolder( r.jenkins, "newbie" );
// it should have no factories
assertEquals(
"A newly-created OrganizationFolder has no MultiBranchProjectFactories.",
0,
newbie.getProjectFactories().size() );
// set exactly one non-default factory
newbie.getProjectFactories().clear();
MockFactory mock_factory = new MockFactory();
newbie.getProjectFactories().add( mock_factory );
// at some point after creating it, onCreatedFromScratch() would be called by Jenkins
newbie.onCreatedFromScratch();
assertEquals(
"An OrganizationFolder created with a non-default project factory should only have one factory after onCreatedFromScratch()",
1,
newbie.getProjectFactories().size() );
assertEquals(
"An OrganizationFolder created with a non-default project factory should only have that particular factory after onCreatedFromScratch()",
mock_factory.getDescriptor(),
newbie.getProjectFactories().get( 0 ).getDescriptor() ) ;
}
@Test
public void modifyAclsWhenInComputedFolder() throws IOException, InterruptedException {
Set<Permission> suppressed_permissions =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
Item.CONFIGURE, Item.DELETE, View.CONFIGURE, View.CREATE, View.DELETE)));
// we need a ComputedFolder to be the parent of our OrganizationFolder
ComputedFolder<OrganizationFolder> computed_folder = new ComputedFolder<OrganizationFolder>(r.jenkins, "top") {
@Override
protected void computeChildren(
ChildObserver<OrganizationFolder> observer,
TaskListener listener)
throws IOException, InterruptedException
{
/*
* We don't actually need to compute children during a unit test.
* We just need an OrganizationFolder to have this ComputedFolder
* as its parent.
*
* Since item parents are set on the item when constructing it,
* we can do that when we create the OrganizationFolder.
*
* Also, #scheduleBuild(...) to enqueue a computeChildren
* doesn't work out-of-the-box in the current unit-test setup.
*/
}
};
OrganizationFolder org_folder = new OrganizationFolder( computed_folder, "org" );
// SYSTEM (the default authentication scope) can do everything, so we need to look like someone else.
Authentication some_user = new TestingAuthenticationToken( this, "testing", null );
// verify that all of of the suppressed permissions are actually suppressed!
for( Permission perm : suppressed_permissions ) {
assertFalse(
"OrganizationFolders in ComputedFolders should suppress the [" + perm.getId() + "] permission.",
org_folder.getACL().hasPermission( some_user, perm ) );
}
}
@Test
public void modifyAclsWhenNotInComputedFolder() throws IOException {
Set<Permission> suppressed_permissions =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
Item.CONFIGURE, Item.DELETE, View.CONFIGURE, View.CREATE, View.DELETE)));
OrganizationFolder top = r.jenkins.createProject(OrganizationFolder.class, "top");
// SYSTEM (the default authentication scope) can do everything, so we need to look like someone else.
Authentication some_user = new TestingAuthenticationToken( this, "testing", null );
// verify that none of the suppressed permissions are suppressed
for( Permission perm : suppressed_permissions ) {
assertTrue(
"Organization Folders in non-computed parents do not suppress the [" + perm.getId() + "] permission.",
top.getACL().hasPermission( some_user, perm ) );
}
}
@TestExtension
public static class ConfigRoundTripDescriptor extends MockFactoryDescriptor {}
public static class MockFactory extends MultiBranchProjectFactory {
@DataBoundConstructor
public MockFactory() {}
static boolean live = true;
@Override
public boolean recognizes(ItemGroup<?> parent, String name, List<? extends SCMSource> scmSources, Map<String, Object> attributes, TaskListener listener) throws IOException, InterruptedException {
return live;
}
@Override
public MultiBranchProject<?, ?> createNewProject(ItemGroup<?> parent, String name, List<? extends SCMSource> scmSources, Map<String,Object> attributes, TaskListener listener) throws IOException, InterruptedException {
return new MultiBranchImpl(parent, name);
}
}
static abstract class MockFactoryDescriptor extends MultiBranchProjectFactoryDescriptor {
MockFactoryDescriptor() {
super(MockFactory.class);
}
@Override
public MultiBranchProjectFactory newInstance() {
return new MockFactory();
}
@Override
public String getDisplayName() {
return "MockFactory";
}
}
}