diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..7d121297 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,29 @@ +name: using mpm to install MATLAB +on: push +jobs: + run-tests-job: + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + name: Run System tests + runs-on: ${{ matrix.os }} + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Setup Java 11 + uses: actions/setup-java@v4 + with: + java-version: '11' + distribution: 'temurin' + cache: maven + + - name: Setup MATLAB + id: setup-matlab + uses: matlab-actions/setup-matlab@v2 + + - name: Running tests + run: mvn --batch-mode verify + env: + MATLAB_ROOT: ${{ steps.setup-matlab.outputs.matlabroot }} + MLM_LICENSE_TOKEN: ${{ secrets.MLM_LICENSE_TOKEN}} \ No newline at end of file diff --git a/azure-pipelines.yml b/azure-pipelines.yml deleted file mode 100644 index afd10b7a..00000000 --- a/azure-pipelines.yml +++ /dev/null @@ -1,37 +0,0 @@ -# Maven -# Build your Java project and run tests with Apache Maven. -# Add steps that analyze code, save build artifacts, deploy, and more: -# https://docs.microsoft.com/azure/devops/pipelines/languages/java - -strategy: - matrix: - linux: - imageName: 'ubuntu-latest' - mac: - imageName: 'macOS-latest' - windows: - imageName: 'windows-latest' - -pool: - vmImage: $(imageName) - -trigger: - - master - -steps: - - task: JavaToolInstaller@0 - inputs: - versionSpec: '11' - jdkArchitectureOption: 'x64' - jdkSourceOption: 'PreInstalled' - - - task: Maven@4 - inputs: - mavenPomFile: 'pom.xml' - mavenOptions: '-Xmx3072m -Dmaven.javadoc.skip=true' - publishJUnitResults: true - testResultsFiles: '**/surefire-reports/TEST-*.xml' - codeCoverageToolOption: 'JaCoCo' - goals: 'verify' - displayName: 'Build $(imageName)' - diff --git a/pom.xml b/pom.xml index d32a73b5..e34d586d 100644 --- a/pom.xml +++ b/pom.xml @@ -125,6 +125,38 @@ 11.0.24 test + + + + + com.jcabi + jcabi-xml + 0.23.1 + test + + + + org.jenkinsci.plugins + pipeline-model-definition + test + + + + + org.jenkins-ci.plugins + pipeline-utility-steps + 2.7.1 + test + + + + + org.jenkins-ci.plugins + cobertura + 1.16 + test + + @@ -215,6 +247,50 @@ + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + + integration-tests + + integration-test + + + + **/*IT.java + + + **/*Test.java + + + + + + failsafe-verify + verify + + verify + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/*Test.java + **/*Tester.java + + + **/*IT.java> + + + + diff --git a/src/test/java/integ/com/mathworks/ci/BuildArtifactActionTest.java b/src/test/java/com/mathworks/ci/BuildArtifactActionTest.java similarity index 100% rename from src/test/java/integ/com/mathworks/ci/BuildArtifactActionTest.java rename to src/test/java/com/mathworks/ci/BuildArtifactActionTest.java diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabTestBuilderPersistenceTest.java b/src/test/java/com/mathworks/ci/RunMatlabTestBuilderPersistenceTest.java similarity index 100% rename from src/test/java/integ/com/mathworks/ci/RunMatlabTestBuilderPersistenceTest.java rename to src/test/java/com/mathworks/ci/RunMatlabTestBuilderPersistenceTest.java diff --git a/src/test/java/integ/com/mathworks/ci/TestMessage.java b/src/test/java/com/mathworks/ci/TestMessage.java similarity index 100% rename from src/test/java/integ/com/mathworks/ci/TestMessage.java rename to src/test/java/com/mathworks/ci/TestMessage.java diff --git a/src/test/java/integ/com/mathworks/ci/TestResultsViewActionTest.java b/src/test/java/com/mathworks/ci/TestResultsViewActionTest.java similarity index 98% rename from src/test/java/integ/com/mathworks/ci/TestResultsViewActionTest.java rename to src/test/java/com/mathworks/ci/TestResultsViewActionTest.java index e6c9f5ba..26c3f07b 100644 --- a/src/test/java/integ/com/mathworks/ci/TestResultsViewActionTest.java +++ b/src/test/java/com/mathworks/ci/TestResultsViewActionTest.java @@ -1,442 +1,442 @@ -package com.mathworks.ci; - -/** - * Copyright 2025 The MathWorks, Inc. - * - */ - -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.math.BigDecimal; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.List; -import java.util.Optional; -import java.util.concurrent.ExecutionException; - -import org.json.simple.parser.ParseException; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; - -import com.mathworks.ci.MatlabBuildWrapperContent; -import com.mathworks.ci.MatlabBuilderConstants; -import com.mathworks.ci.MatlabTestDiagnostics; -import com.mathworks.ci.MatlabTestFile; -import com.mathworks.ci.TestResultsViewAction; -import com.mathworks.ci.TestResultsViewAction.*; -import com.mathworks.ci.UseMatlabVersionBuildWrapper; -import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; - -import hudson.FilePath; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; - -public class TestResultsViewActionTest { - private FreeStyleProject project; - private UseMatlabVersionBuildWrapper buildWrapper; - private RunMatlabBuildBuilder scriptBuilder; - - private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; - - public TestResultsViewActionTest() {} - - @Rule - public JenkinsRule jenkins = new JenkinsRule(); - - @Before - public void testSetup() throws IOException { - this.project = jenkins.createFreeStyleProject(); - this.scriptBuilder = new RunMatlabBuildBuilder(); - this.buildWrapper = new UseMatlabVersionBuildWrapper(); - } - - @After - public void testTearDown() { - this.project = null; - this.scriptBuilder = null; - } - - private String getMatlabroot(String version) throws URISyntaxException { - String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; - String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE; - URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)) - .orElseGet(() -> getResource(defaultVersionInfo)); - File matlabRoot = new File(matlabRootURL.toURI()); - return matlabRoot.getAbsolutePath().replace(File.separator + VERSION_INFO_XML_FILE, "") - .replace("R2017a", version); - } - - private URL getResource(String resource) { - return TestResultsViewAction.class.getClassLoader().getResource(resource); - } - - /** - * Verify if all test results are returned from artifact - * - */ - - @Test - public void verifyAllTestsReturned() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - int actualTestSessions = ta.size(); - Assert.assertEquals("Incorrect test sessions",2,actualTestSessions); - int actualTestFiles1 = ta.get(0).size(); - Assert.assertEquals("Incorrect test files",1,actualTestFiles1); - int actualTestFiles2 = ta.get(1).size(); - Assert.assertEquals("Incorrect test files",1,actualTestFiles2); - int actualTestResults1 = ta.get(0).get(0).getMatlabTestCases().size(); - Assert.assertEquals("Incorrect test results",9,actualTestResults1); - int actualTestResults2 = ta.get(1).get(0).getMatlabTestCases().size(); - Assert.assertEquals("Incorrect test results",1,actualTestResults2); - } - - /** - * Verify if total test results count is correct - * - */ - - @Test - public void verifyTotalTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - int actualCount = ac.getTotalCount(); - Assert.assertEquals("Incorrect total tests count",10,actualCount); - } - - /** - * Verify if passed tests count is correct - * - */ - - @Test - public void verifyPassedTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - int actualCount = ac.getPassedCount(); - Assert.assertEquals("Incorrect passed tests count",4,actualCount); - } - - /** - * Verify if failed tests count is correct - * - */ - - @Test - public void verifyFailedTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - int actualCount = ac.getFailedCount(); - Assert.assertEquals("Incorrect failed tests count",3,actualCount); - } - - /** - * Verify if incomplete tests count is correct - * - */ - - @Test - public void verifyIncompleteTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - int actualCount = ac.getIncompleteCount(); - Assert.assertEquals("Incorrect incomplete tests count",2,actualCount); - } - - /** - * Verify if not run tests count is correct - * - */ - - @Test - public void verifyNotRunTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:\\workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - int actualCount = ac.getNotRunCount(); - Assert.assertEquals("Incorrect not run tests count",1,actualCount); - } - - /** - * Verify if test file path is correct - * - */ - - @Test - public void verifyMatlabTestFilePath() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - - String os = System.getProperty("os.name").toLowerCase(); - String testFolder = "testArtifacts/t1/"; - String workspaceParent = ""; - String expectedParentPath = ""; - if (os.contains("win")) { - testFolder += "windows/"; - workspaceParent = "C:\\"; - expectedParentPath = "workspace\\visualization\\"; - } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) { - testFolder += "linux/"; - workspaceParent = "/home/user/"; - expectedParentPath = "workspace/visualization/"; - } else if (os.contains("mac")) { - testFolder += "mac/"; - workspaceParent = "/Users/username/"; - expectedParentPath = "workspace/visualization/"; - } else { - throw new RuntimeException("Unsupported OS: " + os); - } - copyFileInWorkspace(testFolder + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - final FilePath workspace = new FilePath(new File(workspaceParent + "workspace")); - - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - String actualPath1 = ta.get(0).get(0).getPath(); - Assert.assertEquals("Incorrect test file path",expectedParentPath + "tests",actualPath1); - String actualPath2 = ta.get(1).get(0).getPath(); - Assert.assertEquals("Incorrect test file path",expectedParentPath + "duplicate_tests",actualPath2); - } - - /** - * Verify if test file name is correct - * - */ - - @Test - public void verifyMatlabTestFileName() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - String actualName1 = ta.get(0).get(0).getName(); - Assert.assertEquals("Incorrect test file name","TestExamples1",actualName1); - String actualName2 = ta.get(1).get(0).getName(); - Assert.assertEquals("Incorrect test file name","TestExamples2",actualName2); - } - - /** - * Verify if test file duration is correct - * - */ - - @Test - public void verifyMatlabTestFileDuration() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - BigDecimal actualDuration1 = ta.get(0).get(0).getDuration(); - Assert.assertEquals("Incorrect test file duration",new BigDecimal("1.7"),actualDuration1); - BigDecimal actualDuration2 = ta.get(1).get(0).getDuration(); - Assert.assertEquals("Incorrect test file duration",new BigDecimal("0.1"),actualDuration2); - } - - /** - * Verify if test file status is correct - * - */ - - @Test - public void verifyMatlabTestFileStatus() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - TestStatus actualStatus1 = ta.get(0).get(0).getStatus(); - Assert.assertEquals("Incorrect test file status",TestStatus.FAILED,actualStatus1); - TestStatus actualStatus2 = ta.get(1).get(0).getStatus(); - Assert.assertEquals("Incorrect test file status",TestStatus.INCOMPLETE,actualStatus2); - } - - /** - * Verify if test case name is correct - * - */ - - @Test - public void verifyMatlabTestCaseName() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - String actualName1_1 = ta.get(0).get(0).getMatlabTestCases().get(0).getName(); - Assert.assertEquals("Incorrect test case name","testNonLeapYear",actualName1_1); - String actualName1_5 = ta.get(0).get(0).getMatlabTestCases().get(4).getName(); - Assert.assertEquals("Incorrect test case name","testLeapYear",actualName1_5); - String actualName1_8 = ta.get(0).get(0).getMatlabTestCases().get(7).getName(); - Assert.assertEquals("Incorrect test case name","testValidDateFormat",actualName1_8); - String actualName1_9 = ta.get(0).get(0).getMatlabTestCases().get(8).getName(); - Assert.assertEquals("Incorrect test case name","testInvalidDateFormat",actualName1_9); - String actualName2 = ta.get(1).get(0).getMatlabTestCases().get(0).getName(); - Assert.assertEquals("Incorrect test case name","testNonLeapYear",actualName2); - } - - /** - * Verify if test case status is correct - * - */ - - @Test - public void verifyMatlabTestCaseStatus() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - TestStatus actualStatus1_1 = ta.get(0).get(0).getMatlabTestCases().get(0).getStatus(); - Assert.assertEquals("Incorrect test case status",TestStatus.PASSED,actualStatus1_1); - TestStatus actualStatus1_5 = ta.get(0).get(0).getMatlabTestCases().get(4).getStatus(); - Assert.assertEquals("Incorrect test case status",TestStatus.FAILED,actualStatus1_5); - TestStatus actualStatus1_9 = ta.get(0).get(0).getMatlabTestCases().get(8).getStatus(); - Assert.assertEquals("Incorrect test case status",TestStatus.NOT_RUN,actualStatus1_9); - TestStatus actualStatus2 = ta.get(1).get(0).getMatlabTestCases().get(0).getStatus(); - Assert.assertEquals("Incorrect test case status",TestStatus.INCOMPLETE,actualStatus2); - } - - /** - * Verify if test case duration is correct - * - */ - - @Test - public void verifyMatlabTestCaseDuration() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - BigDecimal actualDuration1_1 = ta.get(0).get(0).getMatlabTestCases().get(0).getDuration(); - Assert.assertEquals("Incorrect test case duration",new BigDecimal("0.1"),actualDuration1_1); - BigDecimal actualDuration1_5 = ta.get(0).get(0).getMatlabTestCases().get(4).getDuration(); - Assert.assertEquals("Incorrect test case duration",new BigDecimal("0.4"),actualDuration1_5); - BigDecimal actualDuration1_9 = ta.get(0).get(0).getMatlabTestCases().get(8).getDuration(); - Assert.assertEquals("Incorrect test case duration",new BigDecimal("0"),actualDuration1_9); - BigDecimal actualDuration2 = ta.get(1).get(0).getMatlabTestCases().get(0).getDuration(); - Assert.assertEquals("Incorrect test case duration",new BigDecimal("0.1"),actualDuration2); - } - - /** - * Verify if test case diagnostics is correct - * - */ - - @Test - public void verifyMatlabTestCaseDiagnostics() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - List> ta = ac.getTestResults(); - - MatlabTestDiagnostics diagnostics1 = ta.get(0).get(0).getMatlabTestCases().get(4).getDiagnostics().get(0); - String actualDiagnosticsEvent1 = diagnostics1.getEvent(); - Assert.assertEquals("Incorrect test diagnostics event","SampleDiagnosticsEvent1",actualDiagnosticsEvent1); - String actualDiagnosticsReport1 = diagnostics1.getReport(); - Assert.assertEquals("Incorrect test diagnostics report","SampleDiagnosticsReport1",actualDiagnosticsReport1); - - MatlabTestDiagnostics diagnostics2 = ta.get(1).get(0).getMatlabTestCases().get(0).getDiagnostics().get(0); - String actualDiagnosticsEvent2 = diagnostics2.getEvent(); - Assert.assertEquals("Incorrect test diagnostics event","SampleDiagnosticsEvent2",actualDiagnosticsEvent2); - String actualDiagnosticsReport2 = diagnostics2.getReport(); - Assert.assertEquals("Incorrect test diagnostics report","SampleDiagnosticsReport2",actualDiagnosticsReport2); - } - - /** - * Verify if actionID is set correctly - * - */ - - @Test - public void verifyActionID() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { - FreeStyleBuild build = getFreestyleBuild(); - final FilePath workspace = new FilePath(new File("C:", "workspace")); - final String actionID = "abc123"; - final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; - FilePath artifactRoot = new FilePath(build.getRootDir()); - copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); - TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); - String actualActionID = ac.getActionID(); - Assert.assertEquals("Incorrect action ID",actionID,actualActionID); - } - - private void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace) - throws IOException, InterruptedException { - final ClassLoader classLoader = getClass().getClassLoader(); - FilePath targetFilePath = new FilePath(targetWorkspace, targetFile); - InputStream in = classLoader.getResourceAsStream(sourceFile); - targetFilePath.copyFrom(in); - // set executable permission - targetFilePath.chmod(0777); - } - - private FreeStyleBuild getFreestyleBuild() throws ExecutionException, InterruptedException, URISyntaxException { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - return build; - } -} +package com.mathworks.ci; + +/** + * Copyright 2025 The MathWorks, Inc. + * + */ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +import org.json.simple.parser.ParseException; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import com.mathworks.ci.MatlabBuildWrapperContent; +import com.mathworks.ci.MatlabBuilderConstants; +import com.mathworks.ci.MatlabTestDiagnostics; +import com.mathworks.ci.MatlabTestFile; +import com.mathworks.ci.TestResultsViewAction; +import com.mathworks.ci.TestResultsViewAction.*; +import com.mathworks.ci.UseMatlabVersionBuildWrapper; +import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; + +import hudson.FilePath; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; + +public class TestResultsViewActionTest { + private FreeStyleProject project; + private UseMatlabVersionBuildWrapper buildWrapper; + private RunMatlabBuildBuilder scriptBuilder; + + private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; + + public TestResultsViewActionTest() {} + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.scriptBuilder = new RunMatlabBuildBuilder(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + } + + @After + public void testTearDown() { + this.project = null; + this.scriptBuilder = null; + } + + private String getMatlabroot(String version) throws URISyntaxException { + String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; + String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE; + URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)) + .orElseGet(() -> getResource(defaultVersionInfo)); + File matlabRoot = new File(matlabRootURL.toURI()); + return matlabRoot.getAbsolutePath().replace(File.separator + VERSION_INFO_XML_FILE, "") + .replace("R2017a", version); + } + + private URL getResource(String resource) { + return TestResultsViewAction.class.getClassLoader().getResource(resource); + } + + /** + * Verify if all test results are returned from artifact + * + */ + + @Test + public void verifyAllTestsReturned() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + int actualTestSessions = ta.size(); + Assert.assertEquals("Incorrect test sessions",2,actualTestSessions); + int actualTestFiles1 = ta.get(0).size(); + Assert.assertEquals("Incorrect test files",1,actualTestFiles1); + int actualTestFiles2 = ta.get(1).size(); + Assert.assertEquals("Incorrect test files",1,actualTestFiles2); + int actualTestResults1 = ta.get(0).get(0).getMatlabTestCases().size(); + Assert.assertEquals("Incorrect test results",9,actualTestResults1); + int actualTestResults2 = ta.get(1).get(0).getMatlabTestCases().size(); + Assert.assertEquals("Incorrect test results",1,actualTestResults2); + } + + /** + * Verify if total test results count is correct + * + */ + + @Test + public void verifyTotalTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + int actualCount = ac.getTotalCount(); + Assert.assertEquals("Incorrect total tests count",10,actualCount); + } + + /** + * Verify if passed tests count is correct + * + */ + + @Test + public void verifyPassedTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + int actualCount = ac.getPassedCount(); + Assert.assertEquals("Incorrect passed tests count",4,actualCount); + } + + /** + * Verify if failed tests count is correct + * + */ + + @Test + public void verifyFailedTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + int actualCount = ac.getFailedCount(); + Assert.assertEquals("Incorrect failed tests count",3,actualCount); + } + + /** + * Verify if incomplete tests count is correct + * + */ + + @Test + public void verifyIncompleteTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + int actualCount = ac.getIncompleteCount(); + Assert.assertEquals("Incorrect incomplete tests count",2,actualCount); + } + + /** + * Verify if not run tests count is correct + * + */ + + @Test + public void verifyNotRunTestsCount() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:\\workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + int actualCount = ac.getNotRunCount(); + Assert.assertEquals("Incorrect not run tests count",1,actualCount); + } + + /** + * Verify if test file path is correct + * + */ + + @Test + public void verifyMatlabTestFilePath() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + + String os = System.getProperty("os.name").toLowerCase(); + String testFolder = "testArtifacts/t1/"; + String workspaceParent = ""; + String expectedParentPath = ""; + if (os.contains("win")) { + testFolder += "windows/"; + workspaceParent = "C:\\"; + expectedParentPath = "workspace\\visualization\\"; + } else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) { + testFolder += "linux/"; + workspaceParent = "/home/user/"; + expectedParentPath = "workspace/visualization/"; + } else if (os.contains("mac")) { + testFolder += "mac/"; + workspaceParent = "/Users/username/"; + expectedParentPath = "workspace/visualization/"; + } else { + throw new RuntimeException("Unsupported OS: " + os); + } + copyFileInWorkspace(testFolder + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + final FilePath workspace = new FilePath(new File(workspaceParent + "workspace")); + + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + String actualPath1 = ta.get(0).get(0).getPath(); + Assert.assertEquals("Incorrect test file path",expectedParentPath + "tests",actualPath1); + String actualPath2 = ta.get(1).get(0).getPath(); + Assert.assertEquals("Incorrect test file path",expectedParentPath + "duplicate_tests",actualPath2); + } + + /** + * Verify if test file name is correct + * + */ + + @Test + public void verifyMatlabTestFileName() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + String actualName1 = ta.get(0).get(0).getName(); + Assert.assertEquals("Incorrect test file name","TestExamples1",actualName1); + String actualName2 = ta.get(1).get(0).getName(); + Assert.assertEquals("Incorrect test file name","TestExamples2",actualName2); + } + + /** + * Verify if test file duration is correct + * + */ + + @Test + public void verifyMatlabTestFileDuration() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + BigDecimal actualDuration1 = ta.get(0).get(0).getDuration(); + Assert.assertEquals("Incorrect test file duration",new BigDecimal("1.7"),actualDuration1); + BigDecimal actualDuration2 = ta.get(1).get(0).getDuration(); + Assert.assertEquals("Incorrect test file duration",new BigDecimal("0.1"),actualDuration2); + } + + /** + * Verify if test file status is correct + * + */ + + @Test + public void verifyMatlabTestFileStatus() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + TestStatus actualStatus1 = ta.get(0).get(0).getStatus(); + Assert.assertEquals("Incorrect test file status",TestStatus.FAILED,actualStatus1); + TestStatus actualStatus2 = ta.get(1).get(0).getStatus(); + Assert.assertEquals("Incorrect test file status",TestStatus.INCOMPLETE,actualStatus2); + } + + /** + * Verify if test case name is correct + * + */ + + @Test + public void verifyMatlabTestCaseName() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + String actualName1_1 = ta.get(0).get(0).getMatlabTestCases().get(0).getName(); + Assert.assertEquals("Incorrect test case name","testNonLeapYear",actualName1_1); + String actualName1_5 = ta.get(0).get(0).getMatlabTestCases().get(4).getName(); + Assert.assertEquals("Incorrect test case name","testLeapYear",actualName1_5); + String actualName1_8 = ta.get(0).get(0).getMatlabTestCases().get(7).getName(); + Assert.assertEquals("Incorrect test case name","testValidDateFormat",actualName1_8); + String actualName1_9 = ta.get(0).get(0).getMatlabTestCases().get(8).getName(); + Assert.assertEquals("Incorrect test case name","testInvalidDateFormat",actualName1_9); + String actualName2 = ta.get(1).get(0).getMatlabTestCases().get(0).getName(); + Assert.assertEquals("Incorrect test case name","testNonLeapYear",actualName2); + } + + /** + * Verify if test case status is correct + * + */ + + @Test + public void verifyMatlabTestCaseStatus() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + TestStatus actualStatus1_1 = ta.get(0).get(0).getMatlabTestCases().get(0).getStatus(); + Assert.assertEquals("Incorrect test case status",TestStatus.PASSED,actualStatus1_1); + TestStatus actualStatus1_5 = ta.get(0).get(0).getMatlabTestCases().get(4).getStatus(); + Assert.assertEquals("Incorrect test case status",TestStatus.FAILED,actualStatus1_5); + TestStatus actualStatus1_9 = ta.get(0).get(0).getMatlabTestCases().get(8).getStatus(); + Assert.assertEquals("Incorrect test case status",TestStatus.NOT_RUN,actualStatus1_9); + TestStatus actualStatus2 = ta.get(1).get(0).getMatlabTestCases().get(0).getStatus(); + Assert.assertEquals("Incorrect test case status",TestStatus.INCOMPLETE,actualStatus2); + } + + /** + * Verify if test case duration is correct + * + */ + + @Test + public void verifyMatlabTestCaseDuration() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + BigDecimal actualDuration1_1 = ta.get(0).get(0).getMatlabTestCases().get(0).getDuration(); + Assert.assertEquals("Incorrect test case duration",new BigDecimal("0.1"),actualDuration1_1); + BigDecimal actualDuration1_5 = ta.get(0).get(0).getMatlabTestCases().get(4).getDuration(); + Assert.assertEquals("Incorrect test case duration",new BigDecimal("0.4"),actualDuration1_5); + BigDecimal actualDuration1_9 = ta.get(0).get(0).getMatlabTestCases().get(8).getDuration(); + Assert.assertEquals("Incorrect test case duration",new BigDecimal("0"),actualDuration1_9); + BigDecimal actualDuration2 = ta.get(1).get(0).getMatlabTestCases().get(0).getDuration(); + Assert.assertEquals("Incorrect test case duration",new BigDecimal("0.1"),actualDuration2); + } + + /** + * Verify if test case diagnostics is correct + * + */ + + @Test + public void verifyMatlabTestCaseDiagnostics() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + List> ta = ac.getTestResults(); + + MatlabTestDiagnostics diagnostics1 = ta.get(0).get(0).getMatlabTestCases().get(4).getDiagnostics().get(0); + String actualDiagnosticsEvent1 = diagnostics1.getEvent(); + Assert.assertEquals("Incorrect test diagnostics event","SampleDiagnosticsEvent1",actualDiagnosticsEvent1); + String actualDiagnosticsReport1 = diagnostics1.getReport(); + Assert.assertEquals("Incorrect test diagnostics report","SampleDiagnosticsReport1",actualDiagnosticsReport1); + + MatlabTestDiagnostics diagnostics2 = ta.get(1).get(0).getMatlabTestCases().get(0).getDiagnostics().get(0); + String actualDiagnosticsEvent2 = diagnostics2.getEvent(); + Assert.assertEquals("Incorrect test diagnostics event","SampleDiagnosticsEvent2",actualDiagnosticsEvent2); + String actualDiagnosticsReport2 = diagnostics2.getReport(); + Assert.assertEquals("Incorrect test diagnostics report","SampleDiagnosticsReport2",actualDiagnosticsReport2); + } + + /** + * Verify if actionID is set correctly + * + */ + + @Test + public void verifyActionID() throws ExecutionException, InterruptedException, URISyntaxException, IOException, ParseException { + FreeStyleBuild build = getFreestyleBuild(); + final FilePath workspace = new FilePath(new File("C:", "workspace")); + final String actionID = "abc123"; + final String targetFile = MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + actionID + ".json"; + FilePath artifactRoot = new FilePath(build.getRootDir()); + copyFileInWorkspace("testArtifacts/t1/windows/" + MatlabBuilderConstants.TEST_RESULTS_VIEW_ARTIFACT + ".json",targetFile,artifactRoot); + TestResultsViewAction ac = new TestResultsViewAction(build, workspace, actionID); + String actualActionID = ac.getActionID(); + Assert.assertEquals("Incorrect action ID",actionID,actualActionID); + } + + private void copyFileInWorkspace(String sourceFile, String targetFile, FilePath targetWorkspace) + throws IOException, InterruptedException { + final ClassLoader classLoader = getClass().getClassLoader(); + FilePath targetFilePath = new FilePath(targetWorkspace, targetFile); + InputStream in = classLoader.getResourceAsStream(sourceFile); + targetFilePath.copyFrom(in); + // set executable permission + targetFilePath.chmod(0777); + } + + private FreeStyleBuild getFreestyleBuild() throws ExecutionException, InterruptedException, URISyntaxException { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); + project.getBuildWrappersList().add(this.buildWrapper); + scriptBuilder.setTasks(""); + project.getBuildersList().add(this.scriptBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + return build; + } +} diff --git a/src/test/java/unit/com/mathworks/ci/actions/MatlabActionTest.java b/src/test/java/com/mathworks/ci/actions/MatlabActionTest.java similarity index 95% rename from src/test/java/unit/com/mathworks/ci/actions/MatlabActionTest.java rename to src/test/java/com/mathworks/ci/actions/MatlabActionTest.java index 795475d4..c53b2ad0 100644 --- a/src/test/java/unit/com/mathworks/ci/actions/MatlabActionTest.java +++ b/src/test/java/com/mathworks/ci/actions/MatlabActionTest.java @@ -1,136 +1,136 @@ -package unit.com.mathworks.ci.actions; - -/** - * Copyright 2024, The MathWorks Inc. - */ - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.nio.file.Files; - -import org.junit.Test; -import org.junit.Before; -import org.junit.runner.RunWith; -import static org.junit.Assert.*; - -import org.mockito.InOrder; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - -import hudson.FilePath; -import hudson.model.Run; -import hudson.model.TaskListener; - -import com.mathworks.ci.MatlabBuilderConstants; -import com.mathworks.ci.BuildArtifactAction; -import com.mathworks.ci.BuildConsoleAnnotator; -import com.mathworks.ci.MatlabExecutionException; -import com.mathworks.ci.actions.RunMatlabCommandAction; -import com.mathworks.ci.utilities.MatlabCommandRunner; -import com.mathworks.ci.parameters.CommandActionParameters; - -@RunWith(MockitoJUnitRunner.Silent.class) -public class MatlabActionTest { - @Mock - CommandActionParameters params; - @Mock - BuildConsoleAnnotator annotator; - @Mock - MatlabCommandRunner runner; - @Mock - PrintStream out; - @Mock - TaskListener listener; - @Mock - Run build; - - @Mock - FilePath tempFolder; - - private boolean setup = false; - private RunMatlabCommandAction action; - - // Not using @BeforeClass to avoid static fields. - @Before - public void init() { - if (!setup) { - setup = true; - action = new RunMatlabCommandAction(runner, annotator, params); - - when(runner.getTempFolder()).thenReturn(tempFolder); - when(tempFolder.getRemote()).thenReturn("/path/less/traveled"); - - when(params.getTaskListener()).thenReturn(listener); - when(listener.getLogger()).thenReturn(out); - - when(params.getBuild()).thenReturn(build); - } - } - - @Test - public void shouldCopyPluginsToTempDirectory() throws IOException, InterruptedException, MatlabExecutionException { - action.run(); - - InOrder inOrder = inOrder(runner); - - inOrder.verify(runner) - .copyFileToTempFolder(MatlabBuilderConstants.DEFAULT_PLUGIN, MatlabBuilderConstants.DEFAULT_PLUGIN); - inOrder.verify(runner) - .copyFileToTempFolder(MatlabBuilderConstants.BUILD_REPORT_PLUGIN, - MatlabBuilderConstants.BUILD_REPORT_PLUGIN); - inOrder.verify(runner) - .copyFileToTempFolder(MatlabBuilderConstants.TASK_RUN_PROGRESS_PLUGIN, - MatlabBuilderConstants.TASK_RUN_PROGRESS_PLUGIN); - } - - @Test - public void shouldOverrideDefaultBuildtoolPlugin() - throws IOException, InterruptedException, MatlabExecutionException { - action.run(); - - verify(runner).addEnvironmentVariable( - "MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE", - "ciplugins.jenkins.getDefaultPlugins"); - } - - @Test - public void shouldCopyBuildResultsToRootAndAddAction() - throws IOException, InterruptedException, MatlabExecutionException { - File tmp = Files.createTempDirectory("temp").toFile(); - tmp.deleteOnExit(); - - File dest = Files.createTempDirectory("dest").toFile(); - dest.deleteOnExit(); - - File json = new File(tmp, "buildArtifact.json"); - json.createNewFile(); - - doReturn(new FilePath(tmp)).when(runner).getTempFolder(); - doReturn(dest).when(build).getRootDir(); - - action.run(); - - // Should have deleted original file - assertFalse(json.exists()); - // Should have copied file to root dir - assertTrue(new File(dest, "buildArtifact" + action.getActionID() + ".json").exists()); - } - - @Test - public void shouldNotAddActionIfNoBuildResult() throws IOException, InterruptedException, MatlabExecutionException { - action.run(); - - verify(build, never()).addAction(any(BuildArtifactAction.class)); - } - - @Test - public void shouldRemoveTempFolder() throws IOException, InterruptedException, MatlabExecutionException { - action.run(); - - verify(runner).removeTempFolder(); - } -} +package com.mathworks.ci.actions; + +/** + * Copyright 2024, The MathWorks Inc. + */ + +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.nio.file.Files; + +import org.junit.Test; +import org.junit.Before; +import org.junit.runner.RunWith; +import static org.junit.Assert.*; + +import org.mockito.InOrder; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import hudson.FilePath; +import hudson.model.Run; +import hudson.model.TaskListener; + +import com.mathworks.ci.MatlabBuilderConstants; +import com.mathworks.ci.BuildArtifactAction; +import com.mathworks.ci.BuildConsoleAnnotator; +import com.mathworks.ci.MatlabExecutionException; +import com.mathworks.ci.actions.RunMatlabCommandAction; +import com.mathworks.ci.utilities.MatlabCommandRunner; +import com.mathworks.ci.parameters.CommandActionParameters; + +@RunWith(MockitoJUnitRunner.Silent.class) +public class MatlabActionTest { + @Mock + CommandActionParameters params; + @Mock + BuildConsoleAnnotator annotator; + @Mock + MatlabCommandRunner runner; + @Mock + PrintStream out; + @Mock + TaskListener listener; + @Mock + Run build; + + @Mock + FilePath tempFolder; + + private boolean setup = false; + private RunMatlabCommandAction action; + + // Not using @BeforeClass to avoid static fields. + @Before + public void init() { + if (!setup) { + setup = true; + action = new RunMatlabCommandAction(runner, annotator, params); + + when(runner.getTempFolder()).thenReturn(tempFolder); + when(tempFolder.getRemote()).thenReturn("/path/less/traveled"); + + when(params.getTaskListener()).thenReturn(listener); + when(listener.getLogger()).thenReturn(out); + + when(params.getBuild()).thenReturn(build); + } + } + + @Test + public void shouldCopyPluginsToTempDirectory() throws IOException, InterruptedException, MatlabExecutionException { + action.run(); + + InOrder inOrder = inOrder(runner); + + inOrder.verify(runner) + .copyFileToTempFolder(MatlabBuilderConstants.DEFAULT_PLUGIN, MatlabBuilderConstants.DEFAULT_PLUGIN); + inOrder.verify(runner) + .copyFileToTempFolder(MatlabBuilderConstants.BUILD_REPORT_PLUGIN, + MatlabBuilderConstants.BUILD_REPORT_PLUGIN); + inOrder.verify(runner) + .copyFileToTempFolder(MatlabBuilderConstants.TASK_RUN_PROGRESS_PLUGIN, + MatlabBuilderConstants.TASK_RUN_PROGRESS_PLUGIN); + } + + @Test + public void shouldOverrideDefaultBuildtoolPlugin() + throws IOException, InterruptedException, MatlabExecutionException { + action.run(); + + verify(runner).addEnvironmentVariable( + "MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE", + "ciplugins.jenkins.getDefaultPlugins"); + } + + @Test + public void shouldCopyBuildResultsToRootAndAddAction() + throws IOException, InterruptedException, MatlabExecutionException { + File tmp = Files.createTempDirectory("temp").toFile(); + tmp.deleteOnExit(); + + File dest = Files.createTempDirectory("dest").toFile(); + dest.deleteOnExit(); + + File json = new File(tmp, "buildArtifact.json"); + json.createNewFile(); + + doReturn(new FilePath(tmp)).when(runner).getTempFolder(); + doReturn(dest).when(build).getRootDir(); + + action.run(); + + // Should have deleted original file + assertFalse(json.exists()); + // Should have copied file to root dir + assertTrue(new File(dest, "buildArtifact" + action.getActionID() + ".json").exists()); + } + + @Test + public void shouldNotAddActionIfNoBuildResult() throws IOException, InterruptedException, MatlabExecutionException { + action.run(); + + verify(build, never()).addAction(any(BuildArtifactAction.class)); + } + + @Test + public void shouldRemoveTempFolder() throws IOException, InterruptedException, MatlabExecutionException { + action.run(); + + verify(runner).removeTempFolder(); + } +} diff --git a/src/test/java/unit/com/mathworks/ci/actions/RunMatlabBuildActionTest.java b/src/test/java/com/mathworks/ci/actions/RunMatlabBuildActionTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/actions/RunMatlabBuildActionTest.java rename to src/test/java/com/mathworks/ci/actions/RunMatlabBuildActionTest.java diff --git a/src/test/java/unit/com/mathworks/ci/actions/RunMatlabCommandActionTest.java b/src/test/java/com/mathworks/ci/actions/RunMatlabCommandActionTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/actions/RunMatlabCommandActionTest.java rename to src/test/java/com/mathworks/ci/actions/RunMatlabCommandActionTest.java diff --git a/src/test/java/unit/com/mathworks/ci/actions/RunMatlabTestsActionTest.java b/src/test/java/com/mathworks/ci/actions/RunMatlabTestsActionTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/actions/RunMatlabTestsActionTest.java rename to src/test/java/com/mathworks/ci/actions/RunMatlabTestsActionTest.java diff --git a/src/test/java/unit/com/mathworks/ci/freestyle/RunMatlabBuildBuilderUnitTest.java b/src/test/java/com/mathworks/ci/freestyle/RunMatlabBuildBuilderUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/freestyle/RunMatlabBuildBuilderUnitTest.java rename to src/test/java/com/mathworks/ci/freestyle/RunMatlabBuildBuilderUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/freestyle/RunMatlabCommandBuilderUnitTest.java b/src/test/java/com/mathworks/ci/freestyle/RunMatlabCommandBuilderUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/freestyle/RunMatlabCommandBuilderUnitTest.java rename to src/test/java/com/mathworks/ci/freestyle/RunMatlabCommandBuilderUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/freestyle/RunMatlabTestsBuilderUnitTest.java b/src/test/java/com/mathworks/ci/freestyle/RunMatlabTestsBuilderUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/freestyle/RunMatlabTestsBuilderUnitTest.java rename to src/test/java/com/mathworks/ci/freestyle/RunMatlabTestsBuilderUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/pipeline/MatlabBuildStepExecutionUnitTest.java b/src/test/java/com/mathworks/ci/pipeline/MatlabBuildStepExecutionUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/pipeline/MatlabBuildStepExecutionUnitTest.java rename to src/test/java/com/mathworks/ci/pipeline/MatlabBuildStepExecutionUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/pipeline/MatlabCommandStepExecutionUnitTest.java b/src/test/java/com/mathworks/ci/pipeline/MatlabCommandStepExecutionUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/pipeline/MatlabCommandStepExecutionUnitTest.java rename to src/test/java/com/mathworks/ci/pipeline/MatlabCommandStepExecutionUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/pipeline/MatlabRunTestsStepExecutionUnitTest.java b/src/test/java/com/mathworks/ci/pipeline/MatlabRunTestsStepExecutionUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/pipeline/MatlabRunTestsStepExecutionUnitTest.java rename to src/test/java/com/mathworks/ci/pipeline/MatlabRunTestsStepExecutionUnitTest.java diff --git a/src/test/java/com/mathworks/ci/systemtests/GlobalToolIT.java b/src/test/java/com/mathworks/ci/systemtests/GlobalToolIT.java new file mode 100644 index 00000000..80fe3f8c --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/GlobalToolIT.java @@ -0,0 +1,190 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.*; +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; +import hudson.matrix.AxisList; +import hudson.matrix.MatrixBuild; +import hudson.matrix.MatrixProject; +import hudson.matrix.MatrixRun; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import org.htmlunit.html.*; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.*; +import static org.junit.Assert.assertEquals; + +public class GlobalToolIT { + private FreeStyleProject project; + private RunMatlabCommandBuilder scriptBuilder; + private UseMatlabVersionBuildWrapper buildWrapper; + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + this.scriptBuilder = new RunMatlabCommandBuilder(); + } + + @After + public void testTearDown() { + this.project = null; + this.scriptBuilder = null; + Utilities.matlabInstDescriptor = null; + this.buildWrapper = null; + } + + @Test + public void verifyGlobalToolForFreeStyle() throws Exception{ + // Adding the MATLAB Global tool + Utilities.setMatlabInstallation("MATLAB_PATH", Utilities.getMatlabRoot(), jenkins); + + // Selecting MATLAB that is defined as Global tool + MatlabInstallation inst = MatlabInstallation.getInstallation("MATLAB_PATH"); + MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(inst.getName(), null); + buildWrapper.setMatlabBuildWrapperContent(content); + project.getBuildWrappersList().add(buildWrapper); + scriptBuilder.setMatlabCommand("disp('apple')"); + project.getBuildersList().add(scriptBuilder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertLogContains(inst.getHome(), build); + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains("apple", build); + assertEquals(countMatches(jenkins.getLog(build), "apple"), 2); + } + + @Test + public void verifyBuildFailsForIncorrectToolPath() throws Exception{ + // Adding the MATLAB Global tool + Utilities.setMatlabInstallation("MATLAB_PATH", "matlab/incorrect/path", jenkins); + + // Selecting MATLAB that is defined as Global tool + MatlabInstallation inst = MatlabInstallation.getInstallation("MATLAB_PATH"); + MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(inst.getName(), null); + buildWrapper.setMatlabBuildWrapperContent(content); + project.getBuildWrappersList().add(buildWrapper); + + scriptBuilder.setMatlabCommand("ver"); + project.getBuildersList().add(scriptBuilder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.FAILURE, build); + // Note: This message might change in future based on Jenkins changes + jenkins.assertLogContains("Verify global tool configuration for the specified node.", build); + jenkins.assertLogContains("MATLAB_PATH", build); + } + + @Test + public void verifyAllToolsAreAvailable() throws Exception { + // Adding the Global tools + String matlabRoot = System.getenv("MATLAB_ROOT"); + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); + + Utilities.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); + Utilities.setMatlabInstallation("MATLAB_PATH_2", matlabRoot22b, jenkins); + + // Getting the Web page for UI testing + HtmlPage configurePage = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput matlabVer = configurePage.getElementByName("com-mathworks-ci-UseMatlabVersionBuildWrapper"); + matlabVer.setChecked(true); + + HtmlSelect matlabOptions = (HtmlSelect) configurePage.getByXPath("//select[contains(@class, \"dropdownList\")]").get(1); + assertEquals(matlabOptions.getOption(0).getValueAttribute(),"MATLAB_PATH_1"); + assertEquals(matlabOptions.getOption(1).getValueAttribute(),"MATLAB_PATH_2"); + assertEquals(matlabOptions.getOption(2).getValueAttribute(),"Custom..."); + } + + @Test + public void verifyGlobalToolMatrix() throws Exception { + Utilities.setMatlabInstallation("MATLAB_PATH_1", Utilities.getMatlabRoot(), jenkins); + Utilities.setMatlabInstallation("MATLAB_PATH_2", Utilities.getMatlabRoot(), jenkins); + + List list= new ArrayList<>(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_2")); + MatlabInstallationAxis axis = new MatlabInstallationAxis(list); + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class);; + matrixProject.setAxes(new AxisList(axis)); + + scriptBuilder.setMatlabCommand("version"); + matrixProject.getBuildersList().add(scriptBuilder); + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); + List runs = build.getRuns(); + // ToDo: Add more relevant verification statement + for (MatrixRun run : runs) { + String matlabName = run.getBuildVariables().get("MATLAB"); + Assert.assertTrue(matlabName.equalsIgnoreCase("MATLAB_PATH_1") || matlabName.equalsIgnoreCase("MATLAB_PATH_2")); + } + jenkins.assertBuildStatus(Result.SUCCESS,build); + } + + // Tool config + @Test + public void verifyGlobalToolDSLPipeline() throws Exception { + Utilities.setMatlabInstallation("MATLAB_PATH_1", Utilities.getMatlabRoot(), jenkins); + String script = "pipeline {\n" + + " agent any\n" + + " tools {\n" + + " matlab 'MATLAB_PATH_1'\n" + + " }\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABCommand 'matlabroot'\n" + + " } \n" + + " } \n" + + " } \n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertBuildStatus(Result.SUCCESS,build); + assertEquals(countMatches(jenkins.getLog(build), Utilities.getMatlabRoot()), 1); + } + @Test + public void verifyGlobalToolScriptedPipeline() throws Exception { + Utilities.setMatlabInstallation("MATLAB_PATH_1", Utilities.getMatlabRoot(), jenkins); + String script = "node {\n" + + " def matlabver\n" + + " stage('Run MATLAB Command') {\n" + + " matlabver = tool 'MATLAB_PATH_1'\n" + + " if (isUnix()){\n" + + " env.PATH = \"${matlabver}/bin:${env.PATH}\" // Linux or macOS agent\n" + + " }else{\n" + + " env.PATH = \"${matlabver}\\\\bin;${env.PATH}\" // Windows agent\n" + + " } \n" + + " runMATLABCommand 'matlabroot'\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertBuildStatus(Result.SUCCESS, build); + assertEquals(countMatches(jenkins.getLog(build), Utilities.getMatlabRoot()), 1); + } + + private WorkflowRun getPipelineBuild(String script) throws Exception{ + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script,true)); + return project.scheduleBuild2(0).get(); + } + +} \ No newline at end of file diff --git a/src/test/java/com/mathworks/ci/systemtests/RunMATLABCommandIT.java b/src/test/java/com/mathworks/ci/systemtests/RunMATLABCommandIT.java new file mode 100644 index 00000000..ccf1d886 --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/RunMATLABCommandIT.java @@ -0,0 +1,230 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.*; +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; +import hudson.EnvVars; +import hudson.matrix.AxisList; +import hudson.matrix.Combination; +import hudson.matrix.MatrixProject; +import hudson.matrix.MatrixRun; +import hudson.matrix.MatrixBuild; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import hudson.slaves.DumbSlave; +import hudson.slaves.EnvironmentVariablesNodeProperty; +import hudson.tasks.Builder; +import org.htmlunit.WebAssert; +import org.htmlunit.html.HtmlPage; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.jvnet.hudson.test.JenkinsRule; + +import java.util.Arrays; +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.countMatches; +import static org.junit.Assert.assertEquals; + +public class RunMATLABCommandIT { + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Test + public void verifyBuildStepWithRunMatlab() throws Exception { + boolean found = false; + FreeStyleProject project = jenkins.createFreeStyleProject(); + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + scriptBuilder.setMatlabCommand(""); + project.getBuildersList().add(scriptBuilder); + List bl = project.getBuildersList(); + for (Builder b : bl) { + if (b.getDescriptor().getDisplayName().equalsIgnoreCase( + TestMessage.getValue("Builder.matlab.script.builder.display.name"))) { + found = true; + } + } + Assert.assertTrue("Build step does not contain Run MATLAB Command option", found); + } + + /* + *Test To Verify if Build passes when matlab command passes + */ + @Test + public void verifyBuildPassesWhenMatlabCommandPasses() throws Exception { + FreeStyleProject project = jenkins.createFreeStyleProject(); + UseMatlabVersionBuildWrapper buildWrapper = new UseMatlabVersionBuildWrapper(); + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + + buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(buildWrapper); + + scriptBuilder.setMatlabCommand("disp 'apple'"); + project.getBuildersList().add(scriptBuilder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains(Utilities.getMatlabRoot(), build); + jenkins.assertLogContains("run-matlab-command", build); + assertEquals(countMatches(jenkins.getLog(build), "apple"), 2); + } + + /* + * Test to verify if Build FAILS when matlab command fails + */ + @Test + public void verifyBuildFailureWhenMatlabCommandFails() throws Exception { + FreeStyleProject project = jenkins.createFreeStyleProject(); + UseMatlabVersionBuildWrapper buildWrapper = new UseMatlabVersionBuildWrapper(); + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + + buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(buildWrapper); + + scriptBuilder.setMatlabCommand("apple"); + project.getBuildersList().add(scriptBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + assertEquals(countMatches(jenkins.getLog(build), "apple"), 3); + jenkins.assertBuildStatus(Result.FAILURE, build); + jenkins.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); + } + + /* + * Test to verify if Matrix build passes . + */ + + @Test + public void verifyMatrixBuildPassesWithMATLABCommand() throws Exception { + String matlabRoot = System.getenv("MATLAB_ROOT"); + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); + + Utilities.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); + Utilities.setMatlabInstallation("MATLAB_PATH_22b", matlabRoot22b, jenkins); + + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); + MatlabInstallationAxis MATLABAxis = new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); + matrixProject.setAxes(new AxisList(MATLABAxis)); + + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + scriptBuilder.setMatlabCommand("disp('apple')"); + matrixProject.getBuildersList().add(scriptBuilder); + + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); + + Combination c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1"))), "MATLAB_PATH_1"); + MatrixRun run = build.getRun(c); + assertEquals(countMatches(jenkins.getLog(run), "apple"), 2); + jenkins.assertLogContains(matlabRoot, run); + jenkins.assertLogNotContains(matlabRoot22b, run); + + c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_22b"))), "MATLAB_PATH_22b"); + run = build.getRun(c); + assertEquals(countMatches(jenkins.getLog(run), "apple"), 2); + jenkins.assertLogContains(matlabRoot22b, run); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + } + + @Test + public void verifyBuildPassesWhenMatlabCommandPassesInPipeline() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABCommand 'matlabroot' \n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script, true)); + WorkflowRun build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + assertEquals(countMatches(jenkins.getLog(build), Utilities.getMatlabRoot()), 1); + } + + @Test + public void verifyBuildFailsWhenMatlabCommandFailsInPipeline() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABCommand 'apple' \n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script, true)); + WorkflowRun build = project.scheduleBuild2(0).get(); + + assertEquals(countMatches(jenkins.getLog(build), "apple"), 3); + jenkins.assertBuildStatus(Result.FAILURE, build); + } + + @Test + public void verifyPipelineOnSlave() throws Exception { + DumbSlave s = jenkins.createOnlineSlave(); + String script = "node ('!built-in'){\n" + + Utilities.getEnvironmentScriptedPipeline() + "\n" + + " runMATLABCommand 'matlabroot'\n" + + "}"; + + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script, true)); + WorkflowRun build = project.scheduleBuild2(0).get(); + + jenkins.assertLogNotContains("Running on Jenkins", build); + jenkins.assertBuildStatus(Result.SUCCESS, build); + assertEquals(countMatches(jenkins.getLog(build), Utilities.getMatlabRoot()), 1); + } + + @Test + public void verifyCommandSupportsEnvVar() throws Exception { + FreeStyleProject project = jenkins.createFreeStyleProject(); + UseMatlabVersionBuildWrapper buildWrapper = new UseMatlabVersionBuildWrapper(); + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + + EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); + EnvVars var = prop.getEnvVars(); + var.put("PWDCMD", "pwd"); + jenkins.jenkins.getGlobalNodeProperties().add(prop); + buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(buildWrapper); + scriptBuilder.setMatlabCommand("$PWDCMD"); + project.getBuildersList().add(scriptBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("pwd", build); + } + + @Test + public void verifyErrorMessageOnEmptyCommand() throws Exception { + FreeStyleProject project = jenkins.createFreeStyleProject(); + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + + project.getBuildersList().add(scriptBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + + WebAssert.assertTextPresent(page, "Specify at least one script, function, or statement to execute."); + } +} diff --git a/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsArtifactsIT.java b/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsArtifactsIT.java new file mode 100644 index 00000000..de8a9af6 --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsArtifactsIT.java @@ -0,0 +1,249 @@ +package com.mathworks.ci.systemtests; + +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import com.mathworks.ci.MatlabBuildWrapperContent; +import com.mathworks.ci.Message; +import com.mathworks.ci.TestMessage; +import com.mathworks.ci.UseMatlabVersionBuildWrapper; +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; +import com.mathworks.ci.freestyle.options.SourceFolder; +import com.mathworks.ci.freestyle.options.SourceFolderPaths; +import hudson.FilePath; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import org.htmlunit.html.HtmlCheckBoxInput; +import org.htmlunit.html.HtmlPage; +import org.htmlunit.html.HtmlTextInput; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.jvnet.hudson.test.ExtractResourceSCM; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class RunMATLABTestsArtifactsIT { + private FreeStyleProject project; + private UseMatlabVersionBuildWrapper buildWrapper; + private RunMatlabTestsBuilder testBuilder; + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.testBuilder = new RunMatlabTestsBuilder(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + testBuilder.setLoggingLevel("default"); + testBuilder.setOutputDetail("default"); + } + + @After + public void testTearDown() { + this.project = null; + this.testBuilder = null; + } + + /* Helper function to read XML file as string */ + public String xmlToString(String codegenReportPath) throws FileNotFoundException { + File codeCoverageFile = new File(codegenReportPath); + XML xml = new XMLDocument(codeCoverageFile); + return xml.toString(); + } + + @Test + public void verifyJUnitFilePathInput() throws Exception{ + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput junitChkbx = page.getElementByName("junitArtifact"); + junitChkbx.setChecked(true); + + HtmlTextInput junitFilePathInput = (HtmlTextInput) page.getElementByName("_.junitReportFilePath"); + Assert.assertEquals(TestMessage.getValue("junit.file.path"),junitFilePathInput.getValueAttribute()); + } + + @Test + public void verifyTAPTestFilePathInput() throws Exception{ + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput tapChkbx = page.getElementByName("tapArtifact"); + tapChkbx.setChecked(true); + + HtmlTextInput tapFilePathInput = (HtmlTextInput) page.getElementByName("_.tapReportFilePath"); + Assert.assertEquals(TestMessage.getValue("taptestresult.file.path"),tapFilePathInput.getValueAttribute()); + + } + + @Test + public void verifyPDFReportFilePathInput() throws Exception{ + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput pdfChkbx = page.getElementByName("pdfReportArtifact"); + pdfChkbx.setChecked(true); + + HtmlTextInput PDFFilePathInput=(HtmlTextInput) page.getElementByName("_.pdfReportFilePath"); + Assert.assertEquals(TestMessage.getValue("pdftestreport.file.path"),PDFFilePathInput.getValueAttribute()); + } + + @Test + public void verifyCoberturaFilePathInput() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput coberturaChkBx = page.getElementByName("coberturaArtifact"); + coberturaChkBx.setChecked(true); + + HtmlTextInput coberturaCodeCoverageFileInput=(HtmlTextInput) page.getElementByName("_.coberturaReportFilePath"); + Assert.assertEquals(TestMessage.getValue("cobertura.file.path"),coberturaCodeCoverageFileInput.getValueAttribute()); + } + + + @Test + public void verifyModelCoverageFilePathInput() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput modelCoverageChkBx = page.getElementByName("modelCoverageArtifact"); + modelCoverageChkBx.setChecked(true); + + HtmlTextInput coberturaModelCoverageFileInput=(HtmlTextInput) page.getElementByName("_.modelCoverageFilePath"); + Assert.assertEquals(TestMessage.getValue("modelcoverage.file.path"),coberturaModelCoverageFileInput.getValueAttribute()); + } + + + @Test + public void verifySTMResultsFilePathInput() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput stmResultsChkBx = page.getElementByName("stmResultsArtifact"); + stmResultsChkBx.setChecked(true); + + HtmlTextInput STMRFilePathInput=(HtmlTextInput) page.getElementByName("_.stmResultsFilePath"); + Assert.assertEquals(TestMessage.getValue("stmresults.file.path"),STMRFilePathInput.getValueAttribute()); + } + + @Test + public void verifyCustomFilePathInputForArtifacts() throws Exception{ + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); + // Adding list of source folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + testingBuilder.setSourceFolder(new SourceFolder(list)); + // Generate artifacts with custom inputs + testingBuilder.setPdfReportArtifact(new RunMatlabTestsBuilder.PdfArtifact("TestArtifacts/pdfReport.pdf")); + testingBuilder.setTapArtifact(new RunMatlabTestsBuilder.TapArtifact("TestArtifacts/tapResult.tap")); + testingBuilder.setJunitArtifact(new RunMatlabTestsBuilder.JunitArtifact("TestArtifacts/junittestreport.xml")); + testingBuilder.setStmResultsArtifact(new RunMatlabTestsBuilder.StmResultsArtifact("TestArtifacts/stmresult.mldatx")); + testingBuilder.setCoberturaArtifact(new RunMatlabTestsBuilder.CoberturaArtifact("TestArtifacts/coberturaresult.xml")); + testingBuilder.setModelCoverageArtifact(new RunMatlabTestsBuilder.ModelCovArtifact("TestArtifacts/mdlCovReport.xml")); + + project.getBuildersList().add(testingBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains("TestArtifacts/stmresult.mldatx", build); + jenkins.assertLogContains("TestArtifacts/mdlCovReport.xml", build); + + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/pdfReport.pdf").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/tapResult.tap").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/junittestreport.xml").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "TestArtifacts/coberturaresult.xml").exists()); + } + + @Test + public void verifyCustomFilenamesForArtifactsPipeline() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " unzip '" + Utilities.getRunMATLABTestsData().getPath() + "'" + "\n" + + " runMATLABTests(sourceFolder:['src'], testResultsTAP: 'test-results/results.tap',\n" + + " testResultsPDF: 'test-results/results.pdf',\n" + + " testResultsJUnit: 'test-results/results.xml',\n" + + " testResultsSimulinkTest: 'test-results/results.mldatx',\n" + + " codeCoverageCobertura: 'code-coverage/coverage.xml',\n" + + " modelCoverageCobertura: 'model-coverage/coverage.xml')\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script,true)); + WorkflowRun build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS,build); + jenkins.assertLogContains("test-results/results.mldatx", build); + jenkins.assertLogContains("model-coverage/coverage.xml", build); + + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "test-results/results.pdf").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "test-results/results.tap").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "test-results/results.xml").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "code-coverage/coverage.xml").exists()); + } + + @Test + public void verifyCoverageReportDoesNotIncludeOtherSourceFolder() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); + testingBuilder.setCoberturaArtifact(new RunMatlabTestsBuilder.CoberturaArtifact("TestArtifacts/coberturaresult.xml")); + project.getBuildersList().add(testingBuilder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + String xmlString = xmlToString(build.getWorkspace() + "/TestArtifacts/coberturaresult.xml"); + assertFalse(xmlString.contains("+scriptgen")); + assertFalse(xmlString.contains("genscript")); + assertFalse(xmlString.contains("runner_")); + jenkins.assertLogContains("testSquare", build); + jenkins.assertBuildStatus(Result.FAILURE,build); + } +} \ No newline at end of file diff --git a/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsCustomizeRunIT.java b/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsCustomizeRunIT.java new file mode 100644 index 00000000..c25d9173 --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsCustomizeRunIT.java @@ -0,0 +1,403 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.*; +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; +import hudson.matrix.*; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import org.htmlunit.WebAssert; +import org.htmlunit.html.HtmlPage; +import org.htmlunit.html.HtmlSelect; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class RunMATLABTestsCustomizeRunIT { + private FreeStyleProject project; + private UseMatlabVersionBuildWrapper buildWrapper; + private RunMatlabTestsBuilder testBuilder; + + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.testBuilder = new RunMatlabTestsBuilder(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + testBuilder.setLoggingLevel("default"); + testBuilder.setOutputDetail("default"); + } + + @After + public void testTearDown() { + this.project = null; + this.testBuilder = null; + } + + @Test + public void verifyLoggingLevelSetToNone() throws Exception { + + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("LoggingLevel', 0", build); + + } + + @Test + public void verifyLoggingLevelSetToTerse() throws Exception { + + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("Terse"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("LoggingLevel', 1", build); + + } + + @Test + public void verifyLoggingLevelSetToConcise() throws Exception { + + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("Concise"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("LoggingLevel', 2", build); + + } + + @Test + public void verifyLoggingLevelSetToDetailed() throws Exception { + + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("Detailed"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("LoggingLevel', 3", build); + } + + /*@Integ + * Test To verify if Output Detail is set correctly + * + */ + + @Test + public void verifyOutputDetailSetToNone() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + + testBuilder.setOutputDetail("None"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("'OutputDetail', 0", build); + } + + @Test + public void verifyOutputDetailSetToTerse() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + + testBuilder.setOutputDetail("Terse"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("'OutputDetail', 1", build); + } + + @Test + public void verifyOutputDetailSetToConcise() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + + testBuilder.setOutputDetail("Concise"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("'OutputDetail', 2", build); + } + + @Test + public void verifyOutputDetailSetToDetailed() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + + testBuilder.setOutputDetail("Detailed"); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build; + build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("'OutputDetail', 3", build); + } + + @Test + public void verifyStrictSet() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + testBuilder.setStrict(true); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("FailOnWarningsPlugin", build); + } + + @Test + public void verifyStrictNotSet() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + testBuilder.setStrict(false); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogNotContains("FailOnWarningsPlugin", build); + + } + + @Test + public void verifyRunParallelSet() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + testBuilder.setUseParallel(true); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("runInParallel", build); + } + + @Test + public void verifyRunParallelNotSet() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + testBuilder.setLoggingLevel("None"); + testBuilder.setUseParallel(false); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogNotContains("runInParallel", build); + } + + /* + * Test to verify Use Parallel check box present. + */ + @Test + public void verifyUseParallelPresent() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.useParallel\"]"); + } + + /* + * Test to verify Strict check box present. + */ + + @Test + public void verifyStrictPresent() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.strict\"]"); + } + + /* + * Test to verify Logging Level is present. + */ + + @Test + public void verifyLoggingLevelPresent() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertElementPresentByXPath(page, "//select[@name=\"_.loggingLevel\"]"); + } + + /* + * Test to verify Output Detail is present. + */ + + @Test + public void verifyOutputDetailPresent() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertElementPresentByXPath(page, "//select[@name=\"_.outputDetail\"]"); + } + + @Test + public void verifyLoggingLevelSetToDefault() throws Exception { + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlSelect loggingLevel = page.getElementByName("_.loggingLevel"); + assertEquals("default", loggingLevel.getAttribute("value")); + } + + @Test + public void verifyRunInParallel() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABTests(useParallel:true)\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertLogContains("runInParallel", build); + jenkins.assertBuildStatus(Result.SUCCESS,build); + } + + @Test + public void verifyStrictSetPipeline() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABTests(strict:true)\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertLogContains("FailOnWarningsPlugin", build); + jenkins.assertBuildStatus(Result.SUCCESS,build); + } + + @Test + public void verifyLoggingLevelSet() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABTests(loggingLevel:'None')\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertLogContains("'LoggingLevel', 0", build); + } + + @Test + public void verifyOutoutDetailSet() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " runMATLABTests(outputDetail:'None')\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertLogContains("'OutputDetail', 0", build); + jenkins.assertBuildStatus(Result.SUCCESS,build); + } + + /* + * Test to verify if Matrix build passes (mock MATLAB). + */ + @Test + public void verifyMatrixBuildPasses() throws Exception { + String matlabRoot = System.getenv("MATLAB_ROOT"); + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); + + Utilities.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); + Utilities.setMatlabInstallation("MATLAB_PATH_22b", matlabRoot22b, jenkins); + + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); + MatlabInstallationAxis MATLABAxis = new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); + matrixProject.setAxes(new AxisList(MATLABAxis)); + + testBuilder.setOutputDetail("None"); + testBuilder.setLoggingLevel("None"); + testBuilder.setStrict(true); + testBuilder.setUseParallel(true); + + matrixProject.getBuildersList().add(testBuilder); + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); + List runs = build.getRuns(); + + for (MatrixRun run : runs) { + jenkins.assertLogContains("LoggingLevel', 0", run); + jenkins.assertLogContains("OutputDetail', 0", run); + jenkins.assertLogContains("FailOnWarningsPlugin", run); + jenkins.assertLogContains("runInParallel", run); + jenkins.assertBuildStatus(Result.SUCCESS, run); + } + + + jenkins.assertLogContains("MATLAB_PATH_1 completed", build); + jenkins.assertLogContains("MATLAB_PATH_22b completed", build); + jenkins.assertBuildStatus(Result.SUCCESS, build); + } + + + private WorkflowRun getPipelineBuild(String script) throws Exception{ + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script,true)); + return project.scheduleBuild2(0).get(); + } +} diff --git a/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsIT.java b/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsIT.java new file mode 100644 index 00000000..edad7a58 --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/RunMATLABTestsIT.java @@ -0,0 +1,360 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.*; +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; +import com.mathworks.ci.freestyle.options.SelectByFolder; +import com.mathworks.ci.freestyle.options.SourceFolder; +import com.mathworks.ci.freestyle.options.SourceFolderPaths; +import com.mathworks.ci.freestyle.options.TestFolders; +import hudson.matrix.*; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import hudson.slaves.DumbSlave; +import hudson.tasks.Builder; +import org.htmlunit.WebAssert; +import org.htmlunit.html.HtmlCheckBoxInput; +import org.htmlunit.html.HtmlInput; +import org.htmlunit.html.HtmlPage; +import org.htmlunit.html.HtmlSelect; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.jvnet.hudson.test.ExtractResourceSCM; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.*; + +import static org.junit.Assert.assertEquals; + +public class RunMATLABTestsIT { + private FreeStyleProject project; + private UseMatlabVersionBuildWrapper buildWrapper; + private RunMatlabTestsBuilder testBuilder; + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.testBuilder = new RunMatlabTestsBuilder(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + testBuilder.setLoggingLevel("default"); + testBuilder.setOutputDetail("default"); + } + + @After + public void testTearDown() { + this.project = null; + this.testBuilder = null; + } + + @Test + public void verifyBuildStepWithMatlabTestBuilder() throws Exception { + boolean found = false; + project.getBuildersList().add(testBuilder); + List bl = project.getBuildersList(); + for (Builder b : bl) { + if (b.getDescriptor().getDisplayName() + .equalsIgnoreCase(Message.getBuilderDisplayName())) { + found = true; + } + } + Assert.assertTrue("Build step does not contain Run MATLAB Tests option", found); + } + + @Test + public void verifyRunMATLABTestsWithAllInputs() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); + testingBuilder.setLoggingLevel("None"); + testingBuilder.setOutputDetail("None"); + + // Adding list of source folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + testingBuilder.setSourceFolder(new SourceFolder(list)); + + // Adding list of test folder + List testFolders = new ArrayList(); + testFolders.add(new TestFolders("test/TestSquare")); + testingBuilder.setSelectByFolder(new SelectByFolder(testFolders)); + + // Adding test tag + testingBuilder.setSelectByTag(new RunMatlabTestsBuilder.SelectByTag("TestTag")); + project.getBuildersList().add(testingBuilder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains("addpath(genpath('src'));", build); + // Based on the test folder and tag, 'testSquare' test should be run + jenkins.assertLogContains("testSquare", build); + jenkins.assertLogContains("HasTag('TestTag')", build); + } + + @Test + public void verifySourceFolderDefaultState() throws Exception { + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput sourceFolder = page.getElementByName("_.sourceFolder"); + sourceFolder.click(); + + WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.srcFolderPath\"]"); + HtmlInput srcFolderPath = page.getElementByName("_.srcFolderPath"); + assertEquals("", srcFolderPath.getTextContent()); + } + + @Test + public void verifySelectByFolderDefaultState() throws Exception { + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput sourceFolder = page.getElementByName("_.selectByFolder"); + sourceFolder.click(); + + WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.testFolders\"]"); + HtmlInput srcFolderPath = page.getElementByName("_.testFolders"); + assertEquals("", srcFolderPath.getTextContent()); + } + + @Test + public void verifySelectByTagDefaultState() throws Exception { + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlCheckBoxInput sourceFolder = page.getElementByName("_.selectByTag"); + sourceFolder.click(); + + WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.testTag\"]"); + HtmlInput srcFolderPath = page.getElementByName("_.testTag"); + assertEquals("", srcFolderPath.getTextContent()); + } + + @Test + public void verifyEmptyParameters() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("run-matlab-command", build); + jenkins.assertLogNotContains("'OutputDetail'", build); + jenkins.assertLogNotContains("'PDFTestReport'", build); + jenkins.assertLogNotContains("'Strict'", build); + jenkins.assertLogNotContains("'SourceFolder'", build); + } + + @Test + public void verifyOutputDetailSetToDefault() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.getBuildersList().add(this.testBuilder); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + HtmlSelect outputDetail = page.getElementByName("_.outputDetail"); + assertEquals("default", outputDetail.getAttribute("value")); + } + + @Test + public void verifyMultipleSourceFolders() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); + testingBuilder.setLoggingLevel("None"); + testingBuilder.setOutputDetail("None"); + + // Adding list of source folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + list.add(new SourceFolderPaths("src1")); + testingBuilder.setSourceFolder(new SourceFolder(list)); + + project.getBuildersList().add(testingBuilder); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains("addpath(genpath('src'));", build); + jenkins.assertLogContains("addpath(genpath('src1'));", build); + } + + @Test + public void verifyMultipleTestFolders() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); + + // Adding list of source folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + testingBuilder.setSourceFolder(new SourceFolder(list)); + + // Adding list of test folder + List testFolders = new ArrayList(); + testFolders.add(new TestFolders("test/TestSquare")); + testFolders.add(new TestFolders("test/TestMultiply")); + testingBuilder.setSelectByFolder(new SelectByFolder(testFolders)); + + project.getBuildersList().add(testingBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + // Based on the test folder, 'testSquare' and 'testMultiply' tests should be run + jenkins.assertLogContains("testSquare", build); + jenkins.assertLogContains("testMultiply", build); + } + + @Test + public void verifyTestsRunInMatrixProject() throws Exception { + String matlabRoot = System.getenv("MATLAB_ROOT"); + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); + + Utilities.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); + Utilities.setMatlabInstallation("MATLAB_PATH_22b", matlabRoot22b, jenkins); + + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); + MatlabInstallationAxis MATLABAxis = new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); + matrixProject.setAxes(new AxisList(MATLABAxis)); + matrixProject.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + RunMatlabTestsBuilder testingBuilder = new RunMatlabTestsBuilder(); + + // Adding list of source folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + testingBuilder.setSourceFolder(new SourceFolder(list)); + + // Adding list of test folder + List testFolders = new ArrayList(); + testFolders.add(new TestFolders("test/TestSquare")); + testFolders.add(new TestFolders("test/TestMultiply")); + testingBuilder.setSelectByFolder(new SelectByFolder(testFolders)); + + //Adding test tag + testingBuilder.setSelectByTag(new RunMatlabTestsBuilder.SelectByTag("TestTag")); + + matrixProject.getBuildersList().add(testingBuilder); + + MatrixBuild build = matrixProject.scheduleBuild2(0).get(); + + Combination c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1"))), "MATLAB_PATH_1"); + MatrixRun run = build.getRun(c); + jenkins.assertLogContains("Running testMultiply",run); + jenkins.assertLogContains("Running testSquare", run); + jenkins.assertLogNotContains("Running squareTest", run); + jenkins.assertBuildStatus(Result.SUCCESS,run); + + c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_22b"))), "MATLAB_PATH_22b"); + run = build.getRun(c); + jenkins.assertLogContains("Running testMultiply",run); + jenkins.assertLogContains("Running testSquare", run); + jenkins.assertLogNotContains("Running squareTest", run); + jenkins.assertBuildStatus(Result.SUCCESS,run); + jenkins.assertLogContains(matlabRoot22b,run); + + jenkins.assertBuildStatus(Result.SUCCESS, run); + } + + /* + * Test to verify if tests are filtered scripted pipeline + */ + @Test + public void verifyTestsAreFiltered() throws Exception{ + String script = "node {\n" + + Utilities.getEnvironmentScriptedPipeline() + "\n" + + addTestData()+"\n" + + "runMATLABTests(sourceFolder:['src'], selectByFolder: ['test/TestMultiply', 'test/TestSquare'], selectByTag: 'TestTag')\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertLogContains("Running testMultiply",build); + jenkins.assertLogContains("Running testSquare", build); + jenkins.assertLogNotContains("Running squareTest", build); + jenkins.assertBuildStatus(Result.SUCCESS,build); + } + + /* + * Test to verify if tests are filtered DSL pipeline + */ + @Test + public void verifyTestsAreFilteredDSL() throws Exception{ + String script = "pipeline {\n" + + " agent any\n" + + Utilities.getEnvironmentDSL() + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + addTestData() + "\n" + + " runMATLABTests(sourceFolder:['src'], selectByFolder: ['test/TestMultiply', 'test/TestSquare'], selectByTag: 'TestTag')"+ + " }\n" + + " }\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertLogContains("Running testMultiply",build); + jenkins.assertLogContains("Running testSquare", build); + jenkins.assertLogNotContains("Running squareTest", build); + jenkins.assertBuildStatus(Result.SUCCESS,build); + } + + @Test + public void verifyCmdOptions() throws Exception { + String script = "node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}"; + WorkflowRun build = getPipelineBuild(script); + + jenkins.assertLogContains("setenv('MW_ORIG_WORKING_FOLDER',", build); + jenkins.assertLogContains("run-matlab-command", build); + } + + @Test + public void verifyExceptionForNonZeroExitCode() throws Exception { + String script = "node {\n" + + Utilities.getEnvironmentScriptedPipeline() + "\n" + + addTestData()+"\n" + + "runMATLABTests()\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertBuildStatus(Result.FAILURE, build); + jenkins.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); + } + + @Test + public void verifyOnslave() throws Exception { + DumbSlave s = jenkins.createOnlineSlave(); + String script = "node('!built-in') {runMATLABTests(testResultsPDF:'myresult/result.pdf')}"; + WorkflowRun build = getPipelineBuild(script); + + jenkins.assertLogNotContains("Running on Jenkins", build); + } + + private String addTestData() throws MalformedURLException { + URL zipFile = Utilities.getRunMATLABTestsData(); + String path = " unzip '" + zipFile.getPath() + "'" + "\n"; + return path; + } + + private WorkflowRun getPipelineBuild(String script) throws Exception{ + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script,true)); + return project.scheduleBuild2(0).get(); + } +} diff --git a/src/test/java/com/mathworks/ci/systemtests/RunMatlabBuildIT.java b/src/test/java/com/mathworks/ci/systemtests/RunMatlabBuildIT.java new file mode 100644 index 00000000..c5ccb0a0 --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/RunMatlabBuildIT.java @@ -0,0 +1,254 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.MatlabBuildWrapperContent; +import com.mathworks.ci.Message; +import com.mathworks.ci.TestMessage; +import com.mathworks.ci.UseMatlabVersionBuildWrapper; +import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; +import com.mathworks.ci.freestyle.options.BuildOptions; +import hudson.EnvVars; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import hudson.slaves.DumbSlave; +import hudson.slaves.EnvironmentVariablesNodeProperty; +import hudson.tasks.Builder; +import org.htmlunit.html.HtmlAnchor; +import org.htmlunit.html.HtmlElement; +import org.htmlunit.html.HtmlPage; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.jvnet.hudson.test.ExtractResourceSCM; +import org.jvnet.hudson.test.JenkinsRule; +import org.xml.sax.SAXException; +import java.io.IOException; +import java.util.List; + +import static junit.framework.Assert.assertTrue; + +public class RunMatlabBuildIT { + private FreeStyleProject project; + private UseMatlabVersionBuildWrapper buildWrapper; + private RunMatlabBuildBuilder runBuilder; + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.runBuilder = new RunMatlabBuildBuilder(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + } + + @After + public void testTearDown() { + this.project = null; + this.runBuilder = null; + this.buildWrapper = null; + } + + @Test + public void verifyBuildStepWithRunMatlab() throws Exception { + boolean found = false; + project.getBuildersList().add(runBuilder); + List bl = project.getBuildersList(); + for (Builder b : bl) { + if (b.getDescriptor().getDisplayName().equalsIgnoreCase( + TestMessage.getValue("Builder.build.builder.display.name"))) { + found = true; + } + } + Assert.assertTrue("Build step does not contain Run MATLAB Build option", found); + } + + /* + * Test to verify if Build FAILS when matlab command fails + */ + + @Test + public void verifyBuildFailureWhenMatlabBuildFails() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + this.runBuilder.setTasks("invalid_task"); + + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.FAILURE, build); + jenkins.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); + } + + + /* Test To Verify if Build passes when matlab command passes + */ + @Test + public void verifyBuildPassesWhenMatlabBuildPasses() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + this.runBuilder.setTasks("check"); + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains("buildtool check", build); + } + + @Test + public void verifyDefaultTaskForNoTaskInput() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // Default test task fails + jenkins.assertBuildStatus(Result.FAILURE, build); + // Test task runs the test + jenkins.assertLogContains("testMultiply", build); + } + + @Test + public void verifyRunningMultipleTasks() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + this.runBuilder.setTasks("check dummy"); + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + jenkins.assertLogContains("buildtool check dummy", build); + jenkins.assertLogContains("In dummy task", build); + } + + @Test + public void verifySpecifyingBuildOptions() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + this.runBuilder.setTasks("check test dummy"); + this.runBuilder.setBuildOptions(new BuildOptions("-continueOnFailure -skip check")); + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // 'test' task fails + jenkins.assertBuildStatus(Result.FAILURE, build); + jenkins.assertLogContains("buildtool check test dummy", build); + jenkins.assertLogNotContains("In check task", build); + jenkins.assertLogContains("In dummy task", build); + } + + @Test + public void verifyPipelineOnSlave() throws Exception { + DumbSlave s = jenkins.createOnlineSlave(); + String script ="node('!built-in') { runMATLABBuild() }"; + + WorkflowJob project = jenkins.createProject(WorkflowJob.class); + project.setDefinition(new CpsFlowDefinition(script, true)); + WorkflowRun build = project.scheduleBuild2(0).get(); + + jenkins.assertLogNotContains("Running on Jenkins", build); + } + + @Test + public void verifyBuildSupportsEnvVar() throws Exception { + EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); + EnvVars var = prop.getEnvVars(); + var.put("TASKS", "compile"); + var.put("BUILD_OPTIONS", "-continueOnFailure -skip test"); + jenkins.jenkins.getGlobalNodeProperties().add(prop); + + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + + project.getBuildWrappersList().add(this.buildWrapper); + runBuilder.setTasks("$TASKS"); + runBuilder.setBuildOptions(new BuildOptions("$BUILD_OPTIONS")); + project.getBuildersList().add(runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + jenkins.assertLogContains("compile", build); + jenkins.assertLogContains("-continueOnFailure -skip test", build); + } + + + @Test + public void verifyBuildSummaryInBuildStatusPage() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + this.runBuilder.setTasks("check test dummy"); + this.runBuilder.setBuildOptions(new BuildOptions("-continueOnFailure -skip dummy")); + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // Verify MATLAB Build Result summary + String BuildResultSummary= getSummaryFromBuildStatus(build); + assertTrue(BuildResultSummary.contains("Tasks run: 3")); + assertTrue(BuildResultSummary.contains("Failed: 1")); + assertTrue(BuildResultSummary.contains("Skipped: 1")); + + jenkins.assertBuildStatus(Result.FAILURE, build); + jenkins.assertLogContains("buildtool check test dummy -continueOnFailure -skip dummy", build); + } + + @Test + public void verifyHyperlinkFromSummaryAndSidePanelAreSame() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + + this.runBuilder.setTasks("check test dummy"); + this.runBuilder.setBuildOptions(new BuildOptions("-continueOnFailure -skip dummy")); + project.getBuildersList().add(this.runBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + // Verify the hyperlink from the summary page and Tab are the same + String buildID = getBuildIDFromSummary(build); + + // Get link of "MATLAB Build Results" tab + String resultTableURL = getBuildPagePathFromSidePanel(build); + + assertTrue(resultTableURL.contains(buildID)); + } + + // Helper functions + private String getBuildIDFromSummary(FreeStyleBuild build) throws IOException, SAXException { + HtmlPage buildPage = jenkins.createWebClient().getPage(build); + HtmlAnchor matlabLink = buildPage.getFirstByXPath("//*[@id='main-panel']/table[1]/tbody//a[contains(text(), 'MATLAB Build Results')]"); + return matlabLink.getHrefAttribute(); + } + + private String getBuildPagePathFromSidePanel(FreeStyleBuild build) throws IOException, SAXException { + HtmlPage buildPage = jenkins.createWebClient().getPage(build); + HtmlElement jenkinsSidePanelElement = buildPage.getFirstByXPath("//*[@id='side-panel']/div"); + HtmlElement buildResultTab = (HtmlElement) jenkinsSidePanelElement.getChildNodes().get(5); + HtmlAnchor href = (HtmlAnchor) buildResultTab.getChildNodes().get(0).getByXPath("//a[span[text()='MATLAB Build Results']]").get(0); + return href.getHrefAttribute(); + } + + private String getSummaryFromBuildStatus(FreeStyleBuild build) throws IOException, SAXException { + HtmlPage buildPage = jenkins.createWebClient().getPage(build); + HtmlElement summaryElement = (HtmlElement) buildPage.getByXPath("//*[@id='main-panel']/table[1]/tbody/tr[3]/td[2]").get(0); + return summaryElement.getTextContent(); + + } +} diff --git a/src/test/java/com/mathworks/ci/systemtests/StartupOptionsIT.java b/src/test/java/com/mathworks/ci/systemtests/StartupOptionsIT.java new file mode 100644 index 00000000..7db37426 --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/StartupOptionsIT.java @@ -0,0 +1,217 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.MatlabBuildWrapperContent; +import com.mathworks.ci.MatlabInstallationAxis; +import com.mathworks.ci.Message; +import com.mathworks.ci.UseMatlabVersionBuildWrapper; +import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; +import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; +import com.mathworks.ci.freestyle.options.*; +import hudson.FilePath; +import hudson.matrix.*; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.*; +import org.junit.rules.Timeout; +import org.jvnet.hudson.test.ExtractResourceSCM; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertTrue; + +public class StartupOptionsIT { + volatile private FreeStyleProject project; + volatile WorkflowJob pipelineProject; + private String envScripted; + private String envDSL; + volatile private UseMatlabVersionBuildWrapper buildWrapper; + volatile FreeStyleBuild build; + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @BeforeClass + public static void checkMatlabRoot() { + // Check if the MATLAB_ROOT environment variable is defined + String matlabRoot = System.getenv("MATLAB_ROOT"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT environment variable is not defined", matlabRoot != null && !matlabRoot.isEmpty()); + } + + @Before + public void testSetup() throws IOException { + project = jenkins.createFreeStyleProject(); + pipelineProject = jenkins.createProject(WorkflowJob.class); + buildWrapper = new UseMatlabVersionBuildWrapper(); + this.envDSL = Utilities.getEnvironmentDSL(); + this.envScripted = Utilities.getEnvironmentScriptedPipeline(); + } + + @After + public void testTearDown() { + project = null; + buildWrapper = null; + pipelineProject = null; + } + + private WorkflowRun getPipelineBuild(String script) throws Exception{ + pipelineProject.setDefinition(new CpsFlowDefinition(script,true)); + return pipelineProject.scheduleBuild2(0).get(); + } + + @Test + public void verifyStartupOptionsInFreeStyleProject() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + project.getBuildWrappersList().add(this.buildWrapper); + //Command Step + RunMatlabCommandBuilder commandStep = + new RunMatlabCommandBuilder(); + commandStep.setMatlabCommand("pwd,version"); + StartupOptions startupOptions = new StartupOptions("-logfile outputCommand.log"); + commandStep.setStartupOptions(startupOptions); + project.getBuildersList().add(commandStep); + + //Run tests step + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + RunMatlabTestsBuilder runTestsStep = new RunMatlabTestsBuilder(); + //Adding src folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + runTestsStep.setSourceFolder(new SourceFolder(list)); + + // Adding list of test folder + List testFolders = new ArrayList(); + testFolders.add(new TestFolders("test/TestSquare")); + runTestsStep.setSelectByFolder(new SelectByFolder(testFolders)); + runTestsStep.setStartupOptions(new StartupOptions("-logfile outputTests.log")); + project.getBuildersList().add(runTestsStep); + + // Run Build step + RunMatlabBuildBuilder buildStep = new RunMatlabBuildBuilder(); + buildStep.setTasks("check"); + buildStep.setStartupOptions(new StartupOptions("-logfile outputBuild.log")); + project.getBuildersList().add(buildStep); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "outputCommand.log").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "outputBuild.log").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(project), "outputTests.log").exists()); + jenkins.assertBuildStatus(Result.SUCCESS, build); + } + + @Test + public void verifyStartupOptionsInDeclarativePipeline() throws Exception { + String script = "pipeline {\n" + + " agent any\n" + + envDSL + "\n" + + " stages{\n" + + " stage('Run MATLAB Command') {\n" + + " steps\n" + + " {\n" + + " unzip '" + Utilities.getRunMATLABTestsData().getPath() + "'" + "\n" + + " runMATLABCommand(command: 'pwd,version', startupOptions: '-logfile outputCommand.log -nojvm')\n" + + " runMATLABTests(sourceFolder: ['src'], testResultsJUnit: 'test-results/results.xml'," + + " codeCoverageCobertura: 'code-coverage/coverage.xml', startupOptions: '-logfile outputTests.log -nojvm')\n" + + " runMATLABBuild(tasks: 'check', startupOptions: '-logfile outputBuild.log -nojvm')" + + " }\n" + + " }\n" + + " }\n" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertBuildStatus(Result.SUCCESS, build); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputCommand.log").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputTests.log").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputBuild.log").exists()); + jenkins.assertLogContains("-logfile outputCommand.log", build); + jenkins.assertLogContains("-logfile outputBuild.log", build); + jenkins.assertLogContains("-logfile outputTests.log", build); + } + + @Test + public void verifyStartupOptionsInScriptedPipeline() throws Exception { + String script = "node {\n" + + envScripted + "\n" + + " unzip '" + Utilities.getRunMATLABTestsData().getPath() + "'" + "\n" + + " runMATLABCommand(command: 'pwd,version', startupOptions: '-logfile outputCommand.log -nojvm')\n" + + " runMATLABTests(sourceFolder: ['src'], testResultsJUnit: 'test-results/results.xml'," + + " codeCoverageCobertura: 'code-coverage/coverage.xml', startupOptions: '-logfile outputTests.log -nojvm')\n" + + " runMATLABBuild(tasks: 'check', startupOptions: '-logfile outputBuild.log -nojvm')" + + "}"; + WorkflowRun build = getPipelineBuild(script); + jenkins.assertBuildStatus(Result.SUCCESS, build); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputCommand.log").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputTests.log").exists()); + assertTrue(new FilePath(jenkins.getInstance().getWorkspaceFor(pipelineProject), "outputBuild.log").exists()); + jenkins.assertLogContains("-logfile outputCommand.log", build); + jenkins.assertLogContains("-logfile outputBuild.log", build); + jenkins.assertLogContains("-logfile outputTests.log", build); + } + + @Test + public void verifyStartUpOptionsInMatrixProject() throws Exception { + String matlabRoot = System.getenv("MATLAB_ROOT"); + String matlabRoot22b = System.getenv("MATLAB_ROOT_22b"); + Assume.assumeTrue("Not running tests as MATLAB_ROOT_22b environment variable is not defined", matlabRoot22b != null && !matlabRoot22b.isEmpty()); + + Utilities.setMatlabInstallation("MATLAB_PATH_1", matlabRoot, jenkins); + Utilities.setMatlabInstallation("MATLAB_PATH_22b", matlabRoot22b, jenkins); + + MatrixProject project = jenkins.createProject(MatrixProject.class); + MatlabInstallationAxis MATLABAxis = new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1", "MATLAB_PATH_22b")); + project.setAxes(new AxisList(MATLABAxis)); + + //Command Step + RunMatlabCommandBuilder commandStep = + new RunMatlabCommandBuilder(); + commandStep.setMatlabCommand("pwd,version"); + StartupOptions startupOptions = new StartupOptions("-logfile outputCommand.log"); + commandStep.setStartupOptions(startupOptions); + project.getBuildersList().add(commandStep); + + //Run tests step + project.setScm(new ExtractResourceSCM(Utilities.getRunMATLABTestsData())); + RunMatlabTestsBuilder runTestsStep = new RunMatlabTestsBuilder(); + //Adding src folder + List list=new ArrayList(); + list.add(new SourceFolderPaths("src")); + runTestsStep.setSourceFolder(new SourceFolder(list)); + + // Adding list of test folder + List testFolders = new ArrayList(); + testFolders.add(new TestFolders("test/TestSquare")); + runTestsStep.setSelectByFolder(new SelectByFolder(testFolders)); + runTestsStep.setStartupOptions(new StartupOptions("-logfile outputTests.log")); + project.getBuildersList().add(runTestsStep); + + //Rub Build step + RunMatlabBuildBuilder buildStep = new RunMatlabBuildBuilder(); + buildStep.setTasks("check"); + buildStep.setStartupOptions(new StartupOptions("-logfile outputBuild.log")); + project.getBuildersList().add(buildStep); + + MatrixBuild build = project.scheduleBuild2(0).get(); + + Combination c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_1"))), "MATLAB_PATH_1"); + MatrixRun run = build.getRun(c); + assertTrue(new FilePath(run.getWorkspace(), "outputCommand.log").exists()); + assertTrue(new FilePath(run.getWorkspace(), "outputBuild.log").exists()); + assertTrue(new FilePath(run.getWorkspace(), "outputTests.log").exists()); + + c = new Combination(new AxisList(new MatlabInstallationAxis(Arrays.asList("MATLAB_PATH_22b"))), "MATLAB_PATH_22b"); + run = build.getRun(c); + assertTrue(new FilePath(run.getWorkspace(), "outputCommand.log").exists()); + assertTrue(new FilePath(run.getWorkspace(), "outputBuild.log").exists()); + assertTrue(new FilePath(run.getWorkspace(), "outputTests.log").exists()); + + jenkins.assertBuildStatus(Result.SUCCESS, build); + } +} diff --git a/src/test/java/com/mathworks/ci/systemtests/UseMATLABVersionIT.java b/src/test/java/com/mathworks/ci/systemtests/UseMATLABVersionIT.java new file mode 100644 index 00000000..4379c10b --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/UseMATLABVersionIT.java @@ -0,0 +1,134 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.MatlabBuildWrapperContent; +import com.mathworks.ci.Message; +import com.mathworks.ci.TestMessage; +import com.mathworks.ci.UseMatlabVersionBuildWrapper; +import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; +import hudson.matrix.*; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Result; +import org.htmlunit.WebAssert; +import org.htmlunit.html.HtmlPage; +import org.junit.*; +import org.junit.rules.Timeout; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import hudson.tasks.BuildWrapper; + +public class UseMATLABVersionIT { + private FreeStyleProject project; + private UseMatlabVersionBuildWrapper buildWrapper; + + @Rule + public JenkinsRule jenkins = new JenkinsRule(); + + @Before + public void testSetup() throws IOException { + this.project = jenkins.createFreeStyleProject(); + this.buildWrapper = new UseMatlabVersionBuildWrapper(); + } + + @After + public void testTearDown() { + this.project = null; + this.buildWrapper = null; + } + + @Test + public void verifyBuildEnvForMatlab() throws Exception { + boolean found = false; + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), "")); + project.getBuildWrappersList().add(this.buildWrapper); + List bw = project.getBuildWrappersList(); + for (BuildWrapper b : bw) { + if (b.getDescriptor().getDisplayName() + .equalsIgnoreCase(Message.getValue("Buildwrapper.display.name"))) { + found = true; + } + } + Assert.assertTrue("Build does not have MATLAB build environment", found); + } + + @Test + public void verifyEmptyMatlabRootError() throws Exception { + project.getBuildWrappersList().add(this.buildWrapper); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.root.empty.error")); + } + + @Test + public void verifyValidMatlabNoWarning() throws Exception { + project.getBuildWrappersList().add(this.buildWrapper); + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), Utilities.getMatlabRoot())); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertTextNotPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.warning")); + } + + @Test + public void verifyMatrixVariableNoErrorOrWarnning() throws Exception { + project.getBuildWrappersList().add(this.buildWrapper); + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), "/test/MATLAB/$VERSION")); + HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); + WebAssert.assertTextNotPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.warning")); + } + + @Test + public void verifyBuilderFailsForInvalidMATLABPath() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), "/fake/matlabroot/that/does/not/exist")); + project.getBuildWrappersList().add(this.buildWrapper); + + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + scriptBuilder.setMatlabCommand("pwd"); + + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertBuildStatus(Result.FAILURE, build); + jenkins.assertLogContains("MatlabNotFoundError", build); + } + + @Test + public void verifyDefaultMatlabNotPicked() throws Exception { + this.buildWrapper.setMatlabBuildWrapperContent( + new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"),"/invalid/path/Matlab")); + project.getBuildWrappersList().add(this.buildWrapper); + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + scriptBuilder.setMatlabCommand("pwd"); + project.getBuildersList().add(scriptBuilder); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + jenkins.assertLogContains("MatlabNotFoundError", build); + } + + @Test + public void verifyMatrixBuildFails() throws Exception { + MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); + Axis axes = new Axis("VERSION", "R2018a", "R2015b"); + matrixProject.setAxes(new AxisList(axes)); + String matlabRoot = "path/to/matlab/R2018a"; + this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( + Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); + matrixProject.getBuildWrappersList().add(this.buildWrapper); + + RunMatlabCommandBuilder scriptBuilder = new RunMatlabCommandBuilder(); + scriptBuilder.setMatlabCommand("pwd"); + matrixProject.getBuildersList().add(scriptBuilder); + Map vals = new HashMap(); + vals.put("VERSION", "R2018a"); + Combination c1 = new Combination(vals); + MatrixRun build = matrixProject.scheduleBuild2(0).get().getRun(c1); + jenkins.assertBuildStatus(Result.FAILURE, build); + vals.put("VERSION", "R2015b"); + Combination c2 = new Combination(vals); + MatrixRun build2 = matrixProject.scheduleBuild2(0).get().getRun(c2); + jenkins.assertLogContains("MatlabNotFoundError", build2); + jenkins.assertBuildStatus(Result.FAILURE, build2); + } +} \ No newline at end of file diff --git a/src/test/java/com/mathworks/ci/systemtests/Utilities.java b/src/test/java/com/mathworks/ci/systemtests/Utilities.java new file mode 100644 index 00000000..f6e26cfb --- /dev/null +++ b/src/test/java/com/mathworks/ci/systemtests/Utilities.java @@ -0,0 +1,77 @@ +package com.mathworks.ci.systemtests; + +import com.mathworks.ci.MatlabInstallation; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; + +import static org.jvnet.hudson.test.JenkinsRule.NO_PROPERTIES; + +public class Utilities { + public static MatlabInstallation.DescriptorImpl matlabInstDescriptor; + + /* + * This method returns the environment path needed to be set for DSL pipeline scripts + */ + public static String getEnvironmentDSL() { + String environment = "environment { \n" + + "PATH = " + "\""+ getBinPath() + "${PATH}"+ "\"" + "\n" + + "}"; + return environment; + } + + /* + * This method returns the environment path needed to be set for Scripted pipeline + */ + public static String getEnvironmentScriptedPipeline() { + String environment = "env.PATH =" + '"' + getBinPath() + "${env.PATH}" + '"'; + return environment; + } + + /* + * This method returns the MATLAB Root needed for Free Style or Multi Config projects + */ + public static String getMatlabRoot() { + return System.getenv("MATLAB_ROOT"); + } + + /* + * This method returns the bin path needed for scripted pipelines based on the testing platform -- Windows or Linux + * or Mac + */ + public static String getBinPath() { + String installedPath = System.getenv("MATLAB_ROOT"); + String binPath = installedPath + "/bin:"; + + if (System.getProperty("os.name").startsWith("Win")) { + binPath = installedPath.replace("\\", "\\\\")+ "\\\\bin;"; + } + return binPath; + } + + public static void setMatlabInstallation(String name, String home, JenkinsRule jenkins) { + if(matlabInstDescriptor == null){ + Utilities.matlabInstDescriptor = jenkins.getInstance().getDescriptorByType(MatlabInstallation.DescriptorImpl.class); + } + MatlabInstallation[] prevInst = getMatlabInstallation(); + ArrayList newInst = new ArrayList<>(Arrays.asList(prevInst)); + MatlabInstallation newMatlabInstallation = new MatlabInstallation(name, home, NO_PROPERTIES); + newInst.add(newMatlabInstallation); + MatlabInstallation[] setInst = new MatlabInstallation[newInst.size()]; + matlabInstDescriptor.setInstallations(newInst.toArray(setInst)); + } + + public static MatlabInstallation[] getMatlabInstallation(){ + // static method to return all installations + return MatlabInstallation.getAll(); + } + + public static URL getRunMATLABTestsData() throws MalformedURLException { + File file = new File(System.getProperty("user.dir") + File.separator +"src" + File.separator + "test" + File.separator + "resources" + File.separator + "TestData" + File.separator + "FilterTestData.zip"); + return file.toURI().toURL(); + } +} diff --git a/src/test/java/unit/com/mathworks/ci/tools/MatlabInstallerUnitTest.java b/src/test/java/com/mathworks/ci/tools/MatlabInstallerUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/tools/MatlabInstallerUnitTest.java rename to src/test/java/com/mathworks/ci/tools/MatlabInstallerUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/utilities/GetSystemPropertiesUnitTest.java b/src/test/java/com/mathworks/ci/utilities/GetSystemPropertiesUnitTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/utilities/GetSystemPropertiesUnitTest.java rename to src/test/java/com/mathworks/ci/utilities/GetSystemPropertiesUnitTest.java diff --git a/src/test/java/unit/com/mathworks/ci/utilities/MatlabCommandRunnerTest.java b/src/test/java/com/mathworks/ci/utilities/MatlabCommandRunnerTest.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/utilities/MatlabCommandRunnerTest.java rename to src/test/java/com/mathworks/ci/utilities/MatlabCommandRunnerTest.java diff --git a/src/test/java/unit/com/mathworks/ci/utilities/MatlabCommandRunnerTester.java b/src/test/java/com/mathworks/ci/utilities/MatlabCommandRunnerTester.java similarity index 100% rename from src/test/java/unit/com/mathworks/ci/utilities/MatlabCommandRunnerTester.java rename to src/test/java/com/mathworks/ci/utilities/MatlabCommandRunnerTester.java diff --git a/src/test/java/integ/com/mathworks/ci/MatlabInstallationTest.java b/src/test/java/integ/com/mathworks/ci/MatlabInstallationTest.java deleted file mode 100644 index 288683e2..00000000 --- a/src/test/java/integ/com/mathworks/ci/MatlabInstallationTest.java +++ /dev/null @@ -1,221 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2020-2024 The MathWorks, Inc. - */ - -import hudson.matrix.AxisList; -import hudson.matrix.MatrixBuild; -import hudson.matrix.MatrixProject; -import hudson.matrix.MatrixRun; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.model.Result; -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.*; -import org.jvnet.hudson.test.JenkinsRule; -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.jvnet.hudson.test.JenkinsRule.NO_PROPERTIES; - -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; - -public class MatlabInstallationTest { - - private MatlabInstallation.DescriptorImpl matlabInstDescriptor; - private static URL url; - private static String FileSeperator; - private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; - private static String matlabExecutorAbsolutePath; - - @Rule - public JenkinsRule jenkins = new JenkinsRule(); - - @BeforeClass - public static void classSetup() throws URISyntaxException, IOException { - ClassLoader classLoader = RunMatlabTestsBuilderTest.class.getClassLoader(); - if (!System.getProperty("os.name").startsWith("Win")) { - FileSeperator = "/"; - url = classLoader.getResource("com/mathworks/ci/linux/bin/matlab.sh"); - try { - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - - // Need to do this operation due to bug in maven Resource copy plugin [ - // https://issues.apache.org/jira/browse/MRESOURCES-132 ] - - ProcessBuilder pb = new ProcessBuilder("chmod", "755", matlabExecutorAbsolutePath); - pb.start(); - } catch (IOException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } else { - FileSeperator = "\\"; - url = classLoader.getResource("com/mathworks/ci/win/bin/matlab.bat"); - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - } - } - - @Before - public void testSetup() { - this.matlabInstDescriptor = jenkins.getInstance().getDescriptorByType(MatlabInstallation.DescriptorImpl.class); - } - - @After - public void testTearDown() { - this.matlabInstDescriptor = null; - } - - private MatlabInstallation setMatlabInstallation(String name, String home) { - MatlabInstallation[] prevInst = getMatlabInstallation(); - ArrayList newInst = new ArrayList<>(Arrays.asList(prevInst)); - MatlabInstallation newMatlabInstallation = new MatlabInstallation(name, home, NO_PROPERTIES); - newInst.add(newMatlabInstallation); - MatlabInstallation[] setInst = new MatlabInstallation[newInst.size()]; - matlabInstDescriptor.setInstallations(newInst.toArray(setInst)); - return newMatlabInstallation; - } - - private MatlabInstallation[] getMatlabInstallation() { - // static method to return all installations - return MatlabInstallation.getAll(); - } - - /* - * Test to verify global tool configuration for MATLAB by doing a configuration - * round trip. - */ - @Test - public void verifyRoundTripInstallation() throws Exception { - MatlabInstallation matlabInst = setMatlabInstallation("R2019b", "C:\\FakePath\\MATLAB\\R2019b"); - MatlabInstallation matlabInst2 = setMatlabInstallation("R2020a", "/fakePath/matlab/R2020a"); - ArrayList instArr = new ArrayList<>(Arrays.asList(matlabInst, matlabInst2)); - - jenkins.configRoundtrip(); - - MatlabInstallation[] configuredMatlab = getMatlabInstallation(); - MatlabInstallation[] expectedMatlab = new MatlabInstallation[instArr.size()]; - assertEquals(2, configuredMatlab.length); - assertArrayEquals(configuredMatlab, instArr.toArray(expectedMatlab)); - } - - /* - * Test to verify usage of MATLAB tool installation in pipeline project. - */ - @Test - public void verifyInstallationInPipeline() throws Exception { - URL url = MatlabInstallationTest.class.getClassLoader().getResource("versioninfo/R2018b"); - setMatlabInstallation("R2018b", new File(url.toURI()).getAbsolutePath()); - jenkins.configRoundtrip(); - WorkflowJob project = jenkins.createProject(WorkflowJob.class); - project.setDefinition(new CpsFlowDefinition("node { \n" - + " def matlabroot \n" - + " matlabroot = tool 'R2018b' \n" - + " withEnv([\"PATH+MATLAB=$matlabroot/bin\"]) { \n" - + " echo env.PATH \n" - + " runMATLABTests(testResultsPDF:'myresult/result.pdf')}}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("versioninfo", build); - jenkins.assertLogContains("2018b", build); - jenkins.assertLogContains("bin", build); - } - - /* - * Test to verify usage of MATLAB tool installation in freestyle project. - */ - @Test - public void verifyInstallationInFreeStyle() throws Exception { - URL url = MatlabInstallationTest.class.getClassLoader().getResource("versioninfo" + FileSeperator + "R2018a"); - setMatlabInstallation("R2018b", new File(url.toURI()).getAbsolutePath()); - jenkins.configRoundtrip(); - - FreeStyleProject fsPrj = jenkins.createFreeStyleProject(); - MatlabInstallation _inst = MatlabInstallation.getInstallation("R2018b"); - MatlabBuildWrapperContent content = new MatlabBuildWrapperContent(_inst.getName(), null); - UseMatlabVersionBuildWrapper buildWrapper = new UseMatlabVersionBuildWrapper(); - buildWrapper.setMatlabBuildWrapperContent(content); - fsPrj.getBuildWrappersList().add(buildWrapper); - - FreeStyleBuild build = fsPrj.scheduleBuild2(0).get(); - // Verify correct MATLAB is invoked - jenkins.assertLogContains(_inst.getHome(), build); - } - - /* - * Test to verify if Matrix build passes with MATLAB installation(mock MATLAB). - */ - @Test - public void verifyInstallationInMatrixBuild() throws Exception { - // configure MATLAB installation - setMatlabInstallation("R2018a", matlabExecutorAbsolutePath); - setMatlabInstallation("R2018b", matlabExecutorAbsolutePath); - jenkins.configRoundtrip(); - - // configure multi-config project - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - List val = new ArrayList<>(Arrays.asList("R2018a", "R2018b")); - MatlabInstallationAxis axes = new MatlabInstallationAxis(val); - matrixProject.setAxes(new AxisList(axes)); - - RunMatlabTestsBuilderTester tester = new RunMatlabTestsBuilderTester("-positive"); - matrixProject.getBuildersList().add(tester); - - MatrixBuild build = matrixProject.scheduleBuild2(0).get(); - List runs = build.getRuns(); - for (MatrixRun run : runs) { - String matlabName = run.getBuildVariables().get("MATLAB"); - Assert.assertTrue(matlabName.equalsIgnoreCase("R2018a") || matlabName.equalsIgnoreCase("R2018b")); - } - - jenkins.assertLogContains("R2018a completed", build); - jenkins.assertLogContains("R2018b completed", build); - jenkins.assertBuildStatus(Result.SUCCESS, build); - } - - /* - * @Integ Test - * Paths should point to MATLAB executable - * Test to verify correct MATLAB installation is added to PATH environment - * variable - */ - - public void verifyInstallationPathVarInMatrixBuild() throws Exception { - // configure MATLAB installation - setMatlabInstallation("R2018a", ""); - setMatlabInstallation("R2018b", ""); - jenkins.configRoundtrip(); - - // configure multi-config project - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - List val = new ArrayList<>(Arrays.asList("R2018a", "R2018b")); - MatlabInstallationAxis axes = new MatlabInstallationAxis(val); - matrixProject.setAxes(new AxisList(axes)); - - RunMatlabTestsBuilder tester = new RunMatlabTestsBuilder(); - matrixProject.getBuildersList().add(tester); - - MatrixBuild build = matrixProject.scheduleBuild2(0).get(); - List runs = build.getRuns(); - for (MatrixRun run : runs) { - String matlabName = run.getBuildVariables().get("MATLAB"); - Assert.assertTrue(matlabName.equalsIgnoreCase("R2018a") || matlabName.equalsIgnoreCase("R2018b")); - // Verify correct MATLAB is added to path and is printed in logs. - jenkins.assertLogContains(MatlabInstallation.getInstallation(matlabName).getHome(), run); - } - - jenkins.assertLogContains("R2018a completed", build); - jenkins.assertLogContains("R2018b completed", build); - jenkins.assertBuildStatus(Result.FAILURE, build); - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabBuildBuilderTest.java b/src/test/java/integ/com/mathworks/ci/RunMatlabBuildBuilderTest.java deleted file mode 100644 index 9810988c..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabBuildBuilderTest.java +++ /dev/null @@ -1,383 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2022-2024 The MathWorks, Inc. - * - */ - -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.jvnet.hudson.test.JenkinsRule; -import hudson.EnvVars; -import hudson.matrix.Axis; -import hudson.matrix.AxisList; -import hudson.matrix.Combination; -import hudson.matrix.MatrixBuild; -import hudson.matrix.MatrixProject; -import hudson.matrix.MatrixRun; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.model.Result; -import hudson.slaves.EnvironmentVariablesNodeProperty; -import hudson.tasks.Builder; - -import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; -import com.mathworks.ci.freestyle.options.StartupOptions; -import com.mathworks.ci.freestyle.options.BuildOptions; - -public class RunMatlabBuildBuilderTest { - - private static String matlabExecutorAbsolutePath; - private FreeStyleProject project; - private UseMatlabVersionBuildWrapper buildWrapper; - private RunMatlabBuildBuilder scriptBuilder; - private static URL url; - private static String FileSeperator; - private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; - - @Rule - public JenkinsRule jenkins = new JenkinsRule(); - - @Rule - public Timeout globalTimeout = Timeout.seconds(500); - - @BeforeClass - public static void classSetup() throws URISyntaxException, IOException { - ClassLoader classLoader = RunMatlabBuildBuilderTest.class.getClassLoader(); - if (!System.getProperty("os.name").startsWith("Win")) { - FileSeperator = "/"; - url = classLoader.getResource("com/mathworks/ci/linux/bin/matlab.sh"); - try { - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - System.out.println("THE EXECUTOR PATH IS" + matlabExecutorAbsolutePath); - - // Need to do this operation due to bug in maven Resource copy plugin [ - // https://issues.apache.org/jira/browse/MRESOURCES-132 ] - - ProcessBuilder pb = new ProcessBuilder("chmod", "755", matlabExecutorAbsolutePath); - pb.start(); - } catch (IOException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } else { - FileSeperator = "\\"; - url = classLoader.getResource("com/mathworks/ci/win/bin/matlab.bat"); - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - } - } - - @Before - public void testSetup() throws IOException { - this.project = jenkins.createFreeStyleProject(); - this.scriptBuilder = new RunMatlabBuildBuilder(); - this.buildWrapper = new UseMatlabVersionBuildWrapper(); - } - - @After - public void testTearDown() { - this.project = null; - this.scriptBuilder = null; - } - - private String getMatlabroot(String version) throws URISyntaxException { - String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; - String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE; - URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)) - .orElseGet(() -> getResource(defaultVersionInfo)); - File matlabRoot = new File(matlabRootURL.toURI()); - return matlabRoot.getAbsolutePath().replace(FileSeperator + VERSION_INFO_XML_FILE, "") - .replace("R2017a", version); - } - - private URL getResource(String resource) { - return RunMatlabBuildBuilderTest.class.getClassLoader().getResource(resource); - } - - /* - * Test to verify build step contains "Run MATLAB Build" option. - */ - @Test - public void verifyBuildStepWithRunMatlab() throws Exception { - boolean found = false; - project.getBuildersList().add(scriptBuilder); - List bl = project.getBuildersList(); - for (Builder b : bl) { - if (b.getDescriptor().getDisplayName().equalsIgnoreCase( - TestMessage.getValue("Builder.build.builder.display.name"))) { - found = true; - } - } - Assert.assertTrue("Build step does not contain Run MATLAB Build option", found); - } - - /* - * Test to verify MATLAB is launched using the default MATLAB runner script. - */ - @Test - public void verifyMATLABlaunchedWithDefaultArguments() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("buildtool", build); - } - - /* - * Test to verify MATLAB is launched always from build workspace. - */ - @Test - public void verifyMATLABlaunchedfromWorkspace() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - String workspace = build.getWorkspace().getName(); - jenkins.assertLogContains("[" + workspace + "]", build); - } - - /* - * Test to verify job fails when invalid MATLAB path is provided and Exception - * is thrown - */ - @Test - public void verifyBuilderFailsForInvalidMATLABPath() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), "/fake/matlabroot/that/does/not/exist")); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.FAILURE, build); - } - - /* - * Test to verify build fails when MATLAB build fails - */ - @Test - public void verifyBuildFailureWhenMatlabBuildFails() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabBuildBuilderTester tester = new RunMatlabBuildBuilderTester(matlabExecutorAbsolutePath, - "-positiveFail"); - scriptBuilder.setTasks(""); - project.getBuildersList().add(tester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.FAILURE, build); - } - - /* - * Test to verify build suceeds when matlab build suceeds - */ - @Test - public void verifyBuildPassesWhenMatlabBuildPasses() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabBuildBuilderTester tester = new RunMatlabBuildBuilderTester(matlabExecutorAbsolutePath, "-positive"); - scriptBuilder.setTasks(""); - project.getBuildersList().add(tester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.SUCCESS, build); - - } - - /* - * Test to verify builder correctly sets tasks that user entered. - */ - @Test - public void verifyBuildPicksTheCorrectBuildBatch() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks("compile"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("Generating MATLAB script with content", build); - jenkins.assertLogContains("buildtool", build); - jenkins.assertLogContains("compile", build); - } - - /* - * Test to verify builder correctly sets startup options that user entered. - */ - @Test - public void verifyBuildPicksTheCorrectStartupOptions() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - scriptBuilder.setStartupOptions(new StartupOptions("-nojvm -uniqueoption")); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("Generating MATLAB script with content", build); - jenkins.assertLogContains("-nojvm -uniqueoption", build); - } - - /* - * Test to verify builder correctly sets build options that user entered. - */ - @Test - public void verifyBuildPicksTheCorrectBuildOptions() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - scriptBuilder.setBuildOptions(new BuildOptions("-continueOnFailure -skip compile")); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("Generating MATLAB script with content", build); - jenkins.assertLogContains("-continueOnFailure -skip compile", build); - } - - /* - * Test to verify if MATLAB scratch file is not generated in workspace for this - * builder. - */ - @Test - public void verifyMATLABscratchFileNotGenerated() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - File matlabRunner = new File(build.getWorkspace() + File.separator + "runMatlabTests.m"); - Assert.assertFalse(matlabRunner.exists()); - } - - /* - * Test to verify build supports resolving environment variable (For matrix - * builds). - */ - @Test - public void verifyBuildSupportsEnvVar() throws Exception { - EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); - EnvVars var = prop.getEnvVars(); - var.put("TASKS", "compile"); - var.put("BUILD_OPTIONS", "-continueOnFailure -skip test"); - jenkins.jenkins.getGlobalNodeProperties().add(prop); - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks("$TASKS"); - scriptBuilder.setBuildOptions(new BuildOptions("$BUILD_OPTIONS")); - project.getBuildersList().add(scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("compile", build); - jenkins.assertLogContains("-continueOnFailure -skip test", build); - } - - /* - * Test to verify if appropriate MATLAB runner file is copied in workspace. - * - * NOTE: This test assumes there is no MATLAB installed and is not on System - * Path. - * - */ - @Test - public void verifyMATLABrunnerFileGenerated() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - } - - /* - * Verify default MATLAB is not picked if invalid MATLAB path is provided - */ - @Test - public void verifyDefaultMatlabNotPicked() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2020b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setTasks(""); - project.getBuildersList().add(scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("MatlabNotFoundError", build); - } - - /* - * Test to verify if Matrix build fails when MATLAB is not available. - * - * NOTE: This test assumes there is no MATLAB installed and is not on System - * Path. - * - */ - @Test - public void verifyMatrixBuildFails() throws Exception { - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - Axis axes = new Axis("VERSION", "R2018a", "R2015b"); - matrixProject.setAxes(new AxisList(axes)); - String matlabRoot = getMatlabroot("R2018b"); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); - matrixProject.getBuildWrappersList().add(this.buildWrapper); - - scriptBuilder.setTasks(""); - matrixProject.getBuildersList().add(scriptBuilder); - - // Check for first matrix combination. - Map vals = new HashMap(); - vals.put("VERSION", "R2018a"); - Combination c1 = new Combination(vals); - MatrixRun build1 = matrixProject.scheduleBuild2(0).get().getRun(c1); - - jenkins.assertLogContains("buildtool", build1); - jenkins.assertBuildStatus(Result.FAILURE, build1); - - // Check for second Matrix combination - vals.put("VERSION", "R2015b"); - Combination c2 = new Combination(vals); - MatrixRun build2 = matrixProject.scheduleBuild2(0).get().getRun(c2); - - jenkins.assertLogContains("MatlabNotFoundError", build2); - jenkins.assertBuildStatus(Result.FAILURE, build2); - } - - /* - * Test to verify if Matrix build passes (mock MATLAB). - */ - @Test - public void verifyMatrixBuildPasses() throws Exception { - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - Axis axes = new Axis("VERSION", "R2018a", "R2018b"); - matrixProject.setAxes(new AxisList(axes)); - String matlabRoot = getMatlabroot("R2018b"); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); - matrixProject.getBuildWrappersList().add(this.buildWrapper); - RunMatlabBuildBuilderTester tester = new RunMatlabBuildBuilderTester(matlabExecutorAbsolutePath, - "-positive"); - - tester.setTasks(""); - matrixProject.getBuildersList().add(tester); - MatrixBuild build = matrixProject.scheduleBuild2(0).get(); - - jenkins.assertLogContains("R2018a completed", build); - jenkins.assertLogContains("R2018b completed", build); - jenkins.assertBuildStatus(Result.SUCCESS, build); - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabBuildBuilderTester.java b/src/test/java/integ/com/mathworks/ci/RunMatlabBuildBuilderTester.java deleted file mode 100644 index 158daacc..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabBuildBuilderTester.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2022-2024 The MathWorks, Inc. - */ - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nonnull; -import org.kohsuke.stapler.StaplerRequest; -import hudson.EnvVars; -import hudson.Extension; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.ProcStarter; -import hudson.model.AbstractProject; -import hudson.model.Result; -import hudson.model.Run; -import hudson.model.TaskListener; -import hudson.tasks.BuildStepDescriptor; -import hudson.tasks.Builder; -import net.sf.json.JSONObject; - -import com.mathworks.ci.freestyle.RunMatlabBuildBuilder; - -public class RunMatlabBuildBuilderTester extends RunMatlabBuildBuilder { - private int buildResult; - private EnvVars env; - private MatlabReleaseInfo matlabRel; - private String commandParameter; - private String matlabExecutorPath; - - public RunMatlabBuildBuilderTester(String matlabExecutorPath, String customTestPointArgument) { - super(); - this.commandParameter = customTestPointArgument; - this.matlabExecutorPath = matlabExecutorPath; - } - - // Getter and Setters to access local members - - private void setEnv(EnvVars env) { - this.env = env; - } - - @Extension - public static class Desriptor extends BuildStepDescriptor { - @Override - public String getDisplayName() { - return null; - } - - @Override - public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { - save(); - return super.configure(req, formData); - } - - @Override - public boolean isApplicable( - @SuppressWarnings("rawtypes") Class jobtype) { - return true; - } - } - - @Override - public void perform(@Nonnull Run build, @Nonnull FilePath workspace, - @Nonnull Launcher launcher, @Nonnull TaskListener listener) - throws InterruptedException, IOException { - - // Set the environment variable specific to the this build - setEnv(build.getEnvironment(listener)); - String matlabRoot = this.env.get("matlabroot"); - - // Get node specific matlabroot to get matlab version information - FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabRoot); - matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot); - - // Invoke MATLAB build and transfer output to standard - // Output Console - - buildResult = execMatlabCommand(workspace, launcher, listener); - - if (buildResult != 0) { - build.setResult(Result.FAILURE); - } - } - - private int execMatlabCommand(FilePath workspace, Launcher launcher, - TaskListener listener) throws IOException, InterruptedException { - ProcStarter matlabLauncher; - try { - matlabLauncher = launcher.launch().pwd(workspace).envs(this.env).cmds(testMatlabBuild()).stdout(listener); - - } catch (Exception e) { - listener.getLogger().println(e.getMessage()); - return 1; - } - return matlabLauncher.join(); - } - - // Private custom method to pass mock MATLAB with parameters. - private List testMatlabBuild() { - List matlabDefaultArgs = new ArrayList(); - matlabDefaultArgs.add(this.matlabExecutorPath); - matlabDefaultArgs.add(this.commandParameter); - return matlabDefaultArgs; - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabBuildStepTest.java b/src/test/java/integ/com/mathworks/ci/RunMatlabBuildStepTest.java deleted file mode 100644 index fa141756..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabBuildStepTest.java +++ /dev/null @@ -1,179 +0,0 @@ -package com.mathworks.ci.pipeline; - -/** - * Copyright 2022-2024 The MathWorks, Inc. - * - */ - -import java.io.IOException; - -import hudson.model.Result; -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; -import hudson.FilePath; -import hudson.slaves.DumbSlave; - -import com.mathworks.ci.Message; - -public class RunMatlabBuildStepTest { - - private WorkflowJob project; - - @Rule - public JenkinsRule j = new JenkinsRule(); - - @Before - public void testSetup() throws IOException { - this.project = j.createProject(WorkflowJob.class); - } - - /* - * Verify when MATLAB is not on system path. - */ - @Test - public void verifyMATLABPathNotSet() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABBuild() }", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("system path", build); - } - - /* - * Verify MATLAB gets invoked from workspace. - */ - @Test - public void verifyMATLABstartsInWorkspace() throws Exception { - DumbSlave s = j.createOnlineSlave(); - project.setDefinition( - new CpsFlowDefinition("node('!built-in') { runMATLABBuild() }", true)); - - FilePath workspace = s.getWorkspaceFor(project); - String workspaceName = workspace.getName(); - WorkflowRun build = project.scheduleBuild2(0).get(); - - j.assertLogContains(workspaceName, build); - } - - /* - * Verify MATLAB is invoked when valid MATLAB is on system path. - */ - // @Test - // public void verifyMATLABPathSet() throws Exception { - // project.setDefinition( - // new CpsFlowDefinition("node { runMATLABBuild() }", true)); - // WorkflowRun build = project.scheduleBuild2(0).get(); - // j.assertLogContains("tester_started", build); - // } - - /* - * Verify Pipeline script runs on Jenkins node with valid MATLAB - */ - @Test - public void verifyPipelineOnSlave() throws Exception { - DumbSlave s = j.createOnlineSlave(); - project.setDefinition(new CpsFlowDefinition( - "node('!built-in') { runMATLABBuild() }", true)); - - s.getWorkspaceFor(project); - WorkflowRun build = project.scheduleBuild2(0).get(); - - j.assertLogNotContains("Running on Jenkins", build); - } - - /* - * Verify build runs with default tasks if no tasks are specified - */ - @Test - public void verifyRunsWithDefaultTasks() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABBuild() }", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("buildtool", build); - } - - /* - * Verify appropriate task is invoked as in pipeline script - */ - @Test - public void verifyTasksSameAsScript() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABBuild(tasks: 'compile') }", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("compile", build); - } - - /* - * Verify appropriate startup options are invoked as in pipeline script - */ - @Test - public void verifyStartupOptionsSameAsScript() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABBuild(startupOptions: '-nojvm -uniqueoption') }", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("-nojvm -uniqueoption", build); - } - - /* - * Verify appropriate build options are invoked as in pipeline script - */ - @Test - public void verifyBuildOptionsSameAsScript() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABBuild(buildOptions: '-continueOnFailure -skip compile') }", - true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("-continueOnFailure -skip compile", build); - } - - /* - * Verify script can run Matrix build - */ - @Test - public void verifyMatrixBuild() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node { matrix {\n" + "agent any\n" + "axes {\n" + "axis {\n" + "name: 'TASKS'\n" - + "values: 'compile','lint'\n } }\n" + "runMATLABBuild(tasks: '${TASKS}')}}", - true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("compile", build); - j.assertLogContains("lint", build); - } - - /* - * Test for verifying Run Matlab Build raises exception for non-zero exit code. - */ - @Test - public void verifyExceptionForNonZeroExitCode() throws Exception { - // exitMatlab is a mock build for run_matlab_build script to exit with 1. - project.setDefinition( - new CpsFlowDefinition("node { try { runMATLABBuild() } catch(exc) { echo exc.getMessage() } }", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); - j.assertBuildStatusSuccess(build); - } - - /* - * Test for verifying Run Matlab Build raises exception for non-zero exit code. - */ - @Test - public void verifyExceptionStackTraceForNonZeroExitCode() throws Exception { - // exitMatlab is a mock build for run_matlab_command script to exit with 1. - project.setDefinition( - new CpsFlowDefinition("node { runMATLABBuild() }", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertBuildStatus(Result.FAILURE, build); - j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabCommandBuilderTest.java b/src/test/java/integ/com/mathworks/ci/RunMatlabCommandBuilderTest.java deleted file mode 100644 index 8b7bbc56..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabCommandBuilderTest.java +++ /dev/null @@ -1,428 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2019-2024 The MathWorks, Inc. - * - * Test class for RunMatlabCommandBuilderTest - * - */ - -import org.htmlunit.WebAssert; -import org.htmlunit.html.HtmlCheckBoxInput; -import org.htmlunit.html.HtmlPage; -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.Timeout; -import org.jvnet.hudson.test.JenkinsRule; -import hudson.EnvVars; -import hudson.matrix.Axis; -import hudson.matrix.AxisList; -import hudson.matrix.Combination; -import hudson.matrix.MatrixBuild; -import hudson.matrix.MatrixProject; -import hudson.matrix.MatrixRun; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.model.Result; -import hudson.slaves.EnvironmentVariablesNodeProperty; -import hudson.tasks.Builder; - -import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; -import com.mathworks.ci.freestyle.options.StartupOptions; - -public class RunMatlabCommandBuilderTest { - - private static String matlabExecutorAbsolutePath; - private FreeStyleProject project; - private UseMatlabVersionBuildWrapper buildWrapper; - private RunMatlabCommandBuilder scriptBuilder; - private static URL url; - private static String FileSeperator; - private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; - - @Rule - public JenkinsRule jenkins = new JenkinsRule(); - - @Rule - public Timeout globalTimeout = Timeout.seconds(500); - - @BeforeClass - public static void classSetup() throws URISyntaxException, IOException { - ClassLoader classLoader = RunMatlabCommandBuilderTest.class.getClassLoader(); - if (!System.getProperty("os.name").startsWith("Win")) { - FileSeperator = "/"; - url = classLoader.getResource("com/mathworks/ci/linux/bin/matlab.sh"); - try { - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - - // Need to do this operation due to bug in maven Resource copy plugin [ - // https://issues.apache.org/jira/browse/MRESOURCES-132 ] - - ProcessBuilder pb = new ProcessBuilder("chmod", "755", matlabExecutorAbsolutePath); - pb.start(); - } catch (IOException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } else { - FileSeperator = "\\"; - url = classLoader.getResource("com/mathworks/ci/win/bin/matlab.bat"); - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - } - } - - @Before - public void testSetup() throws IOException { - - this.project = jenkins.createFreeStyleProject(); - this.scriptBuilder = new RunMatlabCommandBuilder(); - this.buildWrapper = new UseMatlabVersionBuildWrapper(); - } - - @After - public void testTearDown() { - this.project = null; - this.scriptBuilder = null; - } - - private String getMatlabroot(String version) throws URISyntaxException { - String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; - String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE; - URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)) - .orElseGet(() -> getResource(defaultVersionInfo)); - File matlabRoot = new File(matlabRootURL.toURI()); - return matlabRoot.getAbsolutePath().replace(FileSeperator + VERSION_INFO_XML_FILE, "") - .replace("R2017a", version); - } - - private URL getResource(String resource) { - return RunMatlabTestsBuilderTest.class.getClassLoader().getResource(resource); - } - - /* - * Test Case to verify if Build step contains "Run MATLAB Command" option. - */ - @Test - public void verifyBuildStepWithRunMatlab() throws Exception { - boolean found = false; - scriptBuilder.setMatlabCommand(""); - project.getBuildersList().add(scriptBuilder); - List bl = project.getBuildersList(); - for (Builder b : bl) { - if (b.getDescriptor().getDisplayName().equalsIgnoreCase( - TestMessage.getValue("Builder.matlab.script.builder.display.name"))) { - found = true; - } - } - Assert.assertTrue("Build step does not contain Run MATLAB Command option", found); - } - - /* - * Test To verify MATLAB is launched using the default matlab runner binary. - * - */ - - @Test - public void verifyMATLABlaunchedWithDefaultArguments() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - } - - /* - * Test To verify MATLAB is launched always from build workspace. - * - */ - - @Test - public void verifyMATLABlaunchedfromWorkspace() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - String workspace = build.getWorkspace().getName(); - jenkins.assertLogContains(File.separator + workspace, build); - } - - /* - * Test to verify if job fails when invalid MATLAB path is provided and - * Exception is thrown - */ - - @Test - public void verifyBuilderFailsForInvalidMATLABPath() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), "/fake/matlabroot/that/does/not/exist")); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.FAILURE, build); - } - - /* - * Test to verify if Build FAILS when matlab command fails - */ - - @Test - public void verifyBuildFailureWhenMatlabCommandFails() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabCommandBuilderTester tester = new RunMatlabCommandBuilderTester(matlabExecutorAbsolutePath, - "-positiveFail"); - tester.setMatlabCommand("pp"); - project.getBuildersList().add(tester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.FAILURE, build); - } - - /* - * Test to verify if Build FAILS when matlab command fails - */ - - @Test - public void verifyBuildPassesWhenMatlabCommandPasses() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabCommandBuilderTester tester = new RunMatlabCommandBuilderTester(matlabExecutorAbsolutePath, - "-positive"); - tester.setMatlabCommand("pwd"); - project.getBuildersList().add(tester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.SUCCESS, build); - - } - - /* - * Test to verify Builder picks the exact command that user entered. - * - */ - - @Test - public void verifyBuildPicksTheCorretCommandBatch() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertLogContains("Generating MATLAB script with content", build); - jenkins.assertLogContains("pwd", build); - } - - /* - * Test to verify Builder picks the exact startup options that user entered. - * - */ - - @Test - public void verifyBuildPicksTheCorrectStartupOptions() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - scriptBuilder.setStartupOptions(new StartupOptions("-nojvm -uniqueoption")); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertLogContains("Generating MATLAB script with content", build); - jenkins.assertLogContains("pwd", build); - jenkins.assertLogContains("-nojvm -uniqueoption", build); - } - - /* - * Test to verify if MATALB scratch file is not generated in workspace for this - * builder. - */ - @Test - public void verifyMATLABscratchFileNotGenerated() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - File matlabRunner = new File(build.getWorkspace() + File.separator + "runMatlabTests.m"); - Assert.assertFalse(matlabRunner.exists()); - } - - /* - * Test to verify command supports resolving environment variable (For MATRIX - * builds). - * - */ - @Test - public void verifyCommandSupportsEnvVar() throws Exception { - EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty(); - EnvVars var = prop.getEnvVars(); - var.put("PWDCMD", "pwd"); - jenkins.jenkins.getGlobalNodeProperties().add(prop); - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("$PWDCMD"); - project.getBuildersList().add(scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("pwd", build); - } - - /* - * Test to verify if appropriate MATALB runner file is copied in workspace. - * - * NOTE: This test assumes there is no MATLAB installed and is not on System - * Path. - * - */ - @Test - public void verifyMATLABrunnerFileGenerated() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - } - - /* - * Verify default MATLAB is not picked if invalid MATLAB path is provided - */ - @Test - public void verifyDefaultMatlabNotPicked() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2020b"))); - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("pwd"); - project.getBuildersList().add(scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("MatlabNotFoundError", build); - } - - /* - * Test to verify if Matrix build fails when MATLAB is not available. - * - * NOTE: This test assumes there is no MATLAB installed and is not on System - * Path. - * - */ - @Test - public void verifyMatrixBuildFails() throws Exception { - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - Axis axes = new Axis("VERSION", "R2018a", "R2015b"); - matrixProject.setAxes(new AxisList(axes)); - String matlabRoot = getMatlabroot("R2018b"); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); - matrixProject.getBuildWrappersList().add(this.buildWrapper); - - scriptBuilder.setMatlabCommand("pwd"); - matrixProject.getBuildersList().add(scriptBuilder); - Map vals = new HashMap(); - vals.put("VERSION", "R2018a"); - Combination c1 = new Combination(vals); - MatrixRun build = matrixProject.scheduleBuild2(0).get().getRun(c1); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertBuildStatus(Result.FAILURE, build); - vals.put("VERSION", "R2015b"); - Combination c2 = new Combination(vals); - MatrixRun build2 = matrixProject.scheduleBuild2(0).get().getRun(c2); - jenkins.assertLogContains("MatlabNotFoundError", build2); - jenkins.assertBuildStatus(Result.FAILURE, build2); - } - - /* - * Test to verify if Matrix build passes (mock MATLAB). - */ - @Test - public void verifyMatrixBuildPasses() throws Exception { - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - Axis axes = new Axis("VERSION", "R2018a", "R2018b"); - matrixProject.setAxes(new AxisList(axes)); - String matlabRoot = getMatlabroot("R2018b"); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); - matrixProject.getBuildWrappersList().add(this.buildWrapper); - RunMatlabCommandBuilderTester tester = new RunMatlabCommandBuilderTester(matlabExecutorAbsolutePath, - "-positive"); - - tester.setMatlabCommand("pwd"); - matrixProject.getBuildersList().add(tester); - MatrixBuild build = matrixProject.scheduleBuild2(0).get(); - - jenkins.assertLogContains("R2018a completed", build); - jenkins.assertLogContains("R2018b completed", build); - jenkins.assertBuildStatus(Result.SUCCESS, build); - } - - /* - * Test to verify if command parses succesfully when multiple combinations of - * characters are passed. (candidate for integ-tests once integrated) - */ - public void verifyMultispecialChar() throws Exception { - final String actualCommand = "!\"\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; - final String expectedCommand = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - - project.getBuildWrappersList().add(this.buildWrapper); - scriptBuilder.setMatlabCommand("disp(" + actualCommand + ")"); - project.getBuildersList().add(this.scriptBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - - jenkins.assertLogContains("Generating MATLAB script with content", build); - jenkins.assertLogContains(expectedCommand, build); - } - - /* - * Test to verify error message when command is empty. - */ - @Test - public void verifyErrorMessageOnEmptyCommand() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.scriptBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - - WebAssert.assertTextPresent(page, "Specify at least one script, function, or statement to execute."); - } - - /* - * Test to verify no error message when command is provided. - */ - - @Test - public void verifyWhenCommandNonEmpty() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - this.scriptBuilder.setMatlabCommand("NONEMPTY"); - project.getBuildersList().add(this.scriptBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - - WebAssert.assertTextNotPresent(page, "Specify at least one script, function, or statement to execute."); - } - -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabCommandBuilderTester.java b/src/test/java/integ/com/mathworks/ci/RunMatlabCommandBuilderTester.java deleted file mode 100644 index f67fa0ec..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabCommandBuilderTester.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2019-2020 The MathWorks, Inc. Build Tester is used for unit testing to mock the actual - * build. - * - */ - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nonnull; -import org.kohsuke.stapler.StaplerRequest; -import hudson.EnvVars; -import hudson.Extension; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.ProcStarter; -import hudson.model.AbstractProject; -import hudson.model.Result; -import hudson.model.Run; -import hudson.model.TaskListener; -import hudson.tasks.BuildStepDescriptor; -import hudson.tasks.Builder; -import net.sf.json.JSONObject; - -import com.mathworks.ci.freestyle.RunMatlabCommandBuilder; - -public class RunMatlabCommandBuilderTester extends RunMatlabCommandBuilder { - private int buildResult; - private EnvVars env; - private MatlabReleaseInfo matlabRel; - private String commandParameter; - private String matlabExecutorPath; - - public RunMatlabCommandBuilderTester(String matlabExecutorPath, String customTestPointArgument) { - super(); - this.commandParameter = customTestPointArgument; - this.matlabExecutorPath = matlabExecutorPath; - } - - // Getter and Setters to access local members - - private void setEnv(EnvVars env) { - this.env = env; - } - - @Extension - public static class Desriptor extends BuildStepDescriptor { - @Override - public String getDisplayName() { - return null; - } - - @Override - public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { - save(); - return super.configure(req, formData); - } - - @Override - public boolean isApplicable( - @SuppressWarnings("rawtypes") Class jobtype) { - return true; - } - } - - @Override - public void perform(@Nonnull Run build, @Nonnull FilePath workspace, - @Nonnull Launcher launcher, @Nonnull TaskListener listener) - throws InterruptedException, IOException { - - // Set the environment variable specific to the this build - setEnv(build.getEnvironment(listener)); - String matlabRoot = this.env.get("matlabroot"); - - // Get node specific matlabroot to get matlab version information - FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabRoot); - matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot); - - // Invoke MATLAB command and transfer output to standard - // Output Console - - buildResult = execMatlabCommand(workspace, launcher, listener); - - if (buildResult != 0) { - build.setResult(Result.FAILURE); - } - } - - private int execMatlabCommand(FilePath workspace, Launcher launcher, - TaskListener listener) throws IOException, InterruptedException { - ProcStarter matlabLauncher; - try { - matlabLauncher = launcher.launch().pwd(workspace).envs(this.env).cmds(testMatlabCommand()).stdout(listener); - - } catch (Exception e) { - listener.getLogger().println(e.getMessage()); - return 1; - } - return matlabLauncher.join(); - } - - // Private custom method to pass mock MATLAB with parameters. - private List testMatlabCommand() { - List matlabDefaultArgs = new ArrayList(); - matlabDefaultArgs.add(this.matlabExecutorPath); - matlabDefaultArgs.add(this.commandParameter); - return matlabDefaultArgs; - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabCommandStepTest.java b/src/test/java/integ/com/mathworks/ci/RunMatlabCommandStepTest.java deleted file mode 100644 index 47181cea..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabCommandStepTest.java +++ /dev/null @@ -1,155 +0,0 @@ -package com.mathworks.ci.pipeline; - -/** - * Copyright 2020-2024 The MathWorks, Inc. - */ - -import java.io.IOException; - -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; -import hudson.FilePath; -import hudson.slaves.DumbSlave; - -import com.mathworks.ci.Message; - -public class RunMatlabCommandStepTest { - - private WorkflowJob project; - - @Rule - public JenkinsRule j = new JenkinsRule(); - - @Before - public void testSetup() throws IOException { - this.project = j.createProject(WorkflowJob.class); - } - - /* - * Verify when MATLAB is not in PATH variable. - */ - - @Test - public void verifyMATLABPathNotSet() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABCommand(command: 'pwd')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("system path", build); - } - - /* - * Verify MATLAB gets invoked from workspace. - */ - - @Test - public void verifyMATLABstartsInWorkspace() throws Exception { - DumbSlave s = j.createOnlineSlave(); - project.setDefinition( - new CpsFlowDefinition("node('!built-in') { runMATLABCommand(command: 'pwd')}", true)); - - FilePath workspace = s.getWorkspaceFor(project); - String workspaceName = workspace.getName(); - WorkflowRun build = project.scheduleBuild2(0).get(); - - j.assertLogContains(workspaceName, build); - } - - /* - * Verify MATLAB is invoked when valid MATLAB is in PATH. - * - */ - - // @Test - // public void verifyMATLABPathSet() throws Exception { - // project.setDefinition( - // new CpsFlowDefinition("node { runMATLABCommand(command: 'pwd')}", true)); - // WorkflowRun build = project.scheduleBuild2(0).get(); - // j.assertLogContains("tester_started", build); - // } - - /* - * Verify Pipeline script runs on Slave with valid MATLAB - * - */ - - @Test - public void verifyPipelineOnSlave() throws Exception { - DumbSlave s = j.createOnlineSlave(); - project.setDefinition(new CpsFlowDefinition( - "node('!built-in') { runMATLABCommand(command: 'pwd')}", true)); - - s.getWorkspaceFor(project); - WorkflowRun build = project.scheduleBuild2(0).get(); - - j.assertLogNotContains("Running on Jenkins", build); - } - - /* - * Verify appropriate command is invoked as in pipeline script - * - */ - - @Test - public void verifyCommandSameAsScript() throws Exception { - project.setDefinition( - new CpsFlowDefinition("node { runMATLABCommand(command: 'pwd')}", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("pwd", build); - } - - /* - * - * Verify appropriate startup options are invoked as in pipeline script - * - */ - - @Test - public void verifyStartupOptionsSameAsScript() throws Exception { - project.setDefinition( - new CpsFlowDefinition( - "node { runMATLABCommand(command: 'pwd', startupOptions: '-nojvm -uniqueoption')}", true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("-nojvm -uniqueoption", build); - } - - /* - * Verify script can run Matrix build - * - */ - - @Test - public void verifyMatrixBuild() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node { matrix {\n" + "agent any\n" + "axes {\n" + "axis {\n" + "name: 'CMD'\n" - + "values: 'pwd','ver'\n }}\n" + "runMATLABCommand(command: '${CMD}')}}", - true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("pwd", build); - j.assertLogContains("ver", build); - } - - /* - * Test for verifying Run Matlab Command raises exception for non-zero exit - * code. - */ - @Test - public void verifyExceptionForNonZeroExitCode() throws Exception { - // exitMatlab is a mock command for run_matlab_command script to exit with 1. - project.setDefinition( - new CpsFlowDefinition( - "node { try {runMATLABCommand(command: 'exitMatlab')}catch(exc){echo exc.getMessage()}}", - true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); - j.assertBuildStatusSuccess(build); - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabTestsBuilderTest.java b/src/test/java/integ/com/mathworks/ci/RunMatlabTestsBuilderTest.java deleted file mode 100644 index 44566709..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabTestsBuilderTest.java +++ /dev/null @@ -1,713 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2019-2024 The MathWorks, Inc. - * - * Test class for RunMatlabTestsBuilder - */ - -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.*; -import java.util.concurrent.ExecutionException; -import org.htmlunit.html.HtmlInput; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; -import org.htmlunit.WebAssert; -import org.htmlunit.html.HtmlCheckBoxInput; -import org.htmlunit.html.HtmlPage; -import org.htmlunit.html.HtmlSelect; -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder.CoberturaArtifact; -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder.JunitArtifact; -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder.ModelCovArtifact; -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder.PdfArtifact; -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder.StmResultsArtifact; -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder.TapArtifact; -import hudson.FilePath; -import hudson.matrix.Axis; -import hudson.matrix.AxisList; -import hudson.matrix.Combination; -import hudson.matrix.MatrixBuild; -import hudson.matrix.MatrixProject; -import hudson.matrix.MatrixRun; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.model.Result; -import hudson.tasks.Builder; - -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; -import com.mathworks.ci.freestyle.options.StartupOptions; - -import static org.junit.Assert.*; - -public class RunMatlabTestsBuilderTest { - - private static String matlabExecutorAbsolutePath; - private FreeStyleProject project; - private UseMatlabVersionBuildWrapper buildWrapper; - private RunMatlabTestsBuilder testBuilder; - private static URL url; - private static String FileSeperator; - private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; - - @Rule - public JenkinsRule jenkins = new JenkinsRule(); - - @BeforeClass - public static void classSetup() throws URISyntaxException, IOException { - ClassLoader classLoader = RunMatlabTestsBuilderTest.class.getClassLoader(); - if (!System.getProperty("os.name").startsWith("Win")) { - FileSeperator = "/"; - url = classLoader.getResource("com/mathworks/ci/linux/bin/matlab.sh"); - try { - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - - // Need to do this operation due to bug in maven Resource copy plugin [ - // https://issues.apache.org/jira/browse/MRESOURCES-132 ] - - ProcessBuilder pb = new ProcessBuilder("chmod", "755", matlabExecutorAbsolutePath); - pb.start(); - } catch (IOException e) { - e.printStackTrace(); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } else { - FileSeperator = "\\"; - url = classLoader.getResource("com/mathworks/ci/win/bin/matlab.bat"); - matlabExecutorAbsolutePath = new File(url.toURI()).getAbsolutePath(); - } - } - - @Before - public void testSetup() throws IOException { - this.project = jenkins.createFreeStyleProject(); - this.testBuilder = new RunMatlabTestsBuilder(); - testBuilder.setLoggingLevel("default"); - testBuilder.setOutputDetail("default"); - this.buildWrapper = new UseMatlabVersionBuildWrapper(); - } - - @After - public void testTearDown() { - this.project = null; - this.testBuilder = null; - } - - private String getMatlabroot(String version) throws URISyntaxException { - String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; - String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE; - URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)) - .orElseGet(() -> getResource(defaultVersionInfo)); - File matlabRoot = new File(matlabRootURL.toURI()); - return matlabRoot.getAbsolutePath().replace(FileSeperator + VERSION_INFO_XML_FILE, "") - .replace("R2017a", version); - } - - private URL getResource(String resource) { - return RunMatlabTestsBuilderTest.class.getClassLoader().getResource(resource); - } - - /* - * Test Case to verify if Build step contains "Run MATLAB Tests" option. - */ - @Test - public void verifyBuildStepWithMatlabTestBuilder() throws Exception { - boolean found = false; - project.getBuildersList().add(testBuilder); - List bl = project.getBuildersList(); - for (Builder b : bl) { - if (b.getDescriptor().getDisplayName() - .equalsIgnoreCase(Message.getBuilderDisplayName())) { - found = true; - } - } - Assert.assertTrue("Build step does not contain Run MATLAB Tests option", found); - } - - /* - * Test to verify if job fails when invalid MATLAB path is provided and - * Exception is thrown - */ - - @Test - public void verifyBuilderFailsForInvalidMATLABPath() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), "/fake/matlabroot/that/does/not/exist")); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.FAILURE, build); - } - - /* - * Test to verify if Build FAILS when matlab test fails - */ - - @Test - public void verifyBuildFailureWhenMatlabException() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilderTester tester = new RunMatlabTestsBuilderTester(matlabExecutorAbsolutePath, - "-positiveFail"); - project.getBuildersList().add(tester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.FAILURE, build); - } - - /* - * Test to verify if Build PASSES when matlab test PASSES - */ - - @Test - public void verifyBuildPassWhenTestPass() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilderTester tester = new RunMatlabTestsBuilderTester(matlabExecutorAbsolutePath, "-positive"); - project.getBuildersList().add(tester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertBuildStatus(Result.SUCCESS, build); - } - - /* - * Tests to verify if verLessThan() method compares values appropriately. - */ - - @Test - public void verifyVerlessThan() throws Exception { - FilePath matlabRoot = new FilePath(new File(getMatlabroot("R2017a"))); - MatlabReleaseInfo rel = new MatlabReleaseInfo(matlabRoot); - - // verLessthan() will check all the versions against 9.2 which is version of - // R2017a - assertFalse(rel.verLessThan(9.1)); - assertFalse(rel.verLessThan(9.0)); - assertFalse(rel.verLessThan(9.2)); - Assert.assertTrue(rel.verLessThan(9.9)); - Assert.assertTrue(rel.verLessThan(10.1)); - } - - /* - * Test to verify Builder picks the exact startup options that user entered. - * - */ - - @Test - public void verifyBuildPicksTheCorrectStartupOptions() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - testBuilder.setStartupOptions(new StartupOptions("-nojvm -uniqueoption")); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertLogContains("-nojvm -uniqueoption", build); - } - - /* - * Test to verify appropriate test atrtifact values are passed. Need to - * include in integration test. - */ - - public void verifySpecificTestArtifactsParameters() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilder.TapArtifact tap = new TapArtifact("mytap/report.tap"); - - RunMatlabTestsBuilder.StmResultsArtifact stmResults = new StmResultsArtifact("mystm/results.mldatx"); - - testBuilder.setTapArtifact(tap); - testBuilder.setStmResultsArtifact(stmResults); - - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertLogContains("TAPPlugin", build); - jenkins.assertLogContains("mytap/report.tap", build); - jenkins.assertLogContains("TestManagerResultsPlugin", build); - jenkins.assertLogContains("mystm/results.mldatx", build); - } - - /* - * Test to verify default test atrtifact file location. - */ - - @Test - public void verifyDefaultArtifactLocation() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - HtmlCheckBoxInput tapArtifact = page.getElementByName("tapArtifact"); - HtmlCheckBoxInput pdfReportArtifact = page.getElementByName("pdfReportArtifact"); - HtmlCheckBoxInput junitArtifact = page.getElementByName("junitArtifact"); - HtmlCheckBoxInput stmResultsArtifact = page.getElementByName("stmResultsArtifact"); - HtmlCheckBoxInput coberturaArtifact = page.getElementByName("coberturaArtifact"); - HtmlCheckBoxInput modelCoverageArtifact = page.getElementByName("modelCoverageArtifact"); - - tapArtifact.click(); - pdfReportArtifact.click(); - junitArtifact.click(); - stmResultsArtifact.click(); - coberturaArtifact.click(); - modelCoverageArtifact.click(); - Thread.sleep(2000); - - WebAssert.assertTextPresent(page, "matlabTestArtifacts/taptestresults.tap"); - WebAssert.assertTextPresent(page, "matlabTestArtifacts/junittestresults.xml"); - WebAssert.assertTextPresent(page, "matlabTestArtifacts/testreport.pdf"); - WebAssert.assertTextPresent(page, "matlabTestArtifacts/simulinktestresults.mldatx"); - WebAssert.assertTextPresent(page, "matlabTestArtifacts/cobertura.xml"); - WebAssert.assertTextPresent(page, "matlabTestArtifacts/coberturamodelcoverage.xml"); - } - - /* - * Test to verify text box shows up on sourceFolder option click and text is - * empty. - */ - - @Test - public void verifySourceFolderDefaultState() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - HtmlCheckBoxInput sourceFolder = page.getElementByName("_.sourceFolder"); - sourceFolder.click(); - WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.srcFolderPath\"]"); - HtmlInput srcFolderPath = page.getElementByName("_.srcFolderPath"); - assertEquals("", srcFolderPath.getTextContent()); - } - - /* - * Test to verify text box shows up on SelectBy option click and text is empty. - */ - - @Test - public void verifySelectByFolderDefaultState() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - HtmlCheckBoxInput sourceFolder = page.getElementByName("_.selectByFolder"); - sourceFolder.click(); - Thread.sleep(2000); - WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.testFolders\"]"); - HtmlInput srcFolderPath = page.getElementByName("_.testFolders"); - assertEquals("", srcFolderPath.getTextContent()); - } - - /* - * Test to verify text box shows up on SelectByTag option click and text is - * empty. - */ - - @Test - public void verifySelectByTagDefaultState() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2017a"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - HtmlCheckBoxInput sourceFolder = page.getElementByName("_.selectByTag"); - sourceFolder.click(); - Thread.sleep(2000); - WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.testTag\"]"); - HtmlInput srcFolderPath = page.getElementByName("_.testTag"); - assertEquals("", srcFolderPath.getTextContent()); - } - - /* - * Test to verify only specific test atrtifact are passed. - */ - - @Test - public void verifyAllTestArtifactsParameters() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilder.TapArtifact tap = new TapArtifact("mytap/report.tap"); - - RunMatlabTestsBuilder.PdfArtifact pdf = new PdfArtifact("mypdf/report.pdf"); - - RunMatlabTestsBuilder.JunitArtifact junit = new JunitArtifact("myjunit/report.xml"); - - RunMatlabTestsBuilder.CoberturaArtifact cobertura = new CoberturaArtifact("mycobertura/report.xml"); - - RunMatlabTestsBuilder.ModelCovArtifact modelCov = new ModelCovArtifact("mymodel/report.xml"); - - RunMatlabTestsBuilder.StmResultsArtifact stmResults = new StmResultsArtifact("mystm/results.mldatx"); - - testBuilder.setTapArtifact(tap); - testBuilder.setPdfReportArtifact(pdf); - testBuilder.setJunitArtifact(junit); - testBuilder.setCoberturaArtifact(cobertura); - testBuilder.setModelCoverageArtifact(modelCov); - testBuilder.setStmResultsArtifact(stmResults); - - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertLogContains("\'PDFTestReport\',\'mypdf/report.pdf\'", build); - jenkins.assertLogContains("\'TAPTestResults\',\'mytap/report.tap\'", build); - jenkins.assertLogContains("\'JUnitTestResults\',\'myjunit/report.xml\'", build); - jenkins.assertLogContains("\'SimulinkTestResults\',\'mystm/results.mldatx\'", build); - jenkins.assertLogContains("\'CoberturaCodeCoverage\',\'mycobertura/report.xml\'", build); - jenkins.assertLogContains("\'CoberturaModelCoverage\',\'mymodel/report.xml\'", build); - - } - - /* - * Test to verify no parameters are sent in test runner when no artifacts are - * selected. - */ - - @Test - public void verifyEmptyParameters() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - jenkins.assertLogNotContains("'OutputDetail'", build); - jenkins.assertLogNotContains("'PDFTestReport'", build); - jenkins.assertLogNotContains("'Strict'", build); - jenkins.assertLogNotContains("'SourceFolder'", build); - } - - /* - * Test to verify if appropriate MATALB runner file is copied in workspace. - * - * NOTE: This test assumes there is no MATLAB installed and is not on System - * Path. - * - */ - @Test - public void verifyMATLABrunnerFileGeneratedForAutomaticOption() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("run-matlab-command", build); - } - - /* - * Verify default MATLAB is not picked if invalid MATLAB path is provided - */ - @Test - public void verifyDefaultMatlabNotPicked() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2020b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("MatlabNotFoundError", build); - } - - /* - * Test to verify if Matrix build fails when MATLAB is not available. - * - * NOTE: This test assumes there is no MATLAB installed and is not on System - * Path. - * - */ - @Test - public void verifyMatrixBuildFails() throws Exception { - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - Axis axes = new Axis("VERSION", "R2018a", "R2015b"); - matrixProject.setAxes(new AxisList(axes)); - String matlabRoot = getMatlabroot("R2018b"); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); - matrixProject.getBuildWrappersList().add(this.buildWrapper); - - matrixProject.getBuildersList().add(testBuilder); - - // Check for first matrix combination. - - Map vals = new HashMap(); - vals.put("VERSION", "R2018a"); - Combination c1 = new Combination(vals); - MatrixRun build1 = matrixProject.scheduleBuild2(0).get().getRun(c1); - - jenkins.assertLogContains("run-matlab-command", build1); - jenkins.assertBuildStatus(Result.FAILURE, build1); - - // Check for second Matrix combination - vals.put("VERSION", "R2015b"); - Combination c2 = new Combination(vals); - MatrixRun build2 = matrixProject.scheduleBuild2(0).get().getRun(c2); - - jenkins.assertLogContains("MatlabNotFoundError", build2); - jenkins.assertBuildStatus(Result.FAILURE, build2); - } - - /* - * Test to verify if Matrix build passes (mock MATLAB). - */ - @Test - public void verifyMatrixBuildPasses() throws Exception { - MatrixProject matrixProject = jenkins.createProject(MatrixProject.class); - Axis axes = new Axis("VERSION", "R2018a", "R2018b"); - matrixProject.setAxes(new AxisList(axes)); - String matlabRoot = getMatlabroot("R2018b"); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), matlabRoot.replace("R2018b", "$VERSION"))); - matrixProject.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilderTester tester = new RunMatlabTestsBuilderTester(matlabExecutorAbsolutePath, "-positive"); - - matrixProject.getBuildersList().add(tester); - MatrixBuild build = matrixProject.scheduleBuild2(0).get(); - - jenkins.assertLogContains("Triggering", build); - jenkins.assertLogContains("R2018a completed", build); - jenkins.assertLogContains("R2018b completed", build); - jenkins.assertBuildStatus(Result.SUCCESS, build); - } - - /* - * Test to verify if MATALB scratch file is not in workspace. - */ - @Test - public void verifyMATLABscratchFileGenerated() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - File matlabRunner = new File(build.getWorkspace() + File.separator + "runnerScript.m"); - Assert.assertFalse(matlabRunner.exists()); - } - - /* - * Test to verify Use Parallel check box present. - */ - @Test - public void verifyUseParallelPresent() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.useParallel\"]"); - } - - /* - * Test to verify Strict check box present. - */ - - @Test - public void verifyStrictPresent() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertElementPresentByXPath(page, "//input[@name=\"_.strict\"]"); - } - - /* - * Test to verify Logging Level is present. - */ - - @Test - public void verifyLoggingLevelPresent() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertElementPresentByXPath(page, "//select[@name=\"_.loggingLevel\"]"); - } - - /* - * Test to verify Output Detail is present. - */ - - @Test - public void verifyOutputDetailPresent() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertElementPresentByXPath(page, "//select[@name=\"_.outputDetail\"]"); - } - - /* - * Test to verify Logging Level set to default - */ - - @Test - public void verifyLoggingLevelSetToDefault() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - HtmlSelect loggingLevel = page.getElementByName("_.loggingLevel"); - assertEquals("default", loggingLevel.getAttribute("value")); - } - - /* - * Test to verify Output Detail set to default - */ - - @Test - public void verifyOutputDetailSetToDefault() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - project.getBuildersList().add(this.testBuilder); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - HtmlSelect outputDetail = page.getElementByName("_.outputDetail"); - assertEquals("default", outputDetail.getAttribute("value")); - } - - /* - * @Integ - * Test To verify if Logging level is set correctly - * - */ - - public void verifyLoggingLevelSet() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - Map loggingLevel = new HashMap(); - loggingLevel.put("None", "'LoggingLevel', 0"); - loggingLevel.put("Terse", "'LoggingLevel', 1"); - loggingLevel.put("Concise", "'LoggingLevel', 2"); - loggingLevel.put("Detailed", "'LoggingLevel', 3"); - loggingLevel.put("Verbose", "'LoggingLevel', 4"); - loggingLevel.forEach((key, val) -> { - testBuilder.setLoggingLevel(key); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build; - try { - build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains(val, build); - } catch (InterruptedException | ExecutionException | IOException e) { - System.out.println("Build Failed, refer logs for details"); - e.printStackTrace(); - } - }); - } - - /* - * @Integ - * Test To verify if Output Detail is set correctly - * - */ - - public void verifyOutputDetailSet() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - testBuilder.setLoggingLevel("None"); - Map outputDetail = new HashMap(); - outputDetail.put("none", "'OutputDetail', 0"); - outputDetail.put("terse", "'OutputDetail', 1"); - outputDetail.put("concise", "'OutputDetail', 2"); - outputDetail.put("detailed", "'OutputDetail', 3"); - outputDetail.put("verbose", "'OutputDetail', 4"); - outputDetail.forEach((key, val) -> { - testBuilder.setOutputDetail(key); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build; - try { - build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains(val, build); - } catch (InterruptedException | ExecutionException | IOException e) { - System.out.println("Build Failed, refer logs for details"); - e.printStackTrace(); - } - }); - } - - /* - * @Integ - * Test To verify when Strict option set - * - */ - - public void verifyStrictSet() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - testBuilder.setLoggingLevel("None"); - testBuilder.setStrict(true); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("FailOnWarningsPlugin", build); - - } - - /* - * @Integ - * Test To verify when Strict option not set - * - */ - - public void verifyStrictNotSet() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - testBuilder.setLoggingLevel("None"); - testBuilder.setStrict(false); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogNotContains("FailOnWarningsPlugin", build); - - } - - /* - * @Integ - * Test To verify when Run in Parallel option is set - * - */ - - public void verifyRunParallelSet() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - testBuilder.setLoggingLevel("None"); - testBuilder.setUseParallel(true); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("runInParallel", build); - } - - /* - * @Integ - * Test To verify when Run in Parallel option is set - * - */ - - public void verifyRunParallelNotSet() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - project.getBuildWrappersList().add(this.buildWrapper); - testBuilder.setLoggingLevel("None"); - testBuilder.setUseParallel(false); - project.getBuildersList().add(this.testBuilder); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogNotContains("runInParallel", build); - } -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabTestsBuilderTester.java b/src/test/java/integ/com/mathworks/ci/RunMatlabTestsBuilderTester.java deleted file mode 100644 index 0c38793b..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabTestsBuilderTester.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2019-2024 The MathWorks, Inc. - * - * Tester builder for RunMatlabTestsBuilder. - */ - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.Nonnull; -import org.kohsuke.stapler.DataBoundSetter; -import org.kohsuke.stapler.StaplerRequest; -import hudson.EnvVars; -import hudson.Extension; -import hudson.FilePath; -import hudson.Launcher; -import hudson.Launcher.ProcStarter; -import hudson.model.AbstractProject; -import hudson.model.Result; -import hudson.model.Run; -import hudson.model.TaskListener; -import hudson.tasks.BuildStepDescriptor; -import hudson.tasks.Builder; -import net.sf.json.JSONObject; - -import com.mathworks.ci.freestyle.RunMatlabTestsBuilder; - -public class RunMatlabTestsBuilderTester extends RunMatlabTestsBuilder { - - private Artifact tapArtifact = new NullArtifact();; - private Artifact junitArtifact = new NullArtifact();; - private Artifact coberturaArtifact = new NullArtifact();; - private Artifact stmResultsArtifact = new NullArtifact();; - private Artifact modelCoverageArtifact = new NullArtifact(); - private Artifact pdfReportArtifact = new NullArtifact();; - private EnvVars env; - private int buildResult; - private MatlabReleaseInfo matlabRel; - private String matlabroot; - private String commandParameter; - private String matlabExecutorPath; - private String matlabVerName; - - public RunMatlabTestsBuilderTester(String matlabExecutorPath, String customTestPointArgument) { - super(); - this.commandParameter = customTestPointArgument; - this.matlabExecutorPath = matlabExecutorPath; - } - - public RunMatlabTestsBuilderTester(String customTestPointArgument) { - super(); - this.commandParameter = customTestPointArgument; - } - - // Getter and Setters to access local members - - @DataBoundSetter - public void setTapChkBx(TapArtifact tapArtifact) { - this.tapArtifact = tapArtifact; - } - - @DataBoundSetter - public void setJunitChkBx(JunitArtifact junitArtifact) { - this.junitArtifact = junitArtifact; - } - - @DataBoundSetter - public void setCoberturaChkBx(CoberturaArtifact coberturaArtifact) { - this.coberturaArtifact = coberturaArtifact; - } - - @DataBoundSetter - public void setStmResultsChkBx(StmResultsArtifact stmResultsArtifact) { - this.stmResultsArtifact = stmResultsArtifact; - } - - @DataBoundSetter - public void setModelCoverageChkBx(ModelCovArtifact modelCoverageArtifact) { - this.modelCoverageArtifact = modelCoverageArtifact; - } - - @DataBoundSetter - public void setPdfReportChkBx(PdfArtifact pdfReportArtifact) { - this.pdfReportArtifact = pdfReportArtifact; - } - - public Artifact getTapChkBx() { - return tapArtifact; - } - - public Artifact getJunitChkBx() { - return junitArtifact; - } - - public Artifact getCoberturaChkBx() { - return coberturaArtifact; - } - - public Artifact getStmResultsChkBx() { - return stmResultsArtifact; - } - - public Artifact getModelCoverageChkBx() { - return modelCoverageArtifact; - } - - public Artifact getPdfReportChkBx() { - return pdfReportArtifact; - } - - private void setEnv(EnvVars env) { - this.env = env; - } - - public void geetEnv(EnvVars env) { - this.env = env; - } - - public String getMatlabRoot() { - return this.matlabroot; - } - - @Extension - public static class Descriptor extends BuildStepDescriptor { - @Override - public String getDisplayName() { - return null; - } - - @Override - public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { - save(); - return super.configure(req, formData); - } - - /* - * This is to identify which project type in jenkins this should be - * applicable.(non-Javadoc) - * - * @see hudson.tasks.BuildStepDescriptor#isApplicable(java.lang.Class) - * - * if it returns true then this build step will be applicable for all project - * type. - */ - @Override - public boolean isApplicable( - @SuppressWarnings("rawtypes") Class jobtype) { - return true; - } - } - - @Override - public void perform(@Nonnull Run build, @Nonnull FilePath workspace, - @Nonnull Launcher launcher, @Nonnull TaskListener listener) - throws InterruptedException, IOException { - // Set the environment variable specific to the this build - setEnv(build.getEnvironment(listener)); - - this.matlabroot = this.env.get("matlabroot"); - if (this.matlabroot != null) { - FilePath nodeSpecificMatlabRoot = new FilePath(launcher.getChannel(), matlabroot); - matlabRel = new MatlabReleaseInfo(nodeSpecificMatlabRoot); - } - - buildResult = execCommand(workspace, launcher, listener); - if (buildResult != 0) { - build.setResult(Result.FAILURE); - } - } - - public int execCommand(FilePath workspace, Launcher launcher, TaskListener listener) - throws IOException, InterruptedException { - if (this.matlabExecutorPath == null) { - this.matlabVerName = this.env.get(Message.getValue("Axis.matlab.key")); - this.matlabExecutorPath = MatlabInstallation.getInstallation(this.matlabVerName).getHome(); - } - ProcStarter matlabLauncher; - try { - matlabLauncher = launcher.launch().pwd(workspace).envs(this.env).cmds(testMatlabCommand()).stdout(listener); - } catch (Exception e) { - listener.getLogger().println(e.getMessage()); - return 1; - } - return matlabLauncher.join(); - } - - private List testMatlabCommand() { - List matlabDefaultArgs = new ArrayList(); - matlabDefaultArgs.add(this.matlabExecutorPath); - matlabDefaultArgs.add(this.commandParameter); - return matlabDefaultArgs; - } - -} diff --git a/src/test/java/integ/com/mathworks/ci/RunMatlabTestsStepTest.java b/src/test/java/integ/com/mathworks/ci/RunMatlabTestsStepTest.java deleted file mode 100644 index 8e8595ca..00000000 --- a/src/test/java/integ/com/mathworks/ci/RunMatlabTestsStepTest.java +++ /dev/null @@ -1,285 +0,0 @@ -package com.mathworks.ci.pipeline; - -/** - * Copyright 2020-2024 The MathWorks, Inc. - */ - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ExecutionException; -import hudson.model.Result; -import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; -import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; -import hudson.slaves.DumbSlave; - -import com.mathworks.ci.Message; - -public class RunMatlabTestsStepTest { - - private WorkflowJob project; - - @Rule - public JenkinsRule j = new JenkinsRule(); - - @Before - public void testSetup() throws IOException { - this.project = j.createProject(WorkflowJob.class); - } - - /* - * Verify when MATLAB Path is not set - */ - @Test - public void verifyMATLABPathNotSet() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("system path", build); - } - - /* - * Verify when MATLAB PATH is set. - */ - - @Test - public void verifyMATLABPathSet() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("myresult/result.pdf", build); - } - - /* - * Verify Pipeline runs on slave node - */ - - @Test - public void verifyOnslave() throws Exception { - DumbSlave s = j.createOnlineSlave(); - project.setDefinition(new CpsFlowDefinition( - "node('!built-in') {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true)); - s.getWorkspaceFor(project); - WorkflowRun build = project.scheduleBuild2(0).get(); - - j.assertLogNotContains("Running on Jenkins", build); - } - - /* - * Verify artifact path is correct. Need to move this to integration test. - */ - - public void verifyArtifactPath() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("producingPDF('myresult/result.pdf')", build); - } - - /* - * Verify appropriate startup options are invoked as in pipeline script - * - */ - - @Test - public void verifyStartupOptionsSameAsScript() throws Exception { - project.setDefinition( - new CpsFlowDefinition( - "node {runMATLABTests(testResultsPDF:'myresult/result.pdf', startupOptions: '-nojvm -uniqueoption')}", - true)); - - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("-nojvm -uniqueoption", build); - } - - /* - * Verify default command options for test run. - */ - - @Test - public void verifyCmdOptions() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("setenv('MW_ORIG_WORKING_FOLDER',", build); - j.assertLogContains("run-matlab-command", build); - } - - /* - * Verify Artifact is not sent as parameter. - */ - - @Test - public void verifyArtifactParameters() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(testResultsPDF:'myresult/result.pdf')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("'PDFTestReport','myresult/result.pdf'", build); - j.assertLogNotContains("TAPTestResults", build); - j.assertLogNotContains("JUnitTestResults", build); - j.assertLogNotContains("CoberturaCodeCoverage", build); - j.assertLogNotContains("SimulinkTestResults", build); - j.assertLogNotContains("CoberturaModelCoverage", build); - } - - /* - * Verify runMatlabTests runs with empty parameters when nothing no artifact - * selected - */ - - @Test - public void verifyEmptyParameter() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests()}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("run-matlab-command", build); - j.assertLogNotContains("PDFReportPath", build); - j.assertLogNotContains("TAPResultsPath", build); - j.assertLogNotContains("JUnitResultsPath", build); - j.assertLogNotContains("CoberturaCodeCoveragePath", build); - j.assertLogNotContains("SimulinkTestResultsPath", build); - j.assertLogNotContains("CoberturaModelCoveragePath", build); - } - - @Test - public void verifyExceptionForNonZeroExitCode() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests()}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertBuildStatus(Result.FAILURE, build); - j.assertLogContains(String.format(Message.getValue("matlab.execution.exception.prefix"), 1), build); - } - - /* - * @Integ Test - * Verify default command options for test Filter using selectByFolder option - */ - - public void verifyTestSelectByFolder() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(selectByFolder:['mytest1','mytest2'])}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("mytest1", build); - j.assertLogContains("mytest2", build); - } - - /* - * @Integ Test - * Verify default command options for test Filter using selectByTag option - */ - - public void verifyTestSelectByTag() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(selectByTag: 'myTestTag')}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("myTestTag", build); - } - - /* - * @Integ - * Verify outputDetail set - */ - - public void verifyOutputDetailSet() { - Map outputDetail = new HashMap(); - outputDetail.put("none", "'OutputDetail', 0"); - outputDetail.put("terse", "'OutputDetail', 1"); - outputDetail.put("concise", "'OutputDetail', 2"); - outputDetail.put("detailed", "'OutputDetail', 3"); - outputDetail.put("verbose", "'OutputDetail', 4"); - outputDetail.forEach((key, val) -> { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(outputDetail: '" + key + "')}", true)); - WorkflowRun build; - - try { - build = project.scheduleBuild2(0).get(); - j.assertLogContains(val, build); - } catch (InterruptedException | ExecutionException | IOException e) { - System.out.println("Build Failed, refer logs for details"); - e.printStackTrace(); - } - }); - } - - /* - * @Integ - * Verify loggingLevel set - */ - - public void verifyLoggingLevelSet() { - Map outputDetail = new HashMap(); - outputDetail.put("none", "'LoggingLevel', 0"); - outputDetail.put("terse", "'LoggingLevel', 1"); - outputDetail.put("concise", "'LoggingLevel', 2"); - outputDetail.put("detailed", "'LoggingLevel', 3"); - outputDetail.put("verbose", "'LoggingLevel', 4"); - outputDetail.forEach((key, val) -> { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(loggingLevel: '" + key + "', outputDetail: 'None')}", - true)); - WorkflowRun build; - - try { - build = project.scheduleBuild2(0).get(); - j.assertLogContains(val, build); - } catch (InterruptedException | ExecutionException | IOException e) { - System.out.println("Build Failed, refer logs for details"); - e.printStackTrace(); - } - }); - } - - /* - * @Integ - * Verify when useParallel Set - */ - - public void verifyUseParallelSet() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(useParallel: true)}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("runInParallel", build); - } - - /* - * @Integ - * Verify when useParallel Not Set - */ - - public void verifyUseParallelNotSet() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(useParallel: false)}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogNotContains("runInParallel", build); - } - - /* - * @Integ - * Verify when strict Set - */ - - public void verifyStrictSet() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(strict: true)}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogContains("FailOnWarningsPlugin", build); - } - - /* - * @Integ - * Verify when strict is not Set - */ - - public void verifyStrictNotSet() throws Exception { - project.setDefinition(new CpsFlowDefinition( - "node {runMATLABTests(strict: false)}", true)); - WorkflowRun build = project.scheduleBuild2(0).get(); - j.assertLogNotContains("FailOnWarningsPlugin", build); - } -} diff --git a/src/test/java/integ/com/mathworks/ci/UseMatlabVersionBuildWrapperTest.java b/src/test/java/integ/com/mathworks/ci/UseMatlabVersionBuildWrapperTest.java deleted file mode 100644 index bddad7c7..00000000 --- a/src/test/java/integ/com/mathworks/ci/UseMatlabVersionBuildWrapperTest.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.mathworks.ci; - -/** - * Copyright 2019-2024 The MathWorks, Inc. - * - * Test class for AddMatlabToPathBuildWrapper - */ - -import java.io.File; -import java.io.IOException; -import java.net.URISyntaxException; -import java.net.URL; -import java.util.List; -import java.util.Optional; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.jvnet.hudson.test.JenkinsRule; -import org.htmlunit.WebAssert; -import org.htmlunit.html.HtmlPage; -import hudson.model.FreeStyleBuild; -import hudson.model.FreeStyleProject; -import hudson.tasks.BuildWrapper; - -public class UseMatlabVersionBuildWrapperTest { - - private FreeStyleProject project; - private UseMatlabVersionBuildWrapper buildWrapper; - private static String FileSeperator; - private static String VERSION_INFO_XML_FILE = "VersionInfo.xml"; - - @BeforeClass - public static void classSetup() { - if (!System.getProperty("os.name").startsWith("Win")) { - FileSeperator = "/"; - } else { - FileSeperator = "\\"; - } - } - - @Rule - public JenkinsRule jenkins = new JenkinsRule(); - - @Before - public void testSetup() throws IOException { - this.project = jenkins.createFreeStyleProject(); - this.buildWrapper = new UseMatlabVersionBuildWrapper(); - } - - @After - public void testTearDown() { - this.project = null; - } - - // Private Method to get the valid MATLAB roots - private String getMatlabroot(String version) throws URISyntaxException { - String defaultVersionInfo = "versioninfo/R2017a/" + VERSION_INFO_XML_FILE; - String userVersionInfo = "versioninfo/" + version + "/" + VERSION_INFO_XML_FILE; - URL matlabRootURL = Optional.ofNullable(getResource(userVersionInfo)) - .orElseGet(() -> getResource(defaultVersionInfo)); - File matlabRoot = new File(matlabRootURL.toURI()); - return matlabRoot.getAbsolutePath().replace(FileSeperator + VERSION_INFO_XML_FILE, "").replace("R2017a", - version); - } - - private URL getResource(String resource) { - return UseMatlabVersionBuildWrapperTest.class.getClassLoader().getResource(resource); - } - - /* - * Test Case to verify if job contains MATLAB build environment section. - */ - @Test - public void verifyBuildEnvForMatlab() throws Exception { - boolean found = false; - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), "")); - project.getBuildWrappersList().add(this.buildWrapper); - List bw = project.getBuildWrappersList(); - for (BuildWrapper b : bw) { - if (b.getDescriptor().getDisplayName() - .equalsIgnoreCase(Message.getValue("Buildwrapper.display.name"))) { - found = true; - } - } - Assert.assertTrue("Build does not have MATLAB build environment", found); - } - - /* - * Verify if given MATLAB root is added in the PATH. - * Should be added to integration test. - */ - - public void verifyPATHupdated() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("/test/MATLAB/R2019a"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilderTester buildTester = new RunMatlabTestsBuilderTester("", ""); - project.getBuildersList().add(buildTester); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - Assert.assertTrue("Build does not have MATLAB build environment", - this.buildWrapper.getMatlabRootFolder().equalsIgnoreCase(buildTester.getMatlabRoot())); - } - - /* - * Verify if invalid MATLAB path throes error on console. - */ - @Test - public void verifyInvalidPATHError() throws Exception { - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("/test/MATLAB/R2019a"))); - project.getBuildWrappersList().add(this.buildWrapper); - RunMatlabTestsBuilderTester buildTester = new RunMatlabTestsBuilderTester("", ""); - project.getBuildersList().add(buildTester); - project.scheduleBuild2(0).get(); - FreeStyleBuild build = project.scheduleBuild2(0).get(); - jenkins.assertLogContains("MatlabNotFoundError", build); - } - - /* - * Test To verify if UI throws an error when MATLAB root is empty. - * - */ - - @Test - public void verifyEmptyMatlabRootError() throws Exception { - project.getBuildWrappersList().add(this.buildWrapper); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.matlab.root.empty.error")); - } - - /* - * Test To verify UI does throw error when in-valid MATLAB root entered - * - */ - - @Test - public void verifyInvalidMatlabRootDisplaysWarnning() throws Exception { - project.getBuildWrappersList().add(this.buildWrapper); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("/fake/MATLAB/path"))); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertTextPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.warning")); - } - - /* - * Test To verify UI does not throw error when matrix variables are use - * - */ - - @Test - public void verifyMatriVariableNoErrorOrWarnning() throws Exception { - project.getBuildWrappersList().add(this.buildWrapper); - this.buildWrapper.setMatlabBuildWrapperContent(new MatlabBuildWrapperContent( - Message.getValue("matlab.custom.location"), getMatlabroot("/test/MATLAB/$VERSION"))); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertTextNotPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.warning")); - } - - /* - * Test To verify UI does not throw warning when valid Matlab root is entered. - * - */ - - @Test - public void verifyValidMatlabNoWarning() throws Exception { - project.getBuildWrappersList().add(this.buildWrapper); - this.buildWrapper.setMatlabBuildWrapperContent( - new MatlabBuildWrapperContent(Message.getValue("matlab.custom.location"), getMatlabroot("R2018b"))); - HtmlPage page = jenkins.createWebClient().goTo("job/test0/configure"); - WebAssert.assertTextNotPresent(page, TestMessage.getValue("Builder.invalid.matlab.root.warning")); - } - -} diff --git a/src/test/resources/TestData/FilterTestData.zip b/src/test/resources/TestData/FilterTestData.zip new file mode 100644 index 00000000..cbcebcad Binary files /dev/null and b/src/test/resources/TestData/FilterTestData.zip differ diff --git a/src/test/resources/testconfig.properties b/src/test/resources/testconfig.properties index d01de454..3acc113a 100644 --- a/src/test/resources/testconfig.properties +++ b/src/test/resources/testconfig.properties @@ -17,4 +17,11 @@ tools.matlab.batch.executable.win = https://ssd.mathworks.com/supportfiles/ci/ma tools.matlab.mpm.installer.linux = https://www.mathworks.com/mpm/glnxa64/mpm tools.matlab.batch.executable.linux = https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/glnxa64/matlab-batch tools.matlab.mpm.installer.mac = https://www.mathworks.com/mpm/maci64/mpm -tools.matlab.batch.executable.mac = https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/maci64/matlab-batch \ No newline at end of file +tools.matlab.batch.executable.mac = https://ssd.mathworks.com/supportfiles/ci/matlab-batch/v1/maci64/matlab-batch + +pdftestreport.file.path=matlabTestArtifacts/testreport.pdf +taptestresult.file.path=matlabTestArtifacts/taptestresults.tap +junit.file.path=matlabTestArtifacts/junittestresults.xml +stmresults.file.path=matlabTestArtifacts/simulinktestresults.mldatx +cobertura.file.path=matlabTestArtifacts/cobertura.xml +modelcoverage.file.path=matlabTestArtifacts/coberturamodelcoverage.xml \ No newline at end of file