From 4bc9738cf1821f0489791ce7f878f01feb8d0f52 Mon Sep 17 00:00:00 2001 From: Tony Stone Date: Thu, 31 May 2018 18:10:13 -0700 Subject: [PATCH] Adding test for syslogIdentifier. --- Tests/LinuxMain.swift | 1 + .../SDJournalWriterTests.swift | 21 ++++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 6d27bee..ed48db6 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -52,6 +52,7 @@ extension SDJournalWriterTests { ("testConvertLogLvelTrace2WithEmptyConversionTable", testConvertLogLvelTrace2WithEmptyConversionTable), ("testConvertLogLvelTrace3WithEmptyConversionTable", testConvertLogLvelTrace3WithEmptyConversionTable), ("testConvertLogLvelTrace4WithEmptyConversionTable", testConvertLogLvelTrace4WithEmptyConversionTable), + ("testSyslogIdentifier",testSyslogIdentifier) ] } diff --git a/Tests/TraceLogJournalWriterTests/SDJournalWriterTests.swift b/Tests/TraceLogJournalWriterTests/SDJournalWriterTests.swift index 5dd254c..d4b31ef 100644 --- a/Tests/TraceLogJournalWriterTests/SDJournalWriterTests.swift +++ b/Tests/TraceLogJournalWriterTests/SDJournalWriterTests.swift @@ -91,6 +91,16 @@ class SDJournalWriterTests: XCTestCase { XCTAssertEqual(SDJournalWriter(logLevelConversion: [:]).convertLogLevel(for: .trace4), LOG_INFO) } + // MARK: - Init method tests + + func testSyslogIdentifier() { + let syslogIdentifier = "TestSyslogIdentifier" + + _testLog(for: .error, TestStaticContext(), SDJournalWriter(syslogIdentifier: syslogIdentifier), syslogIdentifier) { input, writer in + + writer.log(input.timestamp, level: input.level, tag: input.tag, message: input.message, runtimeContext: input.runtimeContext, staticContext: input.staticContext) + } + } // MARK: - Direct calls to the writer with default conversion table. @@ -209,7 +219,7 @@ class TraceLogWithSDJournalWriterTests: XCTestCase { /// /// /// -private func _testLog(for level: LogLevel, _ staticContext: TestStaticContext, _ writer: SDJournalWriter, logBlock: ((timestamp: Double, level: LogLevel, tag: String, message: String, runtimeContext: TestRuntimeContext, staticContext: TestStaticContext), SDJournalWriter) -> Void) { +private func _testLog(for level: LogLevel, _ staticContext: TestStaticContext, _ writer: SDJournalWriter, _ syslogIdentifier: String = "TraceLogJournalWriterPackageTests.xctest", logBlock: ((timestamp: Double, level: LogLevel, tag: String, message: String, runtimeContext: TestRuntimeContext, staticContext: TestStaticContext), SDJournalWriter) -> Void) { /// This is the time in microseconds since the epoch UTC to match the journals time stamps. let timestamp = Date().timeIntervalSince1970 * 1000.0 @@ -220,7 +230,7 @@ private func _testLog(for level: LogLevel, _ staticContext: TestStaticContext, _ logBlock(input, writer) do { - let found = try journalEntryExists(for: input, writer: writer) + let found = try journalEntryExists(for: input, writer: writer, syslogIdentifier: syslogIdentifier) XCTAssertTrue(found) } catch { @@ -231,10 +241,10 @@ private func _testLog(for level: LogLevel, _ staticContext: TestStaticContext, _ /// /// Valdate that the log record is in the journal /// -private func journalEntryExists(for input: (timestamp: Double, level: LogLevel, tag: String, message: String, runtimeContext: TestRuntimeContext, staticContext: TestStaticContext), writer: SDJournalWriter) throws -> Bool { +private func journalEntryExists(for input: (timestamp: Double, level: LogLevel, tag: String, message: String, runtimeContext: TestRuntimeContext, staticContext: TestStaticContext), writer: SDJournalWriter, syslogIdentifier: String) throws -> Bool { let messageDate = Date(timeIntervalSince1970: input.timestamp / 1000.0) - let data = shell("journalctl -o json --identifier=TraceLogJournalWriterPackageTests.xctest --since='\(dateFormatter.string(from: messageDate))'") + let data = shell("journalctl -o json --identifier=\(syslogIdentifier) --since='\(dateFormatter.string(from: messageDate))'") /// /// The journal entries are returned one JSON object per line so split the @@ -258,7 +268,8 @@ private func journalEntryExists(for input: (timestamp: Double, level: LogLevel, journalEntry["CODE_FILE"] as? String ?? "" == input.staticContext.file && journalEntry["CODE_LINE"] as? String ?? "" == String(input.staticContext.line) && journalEntry["CODE_FUNC"] as? String ?? "" == input.staticContext.function && - journalEntry["MESSAGE"] as? String ?? "" == input.message { + journalEntry["MESSAGE"] as? String ?? "" == input.message && + journalEntry["TAG"] as? String ?? "" == input.tag { return true }