|
1 | 1 | package com.mathworks.ci; |
2 | 2 |
|
| 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; |
3 | 10 | import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
4 | 11 | import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
5 | 12 | 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.*; |
10 | 14 | import org.jvnet.hudson.test.JenkinsRule; |
11 | 15 | import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.net.URISyntaxException; |
12 | 18 | import java.net.URL; |
13 | 19 | import java.util.ArrayList; |
14 | 20 | import java.util.Arrays; |
| 21 | +import java.util.List; |
15 | 22 |
|
16 | 23 | import static org.junit.Assert.assertArrayEquals; |
17 | 24 | import static org.junit.Assert.assertEquals; |
|
21 | 28 | public class MatlabInstallationTest { |
22 | 29 |
|
23 | 30 | 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; |
24 | 35 |
|
25 | 36 | @Rule |
26 | 37 | public JenkinsRule jenkins = new JenkinsRule(); |
27 | 38 |
|
| 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 | + |
28 | 65 | @Before |
29 | 66 | public void testSetup() { |
30 | 67 | this.matlabInstDescriptor = jenkins.getInstance().getDescriptorByType(MatlabInstallation.DescriptorImpl.class); |
@@ -85,4 +122,90 @@ public void verifyInstallationInPipeline() throws Exception { |
85 | 122 | jenkins.assertBuildStatusSuccess(build); |
86 | 123 | jenkins.assertLogContains("versioninfo", build); |
87 | 124 | } |
| 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 | + } |
88 | 211 | } |
0 commit comments