diff --git testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java index 37127ea..03a6321 100644 --- testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java +++ testutils/ptest2/src/main/java/org/apache/hive/ptest/execution/JIRAService.java @@ -101,37 +101,37 @@ void postComment(boolean error, int numTestsExecuted, SortedSet failedTe void postComment(boolean error, int numTestsExecuted, SortedSet failedTests, List messages, Set addedTests) { - DefaultHttpClient httpClient = new DefaultHttpClient(); - try { - BuildInfo buildInfo = formatBuildTag(mBuildTag); - String buildTagForLogs = formatBuildTagForLogs(mBuildTag); - List comments = Lists.newArrayList(); - comments.add(""); - comments.add(""); - if (!failedTests.isEmpty()) { - comments.add("{color:red}Overall{color}: -1 at least one tests failed"); - } else if (numTestsExecuted == 0) { - comments.add("{color:red}Overall{color}: -1 no tests executed"); - } else if (error) { - comments.add("{color:red}Overall{color}: -1 build exited with an error"); - } else { - comments.add("{color:green}Overall{color}: +1 all checks pass"); - } - comments.add(""); - if (!mPatch.isEmpty()) { - comments.add("Here are the results of testing the latest attachment:"); - comments.add(mPatch); - } - comments.add(""); + String comments = generateComments(error, numTestsExecuted, failedTests, messages, addedTests); + publishComments(comments); + } + + @VisibleForTesting + String generateComments(boolean error, int numTestsExecuted, SortedSet failedTests, + List messages, Set addedTests) { + BuildInfo buildInfo = formatBuildTag(mBuildTag); + String buildTagForLogs = formatBuildTagForLogs(mBuildTag); + List comments = Lists.newArrayList(); + comments.add(""); + comments.add(""); + if (!mPatch.isEmpty()) { + comments.add("Here are the results of testing the latest attachment:"); + comments.add(mPatch); + } + comments.add(""); + if (error) { + comments.add(formatError("-1 due to build exiting with an error")); + } else { if (addedTests.size() > 0) { comments.add(formatSuccess("+1 due to " + addedTests.size() + " test(s) being added or modified.")); } else { comments.add(formatError("-1 due to no test(s) being added or modified.")); } comments.add(""); - if (numTestsExecuted > 0) { + if (numTestsExecuted == 0) { + comments.add(formatError("-1 due to no tests executed")); + } else { if (failedTests.isEmpty()) { - comments.add(formatSuccess("+1 " + numTestsExecuted + " tests passed")); + comments.add(formatSuccess("+1 due to " + numTestsExecuted + " tests passed")); } else { comments.add(formatError("-1 due to " + failedTests.size() + " failed/errored test(s), " + numTestsExecuted + " tests executed")); @@ -140,28 +140,34 @@ void postComment(boolean error, int numTestsExecuted, SortedSet failedTe comments.addAll(failedTests); comments.add("{noformat}"); } - comments.add(""); - } - comments.add("Test results: " + mJenkinsURL + "/" + - buildInfo.getFormattedBuildTag() + "/testReport"); - comments.add("Console output: " + mJenkinsURL + "/" + - buildInfo.getFormattedBuildTag() + "/console"); - comments.add("Test logs: " + mLogsURL + buildTagForLogs); - comments.add(""); - if (!messages.isEmpty()) { - comments.add("Messages:"); - comments.add("{noformat}"); - comments.addAll(trimMessages(messages)); - comments.add("{noformat}"); - comments.add(""); } - comments.add("This message is automatically generated."); - String attachmentId = parseAttachementId(mPatch); + } + comments.add(""); + comments.add("Test results: " + mJenkinsURL + "/" + + buildInfo.getFormattedBuildTag() + "/testReport"); + comments.add("Console output: " + mJenkinsURL + "/" + + buildInfo.getFormattedBuildTag() + "/console"); + comments.add("Test logs: " + mLogsURL + buildTagForLogs); + comments.add(""); + if (!messages.isEmpty()) { + comments.add("Messages:"); + comments.add("{noformat}"); + comments.addAll(trimMessages(messages)); + comments.add("{noformat}"); comments.add(""); - comments.add("ATTACHMENT ID: " + attachmentId + - " - " + buildInfo.getBuildName()); - mLogger.info("Comment: " + Joiner.on("\n").join(comments)); - String body = Joiner.on("\n").join(comments); + } + comments.add("This message is automatically generated."); + String attachmentId = parseAttachementId(mPatch); + comments.add(""); + comments.add("ATTACHMENT ID: " + attachmentId + + " - " + buildInfo.getBuildName()); + mLogger.info("Comment: " + Joiner.on("\n").join(comments)); + return Joiner.on("\n").join(comments); + } + + void publishComments(String comments) { + DefaultHttpClient httpClient = new DefaultHttpClient(); + try { String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName); URL apiURL = new URL(mUrl); httpClient.getCredentialsProvider() @@ -174,7 +180,7 @@ void postComment(boolean error, int numTestsExecuted, SortedSet failedTe httpClient.addRequestInterceptor(new PreemptiveAuth(), 0); HttpPost request = new HttpPost(url); ObjectMapper mapper = new ObjectMapper(); - StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(body))); + StringEntity params = new StringEntity(mapper.writeValueAsString(new Body(comments))); request.addHeader("Content-Type", "application/json"); request.setEntity(params); HttpResponse httpResponse = httpClient.execute(request, localcontext); diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.java testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.java index 2ce1dc9..b97b890 100644 --- testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.java +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.java @@ -20,13 +20,42 @@ import com.google.common.collect.Lists; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; + +import com.google.common.io.Resources; import junit.framework.Assert; import org.apache.hive.ptest.execution.JIRAService.BuildInfo; +import org.apache.hive.ptest.execution.conf.TestConfiguration; +import org.approvaltests.Approvals; +import org.approvaltests.reporters.JunitReporter; +import org.approvaltests.reporters.UseReporter; +import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@UseReporter(JunitReporter.class) +public class TestJIRAService extends AbstractTestPhase { + + TestConfiguration conf; + JIRAService jiraService; + + @Before + public void setup() throws Exception { + initialize(getClass().getSimpleName()); + conf = TestConfiguration.fromInputStream( + Resources.getResource("test-configuration.properties").openStream(), logger); + conf.setPatch("https://HIVE-10000.patch"); + jiraService = new JIRAService(logger, conf, "tag-10"); + } -public class TestJIRAService { @Test public void testFormatBuildTagPositive() throws Throwable { @@ -75,4 +104,62 @@ public void testTrimMesssagesTrimmed() { expected.add(0, JIRAService.TRIMMED_MESSAGE); Assert.assertEquals(expected, JIRAService.trimMessages(messages)); } + + @Test + public void testErrorWithMessages() throws Exception { + SortedSet failedTests = new TreeSet(); + List messages = new ArrayList(); + messages.add("Error message 1"); + messages.add("Error message 2"); + Set addedTests = new HashSet(); + Approvals.verify(jiraService.generateComments(true, 0, failedTests, messages, addedTests)); + } + + @Test + public void testErrorWithoutMessages() throws Exception { + SortedSet failedTests = new TreeSet(); + List messages = new ArrayList(); + Set addedTests = new HashSet(); + Approvals.verify(jiraService.generateComments(true, 0, failedTests, messages, addedTests)); + } + + @Test + public void testFailNoAdd() throws Exception { + SortedSet failedTests = new TreeSet(); + failedTests.add("FailedTest1"); + failedTests.add("FailedTest2"); + List messages = new ArrayList(); + Set addedTests = new HashSet(); + Approvals.verify(jiraService.generateComments(false, 5, failedTests, messages, addedTests)); + } + + @Test + public void testFailAdd() throws Exception { + SortedSet failedTests = new TreeSet(); + failedTests.add("FailedTest1"); + failedTests.add("FailedTest2"); + List messages = new ArrayList(); + Set addedTests = new HashSet(); + addedTests.add("AddedTest1"); + addedTests.add("AddedTest2"); + Approvals.verify(jiraService.generateComments(false, 5, failedTests, messages, addedTests)); + } + + @Test + public void testSuccessNoAdd() throws Exception { + SortedSet failedTests = new TreeSet(); + List messages = new ArrayList(); + Set addedTests = new HashSet(); + Approvals.verify(jiraService.generateComments(false, 5, failedTests, messages, addedTests)); + } + + @Test + public void testSuccessAdd() throws Exception { + SortedSet failedTests = new TreeSet(); + List messages = new ArrayList(); + Set addedTests = new HashSet(); + addedTests.add("AddedTest1"); + addedTests.add("AddedTest2"); + Approvals.verify(jiraService.generateComments(false, 5, failedTests, messages, addedTests)); + } } \ No newline at end of file diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testErrorWithMessages.approved.txt testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testErrorWithMessages.approved.txt new file mode 100644 index 0000000..546a5f4 --- /dev/null +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testErrorWithMessages.approved.txt @@ -0,0 +1,20 @@ + + +Here are the results of testing the latest attachment: +https://HIVE-10000.patch + +{color:red}ERROR:{color} -1 due to build exiting with an error + +Test results: https://builds.apache.org/job/tag/10/testReport +Console output: https://builds.apache.org/job/tag/10/console +Test logs: http://builds.apache.org/logs/tag-10/ + +Messages: +{noformat} +Error message 1 +Error message 2 +{noformat} + +This message is automatically generated. + +ATTACHMENT ID: - tag \ No newline at end of file diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testErrorWithoutMessages.approved.txt testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testErrorWithoutMessages.approved.txt new file mode 100644 index 0000000..ec42030 --- /dev/null +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testErrorWithoutMessages.approved.txt @@ -0,0 +1,14 @@ + + +Here are the results of testing the latest attachment: +https://HIVE-10000.patch + +{color:red}ERROR:{color} -1 due to build exiting with an error + +Test results: https://builds.apache.org/job/tag/10/testReport +Console output: https://builds.apache.org/job/tag/10/console +Test logs: http://builds.apache.org/logs/tag-10/ + +This message is automatically generated. + +ATTACHMENT ID: - tag \ No newline at end of file diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testFailAdd.approved.txt testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testFailAdd.approved.txt new file mode 100644 index 0000000..07ee6b4 --- /dev/null +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testFailAdd.approved.txt @@ -0,0 +1,21 @@ + + +Here are the results of testing the latest attachment: +https://HIVE-10000.patch + +{color:green}SUCCESS:{color} +1 due to 2 test(s) being added or modified. + +{color:red}ERROR:{color} -1 due to 2 failed/errored test(s), 5 tests executed +*Failed tests:* +{noformat} +FailedTest1 +FailedTest2 +{noformat} + +Test results: https://builds.apache.org/job/tag/10/testReport +Console output: https://builds.apache.org/job/tag/10/console +Test logs: http://builds.apache.org/logs/tag-10/ + +This message is automatically generated. + +ATTACHMENT ID: - tag \ No newline at end of file diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testFailNoAdd.approved.txt testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testFailNoAdd.approved.txt new file mode 100644 index 0000000..1f86cfc --- /dev/null +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testFailNoAdd.approved.txt @@ -0,0 +1,21 @@ + + +Here are the results of testing the latest attachment: +https://HIVE-10000.patch + +{color:red}ERROR:{color} -1 due to no test(s) being added or modified. + +{color:red}ERROR:{color} -1 due to 2 failed/errored test(s), 5 tests executed +*Failed tests:* +{noformat} +FailedTest1 +FailedTest2 +{noformat} + +Test results: https://builds.apache.org/job/tag/10/testReport +Console output: https://builds.apache.org/job/tag/10/console +Test logs: http://builds.apache.org/logs/tag-10/ + +This message is automatically generated. + +ATTACHMENT ID: - tag \ No newline at end of file diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testSuccessAdd.approved.txt testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testSuccessAdd.approved.txt new file mode 100644 index 0000000..bc8839c --- /dev/null +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testSuccessAdd.approved.txt @@ -0,0 +1,16 @@ + + +Here are the results of testing the latest attachment: +https://HIVE-10000.patch + +{color:green}SUCCESS:{color} +1 due to 2 test(s) being added or modified. + +{color:green}SUCCESS:{color} +1 due to 5 tests passed + +Test results: https://builds.apache.org/job/tag/10/testReport +Console output: https://builds.apache.org/job/tag/10/console +Test logs: http://builds.apache.org/logs/tag-10/ + +This message is automatically generated. + +ATTACHMENT ID: - tag \ No newline at end of file diff --git testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testSuccessNoAdd.approved.txt testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testSuccessNoAdd.approved.txt new file mode 100644 index 0000000..8ea4d37 --- /dev/null +++ testutils/ptest2/src/test/java/org/apache/hive/ptest/execution/TestJIRAService.testSuccessNoAdd.approved.txt @@ -0,0 +1,16 @@ + + +Here are the results of testing the latest attachment: +https://HIVE-10000.patch + +{color:red}ERROR:{color} -1 due to no test(s) being added or modified. + +{color:green}SUCCESS:{color} +1 due to 5 tests passed + +Test results: https://builds.apache.org/job/tag/10/testReport +Console output: https://builds.apache.org/job/tag/10/console +Test logs: http://builds.apache.org/logs/tag-10/ + +This message is automatically generated. + +ATTACHMENT ID: - tag \ No newline at end of file diff --git testutils/ptest2/src/test/resources/test-configuration.properties testutils/ptest2/src/test/resources/test-configuration.properties index caba9ea..73a683b 100644 --- testutils/ptest2/src/test/resources/test-configuration.properties +++ testutils/ptest2/src/test/resources/test-configuration.properties @@ -6,6 +6,8 @@ host.localhost.host = localhost host.localhost.threads = 2 host.localhost.localDirs = /home/hiveptest +logsURL = http://builds.apache.org/logs/ + workingDirectory = /tmp/hive-ptest-units/working/dir profileDirectory = /etc/hiveptest/conf