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

Commit d713c54

Browse files
authored
Merge pull request #198 from mathworks/docker_support
Support docker
2 parents 5710d7b + 7502850 commit d713c54

File tree

8 files changed

+84
-41
lines changed

8 files changed

+84
-41
lines changed

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

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
import java.io.IOException;
1010
import java.io.InputStream;
11-
import java.util.List;
12-
import java.util.UUID;
11+
import org.apache.commons.lang.RandomStringUtils;
1312
import hudson.EnvVars;
1413
import hudson.FilePath;
1514
import hudson.Launcher;
1615
import hudson.Launcher.ProcStarter;
17-
import hudson.model.Computer;
1816
import hudson.model.TaskListener;
1917

2018
public interface MatlabBuild {
@@ -33,14 +31,14 @@ public interface MatlabBuild {
3331
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
3432
Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
3533
throws IOException, InterruptedException {
36-
// Get node specific tmp directory to copy matlab runner script
37-
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
38-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
34+
// Get node specific temp .matlab directory to copy matlab runner script
35+
FilePath targetWorkspace = new FilePath(launcher.getChannel(),
36+
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);
3937
ProcStarter matlabLauncher;
4038
if (launcher.isUnix()) {
4139
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
4240
matlabLauncher = launcher.launch().envs(envVars);
43-
matlabLauncher.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
41+
matlabLauncher.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerScriptName, matlabCommand).stdout(listener);
4442

4543
// Copy runner .sh for linux platform in workspace.
4644
copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName,
@@ -49,7 +47,7 @@ default ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
4947
final String runnerScriptName = uniqueName + "\\run_matlab_command.bat";
5048
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
5149
matlabLauncher = launcher.launch().envs(envVars);
52-
matlabLauncher.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
50+
matlabLauncher.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
5351
.stdout(listener);
5452
// Copy runner.bat for Windows platform in workspace.
5553
copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName,
@@ -71,31 +69,18 @@ default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath
7169
targetFilePath.chmod(0755);
7270
}
7371

74-
default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName, FilePath workspace)
75-
throws IOException, InterruptedException {
76-
/*Use of Computer is not recommended as jenkins hygeine for pipeline support
77-
* https://javadoc.jenkins-ci.org/jenkins/tasks/SimpleBuildStep.html */
78-
79-
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
80-
81-
return new FilePath(launcher.getChannel(), tmpDir + "/" + uniqueName);
82-
}
72+
default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName,
73+
FilePath workspace) throws IOException, InterruptedException {
8374

84-
default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOException, InterruptedException {
85-
Computer cmp = workspace.toComputer();
86-
if (cmp == null) {
87-
throw new IOException(Message.getValue("build.workspace.computer.not.found"));
88-
}
89-
90-
String tmpDirPath = (String) cmp.getSystemProperties().get("java.io.tmpdir");
75+
String tmpDir =
76+
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME;
9177

92-
// Invoke FilePath.normalize for clean file path on any channel.
93-
FilePath tmpDir = new FilePath(cmp.getChannel(), tmpDirPath);
94-
return tmpDir.getRemote();
78+
return new FilePath(launcher.getChannel(), tmpDir + "/" + uniqueName);
9579
}
9680

9781
default String getUniqueNameForRunnerFile() {
98-
return UUID.randomUUID().toString();
82+
//Using 8 bit long random alphanumeric string
83+
return RandomStringUtils.randomAlphanumeric(8);
9984
}
10085

10186
// This method prepares the temp folder by coping all helper files in it.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public class MatlabBuilderConstants {
3232
static final String MATLAB_SCRIPT_GENERATOR = "matlab-script-generator.zip";
3333

3434
//Test runner file prefix
35-
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "test_runner_";
35+
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "runner_";
36+
37+
//Temporary MATLAB folder name in workspace
38+
static final String TEMP_MATLAB_FOLDER_NAME = ".matlab";
3639

3740
// MATLAB runner script
3841
static final String TEST_RUNNER_SCRIPT = "testScript = genscript(${PARAMS});\n" + "\n"

src/main/resources/config.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ Builder.matlab.modelcoverage.support.warning = To generate a Cobertura model cov
1919
Builder.matlab.exportstmresults.support.warning = To export Simulink Test Manager results, use MATLAB R2019a or a newer release.
2020
Builder.matlab.runner.script.target.file.linux.name = run_matlab_command.sh
2121
Builder.matlab.runner.script.target.file.windows.name = run_matlab_command.bat
22-
build.workspace.computer.not.found = Unable to access the computer for this build.
2322
matlab.command.build.step.name = runMATLABCommand
2423
matlab.tests.build.step.name = runMATLABTests
2524
matlab.command.step.display.name = Run MATLAB commands, scripts, or functions

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,18 @@ public void verifyMultispecialChar() throws Exception {
355355
jenkins.assertLogContains("Generating MATLAB script with content", build);
356356
jenkins.assertLogContains(expectedCommand, build);
357357
}
358+
359+
/*
360+
* Test to verify if .matlab temp folder generated in workspace.
361+
*/
362+
@Test
363+
public void verifyMATLABtmpFolderGenerated() throws Exception {
364+
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b")));
365+
project.getBuildWrappersList().add(this.buildWrapper);
366+
scriptBuilder.setMatlabCommand("pwd");
367+
project.getBuildersList().add(this.scriptBuilder);
368+
FreeStyleBuild build = project.scheduleBuild2(0).get();
369+
File matlabRunner = new File(build.getWorkspace() + File.separator + ".matlab");
370+
Assert.assertTrue(matlabRunner.exists());
371+
}
358372
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,19 @@ public void verifyExceptionStackTraceForNonZeroExitCode() throws Exception {
151151
j.assertLogContains("com.mathworks.ci.MatlabExecutionException", build);
152152
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
153153
}
154+
155+
/*
156+
* Verify .matlab folder is generated
157+
*
158+
*/
159+
160+
@Test
161+
public void verifyMATLABtempFolderGenerated() throws Exception {
162+
project.setDefinition(
163+
new CpsFlowDefinition("node { testMATLABCommand(command: 'pwd')}", true));
164+
165+
WorkflowRun build = project.scheduleBuild2(0).get();
166+
j.assertLogContains(".matlab", build);
167+
j.assertBuildStatusSuccess(build);
168+
}
154169
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsBatch() throws Exception {
141141
project.getBuildersList().add(this.testBuilder);
142142
FreeStyleBuild build = project.scheduleBuild2(0).get();
143143
jenkins.assertLogContains("run_matlab_command", build);
144-
jenkins.assertLogContains("test_runner", build);
144+
jenkins.assertLogContains("runner", build);
145145
jenkins.assertLogContains("addpath(", build);
146146
}
147147

@@ -157,7 +157,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsRWindows() throws Exception
157157
project.getBuildersList().add(testBuilder);
158158
FreeStyleBuild build = project.scheduleBuild2(0).get();
159159
jenkins.assertLogContains("run_matlab_command", build);
160-
jenkins.assertLogContains("test_runner", build);
160+
jenkins.assertLogContains("runner", build);
161161
}
162162

163163
/*
@@ -364,7 +364,7 @@ public void verifyAllTestArtifactsParameters() throws Exception {
364364
project.getBuildersList().add(this.testBuilder);
365365
FreeStyleBuild build = project.scheduleBuild2(0).get();
366366
jenkins.assertLogContains("run_matlab_command", build);
367-
jenkins.assertLogContains("test_runner", build);
367+
jenkins.assertLogContains("runner", build);
368368
jenkins.assertLogNotContains("\'PDFTestReport\',\'mypdf/report.pdf\'",build);
369369
jenkins.assertLogNotContains("\'TAPTestResults\',\'mytap/report.tap\'",build);
370370
jenkins.assertLogNotContains("\'JUnitTestResults\',\'myjunit/report.xml\'",build);
@@ -375,7 +375,7 @@ public void verifyAllTestArtifactsParameters() throws Exception {
375375
}
376376

377377
/*
378-
* Test to verify no parameters are sent in test_runner when no artifacts are selected.
378+
* Test to verify no parameters are sent in test runner when no artifacts are selected.
379379
*/
380380

381381
@Test
@@ -385,7 +385,7 @@ public void veriyEmptyParameters() throws Exception {
385385
project.getBuildersList().add(this.testBuilder);
386386
FreeStyleBuild build = project.scheduleBuild2(0).get();
387387
jenkins.assertLogContains("run_matlab_command", build);
388-
jenkins.assertLogContains("test_runner", build);
388+
jenkins.assertLogContains("runner", build);
389389
}
390390

391391

@@ -487,4 +487,17 @@ public void verifyMATLABscratchFileGenerated() throws Exception {
487487
File matlabRunner = new File(build.getWorkspace() + File.separator + "runMatlabTests.m");
488488
Assert.assertFalse(matlabRunner.exists());
489489
}
490+
491+
/*
492+
* Test to verify if .matlab gets created in workspace.
493+
*/
494+
@Test
495+
public void verifyMATLABfolderGenerated() throws Exception {
496+
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b")));
497+
project.getBuildWrappersList().add(this.buildWrapper);
498+
project.getBuildersList().add(testBuilder);
499+
FreeStyleBuild build = project.scheduleBuild2(0).get();
500+
File matlabRunner = new File(build.getWorkspace() + File.separator + ".matlab");
501+
Assert.assertTrue(matlabRunner.exists());
502+
}
490503
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void verifyCmdOptions() throws Exception {
9090
"node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
9191
WorkflowRun build = project.scheduleBuild2(0).get();
9292
j.assertLogContains("addpath(", build);
93-
j.assertLogContains("test_runner", build);
93+
j.assertLogContains("runner", build);
9494
}
9595

9696
/*
@@ -119,7 +119,7 @@ public void verifyEmptyParameter() throws Exception {
119119
project.setDefinition(new CpsFlowDefinition(
120120
"node {runMATLABTests()}", true));
121121
WorkflowRun build = project.scheduleBuild2(0).get();
122-
j.assertLogContains("test_runner", build);
122+
j.assertLogContains("runner", build);
123123
j.assertLogNotContains("PDFReportPath", build);
124124
j.assertLogNotContains("TAPResultsPath", build);
125125
j.assertLogNotContains("JUnitResultsPath", build);
@@ -137,6 +137,19 @@ public void verifyExceptionForNonZeroExitCode() throws Exception {
137137
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
138138
}
139139

140+
/*
141+
* Verify .matlab folder created
142+
*/
143+
144+
@Test
145+
public void verifyMATLABtempFolderGenerated() throws Exception {
146+
project.setDefinition(new CpsFlowDefinition(
147+
"node {testMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
148+
WorkflowRun build = project.scheduleBuild2(0).get();
149+
j.assertBuildStatusSuccess(build);
150+
j.assertLogContains(".matlab", build);
151+
}
152+
140153
/*@Integ Test
141154
* Verify default command options for test Filter using selectByFolder option
142155
*/

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@ public ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher lau
2424
TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
2525
throws IOException, InterruptedException {
2626
// Get node specific tmp directory to copy matlab runner script
27-
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
28-
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
27+
FilePath targetWorkspace = new FilePath(launcher.getChannel(),
28+
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);
29+
2930
ProcStarter matlabLauncher;
3031
if (launcher.isUnix()) {
3132
final String runnerScriptName = uniqueName + "/run_matlab_command_test.sh";
3233
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
33-
.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
34+
.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerScriptName, matlabCommand).stdout(listener);
3435

3536
// Copy runner .sh for linux platform in workspace.
3637
copyFileInWorkspace("run_matlab_command_test.sh", runnerScriptName, targetWorkspace);
3738
} else {
3839
final String runnerScriptName = uniqueName + "\\run_matlab_command_test.bat";
3940
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
4041
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
41-
.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
42+
.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
4243
.stdout(listener);
4344
// Copy runner.bat for Windows platform in workspace.
4445
copyFileInWorkspace("run_matlab_command_test.bat", runnerScriptName, targetWorkspace);

0 commit comments

Comments
 (0)