forked from jwmach1/parameterized-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathParameterizedSchedulerTest.java
More file actions
143 lines (129 loc) · 5.54 KB
/
ParameterizedSchedulerTest.java
File metadata and controls
143 lines (129 loc) · 5.54 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
package org.jenkinsci.plugins.parameterizedscheduler;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.StringParameterDefinition;
import hudson.triggers.Trigger;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.stapler.StaplerRequest;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
public class ParameterizedSchedulerTest {
@Rule
public JenkinsRule r = new JenkinsRule();
@Test
public void freestyle() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("foo", "lol")));
assertThat(p.getLastCompletedBuild(), is(nullValue()));
Trigger<Job> t = new ParameterizedTimerTrigger("* * * * *%foo=bar");
t.start(p, true);
p.addTrigger(t);
new Cron().doRun();
assertThat(p.isInQueue(), is(true));
r.waitUntilNoActivity();
assertThat(p.getLastCompletedBuild(), is(notNullValue()));
assertThat((String) p.getLastCompletedBuild().getAction(ParametersAction.class).getParameter("foo").getValue(), is("bar"));
}
@Test
public void pipeline() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition("", true));
WorkflowRun wfr = p.scheduleBuild2(0).get();
p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("foo", "lol")));
Trigger<Job> t = new ParameterizedTimerTrigger("* * * * *%foo=bar");
t.start(p, true);
p.addTrigger(t);
new Cron().doRun();
r.waitUntilNoActivity();
assertThat(p.getLastCompletedBuild(), is(not(wfr)));
assertThat((String) p.getLastCompletedBuild().getAction(ParametersAction.class).getParameter("foo").getValue(), is("bar"));
}
@Test
public void scripted() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition("properties([\n" +
" parameters([\n" +
" string(name: 'foo', defaultValue: 'lol')\n" +
" ]),\n" +
" pipelineTriggers([\n" +
" parameterizedCron('* * * * *%foo=bar')\n" +
" ])\n" +
"])", true));
WorkflowRun wfr = r.buildAndAssertSuccess(p);
new Cron().doRun();
r.waitUntilNoActivity();
assertThat(p.getLastCompletedBuild(), is(not(wfr)));
assertThat((String) p.getLastCompletedBuild().getAction(ParametersAction.class).getParameter("foo").getValue(), is("bar"));
}
@Test
public void declarative() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class);
p.setDefinition(new CpsFlowDefinition("pipeline {\n" +
" agent any\n" +
" parameters {\n" +
" string(name: 'foo', defaultValue: 'lol')\n" +
" }\n" +
" triggers {\n" +
" parameterizedCron('* * * * *%foo=bar')\n" +
" }\n" +
" stages {\n" +
" stage('Test') {\n" +
" steps {\n" +
" echo 'test'\n" +
" }\n" +
" }\n" +
" }\n" +
"}", true));
WorkflowRun wfr = r.buildAndAssertSuccess(p);
new Cron().doRun();
r.waitUntilNoActivity();
assertThat(p.getLastCompletedBuild(), is(not(wfr)));
assertThat((String) p.getLastCompletedBuild().getAction(ParametersAction.class).getParameter("foo").getValue(), is("bar"));
}
@Test
@Issue("JENKINS-49372")
public void nullValueCreated() throws Exception {
FreeStyleProject p = r.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new NullParameterDefinition("foo")));
assertThat(p.getLastCompletedBuild(), is(nullValue()));
Trigger<Job> t = new ParameterizedTimerTrigger("* * * * *%foo=test");
t.start(p, true);
p.addTrigger(t);
new Cron().doRun();
assertThat(p.isInQueue(), is(true));
r.waitUntilNoActivity();
// Build should complete successfully but will not have any value
assertThat(p.getLastCompletedBuild(), is(notNullValue()));
}
private static class NullParameterDefinition extends ParameterDefinition {
public NullParameterDefinition(@Nonnull String name) {
super(name, null);
}
@CheckForNull
@Override
public ParameterValue createValue(StaplerRequest staplerRequest, JSONObject jsonObject) {
return null;
}
@CheckForNull
@Override
public ParameterValue createValue(StaplerRequest staplerRequest) {
return null;
}
}
}