Skip to content

Commit

Permalink
Adding test for syslogIdentifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonystone committed Jun 1, 2018
1 parent cd34aa2 commit 4bc9738
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extension SDJournalWriterTests {
("testConvertLogLvelTrace2WithEmptyConversionTable", testConvertLogLvelTrace2WithEmptyConversionTable),
("testConvertLogLvelTrace3WithEmptyConversionTable", testConvertLogLvelTrace3WithEmptyConversionTable),
("testConvertLogLvelTrace4WithEmptyConversionTable", testConvertLogLvelTrace4WithEmptyConversionTable),
("testSyslogIdentifier",testSyslogIdentifier)

]
}
Expand Down
21 changes: 16 additions & 5 deletions Tests/TraceLogJournalWriterTests/SDJournalWriterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 4bc9738

Please sign in to comment.