From 7bd2349cb67274a6d1af9e2e27685a193e87747a Mon Sep 17 00:00:00 2001 From: panbingkun Date: Fri, 21 Jun 2024 13:58:40 +0800 Subject: [PATCH] [SPARK-48490][CORE][TESTS][FOLLOWUP] Add some UT to the Windows paths scene --- .../spark/util/PatternLoggingSuite.scala | 5 +++ .../spark/util/StructuredLoggingSuite.scala | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/common/utils/src/test/scala/org/apache/spark/util/PatternLoggingSuite.scala b/common/utils/src/test/scala/org/apache/spark/util/PatternLoggingSuite.scala index 6af5f2d4f19f..2ba2b15c49f3 100644 --- a/common/utils/src/test/scala/org/apache/spark/util/PatternLoggingSuite.scala +++ b/common/utils/src/test/scala/org/apache/spark/util/PatternLoggingSuite.scala @@ -42,6 +42,11 @@ class PatternLoggingSuite extends LoggingSuiteBase with BeforeAndAfterAll { s""".*$level $className: This is a log message\nThis is a new line \t other msg\n""" } + override def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String = { + s""".*$level $className: The first message\nthe first new line\tthe first other msg\n""" + + s"""[\\s\\S]*The second message\nthe second new line\tthe second other msg\n""" + } + override def expectedPatternForBasicMsgWithException(level: Level): String = { s""".*$level $className: This is a log message\n[\\s\\S]*""" } diff --git a/common/utils/src/test/scala/org/apache/spark/util/StructuredLoggingSuite.scala b/common/utils/src/test/scala/org/apache/spark/util/StructuredLoggingSuite.scala index 598ae90402a8..a52c889b8d59 100644 --- a/common/utils/src/test/scala/org/apache/spark/util/StructuredLoggingSuite.scala +++ b/common/utils/src/test/scala/org/apache/spark/util/StructuredLoggingSuite.scala @@ -58,6 +58,11 @@ trait LoggingSuiteBase def basicMsgWithEscapeCharMDC: LogEntry = log"This is a log message\nThis is a new line \t other msg" + // scalastyle:off line.size.limit + def msgWithMDCAndEscapeChar: LogEntry = + log"The first message\nthe first new line\tthe first other msg\n${MDC(LogKeys.PATHS, "C:\\Users\\run-all_1.R\nC:\\Users\\run-all_2.R")}\nThe second message\nthe second new line\tthe second other msg" + // scalastyle:on line.size.limit + def msgWithMDC: LogEntry = log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, "1")}." def msgWithMDCValueIsNull: LogEntry = log"Lost executor ${MDC(LogKeys.EXECUTOR_ID, null)}." @@ -77,6 +82,9 @@ trait LoggingSuiteBase // test for basic message (with escape char mdc) def expectedPatternForBasicMsgWithEscapeCharMDC(level: Level): String + // test for message (with mdc and escape char) + def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String + // test for basic message and exception def expectedPatternForBasicMsgWithException(level: Level): String @@ -130,6 +138,19 @@ trait LoggingSuiteBase } } + test("Logging with MDC and escape char") { + Seq( + (Level.ERROR, () => logError(msgWithMDCAndEscapeChar)), + (Level.WARN, () => logWarning(msgWithMDCAndEscapeChar)), + (Level.INFO, () => logInfo(msgWithMDCAndEscapeChar)), + (Level.DEBUG, () => logDebug(msgWithMDCAndEscapeChar)), + (Level.TRACE, () => logTrace(msgWithMDCAndEscapeChar)) + ).foreach { case (level, logFunc) => + val logOutput = captureLogOutput(logFunc) + assert(expectedPatternForMsgWithMDCAndEscapeChar(level).r.matches(logOutput)) + } + } + test("Basic logging with Exception") { val exception = new RuntimeException("OOM") Seq( @@ -218,6 +239,7 @@ class StructuredLoggingSuite extends LoggingSuiteBase { jsonMapper.readTree(json).toString. replace("", """[^"]+"""). replace("""""""", """.*"""). + replace("", """.*"""). replace("{", """\{""") + "\n" } @@ -254,6 +276,22 @@ class StructuredLoggingSuite extends LoggingSuiteBase { }""") } + override def expectedPatternForMsgWithMDCAndEscapeChar(level: Level): String = { + // scalastyle:off line.size.limit + compactAndToRegexPattern( + s""" + { + "ts": "", + "level": "$level", + "msg": "The first message\\\\nthe first new line\\\\tthe first other msg\\\\n\\\\nThe second message\\\\nthe second new line\\\\tthe second other msg", + "context": { + "paths": "" + }, + "logger": "$className" + }""") + // scalastyle:on line.size.limit + } + override def expectedPatternForBasicMsgWithException(level: Level): String = { compactAndToRegexPattern( s"""