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

Commit 95fabb5

Browse files
committed
Add system tests for Run MATLAB Command step
1 parent 122d4da commit 95fabb5

File tree

5 files changed

+296
-0
lines changed

5 files changed

+296
-0
lines changed

pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@
125125
<version>11.0.24</version>
126126
<scope>test</scope>
127127
</dependency>
128+
129+
<!-- Dependencies for system tests -->
130+
<!-- https://mvnrepository.com/artifact/com.jcabi/jcabi-xml -->
131+
<dependency>
132+
<groupId>com.jcabi</groupId>
133+
<artifactId>jcabi-xml</artifactId>
134+
<version>0.23.1</version>
135+
</dependency>
136+
<!-- https://mvnrepository.com/artifact/org.jenkinsci.plugins/pipeline-model-definition -->
137+
<dependency>
138+
<groupId>org.jenkinsci.plugins</groupId>
139+
<artifactId>pipeline-model-definition</artifactId>
140+
<version>2.2218.v56d0cda_37c72</version>
141+
</dependency>
142+
<!-- Need unzip to add the files in pipeline workspace -->
143+
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/pipeline-utility-steps -->
144+
<dependency>
145+
<groupId>org.jenkins-ci.plugins</groupId>
146+
<artifactId>pipeline-utility-steps</artifactId>
147+
<version>2.7.1</version>
148+
<scope>test</scope>
149+
</dependency>
150+
<!-- TODO: see if this is necessary Using for verifyCodeCoverage -->
151+
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/cobertura -->
152+
<dependency>
153+
<groupId>org.jenkins-ci.plugins</groupId>
154+
<artifactId>cobertura</artifactId>
155+
<version>1.16</version>
156+
</dependency>
157+
128158
</dependencies>
129159
<build>
130160
<plugins>
@@ -215,6 +245,42 @@
215245
</execution>
216246
</executions>
217247
</plugin>
248+
<plugin>
249+
<groupId>org.apache.maven.plugins</groupId>
250+
<artifactId>maven-failsafe-plugin</artifactId>
251+
<version>2.19.1</version>
252+
<executions>
253+
<execution>
254+
<id>integration-tests</id>
255+
<goals>
256+
<goal>integration-test</goal>
257+
</goals>
258+
<configuration>
259+
<includes>
260+
<include>**/*IT.java</include>
261+
</includes>
262+
<excludes>
263+
<exclude>**/*Test.java</exclude>
264+
</excludes>
265+
</configuration>
266+
</execution>
267+
</executions>
268+
</plugin>
269+
<plugin>
270+
<groupId>org.apache.maven.plugins</groupId>
271+
<artifactId>maven-surefire-plugin</artifactId>
272+
<version>2.19.1</version>
273+
<configuration>
274+
<includes>
275+
<include>**/*Test.java</include>
276+
<include>**/*Tester.java</include>
277+
</includes>
278+
<excludes>
279+
<exclude>**/*IT.java</exclude>>
280+
</excludes>
281+
</configuration>
282+
</plugin>
283+
218284
</plugins>
219285
<pluginManagement>
220286
<plugins>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.mathworks.ci.systemTests;
2+
3+
import com.mathworks.ci.MatlabInstallation;
4+
import org.jvnet.hudson.test.JenkinsRule;
5+
6+
import java.io.File;
7+
import java.net.MalformedURLException;
8+
import java.net.URL;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
12+
import static org.jvnet.hudson.test.JenkinsRule.NO_PROPERTIES;
13+
14+
public class MatlabRootSetup {
15+
static String installedPath = "", binPath = "", MATLAB_ROOT="";
16+
public static MatlabInstallation.DescriptorImpl matlabInstDescriptor;
17+
18+
public MatlabRootSetup(){
19+
getBinPath();
20+
}
21+
22+
/*
23+
* This method returns the environment path needed to be set for DSL pipeline scripts
24+
*/
25+
public static String getEnvironmentDSL() {
26+
getBinPath();
27+
String environment = "environment { \n" +
28+
"PATH = " + "\""+ binPath + "${PATH}"+ "\"" + "\n" +
29+
"}";
30+
return environment;
31+
}
32+
33+
/*
34+
* This method returns the environment path needed to be set for Scripted pipeline
35+
*/
36+
public static String getEnvironmentScriptedPipeline() {
37+
getBinPath();
38+
String environment = "";
39+
environment = "env.PATH =" + '"' + binPath + "${env.PATH}" + '"';
40+
return environment;
41+
}
42+
43+
/*
44+
* This method returns the MATLAB Root needed for Free Style or Multi Config projects
45+
*/
46+
public static String getMatlabRoot() {
47+
getBinPath();
48+
MATLAB_ROOT = installedPath;
49+
50+
System.out.println(MATLAB_ROOT);
51+
return MATLAB_ROOT;
52+
}
53+
54+
/*
55+
* This method returns the bin path needed for scripted pipelines based on the testing platform -- Windows or Linux
56+
* or Mac
57+
*/
58+
public static void getBinPath() {
59+
installedPath = System.getenv("MATLAB_ROOT");
60+
if (installedPath == null || installedPath.isEmpty()) {
61+
installedPath = System.getProperty("MATLAB_ROOT");
62+
if (installedPath == null || installedPath.isEmpty()) {
63+
throw new IllegalStateException("MATLAB_ROOT is not set as an environment variable or system property.");
64+
}
65+
}
66+
67+
if (System.getProperty("os.name").startsWith("Win")) {
68+
binPath = installedPath.replace("\\", "\\\\")+ "\\\\bin;";
69+
} else {
70+
binPath = installedPath + "/bin:";
71+
}
72+
73+
// You may want to store or return binPath as needed
74+
// System.out.println("Binary Path: " + binPath);
75+
}
76+
77+
public static MatlabInstallation setMatlabInstallation(String name, String home, JenkinsRule jenkins) {
78+
if(matlabInstDescriptor == null){
79+
MatlabRootSetup.matlabInstDescriptor = jenkins.getInstance().getDescriptorByType(MatlabInstallation.DescriptorImpl.class);
80+
}
81+
MatlabInstallation[] prevInst = getMatlabInstallation();
82+
ArrayList<MatlabInstallation> newInst = new ArrayList<>(Arrays.asList(prevInst));
83+
MatlabInstallation newMatlabInstallation = new MatlabInstallation(name, home, NO_PROPERTIES);
84+
newInst.add(newMatlabInstallation);
85+
MatlabInstallation[] setInst = new MatlabInstallation[newInst.size()];
86+
matlabInstDescriptor.setInstallations(newInst.toArray(setInst));
87+
return newMatlabInstallation;
88+
}
89+
90+
public static MatlabInstallation[] getMatlabInstallation(){
91+
// static method to return all installations
92+
return MatlabInstallation.getAll();
93+
}
94+
95+
public static URL getRunMATLABTestsData() throws MalformedURLException {
96+
File file = new File(System.getProperty("user.dir") + File.separator +"src" + File.separator + "test" + File.separator + "resources" + File.separator + "TestData" + File.separator + "FilterTestData.zip");
97+
URL zipfile = file.toURI().toURL();
98+
return zipfile;
99+
}
100+
101+
public static URL getTestOnWarningData() throws MalformedURLException {
102+
File file = new File(System.getProperty("user.dir") + File.separator +"src" + File.separator + "test" + File.separator + "resources" + File.separator + "TestData" + File.separator + "TestWithWarning.zip");
103+
URL zipfile = file.toURI().toURL();
104+
return zipfile;
105+
}
106+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.mathworks.ci.systemTests;
2+
3+
import com.mathworks.ci.*;
4+
import com.mathworks.ci.freestyle.RunMatlabCommandBuilder;
5+
import hudson.model.FreeStyleBuild;
6+
import hudson.model.FreeStyleProject;
7+
import hudson.model.Result;
8+
import org.junit.*;
9+
import org.junit.rules.Timeout;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
import java.io.IOException;
12+
13+
14+
public class RunMATLABCommandIT {
15+
16+
private FreeStyleProject project;
17+
private UseMatlabVersionBuildWrapper buildWrapper;
18+
private RunMatlabCommandBuilder scriptBuilder;
19+
20+
@Rule
21+
public Timeout timeout = Timeout.seconds(0);
22+
23+
@Rule
24+
public JenkinsRule jenkins = new JenkinsRule();
25+
26+
@BeforeClass
27+
public static void checkMatlabRoot() {
28+
// Check if the MATLAB_ROOT environment variable is defined
29+
String matlabRoot = System.getenv("MATLAB_ROOT");
30+
Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty());
31+
}
32+
33+
@Before
34+
public void testSetup() throws IOException {
35+
this.project = jenkins.createFreeStyleProject();
36+
this.scriptBuilder = new RunMatlabCommandBuilder();
37+
this.buildWrapper = new UseMatlabVersionBuildWrapper();
38+
}
39+
40+
@After
41+
public void testTearDown() {
42+
this.project = null;
43+
this.scriptBuilder = null;
44+
}
45+
46+
/*
47+
* Test to verify if Build FAILS when matlab command fails
48+
*/
49+
50+
@Test
51+
public void verifyBuildFailureWhenMatlabCommandFails() throws Exception {
52+
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot()));
53+
project.getBuildWrappersList().add(this.buildWrapper);
54+
RunMatlabCommandBuilder tester =
55+
new RunMatlabCommandBuilder();
56+
tester.setMatlabCommand("apple");
57+
project.getBuildersList().add(tester);
58+
FreeStyleBuild build = project.scheduleBuild2(0).get();
59+
jenkins.assertLogContains("apple", build);
60+
jenkins.assertBuildStatus(Result.FAILURE, build);
61+
}
62+
63+
64+
/* Test To Verify if Build passes when matlab command passes
65+
*/
66+
@Test
67+
public void verifyBuildPassesWhenMatlabCommandPasses() throws Exception {
68+
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot()));
69+
project.getBuildWrappersList().add(this.buildWrapper);
70+
RunMatlabCommandBuilder tester =
71+
new RunMatlabCommandBuilder();
72+
tester.setMatlabCommand("disp 'apple'");
73+
project.getBuildersList().add(tester);
74+
FreeStyleBuild build = project.scheduleBuild2(0).get();
75+
jenkins.assertBuildStatus(Result.SUCCESS, build);
76+
jenkins.assertLogContains("apple", build);
77+
}
78+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mathworks.ci.systemTests;
2+
3+
import java.io.InputStream;
4+
import java.util.Properties;
5+
6+
public class TestData {
7+
8+
static String value="";
9+
static InputStream inputStream;
10+
11+
public static String getPropValues(String key){
12+
try{
13+
Properties prop=new Properties();
14+
15+
inputStream=TestData.class.getClassLoader().getResourceAsStream("testdataconfig.properties");
16+
if(inputStream!=null){
17+
prop.load(inputStream);
18+
} else {
19+
System.out.println("NOT ABLE TO FIND testdataconfig.properties FILE");
20+
}
21+
value=prop.getProperty(key);
22+
}
23+
catch(Exception e){
24+
System.out.println(e);
25+
}
26+
27+
return value;
28+
}
29+
30+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
matlab.command=pwd
2+
matlab.invalid.command=apple
3+
matlab.invalid.root.path = fake/matlab/root/path
4+
5+
pdftestreport.file.path=matlabTestArtifacts/testreport.pdf
6+
taptestresult.file.path=matlabTestArtifacts/taptestresults.tap
7+
junit.file.path=matlabTestArtifacts/junittestresults.xml
8+
stmresults.file.path=matlabTestArtifacts/simulinktestresults.mldatx
9+
cobertura.file.path=matlabTestArtifacts/cobertura.xml
10+
modelcoverage.file.path=matlabTestArtifacts/coberturamodelcoverage.xml
11+
12+
matlab.windows.installed.path = C:\\Program Files\\MATLAB\\R2023b
13+
matlab.linux.installed.path = /usr/local/MATLAB/R2023b
14+
matlab.mac.installed.path = /mathworks/devel/relarchive/R2021b/executables/GR/maci64/MATLAB_R2021b.app
15+
matlab.version = R2023b
16+
matlab.matrix.version = R2023a

0 commit comments

Comments
 (0)