@@ -13,7 +13,13 @@ import AWSCloudWatchLogs
13
13
14
14
class AWSCloudWatchLoggingPluginIntergrationTests : XCTestCase {
15
15
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
16
21
let amplifyConfigurationLoggingFile = " testconfiguration/AWSCloudWatchLoggingPluginIntegrationTests-amplifyconfiguration_logging "
22
+ #endif
17
23
var loggingConfiguration : AWSCloudWatchLoggingPluginConfiguration ?
18
24
19
25
override func setUp( ) async throws {
@@ -36,6 +42,10 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
36
42
37
43
override func tearDown( ) async throws {
38
44
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)
39
49
}
40
50
41
51
/// - Given: a AWS CloudWatch Logging plugin
@@ -52,91 +62,16 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
52
62
}
53
63
54
64
/// - 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 {
58
68
let category = " Analytics "
59
69
let namespace = UUID ( ) . uuidString
60
70
let message = " this is an error message in the integration test \( Date ( ) . epochMilliseconds) "
61
71
let logger = Amplify . Logging. logger ( forCategory: category, forNamespace: namespace)
62
72
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)
115
73
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)
140
75
logger. info ( message)
141
76
let plugin = try Amplify . Logging. getPlugin ( for: " awsCloudWatchLoggingPlugin " )
142
77
guard let loggingPlugin = plugin as? AWSCloudWatchLoggingPlugin else {
@@ -146,13 +81,15 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
146
81
try await loggingPlugin. flushLogs ( )
147
82
try await Task . sleep ( seconds: 30 )
148
83
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)
155
91
}
92
+
156
93
157
94
/// - Given: a AWS CloudWatch Logging plugin with logging enabled
158
95
/// - When: an error log message is logged and flushed
@@ -172,7 +109,8 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
172
109
try await loggingPlugin. flushLogs ( )
173
110
try await Task . sleep ( seconds: 30 )
174
111
let cloudWatchClient = loggingPlugin. getEscapeHatch ( )
175
- try await verifyMessageSent ( client: cloudWatchClient,
112
+ try await verifyMessageSent ( plugin: loggingPlugin,
113
+ client: cloudWatchClient,
176
114
logGroupName: loggingConfiguration? . logGroupName,
177
115
logLevel: " verbose " ,
178
116
message: message,
@@ -198,19 +136,52 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
198
136
try await loggingPlugin. flushLogs ( )
199
137
try await Task . sleep ( seconds: 30 )
200
138
let cloudWatchClient = loggingPlugin. getEscapeHatch ( )
201
- try await verifyMessageNotSent ( client: cloudWatchClient,
139
+ try await verifyMessageNotSent ( plugin: loggingPlugin,
140
+ client: cloudWatchClient,
202
141
logGroupName: loggingConfiguration? . logGroupName,
203
142
message: message)
204
143
}
205
144
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 ? ,
207
172
logGroupName: String ? ,
208
173
logLevel: String ,
209
174
message: String ,
210
175
category: String ,
211
176
namespace: String ) async throws {
212
177
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 )
214
185
XCTAssertEqual ( events? . count, 1 )
215
186
guard let sentLogMessage = events? . first? . message else {
216
187
XCTFail ( " Unable to verify last log message " )
@@ -222,29 +193,41 @@ class AWSCloudWatchLoggingPluginIntergrationTests: XCTestCase {
222
193
XCTAssertTrue ( sentLogMessage. contains ( namespace) )
223
194
}
224
195
225
- func verifyMessageNotSent( client: CloudWatchLogsClientProtocol ? ,
196
+ func verifyMessageNotSent( plugin: AWSCloudWatchLoggingPlugin ,
197
+ client: CloudWatchLogsClientProtocol ? ,
226
198
logGroupName: String ? ,
227
199
message: String ) async throws {
228
200
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 )
230
208
XCTAssertEqual ( events? . count, 0 )
231
209
}
232
210
233
- func getLastMessageSent( client: CloudWatchLogsClientProtocol ? ,
211
+ func getLastMessageSent( plugin: AWSCloudWatchLoggingPlugin ,
212
+ client: CloudWatchLogsClientProtocol ? ,
234
213
logGroupName: String ? ,
214
+ expectedMessageCount: Int ,
235
215
message: String ,
236
216
requestAttempt: Int ) async throws -> [ CloudWatchLogsClientTypes . FilteredLogEvent ] ? {
237
217
let endTime = Date ( )
238
218
let durationInMinutes = requestAttempt+ 1
239
219
let startTime = endTime. addingTimeInterval ( TimeInterval ( - durationInMinutes*60) )
240
220
var events = try await AWSCloudWatchClientHelper . getFilterLogEventCount ( client: client, filterPattern: message, startTime: startTime, endTime: endTime, logGroupName: logGroupName)
241
221
242
- if events? . count == 0 && requestAttempt <= 5 {
222
+ if events? . count != expectedMessageCount && requestAttempt <= 5 {
223
+ try await plugin. flushLogs ( )
243
224
try await Task . sleep ( seconds: 30 )
244
225
let attempted = requestAttempt + 1
245
226
events = try await getLastMessageSent (
227
+ plugin: plugin,
246
228
client: client,
247
229
logGroupName: logGroupName,
230
+ expectedMessageCount: expectedMessageCount,
248
231
message: message,
249
232
requestAttempt: attempted)
250
233
}
0 commit comments