From 3480821dc3949c8d96d5e9672d53056601f2402c Mon Sep 17 00:00:00 2001 From: Nikhil Bhoski Date: Wed, 11 Mar 2020 14:56:00 +0530 Subject: [PATCH 1/9] Fix for matrix issue g2194915 --- .../java/com/mathworks/ci/MatlabBuild.java | 31 ++++++++++++------- .../mathworks/ci/RunMatlabCommandBuilder.java | 24 +++++++------- .../mathworks/ci/RunMatlabTestsBuilder.java | 23 +++++++------- .../ci/UseMatlabVersionBuildWrapper.java | 11 ++++--- 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/mathworks/ci/MatlabBuild.java b/src/main/java/com/mathworks/ci/MatlabBuild.java index 343e5f05..a5732dd7 100644 --- a/src/main/java/com/mathworks/ci/MatlabBuild.java +++ b/src/main/java/com/mathworks/ci/MatlabBuild.java @@ -9,6 +9,8 @@ import java.io.IOException; import java.io.InputStream; +import java.time.Instant; + import hudson.EnvVars; import hudson.FilePath; import hudson.Launcher; @@ -28,46 +30,47 @@ public interface MatlabBuild { * @param matlabCommand MATLAB command to execute on shell * @return matlabLauncher returns the process launcher to run MATLAB commands */ - default ProcStarter getProcessToRunMatlabCommand(FilePath workspace, Launcher launcher,TaskListener listener, EnvVars envVars, String matlabCommand) throws IOException, InterruptedException { + 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(); FilePath targetWorkspace = new FilePath(launcher.getChannel(), tmpDir); ProcStarter matlabLauncher; if(launcher.isUnix()) { - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/run_matlab_command.sh",matlabCommand).stdout(listener); + final String runnerScriptName = "run_matlab_command"+uniqueName+".sh"; + matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"/"+runnerScriptName,matlabCommand).stdout(listener); //Copy runner .sh for linux platform in workspace. - copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.linux.name", targetWorkspace); + copyFileInWorkspace(MatlabBuilderConstants.SHELL_RUNNER_SCRIPT, runnerScriptName, targetWorkspace); }else { + final String runnerScriptName = "run_matlab_command"+uniqueName+".bat"; launcher = launcher.decorateByPrefix("cmd.exe","/C"); - matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+"run_matlab_command.bat","\""+matlabCommand+"\"").stdout(listener); + matlabLauncher = launcher.launch().pwd(workspace).envs(envVars).cmds(tmpDir+"\\"+runnerScriptName,"\""+matlabCommand+"\"").stdout(listener); //Copy runner.bat for Windows platform in workspace. - copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, "Builder.matlab.runner.script.target.file.windows.name", targetWorkspace); + copyFileInWorkspace(MatlabBuilderConstants.BAT_RUNNER_SCRIPT, runnerScriptName, targetWorkspace); } return matlabLauncher; } - /** + /* * Method to copy given file from source to target node specific workspace. */ default void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace) throws IOException, InterruptedException { final ClassLoader classLoader = getClass().getClassLoader(); - FilePath targetFilePath = new FilePath(targetWorkspace, Message.getValue(targetFile)); + FilePath targetFilePath = new FilePath(targetWorkspace, targetFile); InputStream in = classLoader.getResourceAsStream(sourceFile); targetFilePath.copyFrom(in); // set executable permission - targetFilePath.chmod(0755); - + targetFilePath.chmod(0755); } - default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher) throws IOException, InterruptedException { + default FilePath getNodeSpecificMatlabRunnerScript(Launcher launcher,String uniqueName) throws IOException, InterruptedException { Computer cmp = Computer.currentComputer(); String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir"); if(launcher.isUnix()) { - tmpDir = tmpDir+"/run_matlab_command.sh"; + tmpDir = tmpDir+"/run_matlab_command"+uniqueName+".sh"; }else { - tmpDir = tmpDir+"\\"+"run_matlab_command.bat"; + tmpDir = tmpDir+"\\"+"run_matlab_command"+uniqueName+".bat"; } return new FilePath(launcher.getChannel(), tmpDir); } @@ -77,5 +80,9 @@ default String getNodeSpecificTmpFolderPath() throws IOException, InterruptedExc String tmpDir = (String) cmp.getSystemProperties().get("java.io.tmpdir"); return tmpDir; } + + default String getUniqueNameForRunnerFile() { + return Instant.now().toString().replaceAll("[:,.]", ""); + } } diff --git a/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java b/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java index 9212c10f..7d75c67f 100644 --- a/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java +++ b/src/main/java/com/mathworks/ci/RunMatlabCommandBuilder.java @@ -7,6 +7,8 @@ */ import java.io.IOException; +import java.time.Instant; + import javax.annotation.Nonnull; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; @@ -95,8 +97,7 @@ public boolean isApplicable( public void perform(@Nonnull Run build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { - - try { + // Set the environment variable specific to the this build setEnv(build.getEnvironment(listener)); @@ -108,24 +109,25 @@ public void perform(@Nonnull Run build, @Nonnull FilePath workspace, if (buildResult != 0) { build.setResult(Result.FAILURE); } - } finally { - // Cleanup the runner File from tmp directory - FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher); - if(matlabRunnerScript.exists()) { - matlabRunnerScript.delete(); - } - } } private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException { + final String uniqueFileName = getUniqueNameForRunnerFile(); ProcStarter matlabLauncher; try { - matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand()); + matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars,getCommand(),uniqueFileName); + return matlabLauncher.join(); } catch (Exception e) { listener.getLogger().println(e.getMessage()); return 1; + }finally { + // Cleanup the runner File from tmp directory + FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueFileName); + if(matlabRunnerScript.exists()) { + matlabRunnerScript.delete(); + } } - return matlabLauncher.join(); + } } diff --git a/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java b/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java index f9523d95..d403fcde 100644 --- a/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java +++ b/src/main/java/com/mathworks/ci/RunMatlabTestsBuilder.java @@ -10,6 +10,7 @@ import java.io.File; import java.io.IOException; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.function.Function; @@ -258,7 +259,6 @@ public void perform(@Nonnull Run build, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { - try { // Set the environment variable specific to the this build setEnv(build.getEnvironment(listener)); @@ -269,32 +269,33 @@ public void perform(@Nonnull Run build, @Nonnull FilePath workspace, if (buildResult != 0) { build.setResult(Result.FAILURE); - } - } finally { - // Cleanup the runner File from tmp directory - FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher); - if (matlabRunnerScript.exists()) { - matlabRunnerScript.delete(); - } - } + } } private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher, TaskListener listener, EnvVars envVars) throws IOException, InterruptedException { + final String uniqueFileName = getUniqueNameForRunnerFile(); ProcStarter matlabLauncher; try { matlabLauncher = getProcessToRunMatlabCommand(workspace, launcher, listener, envVars, - constructCommandForTest(getInputArguments())); + constructCommandForTest(getInputArguments()),uniqueFileName); // Copy MATLAB scratch file into the workspace. FilePath targetWorkspace = new FilePath(launcher.getChannel(), workspace.getRemote()); copyFileInWorkspace(MatlabBuilderConstants.MATLAB_RUNNER_RESOURCE, MatlabBuilderConstants.MATLAB_RUNNER_TARGET_FILE, targetWorkspace); + return matlabLauncher.join(); } catch (Exception e) { listener.getLogger().println(e.getMessage()); return 1; + }finally { + // Cleanup the runner File from tmp directory + FilePath matlabRunnerScript = getNodeSpecificMatlabRunnerScript(launcher,uniqueFileName); + if (matlabRunnerScript.exists()) { + matlabRunnerScript.delete(); + } } - return matlabLauncher.join(); + } public String constructCommandForTest(String inputArguments) { diff --git a/src/main/java/com/mathworks/ci/UseMatlabVersionBuildWrapper.java b/src/main/java/com/mathworks/ci/UseMatlabVersionBuildWrapper.java index c0e90cf9..cf6a2eb0 100644 --- a/src/main/java/com/mathworks/ci/UseMatlabVersionBuildWrapper.java +++ b/src/main/java/com/mathworks/ci/UseMatlabVersionBuildWrapper.java @@ -13,10 +13,12 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Function; + import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.QueryParameter; + import hudson.EnvVars; import hudson.Extension; import hudson.FilePath; @@ -30,7 +32,8 @@ public class UseMatlabVersionBuildWrapper extends SimpleBuildWrapper { - private String matlabRootFolder; + + private String matlabRootFolder; private EnvVars env; @DataBoundConstructor @@ -124,7 +127,7 @@ public FormValidation doCheckMatlabRootFolder(@QueryParameter String matlabRootF } @Override - public void setUp(Context context, Run build, FilePath workspace, Launcher launcher, + public synchronized void setUp(Context context, Run build, FilePath workspace, Launcher launcher, TaskListener listener, EnvVars initialEnvironment) throws IOException, InterruptedException { // Set Environment variable @@ -134,9 +137,9 @@ public void setUp(Context context, Run build, FilePath workspace, Launcher // Add "matlabroot" without bin as env variable which will be available across the build. context.env("matlabroot", getLocalMatlab()); // Add matlab bin to path to invoke MATLAB directly on command line. - context.env("PATH+matlabroot", getLocalMatlab() + nodeSpecificFileSep + "bin"); + context.env("PATH+matlabroot", getLocalMatlab() + nodeSpecificFileSep + "bin"); } - + private String getNodeSpecificFileSeperator(Launcher launcher) { if (launcher.isUnix()) { return "/"; From 22140e662c39e7cfc85cf0a47f66f0261b35ea45 Mon Sep 17 00:00:00 2001 From: Nikhil Bhoski Date: Wed, 11 Mar 2020 15:25:07 +0530 Subject: [PATCH 2/9] Removed invalid test case. --- .../com/mathworks/ci/RunMatlabTestsBuilderTest.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/test/java/com/mathworks/ci/RunMatlabTestsBuilderTest.java b/src/test/java/com/mathworks/ci/RunMatlabTestsBuilderTest.java index 280cd874..2c2047c7 100644 --- a/src/test/java/com/mathworks/ci/RunMatlabTestsBuilderTest.java +++ b/src/test/java/com/mathworks/ci/RunMatlabTestsBuilderTest.java @@ -228,19 +228,6 @@ public void verifyRunTestAutomaticallyIsDefault() throws Exception { + "\'CoberturaCodeCoverage\',true,\'CoberturaModelCoverage\',true", build); } - /* - * Test to verify if MATALB scratch file is generated in workspace. - */ - @Test - public void verifyMATLABscratchFileGeneratedForAutomaticOption() throws Exception { - this.buildWrapper.setMatlabRootFolder(getMatlabroot("R2018b")); - project.getBuildWrappersList().add(this.buildWrapper); - setAllTestArtifacts(false, testBuilder); - project.getBuildersList().add(testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - File matlabRunner = new File(build.getWorkspace() + File.separator + "runMatlabTests.m"); - Assert.assertTrue(matlabRunner.exists()); - } /* * Test to verify if appropriate MATALB runner file is copied in workspace. From 42524beebb673690312e2776af8aeeb996bf1db3 Mon Sep 17 00:00:00 2001 From: Nikhil Bhoski Date: Thu, 12 Mar 2020 17:52:51 +0530 Subject: [PATCH 3/9] Changed the file coopy mechanism as per review commnets and added tests to cover it --- pom.xml | 9 +++ .../java/com/mathworks/ci/MatlabBuild.java | 12 ++-- .../mathworks/ci/RunMatlabCommandBuilder.java | 11 ++- .../mathworks/ci/RunMatlabTestsBuilder.java | 11 ++- .../ci/RunMatlabCommandBuilderTest.java | 58 ++++++++++++++++ .../ci/RunMatlabTestsBuilderTest.java | 67 +++++++++++++++++++ 6 files changed, 150 insertions(+), 18 deletions(-) diff --git a/pom.xml b/pom.xml index 172861fc..b2df3af0 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,15 @@ HEAD + + + + org.jenkins-ci.plugins + matrix-project + 1.14 + + +