|
| 1 | +package com.mathworks.ci.systemTests; |
| 2 | + |
| 3 | +import com.mathworks.ci.*; |
| 4 | +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; |
| 5 | +import com.mathworks.ci.freestyle.options.SelectByFolder; |
| 6 | +import com.mathworks.ci.freestyle.options.SourceFolder; |
| 7 | +import com.mathworks.ci.freestyle.options.SourceFolderPaths; |
| 8 | +import com.mathworks.ci.freestyle.options.TestFolders; |
| 9 | +import hudson.matrix.*; |
| 10 | +import hudson.model.FreeStyleBuild; |
| 11 | +import hudson.model.FreeStyleProject; |
| 12 | +import hudson.model.Result; |
| 13 | +import hudson.tasks.Builder; |
| 14 | +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
| 15 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 16 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 17 | +import org.junit.*; |
| 18 | +import org.junit.rules.Timeout; |
| 19 | +import org.jvnet.hudson.test.ExtractResourceSCM; |
| 20 | +import org.jvnet.hudson.test.JenkinsRule; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | +import java.util.*; |
| 25 | + |
| 26 | +public class RunMATLABTestsIT { |
| 27 | + private FreeStyleProject project; |
| 28 | + private UseMatlabVersionBuildWrapper buildWrapper; |
| 29 | + private RunMatlabTestsBuilder testBuilder; |
| 30 | + |
| 31 | + @Rule |
| 32 | + public Timeout timeout = Timeout.seconds(0); |
| 33 | + |
| 34 | + @Rule |
| 35 | + public JenkinsRule jenkins = new JenkinsRule(); |
| 36 | + |
| 37 | + @BeforeClass |
| 38 | + public static void checkMatlabRoot() { |
| 39 | + // Check if the MATLAB_ROOT environment variable is defined |
| 40 | + String matlabRoot = System.getenv("MATLAB_ROOT"); |
| 41 | + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); |
| 42 | + } |
| 43 | + |
| 44 | + @Before |
| 45 | + public void testSetup() throws IOException { |
| 46 | + this.project = jenkins.createFreeStyleProject(); |
| 47 | + this.testBuilder = new RunMatlabTestsBuilder(); |
| 48 | + this.buildWrapper = new UseMatlabVersionBuildWrapper(); |
| 49 | + testBuilder.setLoggingLevel("default"); |
| 50 | + testBuilder.setOutputDetail("default"); |
| 51 | + } |
| 52 | + |
| 53 | + @After |
| 54 | + public void testTearDown() { |
| 55 | + this.project = null; |
| 56 | + this.testBuilder = null; |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void verifyBuildStepWithMatlabTestBuilder() throws Exception { |
| 61 | + boolean found = false; |
| 62 | + project.getBuildersList().add(testBuilder); |
| 63 | + List<Builder> bl = project.getBuildersList(); |
| 64 | + for (Builder b : bl) { |
| 65 | + if (b.getDescriptor().getDisplayName() |
| 66 | + .equalsIgnoreCase(Message.getBuilderDisplayName())) { |
| 67 | + found = true; |
| 68 | + } |
| 69 | + } |
| 70 | + Assert.assertTrue("Build step does not contain Run MATLAB Tests option", found); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void verifyRunMATLABTestsWithAllInputs() throws Exception { |
| 75 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 76 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 77 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 78 | + |
| 79 | + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); |
| 80 | + testingBuilder.setLoggingLevel("None"); |
| 81 | + testingBuilder.setOutputDetail("None"); |
| 82 | + |
| 83 | + // Adding list of source folder |
| 84 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 85 | + list.add(new SourceFolderPaths("src")); |
| 86 | + testingBuilder.setSourceFolder(new SourceFolder(list)); |
| 87 | + |
| 88 | + // Adding list of test folder |
| 89 | + List<TestFolders> testFolders = new ArrayList<TestFolders>(); |
| 90 | + testFolders.add(new TestFolders("test/TestSquare")); |
| 91 | + testingBuilder.setSelectByFolder(new SelectByFolder(testFolders)); |
| 92 | + |
| 93 | + //Adding test tag |
| 94 | + testingBuilder.setSelectByTag(new RunMatlabTestsBuilder.SelectByTag("TestTag")); |
| 95 | + project.getBuildersList().add(testingBuilder); |
| 96 | + |
| 97 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 98 | + |
| 99 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 100 | + jenkins.assertLogContains("addpath(genpath('src'));", build); |
| 101 | + // Based on the test folder and tag, 'testSquare' test should be run |
| 102 | + jenkins.assertLogContains("testSquare", build); |
| 103 | + jenkins.assertLogContains("HasTag('TestTag')", build); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void verifyMultipleSourceFolders() throws Exception { |
| 108 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 109 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 110 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 111 | + |
| 112 | + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); |
| 113 | + testingBuilder.setLoggingLevel("None"); |
| 114 | + testingBuilder.setOutputDetail("None"); |
| 115 | + |
| 116 | + // Adding list of source folder |
| 117 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 118 | + list.add(new SourceFolderPaths("src")); |
| 119 | + list.add(new SourceFolderPaths("src1")); |
| 120 | + testingBuilder.setSourceFolder(new SourceFolder(list)); |
| 121 | + |
| 122 | + project.getBuildersList().add(testingBuilder); |
| 123 | + |
| 124 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 125 | + |
| 126 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 127 | + jenkins.assertLogContains("addpath(genpath('src'));", build); |
| 128 | + jenkins.assertLogContains("addpath(genpath('src1'));", build); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void verifyMultipleTestFolders() throws Exception { |
| 133 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), MatlabRootSetup.getMatlabRoot())); |
| 134 | + project.getBuildWrappersList().add(this.buildWrapper); |
| 135 | + project.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 136 | + |
| 137 | + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); |
| 138 | +// testingBuilder.setLoggingLevel("None"); |
| 139 | +// testingBuilder.setOutputDetail("None"); |
| 140 | + |
| 141 | + // Adding list of source folder |
| 142 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 143 | + list.add(new SourceFolderPaths("src")); |
| 144 | + testingBuilder.setSourceFolder(new SourceFolder(list)); |
| 145 | + |
| 146 | + // Adding list of test folder |
| 147 | + List<TestFolders> testFolders = new ArrayList<TestFolders>(); |
| 148 | + testFolders.add(new TestFolders("test/TestSquare")); |
| 149 | + testFolders.add(new TestFolders("test/TestMultiply")); |
| 150 | + testingBuilder.setSelectByFolder(new SelectByFolder(testFolders)); |
| 151 | + |
| 152 | + project.getBuildersList().add(testingBuilder); |
| 153 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 154 | + |
| 155 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 156 | + // Based on the test folder, 'testSquare' and 'testMultiply' tests should be run |
| 157 | + jenkins.assertLogContains("testSquare", build); |
| 158 | + jenkins.assertLogContains("testMultiply", build); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void verifyTestsRunInMatrixProject() throws Exception { |
| 163 | + String matlabRoot = System.getenv("MATLAB_ROOT"); |
| 164 | + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); |
| 165 | + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); |
| 166 | + |
| 167 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); |
| 168 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_22b", matlabRoot22b, jenkins); |
| 169 | + |
| 170 | + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); |
| 171 | + MatlabInstallationAxis MATLABAxis = new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); |
| 172 | + matrixProject.setAxes(new AxisList(MATLABAxis)); |
| 173 | + matrixProject.setScm(new ExtractResourceSCM(MatlabRootSetup.getRunMATLABTestsData())); |
| 174 | + |
| 175 | + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); |
| 176 | + |
| 177 | + // Adding list of source folder |
| 178 | + List<SourceFolderPaths> list=new ArrayList<SourceFolderPaths>(); |
| 179 | + list.add(new SourceFolderPaths("src")); |
| 180 | + testingBuilder.setSourceFolder(new SourceFolder(list)); |
| 181 | + |
| 182 | + // Adding list of test folder |
| 183 | + List<TestFolders> testFolders = new ArrayList<TestFolders>(); |
| 184 | + testFolders.add(new TestFolders("test/TestSquare")); |
| 185 | + testFolders.add(new TestFolders("test/TestMultiply")); |
| 186 | + testingBuilder.setSelectByFolder(new SelectByFolder(testFolders)); |
| 187 | + |
| 188 | + //Adding test tag |
| 189 | + testingBuilder.setSelectByTag(new RunMatlabTestsBuilder.SelectByTag("TestTag")); |
| 190 | + |
| 191 | + matrixProject.getBuildersList().add(testingBuilder); |
| 192 | + |
| 193 | + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); |
| 194 | + |
| 195 | + Combination c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1"))), "MATLAB_PATH_1"); |
| 196 | + MatrixRun run = build.getRun(c); |
| 197 | + jenkins.assertLogContains("Running testMultiply",run); |
| 198 | + jenkins.assertLogContains("Running testSquare", run); |
| 199 | + jenkins.assertLogNotContains("Running squareTest", run); |
| 200 | + jenkins.assertBuildStatus(Result.SUCCESS,run); |
| 201 | + |
| 202 | + c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_22b"))), "MATLAB_PATH_22b"); |
| 203 | + run = build.getRun(c); |
| 204 | + jenkins.assertLogContains("Running testMultiply",run); |
| 205 | + jenkins.assertLogContains("Running testSquare", run); |
| 206 | + jenkins.assertLogNotContains("Running squareTest", run); |
| 207 | + jenkins.assertBuildStatus(Result.SUCCESS,run); |
| 208 | + jenkins.assertLogContains(matlabRoot22b,run); |
| 209 | + |
| 210 | + jenkins.assertBuildStatus(Result.SUCCESS, run); |
| 211 | + } |
| 212 | + |
| 213 | + /* |
| 214 | + * Test to verify if tests are filtered scripted pipeline |
| 215 | + */ |
| 216 | + @Test |
| 217 | + public void verifyTestsAreFiltered() throws Exception{ |
| 218 | + String script = "node {\n" + |
| 219 | + MatlabRootSetup.getEnvironmentScriptedPipeline() + "\n" + |
| 220 | + addTestData()+"\n" + |
| 221 | + "runMATLABTests(sourceFolder:['src'], selectByFolder: ['test/TestMultiply', 'test/TestSquare'], selectByTag: 'TestTag')\n" + |
| 222 | + "}"; |
| 223 | + WorkflowRun build = getPipelineBuild(script); |
| 224 | + jenkins.assertLogContains("Running testMultiply",build); |
| 225 | + jenkins.assertLogContains("Running testSquare", build); |
| 226 | + jenkins.assertLogNotContains("Running squareTest", build); |
| 227 | + jenkins.assertBuildStatus(Result.SUCCESS,build); |
| 228 | + } |
| 229 | + |
| 230 | + /* |
| 231 | + * Test to verify if tests are filtered DSL pipeline |
| 232 | + */ |
| 233 | + @Test |
| 234 | + public void verifyTestsAreFilteredDSL() throws Exception{ |
| 235 | + String script = "pipeline {\n" + |
| 236 | + "agent any" + "\n" + |
| 237 | + MatlabRootSetup.getEnvironmentDSL() + "\n" + |
| 238 | + "stages{" + "\n" + |
| 239 | + "stage('Run MATLAB Command') {\n" + |
| 240 | + "steps\n" + |
| 241 | + "{"+ |
| 242 | + addTestData()+"\n" + |
| 243 | + "runMATLABTests(sourceFolder:['src'], selectByFolder: ['test/TestMultiply', 'test/TestSquare'], selectByTag: 'TestTag')\n" + |
| 244 | + "}" + "\n" + |
| 245 | + "}" + "\n" + |
| 246 | + "}" + "\n" + |
| 247 | + "}"; |
| 248 | + WorkflowRun build = getPipelineBuild(script); |
| 249 | + jenkins.assertLogContains("Running testMultiply",build); |
| 250 | + jenkins.assertLogContains("Running testSquare", build); |
| 251 | + jenkins.assertLogNotContains("Running squareTest", build); |
| 252 | + jenkins.assertBuildStatus(Result.SUCCESS,build); |
| 253 | + } |
| 254 | + private WorkflowRun getPipelineBuild(String script) throws Exception{ |
| 255 | + WorkflowJob project = jenkins.createProject(WorkflowJob.class); |
| 256 | + project.setDefinition(new CpsFlowDefinition(script,true)); |
| 257 | + return project.scheduleBuild2(0).get(); |
| 258 | + } |
| 259 | + |
| 260 | + private String addTestData() throws MalformedURLException { |
| 261 | + URL zipFile = MatlabRootSetup.getRunMATLABTestsData(); |
| 262 | + String path = " unzip '" + zipFile.getPath() + "'" + "\n"; |
| 263 | + return path; |
| 264 | + } |
| 265 | +} |
0 commit comments