From f5bb373009e3138cb185671b542123a3be99f1ef Mon Sep 17 00:00:00 2001 From: Tuan Pham Date: Thu, 14 Sep 2023 12:47:45 -0500 Subject: [PATCH] test(logging): add more test cases --- ...udWatchLoggingPluginIntegrationTests.swift | 179 +++++++++++++++++- ...oggingPluginIntegrationTestsWatch.xcscheme | 2 +- .../CloudWatchLoggingWatchApp.xcscheme | 2 +- 3 files changed, 174 insertions(+), 9 deletions(-) diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginIntegrationTests.swift b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginIntegrationTests.swift index 8c8c4ab276..70aa9d7bad 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginIntegrationTests.swift +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginIntegrationTests.swift @@ -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 { @@ -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) @@ -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 { @@ -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, diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/AWSCloudWatchLoggingPluginIntegrationTestsWatch.xcscheme b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/AWSCloudWatchLoggingPluginIntegrationTestsWatch.xcscheme index a573f66502..fba1f5ca96 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/AWSCloudWatchLoggingPluginIntegrationTestsWatch.xcscheme +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/AWSCloudWatchLoggingPluginIntegrationTestsWatch.xcscheme @@ -18,7 +18,7 @@ parallelizable = "YES"> diff --git a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/CloudWatchLoggingWatchApp.xcscheme b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/CloudWatchLoggingWatchApp.xcscheme index 131f69aa06..7b58846762 100644 --- a/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/CloudWatchLoggingWatchApp.xcscheme +++ b/AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/CloudWatchLoggingHostApp.xcodeproj/xcshareddata/xcschemes/CloudWatchLoggingWatchApp.xcscheme @@ -34,7 +34,7 @@ parallelizable = "YES">