-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserFlowTest.java
93 lines (74 loc) · 2.5 KB
/
UserFlowTest.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
package mypackage;
import jenkins.model.Jenkins;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.global.UserDefinedGlobalVariableList;
import org.jenkinsci.plugins.workflow.cps.global.WorkflowLibRepository;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
public abstract class UserFlowTest extends Assert {
@Rule
public RestartableJenkinsRule story = new RestartableJenkinsRule();
@Inject
Jenkins jenkins;
@Inject
WorkflowLibRepository repo;
@Inject
UserDefinedGlobalVariableList uvl;
/**
* User can define global variables.
*/
@Test
public void test() {
story.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
copySources();
// simulate the effect of push
uvl.rebuild();
System.out.println("* Testing " + flowName());
//run implementation
perform();
}
}
);
}
protected WorkflowJob createWorkflowProject(boolean inSandbox) throws IOException {
WorkflowJob p = jenkins.createProject(WorkflowJob.class, flowName() + "-" + RandomUtils.nextInt(0,9^3));
p.setDefinition(new CpsFlowDefinition(defineFlow(), inSandbox));
return p;
}
/*
Subclasses must define the flow (content in the Jenkinsfile) which will be executed
*/
public abstract String defineFlow();
/*
Subclasses must implement the test
*/
public abstract void perform() throws Exception;
/*
Defaults to implementor's name with the suffix Test removed
E.g MyVarTest would yield MyVar
*/
protected String flowName() {
String className = this.getClass().getSimpleName().replaceAll("Test$", "");
return className.substring(0, 1).toLowerCase() + className.substring(1);
}
protected void copySources() {
try {
FileUtils.copyDirectory(new File("vars"), new File(repo.workspace, "vars"));
FileUtils.copyDirectory(new File("src"), new File(repo.workspace, "src"));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}