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

Commit 42524be

Browse files
author
Nikhil Bhoski
committed
Changed the file coopy mechanism as per review commnets and added tests to cover it
1 parent 22140e6 commit 42524be

File tree

6 files changed

+150
-18
lines changed

6 files changed

+150
-18
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@
5353
<tag>HEAD</tag>
5454
</scm>
5555

56+
<dependencies>
57+
<!-- https://mvnrepository.com/artifact/org.jenkins-ci.plugins/matrix-project -->
58+
<dependency>
59+
<groupId>org.jenkins-ci.plugins</groupId>
60+
<artifactId>matrix-project</artifactId>
61+
<version>1.14</version>
62+
</dependency>
63+
</dependencies>
64+
5665
<build>
5766
<plugins>
5867
<!-- Plugin to download the matlab run scripts and keep it under class

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import java.io.IOException;
1111
import java.io.InputStream;
12-
import java.time.Instant;
12+
import java.util.UUID;
1313

1414
import hudson.EnvVars;
1515
import hudson.FilePath;
@@ -36,13 +36,13 @@ default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher la
3636
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
3737
ProcStarter matlabLauncher;
3838
if(launcher.isUnix()) {
39-
final String runnerScriptName = "run_matlab_command"+uniqueName+".sh";
39+
final String runnerScriptName = uniqueName+"/run_matlab_command.sh";
4040
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/"+runnerScriptName,matlabCommand).stdout(listener);
4141

4242
//Copy runner .sh for linux platform in workspace.
4343
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName, targetWorkspace);
4444
}else {
45-
final String runnerScriptName = "run_matlab_command"+uniqueName+".bat";
45+
final String runnerScriptName = uniqueName+"\\run_matlab_command.bat";
4646
launcher = launcher.decorateByPrefix("cmd.exe","/C");
4747
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+runnerScriptName,"\""+matlabCommand+"\"").stdout(listener);
4848
//Copy runner.bat for Windows platform in workspace.
@@ -68,9 +68,9 @@ default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher,String uniq
6868
Computer cmp = Computer.currentComputer();
6969
String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir");
7070
if(launcher.isUnix()) {
71-
tmpDir = tmpDir+"/run_matlab_command"+uniqueName+".sh";
71+
tmpDir = tmpDir+"/"+uniqueName;
7272
}else {
73-
tmpDir = tmpDir+"\\"+"run_matlab_command"+uniqueName+".bat";
73+
tmpDir = tmpDir+"\\"+uniqueName;
7474
}
7575
return new FilePath(launcher.getChannel(), tmpDir);
7676
}
@@ -82,7 +82,7 @@ default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedExc
8282
}
8383

8484
default String getUniqueNameForRunnerFile() {
85-
return Instant.now().toString().replaceAll("[:,.]", "");
85+
return UUID.randomUUID().toString();
8686
}
8787

8888
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import java.io.IOException;
10-
import java.time.Instant;
1110

1211
import javax.annotation.Nonnull;
1312
import org.jenkinsci.Symbol;
@@ -113,19 +112,19 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
113112

114113
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
115114
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
116-
final String uniqueFileName = getUniqueNameForRunnerFile();
115+
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
117116
ProcStarter matlabLauncher;
118117
try {
119-
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand(),uniqueFileName);
118+
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand(),uniqueTmpFldrName);
120119
return matlabLauncher.join();
121120
} catch (Exception e) {
122121
listener.getLogger().println(e.getMessage());
123122
return 1;
124123
}finally {
125124
// Cleanup the runner File from tmp directory
126-
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueFileName);
127-
if(matlabRunnerScript.exists()) {
128-
matlabRunnerScript.delete();
125+
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueTmpFldrName);
126+
if(matlabRunnerScript.isDirectory()) {
127+
matlabRunnerScript.deleteRecursive();
129128
}
130129
}
131130

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.io.File;
1212
import java.io.IOException;
13-
import java.time.Instant;
1413
import java.util.ArrayList;
1514
import java.util.List;
1615
import java.util.function.Function;
@@ -274,11 +273,11 @@ public void perform(@Nonnull Run<?, ?> build, @Nonnull FilePath workspace,
274273

275274
private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher,
276275
TaskListener listener, EnvVars envVars) throws IOException, InterruptedException {
277-
final String uniqueFileName = getUniqueNameForRunnerFile();
276+
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
278277
ProcStarter matlabLauncher;
279278
try {
280279
matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,
281-
constructCommandForTest(getInputArguments()),uniqueFileName);
280+
constructCommandForTest(getInputArguments()),uniqueTmpFldrName);
282281

283282
// Copy MATLAB scratch file into the workspace.
284283
FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote());
@@ -290,9 +289,9 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
290289
return 1;
291290
}finally {
292291
// Cleanup the runner File from tmp directory
293-
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueFileName);
294-
if (matlabRunnerScript.exists()) {
295-
matlabRunnerScript.delete();
292+
FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueTmpFldrName);
293+
if (matlabRunnerScript.isDirectory()) {
294+
matlabRunnerScript.deleteRecursive();
296295
}
297296
}
298297

src/test/java/com/mathworks/ci/RunMatlabCommandBuilderTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import java.io.IOException;
1212
import java.net.URISyntaxException;
1313
import java.net.URL;
14+
import java.util.HashMap;
1415
import java.util.List;
16+
import java.util.Map;
1517
import java.util.Optional;
1618
import org.junit.After;
1719
import org.junit.Assert;
@@ -21,6 +23,12 @@
2123
import org.junit.Test;
2224
import org.jvnet.hudson.test.JenkinsRule;
2325
import hudson.EnvVars;
26+
import hudson.matrix.Axis;
27+
import hudson.matrix.AxisList;
28+
import hudson.matrix.Combination;
29+
import hudson.matrix.MatrixBuild;
30+
import hudson.matrix.MatrixProject;
31+
import hudson.matrix.MatrixRun;
2432
import hudson.model.FreeStyleBuild;
2533
import hudson.model.FreeStyleProject;
2634
import hudson.model.Result;
@@ -233,4 +241,54 @@ public void verifyMATLABrunnerFileGenerated() throws Exception {
233241
FreeStyleBuild build = project.scheduleBuild2(0).get();
234242
jenkins.assertLogContains("MATLAB_ROOT", build);
235243
}
244+
245+
/*
246+
* Test to verify if Matrix build fails when MATLAB is not available.
247+
*/
248+
@Test
249+
public void verifyMatrixBuildFails() throws Exception {
250+
MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);
251+
Axis axes = new Axis("VERSION", "R2018a", "R2018b");
252+
matrixProject.setAxes(new AxisList(axes));
253+
String matlabRoot = getMatlabroot("R2018b");
254+
this.buildWrapper.setMatlabRootFolder(matlabRoot.replace("R2018b", "$VERSION"));
255+
matrixProject.getBuildWrappersList().add(this.buildWrapper);
256+
257+
scriptBuilder.setMatlabCommand("pwd");
258+
matrixProject.getBuildersList().add(scriptBuilder);
259+
Map<String, String> vals = new HashMap<String, String>();
260+
vals.put("VERSION", "R2018a");
261+
Combination c1 = new Combination(vals);
262+
MatrixRun build = matrixProject.scheduleBuild2(0).get().getRun(c1);
263+
jenkins.assertLogContains("MATLAB_ROOT", build);
264+
jenkins.assertBuildStatus(Result.FAILURE, build);
265+
vals.put("VERSION", "R2018b");
266+
Combination c2 = new Combination(vals);
267+
MatrixRun build2 = matrixProject.scheduleBuild2(0).get().getRun(c2);
268+
jenkins.assertLogContains("MATLAB_ROOT", build2);
269+
jenkins.assertBuildStatus(Result.FAILURE, build2);
270+
}
271+
272+
/*
273+
* Test to verify if Matrix build passes (mock MATLAB).
274+
*/
275+
@Test
276+
public void verifyMatrixBuildPasses() throws Exception {
277+
MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);
278+
Axis axes = new Axis("VERSION", "R2018a", "R2018b");
279+
matrixProject.setAxes(new AxisList(axes));
280+
String matlabRoot = getMatlabroot("R2018b");
281+
this.buildWrapper.setMatlabRootFolder(matlabRoot.replace("R2018b", "$VERSION"));
282+
matrixProject.getBuildWrappersList().add(this.buildWrapper);
283+
RunMatlabCommandBuilderTester tester = new RunMatlabCommandBuilderTester(matlabExecutorAbsolutePath,
284+
"-positive");
285+
286+
tester.setMatlabCommand("pwd");
287+
matrixProject.getBuildersList().add(tester);
288+
MatrixBuild build = matrixProject.scheduleBuild2(0).get();
289+
290+
jenkins.assertLogContains("R2018a completed", build);
291+
jenkins.assertLogContains("R2018b completed", build);
292+
jenkins.assertBuildStatus(Result.SUCCESS, build);
293+
}
236294
}

src/test/java/com/mathworks/ci/RunMatlabTestsBuilderTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import java.io.IOException;
1212
import java.net.URISyntaxException;
1313
import java.net.URL;
14+
import java.util.HashMap;
1415
import java.util.List;
16+
import java.util.Map;
1517
import java.util.Optional;
18+
1619
import org.junit.After;
1720
import org.junit.Assert;
1821
import org.junit.Before;
@@ -24,6 +27,12 @@
2427
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
2528
import com.gargoylesoftware.htmlunit.html.HtmlPage;
2629
import hudson.FilePath;
30+
import hudson.matrix.Axis;
31+
import hudson.matrix.AxisList;
32+
import hudson.matrix.Combination;
33+
import hudson.matrix.MatrixBuild;
34+
import hudson.matrix.MatrixProject;
35+
import hudson.matrix.MatrixRun;
2736
import hudson.model.FreeStyleBuild;
2837
import hudson.model.FreeStyleProject;
2938
import hudson.model.Result;
@@ -241,6 +250,64 @@ public void verifyMATLABrunnerFileGeneratedForAutomaticOption() throws Exception
241250
FreeStyleBuild build = project.scheduleBuild2(0).get();
242251
jenkins.assertLogContains("MATLAB_ROOT", build);
243252
}
253+
254+
/*
255+
* Test to verify if Matrix build fails when MATLAB is not available.
256+
*/
257+
@Test
258+
public void verifyMatrixBuildFails() throws Exception {
259+
MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);
260+
Axis axes = new Axis("VERSION", "R2018a", "R2018b");
261+
matrixProject.setAxes(new AxisList(axes));
262+
String matlabRoot = getMatlabroot("R2018b");
263+
this.buildWrapper.setMatlabRootFolder(matlabRoot.replace("R2018b", "$VERSION"));
264+
matrixProject.getBuildWrappersList().add(this.buildWrapper);
265+
266+
setAllTestArtifacts(false, testBuilder);
267+
matrixProject.getBuildersList().add(testBuilder);
268+
269+
// Check for first matrix combination.
270+
271+
Map<String, String> vals = new HashMap<String, String>();
272+
vals.put("VERSION", "R2018a");
273+
Combination c1 = new Combination(vals);
274+
MatrixRun build1 = matrixProject.scheduleBuild2(0).get().getRun(c1);
275+
276+
jenkins.assertLogContains("MATLAB_ROOT", build1);
277+
jenkins.assertBuildStatus(Result.FAILURE, build1);
278+
279+
// Check for second Matrix combination
280+
281+
Combination c2 = new Combination(vals);
282+
MatrixRun build2 = matrixProject.scheduleBuild2(0).get().getRun(c2);
283+
284+
jenkins.assertLogContains("MATLAB_ROOT", build2);
285+
jenkins.assertBuildStatus(Result.FAILURE, build2);
286+
}
287+
288+
/*
289+
* Test to verify if Matrix build passes (mock MATLAB).
290+
*/
291+
@Test
292+
public void verifyMatrixBuildPasses() throws Exception {
293+
MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);
294+
Axis axes = new Axis("VERSION", "R2018a", "R2018b");
295+
matrixProject.setAxes(new AxisList(axes));
296+
String matlabRoot = getMatlabroot("R2018b");
297+
this.buildWrapper.setMatlabRootFolder(matlabRoot.replace("R2018b", "$VERSION"));
298+
matrixProject.getBuildWrappersList().add(this.buildWrapper);
299+
RunMatlabTestsBuilderTester tester = new RunMatlabTestsBuilderTester(matlabExecutorAbsolutePath, "-positive");
300+
301+
setAllTestArtifacts(false, tester);
302+
matrixProject.getBuildersList().add(tester);
303+
MatrixBuild build = matrixProject.scheduleBuild2(0).get();
304+
305+
jenkins.assertLogContains("Triggering", build);
306+
jenkins.assertLogContains("R2018a completed", build);
307+
jenkins.assertLogContains("R2018b completed", build);
308+
jenkins.assertBuildStatus(Result.SUCCESS, build);
309+
}
310+
244311

245312
/*
246313
* Test To verify UI displays Cobertura Warning message when unsupported MATLAB version used.

0 commit comments

Comments
 (0)