|
| 1 | +package com.mathworks.ci.systemTests; |
| 2 | + |
| 3 | +import com.mathworks.ci.MatlabBuildWrapperContent; |
| 4 | +import com.mathworks.ci.MatlabInstallationAxis; |
| 5 | +import com.mathworks.ci.Message; |
| 6 | +import com.mathworks.ci.UseMatlabVersionBuildWrapper; |
| 7 | +import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; |
| 8 | +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; |
| 9 | +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; |
| 10 | +import com.mathworks.ci.freestyle.options.*; |
| 11 | +import hudson.FilePath; |
| 12 | +import hudson.matrix.*; |
| 13 | +import hudson.model.FreeStyleBuild; |
| 14 | +import hudson.model.FreeStyleProject; |
| 15 | +import hudson.model.Result; |
| 16 | +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
| 17 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 18 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 19 | +import org.junit.*; |
| 20 | +import org.junit.rules.Timeout; |
| 21 | +import org.jvnet.hudson.test.ExtractResourceSCM; |
| 22 | +import org.jvnet.hudson.test.JenkinsRule; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertTrue; |
| 30 | + |
| 31 | +public class StartupOptionsIT { |
| 32 | + volatile private FreeStyleProject project; |
| 33 | + volatile WorkflowJob pipelineProject; |
| 34 | + private String envScripted; |
| 35 | + private String envDSL; |
| 36 | + volatile private UseMatlabVersionBuildWrapper buildWrapper; |
| 37 | + volatile FreeStyleBuild build; |
| 38 | + |
| 39 | + @Rule |
| 40 | + public Timeout timeout = Timeout.seconds(0); |
| 41 | + |
| 42 | + @Rule |
| 43 | + public JenkinsRule jenkins = new JenkinsRule(); |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void checkMatlabRoot() { |
| 47 | + // Check if the MATLAB_ROOT environment variable is defined |
| 48 | + String matlabRoot = System.getenv("MATLAB_ROOT"); |
| 49 | + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); |
| 50 | + } |
| 51 | + |
| 52 | + @Before |
| 53 | + public void testSetup() throws IOException { |
| 54 | + project = jenkins.createFreeStyleProject(); |
| 55 | + pipelineProject = jenkins.createProject(WorkflowJob.class); |
| 56 | + buildWrapper = new UseMatlabVersionBuildWrapper(); |
| 57 | + this.envDSL = MatlabRootSetup.getEnvironmentDSL(); |
| 58 | + this.envScripted = MatlabRootSetup.getEnvironmentScriptedPipeline(); |
| 59 | + } |
| 60 | + |
| 61 | + @After |
| 62 | + public void testTearDown() { |
| 63 | + project = null; |
| 64 | + buildWrapper = null; |
| 65 | + pipelineProject = null; |
| 66 | + } |
| 67 | + |
| 68 | + private WorkflowRun getPipelineBuild(String script) throws Exception{ |
| 69 | + pipelineProject.setDefinition(new CpsFlowDefinition(script,true)); |
| 70 | + return pipelineProject.scheduleBuild2(0).get(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void verifyStartupOptionsInFreeStyleProject() throws Exception { |
| 75 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 76 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 77 | + //Command Step |
| 78 | + RunMatlabCommandBuilder commandStep = |
| 79 | + new RunMatlabCommandBuilder(); |
| 80 | + commandStep.setMatlabCommand("pwd,version"); |
| 81 | + StartupOptions startupOptions = new StartupOptions("-logfile outputCommand.log"); |
| 82 | + commandStep.setStartupOptions(startupOptions); |
| 83 | + project.getBuildersList().add(commandStep); |
| 84 | + |
| 85 | + //Run tests step |
| 86 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 87 | + RunMatlabTestsBuilder runTestsStep = new RunMatlabTestsBuilder(); |
| 88 | + //Adding src folder |
| 89 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 90 | + list.add(new SourceFolderPaths("src")); |
| 91 | + runTestsStep.setSourceFolder(new SourceFolder(list)); |
| 92 | + |
| 93 | + // Adding list of test folder |
| 94 | + List<TestFolders> testFolders = new ArrayList<TestFolders>(); |
| 95 | + testFolders.add(new TestFolders("test/TestSquare")); |
| 96 | + runTestsStep.setSelectByFolder(new SelectByFolder(testFolders)); |
| 97 | + runTestsStep.setStartupOptions(new StartupOptions("-logfile outputTests.log")); |
| 98 | + project.getBuildersList().add(runTestsStep); |
| 99 | + |
| 100 | + //Rub Build step |
| 101 | + RunMatlabBuildBuilder buildStep = new RunMatlabBuildBuilder(); |
| 102 | + buildStep.setTasks("check"); |
| 103 | + buildStep.setStartupOptions(new StartupOptions("-logfile outputBuild.log")); |
| 104 | + project.getBuildersList().add(buildStep); |
| 105 | + |
| 106 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 107 | + |
| 108 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "outputCommand.log").exists()); |
| 109 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "outputBuild.log").exists()); |
| 110 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "outputTests.log").exists()); |
| 111 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void verifyStartupOptionsInDeclarativePipeline() throws Exception { |
| 116 | + String script = "pipeline {\n" + |
| 117 | + " agent any\n" + |
| 118 | + envDSL + "\n" + |
| 119 | + " stages{\n" + |
| 120 | + " stage('Run MATLAB Command') {\n" + |
| 121 | + " steps\n" + |
| 122 | + " {\n" + |
| 123 | + " unzip '" + MatlabRootSetup.getRunMATLABTestsData().getPath() + "'" + "\n" + |
| 124 | + " runMATLABCommand(command: 'pwd,version', startupOptions: '-logfile outputCommand.log -nojvm')\n" + |
| 125 | + " runMATLABTests(sourceFolder: ['src'], testResultsJUnit: 'test-results/results.xml'," + |
| 126 | + " codeCoverageCobertura: 'code-coverage/coverage.xml', startupOptions: '-logfile outputTests.log -nojvm')\n" + |
| 127 | + " runMATLABBuild(tasks: 'check', startupOptions: '-logfile outputBuild.log -nojvm')" + |
| 128 | + " }\n" + |
| 129 | + " }\n" + |
| 130 | + " }\n" + |
| 131 | + "}"; |
| 132 | + WorkflowRun build = getPipelineBuild(script); |
| 133 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 134 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputCommand.log").exists()); |
| 135 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputTests.log").exists()); |
| 136 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputBuild.log").exists()); |
| 137 | + jenkins.assertLogContains("-logfile outputCommand.log", build); |
| 138 | + jenkins.assertLogContains("-logfile outputBuild.log", build); |
| 139 | + jenkins.assertLogContains("-logfile outputTests.log", build); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void verifyStartupOptionsInScriptedPipeline() throws Exception { |
| 144 | + String script = "node {\n" + |
| 145 | + envScripted + "\n" + |
| 146 | + " unzip '" + MatlabRootSetup.getRunMATLABTestsData().getPath() + "'" + "\n" + |
| 147 | + " runMATLABCommand(command: 'pwd,version', startupOptions: '-logfile outputCommand.log -nojvm')\n" + |
| 148 | + " runMATLABTests(sourceFolder: ['src'], testResultsJUnit: 'test-results/results.xml'," + |
| 149 | + " codeCoverageCobertura: 'code-coverage/coverage.xml', startupOptions: '-logfile outputTests.log -nojvm')\n" + |
| 150 | + " runMATLABBuild(tasks: 'check', startupOptions: '-logfile outputBuild.log -nojvm')" + |
| 151 | + "}"; |
| 152 | + WorkflowRun build = getPipelineBuild(script); |
| 153 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 154 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputCommand.log").exists()); |
| 155 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputTests.log").exists()); |
| 156 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputBuild.log").exists()); |
| 157 | + jenkins.assertLogContains("-logfile outputCommand.log", build); |
| 158 | + jenkins.assertLogContains("-logfile outputBuild.log", build); |
| 159 | + jenkins.assertLogContains("-logfile outputTests.log", build); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void verifyStartUpOptionsInMatrixProject() throws Exception { |
| 164 | + String matlabRoot = System.getenv("MATLAB_ROOT"); |
| 165 | + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); |
| 166 | + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); |
| 167 | + |
| 168 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); |
| 169 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_22b", matlabRoot22b, jenkins); |
| 170 | + |
| 171 | + MatrixProject project = jenkins.createProject(MatrixProject.class); |
| 172 | + MatlabInstallationAxis MATLABAxis = new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); |
| 173 | + project.setAxes(new AxisList(MATLABAxis)); |
| 174 | + |
| 175 | + //Command Step |
| 176 | + RunMatlabCommandBuilder commandStep = |
| 177 | + new RunMatlabCommandBuilder(); |
| 178 | + commandStep.setMatlabCommand("pwd,version"); |
| 179 | + StartupOptions startupOptions = new StartupOptions("-logfile outputCommand.log"); |
| 180 | + commandStep.setStartupOptions(startupOptions); |
| 181 | + project.getBuildersList().add(commandStep); |
| 182 | + |
| 183 | + //Run tests step |
| 184 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 185 | + RunMatlabTestsBuilder runTestsStep = new RunMatlabTestsBuilder(); |
| 186 | + //Adding src folder |
| 187 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 188 | + list.add(new SourceFolderPaths("src")); |
| 189 | + runTestsStep.setSourceFolder(new SourceFolder(list)); |
| 190 | + |
| 191 | + // Adding list of test folder |
| 192 | + List<TestFolders> testFolders = new ArrayList<TestFolders>(); |
| 193 | + testFolders.add(new TestFolders("test/TestSquare")); |
| 194 | + runTestsStep.setSelectByFolder(new SelectByFolder(testFolders)); |
| 195 | + runTestsStep.setStartupOptions(new StartupOptions("-logfile outputTests.log")); |
| 196 | + project.getBuildersList().add(runTestsStep); |
| 197 | + |
| 198 | + //Rub Build step |
| 199 | + RunMatlabBuildBuilder buildStep = new RunMatlabBuildBuilder(); |
| 200 | + buildStep.setTasks("check"); |
| 201 | + buildStep.setStartupOptions(new StartupOptions("-logfile outputBuild.log")); |
| 202 | + project.getBuildersList().add(buildStep); |
| 203 | + |
| 204 | + MatrixBuild build = project.scheduleBuild2(0).get(); |
| 205 | + |
| 206 | + Combination c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1"))), "MATLAB_PATH_1"); |
| 207 | + MatrixRun run = build.getRun(c); |
| 208 | + assertTrue(new FilePath(run.getWorkspace(), "outputCommand.log").exists()); |
| 209 | + assertTrue(new FilePath(run.getWorkspace(), "outputBuild.log").exists()); |
| 210 | + assertTrue(new FilePath(run.getWorkspace(), "outputTests.log").exists()); |
| 211 | + |
| 212 | + c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_22b"))), "MATLAB_PATH_22b"); |
| 213 | + run = build.getRun(c); |
| 214 | + assertTrue(new FilePath(run.getWorkspace(), "outputCommand.log").exists()); |
| 215 | + assertTrue(new FilePath(run.getWorkspace(), "outputBuild.log").exists()); |
| 216 | + assertTrue(new FilePath(run.getWorkspace(), "outputTests.log").exists()); |
| 217 | + |
| 218 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 219 | + } |
| 220 | +} |
0 commit comments