-
Notifications
You must be signed in to change notification settings - Fork 182
/
LockStepHardKillTest.java
178 lines (154 loc) · 7.45 KB
/
LockStepHardKillTest.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
package org.jenkins.plugins.lockableresources;
import static org.junit.Assert.assertNotNull;
import hudson.model.Executor;
import hudson.model.Result;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
public class LockStepHardKillTest extends LockStepTestBase {
@Rule
public JenkinsRule j = new JenkinsRule();
@Issue("JENKINS-36479")
@Test
public void hardKillNewBuildClearsLock() throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource1", "label");
WorkflowJob p1 = j.jenkins.createProject(WorkflowJob.class, "p1");
p1.setDefinition(new CpsFlowDefinition(
"lock('resource1') {"
+ " echo 'JOB 1 inside'\n"
+
// this looks strange, but don not panic
// the job will be killed later
// I use here sleep instead of semaphore, because the
// semaphore step is very instable (slowly ?)
" sleep 600;"
+ " echo 'JOB 1 unblocked';"
+ "}",
true));
WorkflowRun b1 = p1.scheduleBuild2(0).waitForStart();
j.waitForMessage("JOB 1 inside", b1);
WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2");
p2.setDefinition(new CpsFlowDefinition(
"try {\n"
+ "lock('resource1') {\n"
+ " echo 'JOB 2 inside'\n"
+
// also here use long sleep time
// the job will be stopped later (but not killed)
" sleep 600;"
+ "}\n"
+ "}\n"
+ "catch(error) {\n"
+ " echo 'JOB 2 unblocked';\n"
+ " echo 'error:' + error;\n"
+ "}",
true));
WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart();
// Make sure that b2 is blocked on b1's lock.
j.waitForMessage("[resource1] is locked by build " + b1.getFullDisplayName(), b2);
isPaused(b2, 1, 1);
// Now b2 is still sitting waiting for a lock. Create b3 and launch it to clear the
// lock.
WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3");
p3.setDefinition(new CpsFlowDefinition(
"lock('resource1') {\n" + " echo 'JOB 3 inside'\n" + "}\n" + "echo 'JOB 3 unblocked'", true));
WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart();
// Make sure that b3 is also blocked still on b1's lock.
j.waitForMessage("[resource1] is locked by build " + b1.getFullDisplayName(), b3);
isPaused(b3, 1, 1);
// Kill b1 hard.
b1.doKill();
j.waitForMessage("Hard kill!", b1);
j.waitForCompletion(b1);
j.assertBuildStatus(Result.ABORTED, b1);
// Verify that b2 gets the lock.
j.waitForMessage("Trying to acquire lock on [Resource: resource1]", b2);
j.waitForMessage("JOB 2 inside", b2);
j.assertLogNotContains("JOB 3 inside", b3);
// terminate current step
b2.doTerm();
// Verify that b2 releases the lock and finishes successfully.
j.waitForMessage("Lock released on resource [Resource: resource1]", b2);
j.waitForMessage("JOB 2 unblocked", b2);
j.assertBuildStatusSuccess(j.waitForCompletion(b2));
isPaused(b2, 1, 0);
// Now b3 should get the lock and do its thing.
j.waitForMessage("Trying to acquire lock on [Resource: resource1]", b3);
j.waitForMessage("Lock released on resource [Resource: resource1]", b3);
j.waitForMessage("JOB 3 unblocked", b3);
j.assertBuildStatusSuccess(j.waitForCompletion(b3));
isPaused(b3, 1, 0);
}
@Issue("JENKINS-40368")
@Test
public void hardKillWithWaitingRuns() throws Exception {
LockableResourcesManager.get().createResource("resource1");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("lock('resource1') { semaphore('wait-inside') }", true));
WorkflowRun prevBuild = null;
for (int i = 0; i < 3; i++) {
WorkflowRun rNext = p.scheduleBuild2(0).waitForStart();
if (prevBuild != null) {
j.waitForMessage("[resource1] is locked by build " + prevBuild.getFullDisplayName(), rNext);
isPaused(rNext, 1, 1);
interruptTermKill(prevBuild);
}
j.waitForMessage("Trying to acquire lock on [Resource: resource1]", rNext);
SemaphoreStep.waitForStart("wait-inside/" + (i + 1), rNext);
isPaused(rNext, 1, 0);
prevBuild = rNext;
}
SemaphoreStep.success("wait-inside/3", null);
j.assertBuildStatus(Result.SUCCESS, j.waitForCompletion(prevBuild));
}
@Issue("JENKINS-40368")
@Test
public void hardKillWithWaitingRunsOnLabel() throws Exception {
LockableResourcesManager.get().createResourceWithLabel("resource1", "label1");
LockableResourcesManager.get().createResourceWithLabel("resource2", "label1");
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("lock(label: 'label1', quantity: 1) { semaphore('wait-inside')}", true));
WorkflowRun firstPrev = null;
WorkflowRun secondPrev = null;
for (int i = 0; i < 3; i++) {
WorkflowRun firstNext = p.scheduleBuild2(0).waitForStart();
j.waitForMessage("Trying to acquire lock on", firstNext);
WorkflowRun secondNext = p.scheduleBuild2(0).waitForStart();
j.waitForMessage("Trying to acquire lock on", secondNext);
if (firstPrev != null) {
j.waitForMessage(", waiting for execution ...", firstNext);
isPaused(firstNext, 1, 1);
j.waitForMessage(", waiting for execution ...", secondNext);
isPaused(secondNext, 1, 1);
}
interruptTermKill(firstPrev);
j.waitForMessage("Lock acquired on ", firstNext);
interruptTermKill(secondPrev);
j.waitForMessage("Lock acquired on ", secondNext);
SemaphoreStep.waitForStart("wait-inside/" + ((i * 2) + 1), firstNext);
SemaphoreStep.waitForStart("wait-inside/" + ((i * 2) + 2), secondNext);
isPaused(firstNext, 1, 0);
isPaused(secondNext, 1, 0);
firstPrev = firstNext;
secondPrev = secondNext;
}
SemaphoreStep.success("wait-inside/5", null);
SemaphoreStep.success("wait-inside/6", null);
j.assertBuildStatus(Result.SUCCESS, j.waitForCompletion(firstPrev));
j.assertBuildStatus(Result.SUCCESS, j.waitForCompletion(secondPrev));
}
private void interruptTermKill(WorkflowRun b) throws Exception {
if (b != null) {
Executor ex = b.getExecutor();
assertNotNull(ex);
ex.interrupt();
j.waitForCompletion(b);
j.assertBuildStatus(Result.ABORTED, b);
}
}
}