Skip to content

Commit cef43c3

Browse files
authored
test(logging): fix integration test failures (#3297)
* test(logging): fix integration tests * test(logging): fix integration tests * test(logging): clean up test schemes * test: fixing integration test workflow * test: updating integration test workflows * update integ workflow * fix logging watchos integ test * chore: update logging integration test to run in parallel
1 parent ec5a64d commit cef43c3

File tree

6 files changed

+97
-132
lines changed

6 files changed

+97
-132
lines changed

.github/workflows/integ_test_logging.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@ jobs:
8585
with:
8686
project_path: ./AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp
8787
scheme: AWSCloudWatchLoggingPluginIntegrationTestsWatch
88-
destination: platform=watchOS Simulator,name=Apple Watch Series 8 (45mm),OS=latest
88+
destination: platform=watchOS Simulator,name=Apple Watch Series 9 (45mm),OS=latest
8989
sdk: watchsimulator
90-
xcode_path: '/Applications/Xcode_14.3.app'
90+
xcode_path: '/Applications/Xcode_15.0.app'

AmplifyPlugins/Logging/Tests/AWSCloudWatchLoggingPluginHostApp/AWSCloudWatchLoggingPluginIntegrationTests/AWSCloudWatchLoggingPluginIntegrationTests.swift

Lines changed: 76 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ import AWSCloudWatchLogs
1313

1414
class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
1515
let amplifyConfigurationFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration"
16+
#if os(tvOS)
17+
let amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_tvOS"
18+
#elseif os(watchOS)
19+
let amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging_watchOS"
20+
#else
1621
let amplifyConfigurationLoggingFile = "testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging"
22+
#endif
1723
var loggingConfiguration: AWSCloudWatchLoggingPluginConfiguration?
1824

1925
override func setUp() async throws {
@@ -36,6 +42,10 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
3642

3743
override func tearDown() async throws {
3844
await Amplify.reset()
45+
let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.path ?? NSTemporaryDirectory()
46+
let directory = documents.appendingPathComponent("amplify")
47+
.appendingPathComponent("logging")
48+
try FileManager.default.removeItem(atPath: directory)
3949
}
4050

4151
/// - Given: a AWS CloudWatch Logging plugin
@@ -52,91 +62,16 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
5262
}
5363

5464
/// - Given: a AWS CloudWatch Logging plugin
55-
/// - When: an error log message is logged and flushed
56-
/// - Then: the error log message is logged and sent to AWS CloudWatch
57-
func testFlushLogWithErrorMessage() async throws {
65+
/// - When: log messages is logged and flushed
66+
/// - Then: the log messages are logged and sent to AWS CloudWatch
67+
func testFlushLogWithMessages() async throws {
5868
let category = "Analytics"
5969
let namespace = UUID().uuidString
6070
let message = "this is an error message in the integration test \(Date().epochMilliseconds)"
6171
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
6272
logger.error(message)
63-
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
64-
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
65-
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
66-
return
67-
}
68-
try await loggingPlugin.flushLogs()
69-
try await Task.sleep(seconds: 30)
70-
let cloudWatchClient = loggingPlugin.getEscapeHatch()
71-
try await verifyMessageSent(client: cloudWatchClient,
72-
logGroupName: loggingConfiguration?.logGroupName,
73-
logLevel: "error",
74-
message: message,
75-
category: category,
76-
namespace: namespace)
77-
}
78-
79-
/// - Given: a AWS CloudWatch Logging plugin
80-
/// - When: an warn log message is logged and flushed
81-
/// - Then: the warn log message is logged and sent to AWS CloudWatch
82-
func testFlushLogWithWarnMessage() async throws {
83-
let category = "API"
84-
let namespace = UUID().uuidString
85-
let message = "this is an warn message in the integration test \(Date().epochMilliseconds)"
86-
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
87-
logger.warn(message)
88-
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
89-
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
90-
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
91-
return
92-
}
93-
try await loggingPlugin.flushLogs()
94-
try await Task.sleep(seconds: 30)
95-
let cloudWatchClient = loggingPlugin.getEscapeHatch()
96-
try await verifyMessageSent(client: cloudWatchClient,
97-
logGroupName: loggingConfiguration?.logGroupName,
98-
logLevel: "warn",
99-
message: message,
100-
category: category,
101-
namespace: namespace)
102-
}
103-
104-
/// - Given: a AWS CloudWatch Logging plugin
105-
/// - When: an debug log message is logged and flushed
106-
/// - Then: the debug log message is logged and sent to AWS CloudWatch
107-
func testFlushLogWithDebugMessage() async throws {
108-
let category = "Geo"
109-
let namespace = UUID().uuidString
110-
let dateFormatter = DateFormatter()
111-
dateFormatter.dateStyle = .long
112-
dateFormatter.timeStyle = .long
113-
let message = "this is an debug message in the integration test \(Date().epochMilliseconds)"
114-
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
11573
logger.debug(message)
116-
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
117-
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
118-
XCTFail("Could not get plugin of type AWSCloudWatchLoggingPlugin")
119-
return
120-
}
121-
try await loggingPlugin.flushLogs()
122-
try await Task.sleep(seconds: 30)
123-
let cloudWatchClient = loggingPlugin.getEscapeHatch()
124-
try await verifyMessageSent(client: cloudWatchClient,
125-
logGroupName: loggingConfiguration?.logGroupName,
126-
logLevel: "debug",
127-
message: message,
128-
category: category,
129-
namespace: namespace)
130-
}
131-
132-
/// - Given: a AWS CloudWatch Logging plugin
133-
/// - When: an info log message is logged and flushed
134-
/// - Then: the info log message is logged and sent to AWS CloudWatch
135-
func testFlushLogWithInfoMessage() async throws {
136-
let category = "Auth"
137-
let namespace = UUID().uuidString
138-
let message = "this is an info message in the integration test \(Date().epochMilliseconds)"
139-
let logger = Amplify.Logging.logger(forCategory: category, forNamespace: namespace)
74+
logger.warn(message)
14075
logger.info(message)
14176
let plugin = try Amplify.Logging.getPlugin(for: "awsCloudWatchLoggingPlugin")
14277
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
@@ -146,13 +81,15 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
14681
try await loggingPlugin.flushLogs()
14782
try await Task.sleep(seconds: 30)
14883
let cloudWatchClient = loggingPlugin.getEscapeHatch()
149-
try await verifyMessageSent(client: cloudWatchClient,
150-
logGroupName: loggingConfiguration?.logGroupName,
151-
logLevel: "info",
152-
message: message,
153-
category: category,
154-
namespace: namespace)
84+
try await verifyMessagesSent(plugin: loggingPlugin,
85+
client: cloudWatchClient,
86+
logGroupName: loggingConfiguration?.logGroupName,
87+
messageCount: 4,
88+
message: message,
89+
category: category,
90+
namespace: namespace)
15591
}
92+
15693

15794
/// - Given: a AWS CloudWatch Logging plugin with logging enabled
15895
/// - When: an error log message is logged and flushed
@@ -172,7 +109,8 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
172109
try await loggingPlugin.flushLogs()
173110
try await Task.sleep(seconds: 30)
174111
let cloudWatchClient = loggingPlugin.getEscapeHatch()
175-
try await verifyMessageSent(client: cloudWatchClient,
112+
try await verifyMessageSent(plugin: loggingPlugin,
113+
client: cloudWatchClient,
176114
logGroupName: loggingConfiguration?.logGroupName,
177115
logLevel: "verbose",
178116
message: message,
@@ -198,19 +136,52 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
198136
try await loggingPlugin.flushLogs()
199137
try await Task.sleep(seconds: 30)
200138
let cloudWatchClient = loggingPlugin.getEscapeHatch()
201-
try await verifyMessageNotSent(client: cloudWatchClient,
139+
try await verifyMessageNotSent(plugin: loggingPlugin,
140+
client: cloudWatchClient,
202141
logGroupName: loggingConfiguration?.logGroupName,
203142
message: message)
204143
}
205144

206-
func verifyMessageSent(client: CloudWatchLogsClientProtocol?,
145+
func verifyMessagesSent(plugin: AWSCloudWatchLoggingPlugin,
146+
client: CloudWatchLogsClientProtocol?,
147+
logGroupName: String?,
148+
messageCount: Int,
149+
message: String,
150+
category: String,
151+
namespace: String) async throws {
152+
153+
let events = try await getLastMessageSent(
154+
plugin: plugin,
155+
client: client,
156+
logGroupName: logGroupName,
157+
expectedMessageCount: messageCount,
158+
message: message,
159+
requestAttempt: 0)
160+
XCTAssertEqual(events?.count, messageCount)
161+
guard let sentLogMessage = events?.first?.message else {
162+
XCTFail("Unable to verify last log message")
163+
return
164+
}
165+
XCTAssertTrue(sentLogMessage.contains(message))
166+
XCTAssertTrue(sentLogMessage.contains(category))
167+
XCTAssertTrue(sentLogMessage.contains(namespace))
168+
}
169+
170+
func verifyMessageSent(plugin: AWSCloudWatchLoggingPlugin,
171+
client: CloudWatchLogsClientProtocol?,
207172
logGroupName: String?,
208173
logLevel: String,
209174
message: String,
210175
category: String,
211176
namespace: String) async throws {
212177

213-
let events = try await getLastMessageSent(client: client, logGroupName: logGroupName, message: message, requestAttempt: 0)
178+
let events = try await getLastMessageSent(
179+
plugin: plugin,
180+
client: client,
181+
logGroupName: logGroupName,
182+
expectedMessageCount: 1,
183+
message: message,
184+
requestAttempt: 0)
214185
XCTAssertEqual(events?.count, 1)
215186
guard let sentLogMessage = events?.first?.message else {
216187
XCTFail("Unable to verify last log message")
@@ -222,29 +193,41 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
222193
XCTAssertTrue(sentLogMessage.contains(namespace))
223194
}
224195

225-
func verifyMessageNotSent(client: CloudWatchLogsClientProtocol?,
196+
func verifyMessageNotSent(plugin: AWSCloudWatchLoggingPlugin,
197+
client: CloudWatchLogsClientProtocol?,
226198
logGroupName: String?,
227199
message: String) async throws {
228200

229-
let events = try await getLastMessageSent(client: client, logGroupName: logGroupName, message: message, requestAttempt: 0)
201+
let events = try await getLastMessageSent(
202+
plugin: plugin,
203+
client: client,
204+
logGroupName: logGroupName,
205+
expectedMessageCount: 1,
206+
message: message,
207+
requestAttempt: 0)
230208
XCTAssertEqual(events?.count, 0)
231209
}
232210

233-
func getLastMessageSent(client: CloudWatchLogsClientProtocol?,
211+
func getLastMessageSent(plugin: AWSCloudWatchLoggingPlugin,
212+
client: CloudWatchLogsClientProtocol?,
234213
logGroupName: String?,
214+
expectedMessageCount: Int,
235215
message: String,
236216
requestAttempt: Int) async throws -> [CloudWatchLogsClientTypes.FilteredLogEvent]? {
237217
let endTime = Date()
238218
let durationInMinutes = requestAttempt+1
239219
let startTime = endTime.addingTimeInterval(TimeInterval(-durationInMinutes*60))
240220
var events = try await AWSCloudWatchClientHelper.getFilterLogEventCount(client: client, filterPattern: message, startTime: startTime, endTime: endTime, logGroupName: logGroupName)
241221

242-
if events?.count == 0 && requestAttempt <= 5 {
222+
if events?.count != expectedMessageCount && requestAttempt <= 5 {
223+
try await plugin.flushLogs()
243224
try await Task.sleep(seconds: 30)
244225
let attempted = requestAttempt + 1
245226
events = try await getLastMessageSent(
227+
plugin: plugin,
246228
client: client,
247229
logGroupName: logGroupName,
230+
expectedMessageCount: expectedMessageCount,
248231
message: message,
249232
requestAttempt: attempted)
250233
}

0 commit comments

Comments
 (0)