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

Commit db16d9a

Browse files
committed
Update texts and add installation for environment
1 parent 2662574 commit db16d9a

File tree

11 files changed

+228
-77
lines changed

11 files changed

+228
-77
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
117117
* Handle the case for using MATLAB Axis for multi conf projects by adding appropriate
118118
* matlabroot to env PATH
119119
* */
120-
Utilities.addMatlabToEnvPathFrmAxis(Computer.currentComputer(), listener, getEnv());
120+
Utilities.addMatlabToEnvPathFrmAxis(Computer.currentComputer(), listener, envVars);
121121

122122
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
123123
final String uniqueCommandFile =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ private synchronized int execMatlabCommand(FilePath workspace, Launcher launcher
254254
* Handle the case for using MATLAB Axis for multi conf projects by adding appropriate
255255
* matlabroot to env PATH
256256
* */
257-
Utilities.addMatlabToEnvPathFrmAxis(Computer.currentComputer(), listener, getEnv());
257+
Utilities.addMatlabToEnvPathFrmAxis(Computer.currentComputer(), listener, envVars);
258258

259259
final String uniqueTmpFldrName = getUniqueNameForRunnerFile();
260260
ProcStarter matlabLauncher;

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public String getMatlabRootFolder() {
4545
return this.matlabRootFolder;
4646
}
4747

48-
public String getMatlabInstHome(Computer cmp, TaskListener listener)
48+
public String getMatlabInstHome(Computer cmp, TaskListener listener, EnvVars env)
4949
throws IOException, InterruptedException {
50-
return Utilities.getNodeSpecificHome(this.matlabInstName, cmp.getNode(), listener);
50+
return Utilities.getNodeSpecificHome(this.matlabInstName, cmp.getNode(), listener, env);
5151
}
5252

5353
public String getMatlabInstName() {
@@ -60,10 +60,6 @@ public String getMatlabInstName() {
6060
return matlabInstName;
6161
}
6262

63-
public void setMatlabRootFolder(String matlabRootFolder) {
64-
this.matlabRootFolder = matlabRootFolder;
65-
}
66-
6763
public boolean isInitialized () {
6864
return this.matlabRootFolder == null && this.matlabInstName == null;
6965
}
@@ -80,7 +76,7 @@ private String getLocalMatlab(Computer cmp, TaskListener listener)
8076
throws IOException, InterruptedException {
8177
String matlabroot = getMatlabRootFolder();
8278
if (matlabroot == null || matlabroot.isEmpty()){
83-
matlabroot = getMatlabInstHome(cmp, listener);
79+
matlabroot = getMatlabInstHome(cmp, listener, this.env);
8480
}
8581

8682
return this.env == null ? matlabroot : this.env.expand(matlabroot);
@@ -95,19 +91,19 @@ private void setEnv(EnvVars env) {
9591
public static final class UseMatlabVersionDescriptor extends BuildWrapperDescriptor {
9692

9793
MatlabReleaseInfo rel;
98-
String matlabRootFolder;
94+
//String matlabRootFolder;
9995
private boolean isMatrix;
10096
private final String customLocation = Message.getValue("matlab.custom.location");
10197
private final String matlabAxisWarning = Message.getValue("Use.matlab.version.axis.warning");
10298
private AbstractProject<?, ?> project;
10399

104-
public String getMatlabRootFolder() {
100+
/* public String getMatlabRootFolder() {
105101
return matlabRootFolder;
106-
}
102+
}*/
107103

108-
public void setMatlabRootFolder(String matlabRootFolder) {
104+
/* public void setMatlabRootFolder(String matlabRootFolder) {
109105
this.matlabRootFolder = matlabRootFolder;
110-
}
106+
}*/
111107

112108
@Override
113109
public boolean isApplicable(AbstractProject<?, ?> item) {
@@ -158,7 +154,7 @@ public FormValidation doCheckMatlabRootFolder(@QueryParameter String matlabRootF
158154
/*
159155
* If isMatrix or if empty installations or if selectedOption is specify loc
160156
* */
161-
setMatlabRootFolder(matlabRootFolder);
157+
//setMatlabRootFolder(matlabRootFolder);
162158
List<Function<String, FormValidation>> listOfCheckMethods =
163159
new ArrayList<Function<String, FormValidation>>();
164160
listOfCheckMethods.add(chkMatlabEmpty);
@@ -204,16 +200,18 @@ public synchronized void setUp(Context context, Run<?, ?> build, FilePath worksp
204200

205201
FilePath matlabExecutablePath = new FilePath(launcher.getChannel(),
206202
getLocalMatlab(Computer.currentComputer(), listener) + "/bin/" + getNodeSpecificExecutable(launcher));
207-
listener.getLogger().println("\n MATLAB executed from : " + matlabExecutablePath.getRemote() + "\n");
203+
208204
if (!matlabExecutablePath.exists()) {
209205
throw new MatlabNotFoundError(Message.getValue("matlab.not.found.error"));
210206
}
211207
// Add "matlabroot" without bin as env variable which will be available across the build.
212208
context.env("matlabroot", getLocalMatlab(Computer.currentComputer(), listener));
213209
// Add matlab bin to path to invoke MATLAB directly on command line.
214-
context.env("PATH+matlabroot", matlabExecutablePath.getParent().getRemote());
210+
context.env("PATH+matlabroot", matlabExecutablePath.getParent().getRemote());
211+
// Specify which MATLAB was added to path.
212+
listener.getLogger().println("\n" + String.format(Message.getValue("matlab.added.to.path.from"), matlabExecutablePath.getParent().getRemote()) + "\n");
215213
}
216-
214+
217215
private String getNodeSpecificExecutable(Launcher launcher) {
218216
return (launcher.isUnix()) ? "matlab" : "matlab.exe";
219217
}

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

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

1010
import hudson.EnvVars;
1111
import hudson.FilePath;
12+
import hudson.Launcher;
13+
import hudson.Util;
1214
import hudson.model.Computer;
1315
import hudson.model.Node;
1416
import hudson.model.TaskListener;
@@ -41,27 +43,37 @@ public static void addMatlabToEnvPathFrmAxis(Computer cmp, TaskListener listener
4143
return;
4244
}
4345

44-
String home = getNodeSpecificHome(name, cmp.getNode(), listener);
45-
String pathToExecutable = home + "/bin";
46-
env.put("PATH+matlabroot", pathToExecutable);
46+
String matlabExecutablePath = getNodeSpecificHome(name, cmp.getNode(), listener, env) + "/bin";
47+
env.put("PATH+matlabroot", matlabExecutablePath);
48+
// Specify which MATLAB was added to path.
49+
listener.getLogger().println("\n" + String.format(Message.getValue("matlab.added.to.path.from"), matlabExecutablePath) + "\n");
4750
}
4851

49-
public static String getNodeSpecificHome(String instName, Node node, TaskListener listener)
52+
public static String getNodeSpecificHome(String instName, Node node, TaskListener listener, EnvVars env)
5053
throws IOException, InterruptedException {
5154
MatlabInstallation inst = MatlabInstallation.getInstallation(instName);
5255
if(inst != null) {
53-
inst = inst.forNode(node, listener);
54-
FilePath matlabExecutablePath = node.createPath(inst.getHome());
55-
// If no MATLAB version is configured for current node, throw error.
56-
if (matlabExecutablePath == null || !matlabExecutablePath.exists()) {
57-
throw new MatlabNotFoundError(String.format(Message.getValue("matlab.not.found.error.for.node"), instName, Objects
58-
.requireNonNull(node).getDisplayName()));
56+
if (node != null) {
57+
inst = inst.forNode(node, listener);
5958
}
60-
61-
return matlabExecutablePath.getRemote();
59+
if (env != null) {
60+
inst = inst.forEnvironment(env);
61+
}
62+
String home = Util.fixEmpty(inst.getHome());
63+
if (node != null) {
64+
FilePath matlabExecutablePath = node.createPath(inst.getHome());
65+
// If no MATLAB version is configured for current node, throw error.
66+
if (matlabExecutablePath == null || !matlabExecutablePath.exists()) {
67+
throw new MatlabNotFoundError(String.format(Message.getValue("matlab.not.found.error.for.node"), instName, Objects
68+
.requireNonNull(node).getDisplayName()));
69+
}
70+
return matlabExecutablePath.getRemote();
71+
}
72+
return home;
6273
}
6374

6475
// Following will error out in BuildWrapper
6576
return "";
6677
}
78+
6779
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<div>
2-
Specify the MATLAB version for this build. Select MATLAB from the dropdown box that lists installations that are globally configured in Jenkins. You can also specify a custom value for MATLAB installation by choosing 'Custom...' and enter the full path to the MATLAB root folder .
2+
<p>
3+
Use this list to specify the MATLAB version for the build. The plugin prepends your specified MATLAB to the PATH environment variable and uses it to perform the build. <br>
4+
The list contains the names of all MATLAB versions that have been registered as a tool in <u>Jenkins Global Tool Configuration</u>. Select one of the tools, or select <b>Custom…</b> if you want to specify a different version. When you select <b>Custom…</b>, you must enter the full path to the desired MATLAB root folder in the <b>MATLAB root</b> box.
5+
</p>
6+
<p><br>
7+
<b>Note:</b> For a Multi-configuration project that has 'MATLAB' axis added, 'Use MATLAB Version' value takes precedence over the values specified by the 'MATLAB' axis.
8+
</p>
39
</div>

src/main/resources/config.properties

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ builder.matlab.automatictestoption.display.name = Automatic
1313
builder.matlab.customcommandoption.display.name = Custom
1414
Releaseinfo.matlab.version.not.found.error = Error finding MATLAB release for given MATLAB root. Verify MATLAB root path.
1515
matlab.not.found.error = Unable to launch MATLAB from the specified location. Verify the path to MATLAB root folder.
16-
matlab.not.found.error.for.node = Unable to launch MATLAB with name %s at node %s . Verify MATLAB tool installation for selected node.
16+
matlab.not.found.error.for.node = Unable to launch MATLAB registered with the name %s on the node %s. Verify global tool configuration for the specified node.
1717
Builder.matlab.modelcoverage.support.warning = To generate a Cobertura model coverage report, use MATLAB R2018b or a newer release.
1818
Builder.matlab.exportstmresults.support.warning = To export Simulink Test Manager results, use MATLAB R2019a or a newer release.
1919
Builder.matlab.runner.script.target.file.linux.name = run_matlab_command.sh
@@ -24,7 +24,9 @@ matlab.tests.build.step.name = runMATLABTests
2424
matlab.command.step.display.name = Run MATLAB commands, scripts, or functions
2525
matlab.tests.step.display.name = Run MATLAB tests and generate artifacts
2626
matlab.custom.location = Custom...
27+
matlab.added.to.path.from = Prepending MATLAB to PATH variable from this location: %s
2728
Axis.matlab.key = MATLAB
28-
Axis.use.matlab.warning = 'Use MATLAB version' is selected. MATLAB axis values will be overridden with 'Use MATLAB Version' values.
29-
Axis.no.installed.matlab.error = No MATLAB installation is configured. Default configuration will be executed.
30-
Use.matlab.version.axis.warning = 'MATLAB' Axis is added. MATLAB axis values will be overridden with 'Use MATLAB Version' values.
29+
Axis.use.matlab.warning = This project specifies MATLAB versions using the added 'MATLAB' axis as well as the 'Use MATLAB version' option. The values specified by 'Use MATLAB version' take precedence over the values specified by the 'MATLAB' axis.
30+
Axis.no.installed.matlab.error = Because the project does not have any MATLAB versions configured in Jenkins Global Tool Configuration, the plugin runs the default configuration.
31+
Use.matlab.version.axis.warning = This project specifies MATLAB version using the 'Use MATLAB version' option as well as the added 'MATLAB' axis. The values specified by 'Use MATLAB version' take precedence over the values specified by the 'MATLAB' axis.
32+

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

Lines changed: 127 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package com.mathworks.ci;
22

3+
import hudson.matrix.AxisList;
4+
import hudson.matrix.MatrixBuild;
5+
import hudson.matrix.MatrixProject;
6+
import hudson.matrix.MatrixRun;
7+
import hudson.model.FreeStyleBuild;
8+
import hudson.model.FreeStyleProject;
9+
import hudson.model.Result;
310
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
411
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
512
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
6-
import org.junit.After;
7-
import org.junit.Before;
8-
import org.junit.Rule;
9-
import org.junit.Test;
13+
import org.junit.*;
1014
import org.jvnet.hudson.test.JenkinsRule;
1115
import java.io.File;
16+
import java.io.IOException;
17+
import java.net.URISyntaxException;
1218
import java.net.URL;
1319
import java.util.ArrayList;
1420
import java.util.Arrays;
21+
import java.util.List;
1522

1623
import static org.junit.Assert.assertArrayEquals;
1724
import static org.junit.Assert.assertEquals;
@@ -21,10 +28,40 @@
2128
public class MatlabInstallationTest {
2229

2330
private MatlabInstallation.DescriptorImpl matlabInstDescriptor;
31+
private static URL url;
32+
private static String FileSeperator;
33+
private static String VERSION_INFO_XML_FILE = "VersionInfo.xml";
34+
private static String matlabExecutorAbsolutePath;
2435

2536
@Rule
2637
public JenkinsRule jenkins = new JenkinsRule();
2738

39+
@BeforeClass
40+
public static void classSetup() throws URISyntaxException, IOException {
41+
ClassLoader classLoader = RunMatlabTestsBuilderTest.class.getClassLoader();
42+
if (!System.getProperty("os.name").startsWith("Win")) {
43+
FileSeperator = "/";
44+
url = classLoader.getResource("com/mathworks/ci/linux/bin/matlab.sh");
45+
try {
46+
matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath();
47+
48+
// Need to do this operation due to bug in maven Resource copy plugin [
49+
// https://issues.apache.org/jira/browse/MRESOURCES-132 ]
50+
51+
ProcessBuilder pb = new ProcessBuilder("chmod", "755", matlabExecutorAbsolutePath);
52+
pb.start();
53+
} catch (IOException e) {
54+
e.printStackTrace();
55+
} catch (URISyntaxException e) {
56+
e.printStackTrace();
57+
}
58+
} else {
59+
FileSeperator = "\\";
60+
url = classLoader.getResource("com/mathworks/ci/win/bin/matlab.bat");
61+
matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath();
62+
}
63+
}
64+
2865
@Before
2966
public void testSetup() {
3067
this.matlabInstDescriptor = jenkins.getInstance().getDescriptorByType(MatlabInstallation.DescriptorImpl.class);
@@ -85,4 +122,90 @@ public void verifyInstallationInPipeline() throws Exception {
85122
jenkins.assertBuildStatusSuccess(build);
86123
jenkins.assertLogContains("versioninfo", build);
87124
}
125+
/*
126+
* Test to verify usage of MATLAB tool installation in freestyle project.
127+
* */
128+
@Test
129+
public void verifyInstallationInFreeStyle() throws Exception {
130+
URL url = MatlabInstallationTest.class.getClassLoader().getResource("versioninfo" + FileSeperator + "R2018a");
131+
setMatlabInstallation("R2018b", new File(url.toURI()).getAbsolutePath());
132+
jenkins.configRoundtrip();
133+
134+
FreeStyleProject fsPrj = jenkins.createFreeStyleProject();
135+
MatlabInstallation _inst = MatlabInstallation.getInstallation("R2018b");
136+
MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(_inst.getName(), null);
137+
UseMatlabVersionBuildWrapper buildWrapper = new UseMatlabVersionBuildWrapper();
138+
buildWrapper.setMatlabBuildWrapperContent(content);
139+
fsPrj.getBuildWrappersList().add(buildWrapper);
140+
141+
FreeStyleBuild build = fsPrj.scheduleBuild2(0).get();
142+
// Verify correct MATLAB is invoked
143+
jenkins.assertLogContains(_inst.getHome(), build);
144+
}
145+
146+
/*
147+
* Test to verify if Matrix build passes with MATLAB installation(mock MATLAB).
148+
*/
149+
@Test
150+
public void verifyInstallationInMatrixBuild() throws Exception {
151+
// configure MATLAB installation
152+
setMatlabInstallation("R2018a", matlabExecutorAbsolutePath);
153+
setMatlabInstallation("R2018b", matlabExecutorAbsolutePath);
154+
jenkins.configRoundtrip();
155+
156+
// configure multi-config project
157+
MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);
158+
List<String> val = new ArrayList<>(Arrays.asList("R2018a", "R2018b"));
159+
MatlabInstallationAxis axes = new MatlabInstallationAxis(val);
160+
matrixProject.setAxes(new AxisList(axes));
161+
162+
RunMatlabTestsBuilderTester tester = new RunMatlabTestsBuilderTester("-positive");
163+
matrixProject.getBuildersList().add(tester);
164+
165+
MatrixBuild build = matrixProject.scheduleBuild2(0).get();
166+
List<MatrixRun> runs = build.getRuns();
167+
for (MatrixRun run : runs) {
168+
String matlabName = run.getBuildVariables().get("MATLAB");
169+
Assert.assertTrue(matlabName.equalsIgnoreCase("R2018a") || matlabName.equalsIgnoreCase("R2018b"));
170+
}
171+
172+
jenkins.assertLogContains("R2018a completed", build);
173+
jenkins.assertLogContains("R2018b completed", build);
174+
jenkins.assertBuildStatus(Result.SUCCESS, build);
175+
}
176+
177+
/*
178+
* @Integ Test
179+
* Paths should point to MATLAB executable
180+
* Test to verify correct MATLAB installation is added to PATH environment variable
181+
*/
182+
183+
public void verifyInstallationPathVarInMatrixBuild() throws Exception {
184+
// configure MATLAB installation
185+
setMatlabInstallation("R2018a", "<Path to MATLAB ver R2018a>");
186+
setMatlabInstallation("R2018b", "<Path to MATLAB ver R2018b>");
187+
jenkins.configRoundtrip();
188+
189+
// configure multi-config project
190+
MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);
191+
List<String> val = new ArrayList<>(Arrays.asList("R2018a", "R2018b"));
192+
MatlabInstallationAxis axes = new MatlabInstallationAxis(val);
193+
matrixProject.setAxes(new AxisList(axes));
194+
195+
RunMatlabTestsBuilder tester = new RunMatlabTestsBuilder();
196+
matrixProject.getBuildersList().add(tester);
197+
198+
MatrixBuild build = matrixProject.scheduleBuild2(0).get();
199+
List<MatrixRun> runs = build.getRuns();
200+
for (MatrixRun run : runs) {
201+
String matlabName = run.getBuildVariables().get("MATLAB");
202+
Assert.assertTrue(matlabName.equalsIgnoreCase("R2018a") || matlabName.equalsIgnoreCase("R2018b"));
203+
// Verify correct MATLAB is added to path and is printed in logs.
204+
jenkins.assertLogContains(MatlabInstallation.getInstallation(matlabName).getHome(), run);
205+
}
206+
207+
jenkins.assertLogContains("R2018a completed", build);
208+
jenkins.assertLogContains("R2018b completed", build);
209+
jenkins.assertBuildStatus(Result.FAILURE, build);
210+
}
88211
}

0 commit comments

Comments
 (0)