|
| 1 | +package com.mathworks.ci.systemTests; |
| 2 | + |
| 3 | +import com.jcabi.xml.XML; |
| 4 | +import com.jcabi.xml.XMLDocument; |
| 5 | +import com.mathworks.ci.MatlabBuildWrapperContent; |
| 6 | +import com.mathworks.ci.Message; |
| 7 | +import com.mathworks.ci.UseMatlabVersionBuildWrapper; |
| 8 | +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; |
| 9 | +import com.mathworks.ci.freestyle.options.SourceFolder; |
| 10 | +import com.mathworks.ci.freestyle.options.SourceFolderPaths; |
| 11 | +import hudson.FilePath; |
| 12 | +import hudson.model.FreeStyleBuild; |
| 13 | +import hudson.model.FreeStyleProject; |
| 14 | +import hudson.model.Result; |
| 15 | +import org.htmlunit.html.HtmlCheckBoxInput; |
| 16 | +import org.htmlunit.html.HtmlPage; |
| 17 | +import org.htmlunit.html.HtmlTextInput; |
| 18 | +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
| 19 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 20 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 21 | +import org.junit.*; |
| 22 | +import org.junit.rules.Timeout; |
| 23 | +import org.jvnet.hudson.test.ExtractResourceSCM; |
| 24 | +import org.jvnet.hudson.test.JenkinsRule; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileNotFoundException; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +public class RunMATLABTestsArtifactsIT { |
| 36 | + private FreeStyleProject project; |
| 37 | + private UseMatlabVersionBuildWrapper buildWrapper; |
| 38 | + private RunMatlabTestsBuilder testBuilder; |
| 39 | + |
| 40 | + @Rule |
| 41 | + public Timeout timeout = Timeout.seconds(0); |
| 42 | + |
| 43 | + @Rule |
| 44 | + public JenkinsRule jenkins = new JenkinsRule(); |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void checkMatlabRoot() { |
| 48 | + // Check if the MATLAB_ROOT environment variable is defined |
| 49 | + String matlabRoot = System.getenv("MATLAB_ROOT"); |
| 50 | + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); |
| 51 | + } |
| 52 | + |
| 53 | + @Before |
| 54 | + public void testSetup() throws IOException { |
| 55 | + this.project = jenkins.createFreeStyleProject(); |
| 56 | + this.testBuilder = new RunMatlabTestsBuilder(); |
| 57 | + this.buildWrapper = new UseMatlabVersionBuildWrapper(); |
| 58 | + testBuilder.setLoggingLevel("default"); |
| 59 | + testBuilder.setOutputDetail("default"); |
| 60 | + } |
| 61 | + |
| 62 | + @After |
| 63 | + public void testTearDown() { |
| 64 | + this.project = null; |
| 65 | + this.testBuilder = null; |
| 66 | + } |
| 67 | + |
| 68 | + /* Helper function to read XML file as string */ |
| 69 | + public String xmlToString(String codegenReportPath) throws FileNotFoundException { |
| 70 | + File codeCoverageFile = new File(codegenReportPath); |
| 71 | + XML xml = new XMLDocument(codeCoverageFile); |
| 72 | + return xml.toString(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void verifyJUnitFilePathInput() throws Exception{ |
| 77 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 78 | + |
| 79 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 80 | + project.getBuildersList().add(this.testBuilder); |
| 81 | + |
| 82 | + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); |
| 83 | + HtmlCheckBoxInput junitChkbx = page.getElementByName("junitArtifact"); |
| 84 | + junitChkbx.setChecked(true); |
| 85 | + Thread.sleep(2000); |
| 86 | + |
| 87 | + HtmlTextInput junitFilePathInput=(HtmlTextInput) page.getElementByName("_.junitReportFilePath"); |
| 88 | + Assert.assertEquals(TestData.getPropValues("junit.file.path"),junitFilePathInput.getValueAttribute()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void verifyTAPTestFilePathInput() throws Exception{ |
| 93 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 94 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 95 | + project.getBuildersList().add(this.testBuilder); |
| 96 | + |
| 97 | + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); |
| 98 | + HtmlCheckBoxInput tapChkbx = page.getElementByName("tapArtifact"); |
| 99 | + tapChkbx.setChecked(true); |
| 100 | + Thread.sleep(2000); |
| 101 | + |
| 102 | + HtmlTextInput tapFilePathInput=(HtmlTextInput) page.getElementByName("_.tapReportFilePath"); |
| 103 | + Assert.assertEquals(TestData.getPropValues("taptestresult.file.path"),tapFilePathInput.getValueAttribute()); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void verifyPDFReportFilePathInput() throws Exception{ |
| 109 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 110 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 111 | + project.getBuildersList().add(this.testBuilder); |
| 112 | + |
| 113 | + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); |
| 114 | + HtmlCheckBoxInput pdfChkbx = page.getElementByName("pdfReportArtifact"); |
| 115 | + pdfChkbx.setChecked(true); |
| 116 | + Thread.sleep(2000); |
| 117 | + |
| 118 | + HtmlTextInput PDFFilePathInput=(HtmlTextInput) page.getElementByName("_.pdfReportFilePath"); |
| 119 | + Assert.assertEquals(TestData.getPropValues("pdftestreport.file.path"),PDFFilePathInput.getValueAttribute()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void verifyCoberturaFilePathInput() throws Exception { |
| 124 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 125 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 126 | + project.getBuildersList().add(this.testBuilder); |
| 127 | + |
| 128 | + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); |
| 129 | + HtmlCheckBoxInput coberturaChkBx = page.getElementByName("coberturaArtifact"); |
| 130 | + coberturaChkBx.setChecked(true); |
| 131 | + Thread.sleep(2000); |
| 132 | + |
| 133 | + HtmlTextInput coberturaCodeCoverageFileInput=(HtmlTextInput) page.getElementByName("_.coberturaReportFilePath"); |
| 134 | + Assert.assertEquals(TestData.getPropValues("cobertura.file.path"),coberturaCodeCoverageFileInput.getValueAttribute()); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + @Test |
| 139 | + public void verifyModelCoverageFilePathInput() throws Exception { |
| 140 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 141 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 142 | + project.getBuildersList().add(this.testBuilder); |
| 143 | + |
| 144 | + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); |
| 145 | + HtmlCheckBoxInput modelCoverageChkBx = page.getElementByName("modelCoverageArtifact"); |
| 146 | + modelCoverageChkBx.setChecked(true); |
| 147 | + Thread.sleep(2000); |
| 148 | + |
| 149 | + HtmlTextInput coberturaModelCoverageFileInput=(HtmlTextInput) page.getElementByName("_.modelCoverageFilePath"); |
| 150 | + Assert.assertEquals(TestData.getPropValues("modelcoverage.file.path"),coberturaModelCoverageFileInput.getValueAttribute()); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + @Test |
| 155 | + public void verifySTMResultsFilePathInput() throws Exception { |
| 156 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 157 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 158 | + project.getBuildersList().add(this.testBuilder); |
| 159 | + |
| 160 | + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); |
| 161 | + HtmlCheckBoxInput stmResultsChkBx = page.getElementByName("stmResultsArtifact"); |
| 162 | + stmResultsChkBx.setChecked(true); |
| 163 | + Thread.sleep(2000); |
| 164 | + |
| 165 | + HtmlTextInput STMRFilePathInput=(HtmlTextInput) page.getElementByName("_.stmResultsFilePath"); |
| 166 | + Assert.assertEquals(TestData.getPropValues("stmresults.file.path"),STMRFilePathInput.getValueAttribute()); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void verifyCustomFilePathInputForArtifacts() throws Exception{ |
| 171 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 172 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 173 | + |
| 174 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 175 | + |
| 176 | + |
| 177 | + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); |
| 178 | + // Adding list of source folder |
| 179 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 180 | + list.add(new SourceFolderPaths("src")); |
| 181 | + testingBuilder.setSourceFolder(new SourceFolder(list)); |
| 182 | + // Generate artifacts with custom inputs |
| 183 | + testingBuilder.setPdfReportArtifact(new RunMatlabTestsBuilder.PdfArtifact("TestArtifacts/pdfReport.pdf")); |
| 184 | + testingBuilder.setTapArtifact(new RunMatlabTestsBuilder.TapArtifact("TestArtifacts/tapResult.tap")); |
| 185 | + testingBuilder.setJunitArtifact(new RunMatlabTestsBuilder.JunitArtifact("TestArtifacts/junittestreport.xml")); |
| 186 | + testingBuilder.setStmResultsArtifact(new RunMatlabTestsBuilder.StmResultsArtifact("TestArtifacts/stmresult.mldatx")); |
| 187 | + testingBuilder.setCoberturaArtifact(new RunMatlabTestsBuilder.CoberturaArtifact("TestArtifacts/coberturaresult.xml")); |
| 188 | + testingBuilder.setModelCoverageArtifact(new RunMatlabTestsBuilder.ModelCovArtifact("TestArtifacts/mdlCovReport.xml")); |
| 189 | + |
| 190 | + project.getBuildersList().add(testingBuilder); |
| 191 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 192 | + |
| 193 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 194 | + jenkins.assertLogContains("TestArtifacts/stmresult.mldatx", build); |
| 195 | + jenkins.assertLogContains("TestArtifacts/mdlCovReport.xml", build); |
| 196 | + |
| 197 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/pdfReport.pdf").exists()); |
| 198 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/tapResult.tap").exists()); |
| 199 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/junittestreport.xml").exists()); |
| 200 | + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/coberturaresult.xml").exists()); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void verifyCustomeFilenamesForArtifactsPipeline() throws Exception { |
| 205 | + String script = "pipeline {\n" + |
| 206 | + " agent any\n" + |
| 207 | + MatlabRootSetup.getEnvironmentDSL() + "\n" + |
| 208 | + " stages{\n" + |
| 209 | + " stage('Run MATLAB Command') {\n" + |
| 210 | + " steps\n" + |
| 211 | + " {\n" + |
| 212 | + " unzip '" + MatlabRootSetup.getRunMATLABTestsData().getPath() + "'" + "\n" + |
| 213 | + " runMATLABTests(sourceFolder:['src'], testResultsTAP: 'test-results/results.tap',\n" + |
| 214 | + " testResultsJUnit: 'test-results/results.xml',\n" + |
| 215 | + " testResultsSimulinkTest: 'test-results/results.mldatx',\n" + |
| 216 | + " codeCoverageCobertura: 'code-coverage/coverage.xml',\n" + |
| 217 | + " modelCoverageCobertura: 'model-coverage/coverage.xml')\n" + |
| 218 | + " }\n" + |
| 219 | + " }\n" + |
| 220 | + " }\n" + |
| 221 | + "}"; |
| 222 | + WorkflowRun build = getPipelineBuild(script); |
| 223 | + jenkins.assertBuildStatus(Result.SUCCESS,build); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + public void verifyCOverageReportDoesNotIncludeOtherSourceFolder() throws Exception { |
| 228 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 229 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 230 | + |
| 231 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 232 | + |
| 233 | + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); |
| 234 | + testingBuilder.setCoberturaArtifact(new RunMatlabTestsBuilder.CoberturaArtifact("TestArtifacts/coberturaresult.xml")); |
| 235 | + project.getBuildersList().add(testingBuilder); |
| 236 | + |
| 237 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 238 | + |
| 239 | + String xmlString = xmlToString(build.getWorkspace() + "/TestArtifacts/coberturaresult.xml"); |
| 240 | + assertFalse(xmlString.contains("+scriptgen")); |
| 241 | + assertFalse(xmlString.contains("genscript")); |
| 242 | + assertFalse(xmlString.contains("runner_")); |
| 243 | + jenkins.assertLogContains("testSquare", build); |
| 244 | + jenkins.assertBuildStatus(Result.FAILURE,build); |
| 245 | + } |
| 246 | + |
| 247 | + private WorkflowRun getPipelineBuild(String script) throws Exception{ |
| 248 | + WorkflowJob project = jenkins.createProject(WorkflowJob.class); |
| 249 | + project.setDefinition(new CpsFlowDefinition(script,true)); |
| 250 | + return project.scheduleBuild2(0).get(); |
| 251 | + } |
| 252 | + |
| 253 | +} |
0 commit comments