Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit 5bbcb58

Browse files
author
Nikhil Bhoski
committed
Added unit tests for pipeline
1 parent 52c4c9b commit 5bbcb58

File tree

10 files changed

+411
-14
lines changed

10 files changed

+411
-14
lines changed

pom.xml

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</developers>
2121

2222
<properties>
23-
<jenkins.version>2.7.3</jenkins.version>
23+
<jenkins.version>2.164.3</jenkins.version>
2424
<java.level>8</java.level>
2525
</properties>
2626

@@ -52,26 +52,59 @@
5252
<url>http://github.com/jenkinsci/matlab-plugin</url>
5353
<tag>HEAD</tag>
5454
</scm>
55-
55+
<dependencyManagement>
56+
<dependencies>
57+
<dependency>
58+
<groupId>io.jenkins.tools.bom</groupId>
59+
<artifactId>bom-2.164.x</artifactId>
60+
<version>4</version>
61+
<scope>import</scope>
62+
<type>pom</type>
63+
</dependency>
64+
</dependencies>
65+
</dependencyManagement>
5666
<dependencies>
67+
5768
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/matrix-project -->
5869
<dependency>
5970
<groupId>org.jenkins-ci.plugins</groupId>
6071
<artifactId>matrix-project</artifactId>
61-
<version>1.14</version>
72+
<!-- <version>1.14</version> -->
6273
</dependency>
6374

6475
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-step-api -->
6576
<dependency>
6677
<groupId>org.jenkins-ci.plugins.workflow</groupId>
6778
<artifactId>workflow-step-api</artifactId>
68-
<version>2.22</version>
79+
<!-- <version>2.22</version> -->
6980
</dependency>
7081
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins.workflow/workflow-api -->
7182
<dependency>
7283
<groupId>org.jenkins-ci.plugins.workflow</groupId>
7384
<artifactId>workflow-api</artifactId>
74-
<version>2.23</version>
85+
<!-- <version>2.23</version> -->
86+
</dependency>
87+
<!-- Jenkins workflow test dependencies -->
88+
89+
<dependency>
90+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
91+
<artifactId>workflow-basic-steps</artifactId>
92+
<scope>test</scope>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
96+
<artifactId>workflow-cps</artifactId>
97+
<scope>test</scope>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
101+
<artifactId>workflow-durable-task-step</artifactId>
102+
<scope>test</scope>
103+
</dependency>
104+
<dependency>
105+
<groupId>org.jenkins-ci.plugins.workflow</groupId>
106+
<artifactId>workflow-job</artifactId>
107+
<scope>test</scope>
75108
</dependency>
76109
</dependencies>
77110

src/main/java/com/mathworks/ci/RunMatlabCommandStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class RunMatlabCommandStep extends Step {
2121
private static boolean COPY_SCRATCH_FILE = false;
2222

2323
@DataBoundConstructor
24-
public RunMatlabCommandStep(String command) {
25-
this.matlabCommand = command;
24+
public RunMatlabCommandStep(String matlabCommand) {
25+
this.matlabCommand = matlabCommand;
2626

2727
}
2828

src/main/java/com/mathworks/ci/RunMatlabTestsStep.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public class RunMatlabTestsStep extends Step {
2828
private String codeCoverageCobertura;
2929
private String testResultsSimulinkTest;
3030
private String modelCoverageCobertura;
31-
private static final String PDF_REPORT_PATH = "'PDFReportPath'";
32-
private static final String TAP_RESULTS_PATH = "'TAPResultsPath'";
33-
private static final String JUNIT_RESULTS_PATH = "'JUnitResultsPath'";
34-
private static final String COBERTURA_CODE_COVERAGE_PATH = "'CoberturaCodeCoveragePath'";
35-
private static final String STM_RESULTS_PATH = "'SimulinkTestResultsPath'";
36-
private static final String COBERTURA_MODEL_COVERAGE_PATH = "'CoberturaModelCoveragePath'";
31+
private static final String PDF_REPORT_PATH = "PDFReportPath";
32+
private static final String TAP_RESULTS_PATH = "TAPResultsPath";
33+
private static final String JUNIT_RESULTS_PATH = "JUnitResultsPath";
34+
private static final String COBERTURA_CODE_COVERAGE_PATH = "CoberturaCodeCoveragePath";
35+
private static final String STM_RESULTS_PATH = "SimulinkTestResultsPath";
36+
private static final String COBERTURA_MODEL_COVERAGE_PATH = "CoberturaModelCoveragePath";
3737
private static boolean COPY_SCRATCH_FILE = true;
3838

3939
@DataBoundConstructor
@@ -146,7 +146,7 @@ private String getInputArgs() {
146146

147147
private void addInputArgs(String reportName, String reportPath, List<String> inputArgs) {
148148
if (reportPath != null) {
149-
inputArgs.add(reportName + "," + "'" + reportPath + "'");
149+
inputArgs.add("'" + reportName + "'" + "," + "'" + reportPath + "'");
150150
}
151151
}
152152
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.mathworks.ci;
2+
3+
import java.io.IOException;
4+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
5+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
6+
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
7+
import org.junit.Before;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
import hudson.FilePath;
12+
import hudson.slaves.DumbSlave;
13+
14+
public class RunMatlabCommandStepTest {
15+
16+
17+
private WorkflowJob project;
18+
19+
20+
21+
@Rule
22+
public JenkinsRule j = new JenkinsRule();
23+
24+
@Before
25+
public void testSetup() throws IOException {
26+
this.project = j.createProject(WorkflowJob.class);
27+
}
28+
29+
30+
/*
31+
* Verify when MATLAB is not in PATH variable.
32+
*/
33+
34+
@Test
35+
public void verifyMATLABPathNotSet() throws Exception {
36+
project.setDefinition(
37+
new CpsFlowDefinition("node { writeFile text: 'worksapce', file: 'test.txt'\n"
38+
+ "runMATLABCommand(matlabCommand: 'pwd')}", true));
39+
WorkflowRun build = project.scheduleBuild2(0).get();
40+
j.assertLogContains("MATLAB_ROOT", build);
41+
}
42+
43+
/*
44+
* Verify MATLAB is invoked when valid MATLAB is in PATH.
45+
*
46+
*/
47+
48+
@Test
49+
public void verifyMATLABPathSet() throws Exception {
50+
project.setDefinition(
51+
new CpsFlowDefinition("node { writeFile text: 'worksapce', file: 'test.txt'\n"
52+
+ "testMATLABCommand(matlabCommand: 'pwd')}", true));
53+
WorkflowRun build = project.scheduleBuild2(0).get();
54+
j.assertLogContains("tester_started", build);
55+
}
56+
57+
/*
58+
* Verify Pipeline script runs on Slave with valid MATLAB
59+
*
60+
*/
61+
62+
@Test
63+
public void verifyPipelineOnSlave() throws Exception {
64+
DumbSlave s = j.createOnlineSlave();
65+
project.setDefinition(new CpsFlowDefinition(
66+
"node('!master') { writeFile text: 'worksapce', file: 'test.txt'\n"
67+
+ "testMATLABCommand(matlabCommand: 'pwd')}",
68+
true));
69+
70+
s.getWorkspaceFor(project);
71+
WorkflowRun build = project.scheduleBuild2(0).get();
72+
j.assertLogContains("tester_started", build);
73+
}
74+
75+
/*
76+
* Verify appropriate command is invoked as in pipeline script
77+
*
78+
*/
79+
80+
@Test
81+
public void verifyCommandSamAsScript() throws Exception {
82+
DumbSlave s = j.createOnlineSlave();
83+
project.setDefinition(
84+
new CpsFlowDefinition("node { writeFile text: 'worksapce', file: 'test.txt'\n"
85+
+ "runMATLABCommand(matlabCommand: 'pwd')}", true));
86+
87+
s.getWorkspaceFor(project);
88+
WorkflowRun build = project.scheduleBuild2(0).get();
89+
j.assertLogContains("pwd", build);
90+
}
91+
92+
/*
93+
* Verify script can run Matrix build
94+
*
95+
*/
96+
97+
@Test
98+
public void verifyMatrixBuild() throws Exception {
99+
project.setDefinition(
100+
new CpsFlowDefinition("node { writeFile text: 'worksapce', file: 'test.txt'\n"
101+
+ "matrix {\n" + "agent any\n" + "axes {\n" + "axis {\n" + "name: 'CMD'\n"
102+
+ "values: 'pwd','ver'\n }}\n"
103+
+ "runMATLABCommand(matlabCommand: '${CMD}')}}", true));
104+
105+
WorkflowRun build = project.scheduleBuild2(0).get();
106+
j.assertLogContains("pwd", build);
107+
j.assertLogContains("ver", build);
108+
}
109+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.mathworks.ci;
2+
3+
import java.util.Set;
4+
import org.jenkinsci.plugins.workflow.steps.StepContext;
5+
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
6+
import org.jenkinsci.plugins.workflow.steps.StepExecution;
7+
import org.kohsuke.stapler.DataBoundConstructor;
8+
import com.google.common.collect.ImmutableSet;
9+
import hudson.EnvVars;
10+
import hudson.Extension;
11+
import hudson.FilePath;
12+
import hudson.Launcher;
13+
import hudson.model.Run;
14+
import hudson.model.TaskListener;
15+
16+
public class RunMatlabCommandStepTester extends RunMatlabCommandStep {
17+
@DataBoundConstructor
18+
public RunMatlabCommandStepTester(String matlabCommand) {
19+
super(matlabCommand);
20+
}
21+
22+
@Override
23+
public StepExecution start(StepContext context) throws Exception {
24+
25+
return new TestStepExecution(context,this.getMatlabCommand(), false);
26+
}
27+
28+
@Extension
29+
public static class CommandStepTestDescriptor extends StepDescriptor {
30+
31+
@Override
32+
public Set<? extends Class<?>> getRequiredContext() {
33+
return ImmutableSet.of(TaskListener.class, FilePath.class, Launcher.class,
34+
EnvVars.class, Run.class);
35+
}
36+
37+
@Override
38+
public String getFunctionName() {
39+
return "testMATLABCommand";
40+
}
41+
}
42+
43+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.mathworks.ci;
2+
3+
import java.io.IOException;
4+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
5+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
6+
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
7+
import org.junit.Before;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
import hudson.slaves.DumbSlave;
12+
13+
public class RunMatlabTestsStepTest {
14+
15+
private WorkflowJob project;
16+
17+
@Rule
18+
public JenkinsRule j = new JenkinsRule();
19+
20+
@Before
21+
public void testSetup() throws IOException {
22+
this.project = j.createProject(WorkflowJob.class);
23+
}
24+
25+
26+
/*
27+
* Verify when MATLAB Path is not set
28+
*/
29+
@Test
30+
public void verifyMATLABPathNotSet() throws Exception {
31+
project.setDefinition(new CpsFlowDefinition(
32+
"node {runMATLABTests(testResultsPdf:'myresult/result.pdf')}", true));
33+
WorkflowRun build = project.scheduleBuild2(0).get();
34+
j.assertLogContains("MATLAB_ROOT", build);
35+
}
36+
37+
38+
/*
39+
* VErify when MATLAB PATH is set.
40+
*/
41+
42+
@Test
43+
public void verifyMATLABPathSet() throws Exception {
44+
project.setDefinition(new CpsFlowDefinition(
45+
"node {testMATLABTests(testResultsPdf:'myresult/result.pdf')}", true));
46+
WorkflowRun build = project.scheduleBuild2(0).get();
47+
j.assertLogContains("tester_started", build);
48+
}
49+
50+
/*
51+
* Verify Pipeline runs on slave node
52+
*/
53+
54+
@Test
55+
public void verifyOnslave() throws Exception {
56+
DumbSlave s = j.createOnlineSlave();
57+
project.setDefinition(new CpsFlowDefinition(
58+
"node('!master') {testMATLABTests(testResultsPdf:'myresult/result.pdf')}", true));
59+
s.getWorkspaceFor(project);
60+
WorkflowRun build = project.scheduleBuild2(0).get();
61+
j.assertLogContains("tester_started", build);
62+
}
63+
64+
/*
65+
* Verify artifact path is correct.
66+
*/
67+
68+
@Test
69+
public void verifyArtifactPath() throws Exception {
70+
project.setDefinition(new CpsFlowDefinition(
71+
"node {runMATLABTests(testResultsPdf:'myresult/result.pdf')}", true));
72+
WorkflowRun build = project.scheduleBuild2(0).get();
73+
j.assertLogContains("'PDFReportPath','myresult/result.pdf'", build);
74+
}
75+
76+
/*
77+
* Verify Artifact is not sent as parameter if not selected in script.
78+
*/
79+
80+
@Test
81+
public void verifyArtifactParameters() throws Exception {
82+
project.setDefinition(new CpsFlowDefinition(
83+
"node {runMATLABTests(testResultsPdf:'myresult/result.pdf')}", true));
84+
WorkflowRun build = project.scheduleBuild2(0).get();
85+
j.assertLogContains("'PDFReportPath','myresult/result.pdf'", build);
86+
j.assertLogNotContains("TAPResultsPath", build);
87+
j.assertLogNotContains("JUnitResultsPath", build);
88+
j.assertLogNotContains("CoberturaCodeCoveragePath", build);
89+
j.assertLogNotContains("SimulinkTestResultsPath", build);
90+
j.assertLogNotContains("CoberturaModelCoveragePath", build);
91+
}
92+
}

0 commit comments

Comments
 (0)