-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOTATests.swift
316 lines (250 loc) · 13.7 KB
/
OTATests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// Copyright 2010-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
import XCTest
import AWSIoT
class OTATests: XCTestCase {
/// Expectatioun timeout value for any network operations
static let networkTimeout: TimeInterval = 120
static let dataManagerServiceKey = "OTATests-datamanager"
/// Membership in this Thing Group will cause the OTA job to be queued for the thing
static let thingGroupName = "OTA_INTEG_TEST"
// MARK: - Thing properties
let thingName: String = {
let timestamp = IoTTestHelpers.thingNameDateFormatter.string(from: Date())
return "OTATests-TestThing-\(timestamp)"
}()
// MARK: - Test lifecycle
override static func setUp() {
AWSDDLog.sharedInstance.logLevel = .debug
AWSDDLog.add(AWSDDTTYLogger.sharedInstance)
AWSTestUtility.setupSessionCredentialsProvider()
// Default IoT will be used for managing Things and Groups
// IoT DataManager used to subscribe to topics must use the ATS-enabled data endpoint
let defaultCredentialsProvider = AWSServiceManager.default().defaultServiceConfiguration.credentialsProvider
let endpointString = AWSTestUtility.getIoTEndPoint("iot-us-east1-endpoint")!
let endpoint = AWSEndpoint(urlString: endpointString)
// Stream create/describe/list operations do not use the custom endpoint specified by
// `AWSTestUtility.getIoTEndPoint("iot-us-east1-endpoint")`. Instead, these operations go to the global service
// URL (e.g., "iot.us-east-1.amazonaws.com"
let iotConfig = AWSServiceConfiguration(region: .USEast1,
endpoint: endpoint,
credentialsProvider: defaultCredentialsProvider)!
let lwt = AWSIoTMQTTLastWillAndTestament()
lwt.message = "OTATest-lwt"
lwt.topic = "OTATest-lwt-topic"
lwt.qos = .messageDeliveryAttemptedAtLeastOnce
let mqttConfig = AWSIoTMQTTConfiguration(keepAliveTimeInterval: 30.0,
baseReconnectTimeInterval: 1.0,
minimumConnectionTimeInterval: 20,
maximumReconnectTimeInterval: 8,
runLoop: .current,
runLoopMode: RunLoop.Mode.default.rawValue,
autoResubscribe: true,
lastWillAndTestament: lwt)
AWSIoTDataManager.register(with: iotConfig,
with: mqttConfig,
forKey: dataManagerServiceKey)
}
override static func tearDown() {
AWSIoT.remove(forKey: dataManagerServiceKey)
}
override func tearDown() {
// Make a best effort attempt to delete the thing created for the tests below, but ignore any errors encountered
// during the operation
do {
try IoTTestHelpers.deleteThing(withName: thingName)
} catch {
print("Ignoring error deleting thing: \(error)")
}
}
// MARK: - Test
/// - Given: A Thing added to an OTA-enabled group
/// - When: I notify the system I am ready to receive an OTA job
/// - Then: I am able to process a complete OTA job payload from start to finish
func testFullOTAJob() throws {
try IoTTestHelpers.createThing(withName: thingName)
try OTATests.addThingToOTAGroup(thingName: thingName)
let otaJobResponse = try OTATests.startOTAJob(for: thingName, testCase: self)
let clientToken = IoTTestHelpers.getClientToken(forThingName: thingName)
var otaFilePayload = OTAFilePayload(clientToken: clientToken, file: otaJobResponse.firstFile)
let otaDataCallback: AWSIoTMQTTNewMessageBlock = { data in
do {
let byteArray = [UInt8](data)
guard let cbor = try CBOR.decode(byteArray) else {
throw "Unable to decode CBOR in data callback"
}
guard let response = OTABlockResponse(fromCBOR: cbor) else {
throw "Unable to create response from cbor: \(cbor)"
}
otaFilePayload.fulfill(response: response)
} catch {
print("Unable to decode reponse: \(error)")
}
}
let manager = AWSIoTDataManager(forKey: OTATests.dataManagerServiceKey)
let dataDownloadTopic = IoTTestHelpers.getDataDownloadTopic(forThingName: thingName,
streamName: otaJobResponse.streamName)
let dataSubscriptionAcknowledged = DispatchSemaphore(value: 0)
let dataSubscriptionACKCallback: AWSIoTMQTTAckBlock = {
dataSubscriptionAcknowledged.signal()
}
// Wrap up the test
defer {
manager.unsubscribeTopic(dataDownloadTopic)
manager.disconnect()
// Wait a few seconds for the unsubscribe to finish, so we don't incur any bad access errors in the test
sleep(3)
}
DispatchQueue.global().async {
manager.subscribe(toTopic: dataDownloadTopic,
qoS: .messageDeliveryAttemptedAtMostOnce,
messageCallback: otaDataCallback,
ackCallback: dataSubscriptionACKCallback)
}
_ = dataSubscriptionAcknowledged
.wait(timeout: .now() + DispatchTimeInterval(timeInterval: OTATests.networkTimeout))
let jobDeadline = Date(timeIntervalSinceNow: OTATests.networkTimeout)
while Date() < jobDeadline && otaFilePayload.unfufilledBlockCount > 0 {
let windowRequest = otaFilePayload.nextWindowRequest()
// Uncomment the line below to view the windowRequest--this is especially useful for seeing the bitmap
// used to request the next set of blocks from the service.
// print("windowRequest: \(windowRequest.debugDescription)")
let windowRequestCBOR = windowRequest.cborEncoded
let deadline = Date(timeIntervalSinceNow: 2.5)
let expectedUnfulfilledBlocksAfterWindow = otaFilePayload.unfufilledBlockCount - windowRequest.numBlocks
let getDataTopic = IoTTestHelpers.getDataTopic(forThingName: thingName,
streamName: otaJobResponse.streamName)
manager.publishData(windowRequestCBOR,
onTopic: getDataTopic,
qoS: .messageDeliveryAttemptedAtMostOnce)
// Wait for window to process, or for deadline to expire. If the deadline expires before we process all
// blocks in the window, we'll re-request the block from the service
while Date() < deadline && otaFilePayload.unfufilledBlockCount > expectedUnfulfilledBlocksAfterWindow {
print("\(Date()) < \(deadline); \(otaFilePayload.unfufilledBlockCount) > \(expectedUnfulfilledBlocksAfterWindow)")
sleep(1)
}
}
XCTAssertEqual(otaFilePayload.unfufilledBlockCount, 0, "All blocks should be fulfilled at the end of the OTA job")
var actualPayloadBytes = [UInt8]()
otaFilePayload.responses.compactMap { $0 }.forEach {
actualPayloadBytes.append(contentsOf: $0.payload)
}
let actualData = Data(actualPayloadBytes)
let expectedDataPath = Bundle(for: type(of: self)).path(forResource: "ota_integ_test", ofType: "bin")!
let expectedDataURL = URL(fileURLWithPath: expectedDataPath)
let expectedData = try! Data(contentsOf: expectedDataURL)
XCTAssertEqual(actualData, expectedData)
// Send completion message
let completionMessage = #"{"status":"succeeded","statusDetails":{"reason":"accepted v1.0.0"}}"#
let completionTopic = IoTTestHelpers.getOTAUpdateJobTopic(forThingName: thingName,
jobId: otaJobResponse.execution.jobId)
manager.publishString(completionMessage,
onTopic: completionTopic,
qoS: .messageDeliveryAttemptedAtMostOnce)
}
// MARK: - Utilities
static func addThingToOTAGroup(thingName: String) throws {
guard let request = AWSIoTAddThingToThingGroupRequest() else {
throw "Unable to create request"
}
request.thingName = thingName
request.thingGroupName = OTATests.thingGroupName
let iot = AWSIoT.default()
var responseFromTask: AWSIoTAddThingToThingGroupResponse?
iot.addThing(toThingGroup: request).continueWith { task in
if let error = task.error {
print("Error in task: \(error)")
return nil
}
responseFromTask = task.result
return nil
}.waitUntilFinished()
guard responseFromTask != nil else {
throw "Response unexpectedly nil"
}
}
static func startOTAJob(for thingName: String,
testCase: XCTestCase? = nil,
file: String = #file,
line: Int = #line) throws -> OTAJobResponse {
let manager = AWSIoTDataManager(forKey: OTATests.dataManagerServiceKey)
let connected = DispatchSemaphore(value: 0)
manager.connectUsingWebSocket(withClientId: thingName, cleanSession: true) { status in
switch status {
case .connected:
connected.signal()
default:
break;
}
}
_ = connected.wait(timeout: .now()
+ DispatchTimeInterval(timeInterval: OTATests.networkTimeout))
let allSubscriptionsAcknowledged = DispatchGroup()
let acceptedACKCallback: AWSIoTMQTTAckBlock = {
allSubscriptionsAcknowledged.leave()
}
let notifyNextACKCallback: AWSIoTMQTTAckBlock = {
allSubscriptionsAcknowledged.leave()
}
// We could receive this document on either the /accepted or /notify-next callback, so we simply wait for it to
// be received by one of them by reusing the callback. Subsequent invocations of the callback will have no
// meaningful effect.
var otaJobResponseFromResponse: OTAJobResponse?
let otaJobResponseReceived = DispatchSemaphore(value: 0)
let jobDocumentCallback: AWSIoTMQTTExtendedNewMessageBlock = { mqttClient, topic, data in
do {
let stringFromData = String(data: data, encoding: .utf8) ?? "<unable to decode>"
print("Received response on \(topic): \(stringFromData)")
otaJobResponseFromResponse = try OTAJobResponse(jsonData: data)
print("jobId \(otaJobResponseFromResponse!.execution.jobId) is ready")
otaJobResponseReceived.signal()
} catch {
// Uncomment lines below to view decoding errors. These are innocuous during normal test execution,
// since the first response for a newly-created job won't include all the response info needed to
// deserialize.
// let stringFromData = String(data: data, encoding: .utf8) ?? "(unable to decode)"
// print("Callback received on \(topic), but could not extract jobID: \(error); data: \(stringFromData)")
return
}
}
allSubscriptionsAcknowledged.enter()
manager.subscribe(toTopic: IoTTestHelpers.getOTAGetAcceptedJobTopic(forThingName: thingName),
qoS: .messageDeliveryAttemptedAtMostOnce,
extendedCallback: jobDocumentCallback,
ackCallback: acceptedACKCallback)
allSubscriptionsAcknowledged.enter()
manager.subscribe(toTopic: IoTTestHelpers.getOTANotifyNextJobTopic(forThingName: thingName),
qoS: .messageDeliveryAttemptedAtMostOnce,
extendedCallback: jobDocumentCallback,
ackCallback: notifyNextACKCallback)
_ = allSubscriptionsAcknowledged
.wait(timeout: .now() + DispatchTimeInterval(timeInterval: OTATests.networkTimeout))
// Both subscriptions are active, now publish a "ready" message
let clientToken = IoTTestHelpers.getClientToken(forThingName: thingName)
manager.publishString(#"{"clientToken":"\#(clientToken)"}"#,
onTopic: IoTTestHelpers.getOTAGetNextJobTopic(forThingName: thingName),
qoS: .messageDeliveryAttemptedAtMostOnce)
_ = otaJobResponseReceived
.wait(timeout: .now() + DispatchTimeInterval(timeInterval: OTATests.networkTimeout))
guard let otaJobResponse = otaJobResponseFromResponse else {
testCase?.recordFailure(withDescription: "otaJobResponse should not be nil",
inFile: file,
atLine: line,
expected: true)
throw "otaJobResponse should not be nil"
}
return otaJobResponse
}
}