-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathDeclarativePipelineTest.java
More file actions
157 lines (147 loc) · 6 KB
/
DeclarativePipelineTest.java
File metadata and controls
157 lines (147 loc) · 6 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
package org.jenkins.plugins.lockableresources;
import static org.junit.jupiter.api.Assertions.assertNull;
import com.google.common.base.Joiner;
import hudson.model.Result;
import org.jenkins.plugins.lockableresources.util.Constants;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
@WithJenkins
class DeclarativePipelineTest {
// ---------------------------------------------------------------------------
@BeforeEach
void setUp() {
// to speed up the test
System.setProperty(Constants.SYSTEM_PROPERTY_DISABLE_SAVE, "true");
}
@Test
void lockByIdInOptionsSection(JenkinsRule j) throws Exception {
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
m(
"pipeline {",
" agent none",
" options {",
" lock resource: 'resource1'",
" }",
" stages {",
" stage('test') {",
" steps {",
" echo 'foo'",
" }",
" }",
" }",
"}"),
true));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.SUCCESS, b1);
j.assertLogContains("Resource [resource1] did not exist. Created.", b1);
assertNull(LockableResourcesManager.get().fromName("resource1"));
}
@Test
void lockByLabelInOptionsSection(JenkinsRule j) throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource1", "label1");
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
m(
"pipeline {",
" agent none",
" options {",
" lock label: 'label1', resource : null",
" }",
" stages {",
" stage('test') {",
" steps {",
" echo 'foo'",
" }",
" }",
" }",
"}"),
true));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.SUCCESS, b1);
j.assertLogContains("Lock acquired on [Label: label1]", b1);
}
@Test
void stepScriptLockByLabel(JenkinsRule j) throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource1", "label1");
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
m(
"pipeline {",
" agent none",
" stages {",
" stage('test') {",
" steps {",
" script {",
" lock(label: 'label1', resource : null, variable: 'LABEL_LOCKED', quantity: 1) {",
" echo \"Lock acquired: ${LABEL_LOCKED}\"",
" }",
" }",
" }",
" }",
" }",
"}"),
true));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.SUCCESS, b1);
j.assertLogContains("Lock acquired: resource1", b1);
}
@Test
void stepLockByLabel(JenkinsRule j) throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource1", "label1");
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
m(
"pipeline {",
" agent none",
" stages {",
" stage('test') {",
" steps {",
" lock(label: 'label1', resource : null, variable: 'LABEL_LOCKED', quantity: 1) {",
" echo \"Lock acquired: ${LABEL_LOCKED}\"",
" }",
" }",
" }",
" }",
"}"),
true));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.SUCCESS, b1);
j.assertLogContains("Lock acquired: resource1", b1);
}
@Test
void missingLabel(JenkinsRule j) throws Exception {
WorkflowJob p = j.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
m(
"pipeline {",
" agent none",
" stages {",
" stage('test') {",
" steps {",
" lock() {",
" echo \"This will still be executed\"",
" }",
" }",
" }",
" }",
"}"),
true));
WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
j.waitForCompletion(b1);
j.assertBuildStatus(Result.FAILURE, b1);
j.assertLogContains("Missing required parameter: \"resource\"", b1);
}
private static String m(String... lines) {
return Joiner.on('\n').join(lines);
}
}