Skip to content

Commit

Permalink
Merge pull request #198 from mathworks/docker_support
Browse files Browse the repository at this point in the history
Support docker
  • Loading branch information
nbhoski authored Aug 13, 2021
2 parents 5710d7b + 7502850 commit d713c54
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 41 deletions.
41 changes: 13 additions & 28 deletions src/main/java/com/mathworks/ci/MatlabBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.RandomStringUtils;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.ProcStarter;
import hudson.model.Computer;
import hudson.model.TaskListener;

public interface MatlabBuild {
Expand All @@ -33,14 +31,14 @@ public interface MatlabBuild {
default ProcStarter getProcessToRunMatlabCommand(FilePath workspace,
Launcher launcher, TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
throws IOException, InterruptedException {
// Get node specific tmp directory to copy matlab runner script
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
// Get node specific temp .matlab directory to copy matlab runner script
FilePath targetWorkspace = new FilePath(launcher.getChannel(),
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);
ProcStarter matlabLauncher;
if (launcher.isUnix()) {
final String runnerScriptName = uniqueName + "/run_matlab_command.sh";
matlabLauncher = launcher.launch().envs(envVars);
matlabLauncher.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
matlabLauncher.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerScriptName, matlabCommand).stdout(listener);

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

default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName, FilePath workspace)
throws IOException, InterruptedException {
/*Use of Computer is not recommended as jenkins hygeine for pipeline support
* https://javadoc.jenkins-ci.org/jenkins/tasks/SimpleBuildStep.html */

String tmpDir = getNodeSpecificTmpFolderPath(workspace);

return new FilePath(launcher.getChannel(), tmpDir + "/" + uniqueName);
}
default FilePath getFilePathForUniqueFolder(Launcher launcher, String uniqueName,
FilePath workspace) throws IOException, InterruptedException {

default String getNodeSpecificTmpFolderPath(FilePath workspace) throws IOException, InterruptedException {
Computer cmp = workspace.toComputer();
if (cmp == null) {
throw new IOException(Message.getValue("build.workspace.computer.not.found"));
}

String tmpDirPath = (String) cmp.getSystemProperties().get("java.io.tmpdir");
String tmpDir =
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME;

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

default String getUniqueNameForRunnerFile() {
return UUID.randomUUID().toString();
//Using 8 bit long random alphanumeric string
return RandomStringUtils.randomAlphanumeric(8);
}

// This method prepares the temp folder by coping all helper files in it.
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/mathworks/ci/MatlabBuilderConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ public class MatlabBuilderConstants {
static final String MATLAB_SCRIPT_GENERATOR = "matlab-script-generator.zip";

//Test runner file prefix
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "test_runner_";
static final String MATLAB_TEST_RUNNER_FILE_PREFIX = "runner_";

//Temporary MATLAB folder name in workspace
static final String TEMP_MATLAB_FOLDER_NAME = ".matlab";

// MATLAB runner script
static final String TEST_RUNNER_SCRIPT = "testScript = genscript(${PARAMS});\n" + "\n"
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Builder.matlab.modelcoverage.support.warning = To generate a Cobertura model cov
Builder.matlab.exportstmresults.support.warning = To export Simulink Test Manager results, use MATLAB R2019a or a newer release.
Builder.matlab.runner.script.target.file.linux.name = run_matlab_command.sh
Builder.matlab.runner.script.target.file.windows.name = run_matlab_command.bat
build.workspace.computer.not.found = Unable to access the computer for this build.
matlab.command.build.step.name = runMATLABCommand
matlab.tests.build.step.name = runMATLABTests
matlab.command.step.display.name = Run MATLAB commands, scripts, or functions
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/mathworks/ci/RunMatlabCommandBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,18 @@ public void verifyMultispecialChar() throws Exception {
jenkins.assertLogContains("Generating MATLAB script with content", build);
jenkins.assertLogContains(expectedCommand, build);
}

/*
* Test to verify if .matlab temp folder generated in workspace.
*/
@Test
public void verifyMATLABtmpFolderGenerated() throws Exception {
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b")));
project.getBuildWrappersList().add(this.buildWrapper);
scriptBuilder.setMatlabCommand("pwd");
project.getBuildersList().add(this.scriptBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
File matlabRunner = new File(build.getWorkspace() + File.separator + ".matlab");
Assert.assertTrue(matlabRunner.exists());
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/mathworks/ci/RunMatlabCommandStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,19 @@ public void verifyExceptionStackTraceForNonZeroExitCode() throws Exception {
j.assertLogContains("com.mathworks.ci.MatlabExecutionException", build);
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
}

/*
* Verify .matlab folder is generated
*
*/

@Test
public void verifyMATLABtempFolderGenerated() throws Exception {
project.setDefinition(
new CpsFlowDefinition("node { testMATLABCommand(command: 'pwd')}", true));

WorkflowRun build = project.scheduleBuild2(0).get();
j.assertLogContains(".matlab", build);
j.assertBuildStatusSuccess(build);
}
}
23 changes: 18 additions & 5 deletions src/test/java/com/mathworks/ci/RunMatlabTestsBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void verifyMATLABlaunchedWithDefaultArgumentsBatch() throws Exception {
project.getBuildersList().add(this.testBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
jenkins.assertLogContains("run_matlab_command", build);
jenkins.assertLogContains("test_runner", build);
jenkins.assertLogContains("runner", build);
jenkins.assertLogContains("addpath(", build);
}

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

/*
Expand Down Expand Up @@ -364,7 +364,7 @@ public void verifyAllTestArtifactsParameters() throws Exception {
project.getBuildersList().add(this.testBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
jenkins.assertLogContains("run_matlab_command", build);
jenkins.assertLogContains("test_runner", build);
jenkins.assertLogContains("runner", build);
jenkins.assertLogNotContains("\'PDFTestReport\',\'mypdf/report.pdf\'",build);
jenkins.assertLogNotContains("\'TAPTestResults\',\'mytap/report.tap\'",build);
jenkins.assertLogNotContains("\'JUnitTestResults\',\'myjunit/report.xml\'",build);
Expand All @@ -375,7 +375,7 @@ public void verifyAllTestArtifactsParameters() throws Exception {
}

/*
* Test to verify no parameters are sent in test_runner when no artifacts are selected.
* Test to verify no parameters are sent in test runner when no artifacts are selected.
*/

@Test
Expand All @@ -385,7 +385,7 @@ public void veriyEmptyParameters() throws Exception {
project.getBuildersList().add(this.testBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
jenkins.assertLogContains("run_matlab_command", build);
jenkins.assertLogContains("test_runner", build);
jenkins.assertLogContains("runner", build);
}


Expand Down Expand Up @@ -487,4 +487,17 @@ public void verifyMATLABscratchFileGenerated() throws Exception {
File matlabRunner = new File(build.getWorkspace() + File.separator + "runMatlabTests.m");
Assert.assertFalse(matlabRunner.exists());
}

/*
* Test to verify if .matlab gets created in workspace.
*/
@Test
public void verifyMATLABfolderGenerated() throws Exception {
this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b")));
project.getBuildWrappersList().add(this.buildWrapper);
project.getBuildersList().add(testBuilder);
FreeStyleBuild build = project.scheduleBuild2(0).get();
File matlabRunner = new File(build.getWorkspace() + File.separator + ".matlab");
Assert.assertTrue(matlabRunner.exists());
}
}
17 changes: 15 additions & 2 deletions src/test/java/com/mathworks/ci/RunMatlabTestsStepTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void verifyCmdOptions() throws Exception {
"node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
WorkflowRun build = project.scheduleBuild2(0).get();
j.assertLogContains("addpath(", build);
j.assertLogContains("test_runner", build);
j.assertLogContains("runner", build);
}

/*
Expand Down Expand Up @@ -119,7 +119,7 @@ public void verifyEmptyParameter() throws Exception {
project.setDefinition(new CpsFlowDefinition(
"node {runMATLABTests()}", true));
WorkflowRun build = project.scheduleBuild2(0).get();
j.assertLogContains("test_runner", build);
j.assertLogContains("runner", build);
j.assertLogNotContains("PDFReportPath", build);
j.assertLogNotContains("TAPResultsPath", build);
j.assertLogNotContains("JUnitResultsPath", build);
Expand All @@ -137,6 +137,19 @@ public void verifyExceptionForNonZeroExitCode() throws Exception {
j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build);
}

/*
* Verify .matlab folder created
*/

@Test
public void verifyMATLABtempFolderGenerated() throws Exception {
project.setDefinition(new CpsFlowDefinition(
"node {testMATLABTests(testResultsPDF:'myresult/result.pdf')}", true));
WorkflowRun build = project.scheduleBuild2(0).get();
j.assertBuildStatusSuccess(build);
j.assertLogContains(".matlab", build);
}

/*@Integ Test
* Verify default command options for test Filter using selectByFolder option
*/
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/com/mathworks/ci/TestStepExecution.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ public ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher lau
TaskListener listener, EnvVars envVars, String matlabCommand, String uniqueName)
throws IOException, InterruptedException {
// Get node specific tmp directory to copy matlab runner script
String tmpDir = getNodeSpecificTmpFolderPath(workspace);
FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir);
FilePath targetWorkspace = new FilePath(launcher.getChannel(),
workspace.getRemote() + "/" + MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME);

ProcStarter matlabLauncher;
if (launcher.isUnix()) {
final String runnerScriptName = uniqueName + "/run_matlab_command_test.sh";
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
.cmds(tmpDir + "/" + runnerScriptName, matlabCommand).stdout(listener);
.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "/" + runnerScriptName, matlabCommand).stdout(listener);

// Copy runner .sh for linux platform in workspace.
copyFileInWorkspace("run_matlab_command_test.sh", runnerScriptName, targetWorkspace);
} else {
final String runnerScriptName = uniqueName + "\\run_matlab_command_test.bat";
launcher = launcher.decorateByPrefix("cmd.exe", "/C");
matlabLauncher = launcher.launch().pwd(workspace).envs(envVars)
.cmds(tmpDir + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
.cmds(MatlabBuilderConstants.TEMP_MATLAB_FOLDER_NAME + "\\" + runnerScriptName, "\"" + matlabCommand + "\"")
.stdout(listener);
// Copy runner.bat for Windows platform in workspace.
copyFileInWorkspace("run_matlab_command_test.bat", runnerScriptName, targetWorkspace);
Expand Down

0 comments on commit d713c54

Please sign in to comment.