From 32e501df6c0fb66a77d278a2181e2bbc8abb14bc Mon Sep 17 00:00:00 2001 From: Mingliang Liu Date: Tue, 11 Jun 2019 00:44:54 -0700 Subject: [PATCH] HBASE-22545 TestLogLevel broken Signed-off-by: Josh Elser --- .../hadoop/hbase/http/log/TestLogLevel.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java index 5b86f912565..489dbd85e39 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java @@ -52,6 +52,7 @@ import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.authorize.AccessControlList; import org.apache.hadoop.security.ssl.SSLFactory; import org.apache.hadoop.test.GenericTestUtils; +import org.apache.hadoop.util.StringUtils; import org.apache.log4j.Level; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; @@ -409,7 +410,7 @@ public class TestLogLevel { fail("A HTTPS Client should not have succeeded in connecting to a " + "HTTP server"); } catch (SSLException e) { - GenericTestUtils.assertExceptionContains("Unrecognized SSL message", e); + exceptionShouldContains("Unrecognized SSL message", e); } } @@ -428,7 +429,7 @@ public class TestLogLevel { fail("A HTTPS Client should not have succeeded in connecting to a " + "HTTP server"); } catch (SSLException e) { - GenericTestUtils.assertExceptionContains("Unrecognized SSL message", e); + exceptionShouldContains("Unrecognized SSL message", e); } } @@ -448,8 +449,7 @@ public class TestLogLevel { fail("A HTTP Client should not have succeeded in connecting to a " + "HTTPS server"); } catch (SocketException e) { - GenericTestUtils.assertExceptionContains( - "Unexpected end of file from server", e); + exceptionShouldContains("Unexpected end of file from server", e); } } @@ -469,8 +469,29 @@ public class TestLogLevel { fail("A HTTP Client should not have succeeded in connecting to a " + "HTTPS server"); } catch (SocketException e) { - GenericTestUtils.assertExceptionContains( - "Unexpected end of file from server", e); + exceptionShouldContains("Unexpected end of file from server", e); } } + + /** + * Assert that a throwable or one of its causes should contain the substr in its message. + * + * Ideally we should use {@link GenericTestUtils#assertExceptionContains(String, Throwable)} util + * method which asserts t.toString() contains the substr. As the original throwable may have been + * wrapped in Hadoop3 because of HADOOP-12897, it's required to check all the wrapped causes. + * After stop supporting Hadoop2, this method can be removed and assertion in tests can use + * t.getCause() directly, similar to HADOOP-15280. + */ + private static void exceptionShouldContains(String substr, Throwable throwable) { + Throwable t = throwable; + while (t != null) { + String msg = t.toString(); + if (msg != null && msg.contains(substr)) { + return; + } + t = t.getCause(); + } + throw new AssertionError("Expected to find '" + substr + "' but got unexpected exception:" + + StringUtils.stringifyException(throwable), throwable); + } }