Skip to content

Commit

Permalink
test(logging): add more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode committed Sep 14, 2023
1 parent 4d000ef commit f5bb373
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
override func tearDown() async throws {
await Amplify.reset()
}


/// - Given: a AWS CloudWatch Logging plugin
/// - When: the escape hatch is requested
/// - Then: the AWS CloudWatch client is returned
func testGetEscapeHatch() throws {
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
Expand All @@ -48,9 +51,12 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
XCTAssertNotNil(cloudWatchClient)
}

func testFlushLog() async throws {
/// - Given: a AWS CloudWatch Logging plugin
/// - When: an error log message is logged and flushed
/// - Then: the error log message is logged and sent to AWS CloudWatch
func testFlushLogWithErrorMessage() async throws {
let category = "Analytics"
let namespace = "Integration"
let namespace = UUID().uuidString
let message = "this is an error message in the integration test"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
logger.error(message)
Expand All @@ -60,17 +66,168 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 15)
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
logLevel: "error",
message: message,
category: category,
namespace: namespace)
}

/// - Given: a AWS CloudWatch Logging plugin
/// - When: an warn log message is logged and flushed
/// - Then: the warn log message is logged and sent to AWS CloudWatch
func testFlushLogWithWarnMessage() async throws {
let category = "API"
let namespace = UUID().uuidString
let message = "this is an warn message in the integration test"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
logger.warn(message)
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
logLevel: "warn",
message: message,
category: category,
namespace: namespace)
}

/// - Given: a AWS CloudWatch Logging plugin
/// - When: an debug log message is logged and flushed
/// - Then: the debug log message is logged and sent to AWS CloudWatch
func testFlushLogWithDebugMessage() async throws {
let category = "Geo"
let namespace = UUID().uuidString
let message = "this is an debug message in the integration test"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
logger.debug(message)
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
logLevel: "debug",
message: message,
category: category,
namespace: namespace)
}

/// - Given: a AWS CloudWatch Logging plugin
/// - When: an info log message is logged and flushed
/// - Then: the info log message is logged and sent to AWS CloudWatch
func testFlushLogWithInfoMessage() async throws {
let category = "Auth"
let namespace = UUID().uuidString
let message = "this is an info message in the integration test"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
logger.info(message)
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
logLevel: "info",
message: message,
category: category,
namespace: namespace)
}

/// - Given: a AWS CloudWatch Logging plugin
/// - When: an verbose log message is logged and flushed
/// - Then: the verbose log message is logged and sent to AWS CloudWatch
func testFlushLogWithVerboseMessage() async throws {
let category = "Datastore"
let namespace = UUID().uuidString
let message = "this is an verbose message in the integration test"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
logger.verbose(message)
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
logLevel: "verbose",
message: message,
category: category,
namespace: namespace)
}

/// - Given: a AWS CloudWatch Logging plugin with logging enabled
/// - When: an error log message is logged and flushed
/// - Then: the eror log message is logged and sent to AWS CloudWatch
func testFlushLogWithVerboseMessageAfterEnablingPlugin() async throws {
let category = "Storage"
let namespace = UUID().uuidString
let message = "this is an verbose message in the integration test after enabling logging"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
Amplify.Logging.enable()
logger.verbose(message)
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
logLevel: "verbose",
message: message,
category: category,
namespace: namespace)
}

/// - Given: a AWS CloudWatch Logging plugin with logging disabled
/// - When: an error log message is logged and flushed
/// - Then: the eror log message is not logged and sent to AWS CloudWatch
func testFlushLogWithVerboseMessageAfterDisablingPlugin() async throws {
let category = "Push Notifications"
let namespace = UUID().uuidString
let message = "this is an verbose message in the integration test after disabling logging"
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
Amplify.Logging.disable()
logger.verbose(message)
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
return
}
try await loggingPlugin.flushLogs()
try await Task.sleep(seconds: 30)
let cloudWatchClient = loggingPlugin.getEscapeHatch()
try await verifyMessageNotSent(client: cloudWatchClient,
logGroupName: loggingConfiguration?.logGroupName,
message: message)
}

func verifyMessageSent(client: CloudWatchLogsClientProtocol?,
logGroupName: String?,
logLevel: String,
message: String,
category: String,
namespace: String) async throws {
Expand All @@ -81,23 +238,31 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
XCTFail("Unable to verify last log message")
return
}
print(sentLogMessage)
XCTAssertTrue(sentLogMessage.lowercased().contains(logLevel))
XCTAssertTrue(sentLogMessage.contains(message))
XCTAssertTrue(sentLogMessage.contains(category))
XCTAssertTrue(sentLogMessage.contains(namespace))
}

func verifyMessageNotSent(client: CloudWatchLogsClientProtocol?,
logGroupName: String?,
message: String) async throws {

let events = try await getLastMessageSent(client: client, logGroupName: logGroupName, message: message, requestAttempt: 0)
XCTAssertEqual(events?.count, 0)
}

func getLastMessageSent(client: CloudWatchLogsClientProtocol?,
logGroupName: String?,
message: String,
requestAttempt: Int) async throws -> [CloudWatchLogsClientTypes.FilteredLogEvent]? {
let startTime = Date().addingTimeInterval(TimeInterval(-2*60))
let startTime = Date().addingTimeInterval(TimeInterval(-3*60))
let endTime = Date()

var events = try await AWSCloudWatchClientHelper.getFilterLogEventCount(client: client, filterPattern: message, startTime: startTime, endTime: endTime, logGroupName: logGroupName)

if events?.count == 0 && requestAttempt <= 3 {
try await Task.sleep(seconds: 15)
try await Task.sleep(seconds: 30)
let attempted = requestAttempt + 1
events = try await getLastMessageSent(
client: client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "733390F12AAB8AF0006E3625"
BlueprintIdentifier = "73578A312AAB94D100505FB3"
BuildableName = "AWSCloudWatchLoggingPluginIntegrationTestsWatch.xctest"
BlueprintName = "AWSCloudWatchLoggingPluginIntegrationTestsWatch"
ReferencedContainer = "container:CloudWatchLoggingHostApp.xcodeproj">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "733390F12AAB8AF0006E3625"
BlueprintIdentifier = "73578A312AAB94D100505FB3"
BuildableName = "AWSCloudWatchLoggingPluginIntegrationTestsWatch.xctest"
BlueprintName = "AWSCloudWatchLoggingPluginIntegrationTestsWatch"
ReferencedContainer = "container:CloudWatchLoggingHostApp.xcodeproj">
Expand Down

0 comments on commit f5bb373

Please sign in to comment.