|
| 1 | +package com.mathworks.ci.systemTests; |
| 2 | + |
| 3 | +import com.mathworks.ci.*; |
| 4 | +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; |
| 5 | +import hudson.matrix.AxisList; |
| 6 | +import hudson.matrix.MatrixBuild; |
| 7 | +import hudson.matrix.MatrixProject; |
| 8 | +import hudson.matrix.MatrixRun; |
| 9 | +import hudson.model.FreeStyleBuild; |
| 10 | +import hudson.model.FreeStyleProject; |
| 11 | +import hudson.model.Result; |
| 12 | +import hudson.slaves.DumbSlave; |
| 13 | +import org.htmlunit.html.*; |
| 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.JenkinsRule; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static junit.framework.TestCase.assertTrue; |
| 27 | + |
| 28 | +public class GlobalToolIT { |
| 29 | + private FreeStyleProject project; |
| 30 | + private RunMatlabCommandBuilder scriptBuilder; |
| 31 | + private UseMatlabVersionBuildWrapper buildWrapper; |
| 32 | + |
| 33 | + @Rule |
| 34 | + public Timeout timeout = Timeout.seconds(0); |
| 35 | + |
| 36 | + @Rule |
| 37 | + public JenkinsRule jenkins = new JenkinsRule(); |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void checkMatlabRoot() { |
| 41 | + // Check if the MATLAB_ROOT environment variable is defined |
| 42 | + String matlabRoot = System.getenv("MATLAB_ROOT"); |
| 43 | + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); |
| 44 | + } |
| 45 | + |
| 46 | + @Before |
| 47 | + public void testSetup() throws IOException { |
| 48 | + this.project = jenkins.createFreeStyleProject(); |
| 49 | + this.buildWrapper = new UseMatlabVersionBuildWrapper(); |
| 50 | + this.scriptBuilder = new RunMatlabCommandBuilder(); |
| 51 | + } |
| 52 | + |
| 53 | + @After |
| 54 | + public void testTearDown() { |
| 55 | + this.project = null; |
| 56 | + this.scriptBuilder = null; |
| 57 | + MatlabRootSetup.matlabInstDescriptor = null; |
| 58 | + this.buildWrapper = null; |
| 59 | + } |
| 60 | + |
| 61 | + public void useCommandFreeStyle(String command) { |
| 62 | + scriptBuilder.setMatlabCommand(TestData.getPropValues("matlab.command")); |
| 63 | + project.getBuildersList().add(scriptBuilder); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void verifyGlobalToolForFreeStyle() throws Exception{ |
| 68 | + // Adding the MATLAB Global tool |
| 69 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 70 | + |
| 71 | + // Selecting MATLAB that is defined as Global tool |
| 72 | + MatlabInstallation _inst = MatlabInstallation.getInstallation("MATLAB_PATH"); |
| 73 | + MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(_inst.getName(), null); |
| 74 | + buildWrapper.setMatlabBuildWrapperContent(content); |
| 75 | + project.getBuildWrappersList().add(buildWrapper); |
| 76 | + useCommandFreeStyle("version"); |
| 77 | + |
| 78 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 79 | + |
| 80 | + jenkins.assertLogContains(_inst.getHome(), build); |
| 81 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void verifyBuildFailsForIncorrectToolPath() throws Exception{ |
| 86 | + // Adding the MATLAB Global tool |
| 87 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH", "matlab/incorrect/path", jenkins); |
| 88 | + jenkins.configRoundtrip(); |
| 89 | + |
| 90 | + // Selecting MATLAB that is defined as Global tool |
| 91 | + MatlabInstallation _inst = MatlabInstallation.getInstallation("MATLAB_PATH"); |
| 92 | + MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(_inst.getName(), null); |
| 93 | + buildWrapper.setMatlabBuildWrapperContent(content); |
| 94 | + project.getBuildWrappersList().add(buildWrapper); |
| 95 | + |
| 96 | + scriptBuilder.setMatlabCommand(TestData.getPropValues("matlab.command")); |
| 97 | + project.getBuildersList().add(scriptBuilder); |
| 98 | + |
| 99 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 100 | + |
| 101 | + jenkins.assertBuildStatus(Result.FAILURE, build); |
| 102 | + jenkins.assertLogContains("Verify global tool configuration for the specified node.", build); |
| 103 | + } |
| 104 | + |
| 105 | + //Modify it to use the Custom Option |
| 106 | + |
| 107 | + public void verifyUseMATLABVersionBuildPasses() throws Exception { |
| 108 | + HtmlPage configurePage = jenkins.createWebClient().goTo("job/test0/configure"); |
| 109 | + HtmlTextInput matlabRoot =configurePage.getElementByName("_.matlabRootFolder"); |
| 110 | + matlabRoot.setValueAttribute(MatlabRootSetup.getMatlabRoot()); |
| 111 | + HtmlButton saveButton = (HtmlButton) configurePage.getElementByName("Submit").getFirstChild().getFirstChild(); |
| 112 | + RunMatlabCommandBuilder tester = |
| 113 | + new RunMatlabCommandBuilder(); |
| 114 | + tester.setMatlabCommand(TestData.getPropValues("matlab.command")); |
| 115 | + project.getBuildersList().add(tester); |
| 116 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 117 | + String build_log = jenkins.getLog(build); |
| 118 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void verifyAllToolsAreAvailable() throws Exception { |
| 123 | + // Adding the Global tools |
| 124 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_1", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 125 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_2", MatlabRootSetup.getMatlabRoot().replace(TestData.getPropValues("matlab.version"), "R2020a"), jenkins); |
| 126 | + jenkins.configRoundtrip(); |
| 127 | + // Getting the Web page for UI testing |
| 128 | + HtmlPage configurePage = jenkins.createWebClient().goTo("job/test0/configure"); |
| 129 | + HtmlCheckBoxInput matlabver=configurePage.getElementByName("com-mathworks-ci-UseMatlabVersionBuildWrapper"); |
| 130 | + matlabver.setChecked(true); |
| 131 | + Thread.sleep(2000); |
| 132 | + List<HtmlSelect> matlabver_1=configurePage.getByXPath("//select[contains(@class, \"dropdownList\")]"); |
| 133 | + // One of the drop downlists should have three options |
| 134 | + boolean allOptionsAvailable = false; |
| 135 | + for (HtmlSelect e : matlabver_1){ |
| 136 | + if(e.getOptionSize() == 3){ |
| 137 | + if(e.getOption(0).getValueAttribute().equals("MATLAB_PATH_1") && |
| 138 | + e.getOption(1).getValueAttribute().equals("MATLAB_PATH_2") && |
| 139 | + e.getOption(2).getValueAttribute().equals("Custom...")) { |
| 140 | + allOptionsAvailable = true; |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + assertTrue(allOptionsAvailable); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + @Test |
| 149 | + public void verifyNodes() throws Exception { |
| 150 | + DumbSlave slave = jenkins.createSlave("slave","Slave_machine",null); |
| 151 | +// project.setAssignedNode(slave); |
| 152 | + // Adding the MATLAB Global tool |
| 153 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 154 | + |
| 155 | + // Selecting MATLAB that is defined as Global tool |
| 156 | + MatlabInstallation _inst = MatlabInstallation.getInstallation("MATLAB_PATH"); |
| 157 | + MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(_inst.getName(), null); |
| 158 | + buildWrapper.setMatlabBuildWrapperContent(content); |
| 159 | + project.getBuildWrappersList().add(buildWrapper); |
| 160 | + useCommandFreeStyle("version"); |
| 161 | + |
| 162 | + FreeStyleBuild build = project.scheduleBuild2(0).get(); |
| 163 | + |
| 164 | + jenkins.assertLogContains(_inst.getHome(), build); |
| 165 | + String build_log = jenkins.getLog(build); |
| 166 | + |
| 167 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 168 | + } |
| 169 | + |
| 170 | + /* |
| 171 | + * Test to verify if Matrix build passes . |
| 172 | + */ |
| 173 | + |
| 174 | + @Test |
| 175 | + public void verifyToolMatrixPreference() throws Exception { |
| 176 | + List<String> list= new ArrayList<>(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); |
| 177 | + MatlabInstallationAxis axis = new MatlabInstallationAxis(list); |
| 178 | + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);; |
| 179 | + matrixProject.setAxes(new AxisList(axis)); |
| 180 | + |
| 181 | + String matlabRoot = MatlabRootSetup.getMatlabRoot(); |
| 182 | + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), matlabRoot)); |
| 183 | + matrixProject.getBuildWrappersList().add(buildWrapper); |
| 184 | + |
| 185 | + scriptBuilder.setMatlabCommand("disp 'apple'"); |
| 186 | + matrixProject.getBuildersList().add(scriptBuilder); |
| 187 | + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); |
| 188 | + List<MatrixRun> runs = build.getRuns(); |
| 189 | + // TODO: add more relevant verification statement |
| 190 | + for (MatrixRun run : runs) { |
| 191 | + jenkins.assertLogContains("apple", run); |
| 192 | + } |
| 193 | + jenkins.assertBuildStatus(Result.SUCCESS,build); |
| 194 | + } |
| 195 | + |
| 196 | + // Verify "Use MATLAB Version" takes precedence over Global tools in Matrix Project |
| 197 | + @Test |
| 198 | + public void verifyGlobalToolMatrix() throws Exception { |
| 199 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_1", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 200 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_2", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 201 | + |
| 202 | + List<String> list= new ArrayList<>(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_2")); |
| 203 | + MatlabInstallationAxis axis = new MatlabInstallationAxis(list); |
| 204 | + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);; |
| 205 | + matrixProject.setAxes(new AxisList(axis)); |
| 206 | + |
| 207 | + scriptBuilder.setMatlabCommand("version"); |
| 208 | + matrixProject.getBuildersList().add(scriptBuilder); |
| 209 | + System.out.println("STARTING BUILD"); |
| 210 | + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); |
| 211 | + System.out.println("SCHEDULED BUILD"); |
| 212 | + List<MatrixRun> runs = build.getRuns(); |
| 213 | + System.out.println("RAN BUILD"); |
| 214 | + // ToDo: Add more relevant verification statement |
| 215 | + for (MatrixRun run : runs) { |
| 216 | + String matlabName = run.getBuildVariables().get("MATLAB"); |
| 217 | + Assert.assertTrue(matlabName.equalsIgnoreCase("MATLAB_PATH_1") || matlabName.equalsIgnoreCase("MATLAB_PATH_2")); |
| 218 | + } |
| 219 | + jenkins.assertBuildStatus(Result.SUCCESS,build); |
| 220 | + } |
| 221 | + |
| 222 | + // Tool config |
| 223 | + @Test |
| 224 | + public void verifyGlobalToolDSLPipeline() throws Exception { |
| 225 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_1", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 226 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_2", MatlabRootSetup.getMatlabRoot().replace("R2020b", "R2020a"), jenkins); |
| 227 | + String script = "pipeline {\n" + |
| 228 | + " agent any\n" + |
| 229 | + MatlabRootSetup.getEnvironmentDSL() + "\n" + |
| 230 | + " tools {\n" + |
| 231 | + " matlab 'MATLAB_PATH_1'\n" + |
| 232 | + " }\n" + |
| 233 | + " stages{\n" + |
| 234 | + " stage('Run MATLAB Command') {\n" + |
| 235 | + " steps\n" + |
| 236 | + " {\n" + |
| 237 | + " runMATLABCommand 'version'\n" + |
| 238 | + " } \n" + |
| 239 | + " } \n" + |
| 240 | + " } \n" + |
| 241 | + "}"; |
| 242 | + WorkflowRun build = getPipelineBuild(script); |
| 243 | + jenkins.assertBuildStatus(Result.SUCCESS,build); |
| 244 | + } |
| 245 | + @Test |
| 246 | + public void verifyGlobalToolScriptedPipeline() throws Exception { |
| 247 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_1", MatlabRootSetup.getMatlabRoot(), jenkins); |
| 248 | + MatlabRootSetup.setMatlabInstallation("MATLAB_PATH_2", MatlabRootSetup.getMatlabRoot().replace("R2020b", "R2020a"), jenkins); |
| 249 | + String script = "node {\n" + |
| 250 | + " def matlabver\n" + |
| 251 | + " stage('Run MATLAB Command') {\n" + |
| 252 | + " matlabver = tool 'MATLAB_PATH_1'\n" + |
| 253 | + " if (isUnix()){\n" + |
| 254 | + " env.PATH = \"${matlabver}/bin:${env.PATH}\" // Linux or macOS agent\n" + |
| 255 | + " }else{\n" + |
| 256 | + " env.PATH = \"${matlabver}\\\\bin;${env.PATH}\" // Windows agent\n" + |
| 257 | + " } \n" + |
| 258 | + " runMATLABCommand 'version'\n" + |
| 259 | + " }\n" + |
| 260 | + "}"; |
| 261 | + WorkflowRun build = getPipelineBuild(script); |
| 262 | + jenkins.assertBuildStatus(Result.SUCCESS, build); |
| 263 | + } |
| 264 | + |
| 265 | + private WorkflowRun getPipelineBuild(String script) throws Exception{ |
| 266 | + WorkflowJob project = jenkins.createProject(WorkflowJob.class); |
| 267 | + project.setDefinition(new CpsFlowDefinition(script,true)); |
| 268 | + return project.scheduleBuild2(0).get(); |
| 269 | + } |
| 270 | + |
| 271 | +} |
0 commit comments