-
Notifications
You must be signed in to change notification settings - Fork 891
/
Copy pathAWSEC2Tests.m
311 lines (238 loc) · 13.3 KB
/
AWSEC2Tests.m
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
//
// Copyright 2010-2017 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 <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "AWSEC2.h"
#import "AWSTestUtility.h"
#import "AWSEC2Serializer.h"
#import "AWSXMLDictionary.h"
@interface AWSEC2Tests : XCTestCase
@end
@implementation AWSEC2Tests
+ (void)setUp {
[super setUp];
[AWSTestUtility setupSessionCredentialsProvider];
}
- (void)testClockSkewEC2 {
[AWSTestUtility setupSwizzling];
XCTAssertFalse([NSDate aws_getRuntimeClockSkew], @"current RunTimeClockSkew is not zero!");
[AWSTestUtility setMockDate:[NSDate dateWithTimeIntervalSince1970:3600]];
AWSEC2 *ec2 = [AWSEC2 defaultEC2];
XCTAssertNotNil(ec2);
AWSEC2DescribeInstancesRequest *describeInstancesRequest = [AWSEC2DescribeInstancesRequest new];
[[[ec2 describeInstances:describeInstancesRequest] continueWithBlock:^id(AWSTask *task) {
XCTAssertNil(task.error);
if (task.result) {
XCTAssertTrue([task.result isKindOfClass:[AWSEC2DescribeInstancesResult class]]);
AWSEC2DescribeInstancesResult *describeInstancesResult = task.result;
XCTAssertNotNil(describeInstancesResult.reservations);
}
return nil;
}] waitUntilFinished];
[AWSTestUtility revertSwizzling];
}
- (void)testDescribeInstances {
AWSEC2 *ec2 = [AWSEC2 defaultEC2];
AWSEC2DescribeInstancesRequest *describeInstancesRequest = [AWSEC2DescribeInstancesRequest new];
AWSEC2Filter *platformFilter = [AWSEC2Filter new];
platformFilter.name = @"platform";
platformFilter.values = @[@"windows"];
describeInstancesRequest.filters = @[platformFilter];
[[[ec2 describeInstances:describeInstancesRequest] continueWithBlock:^id(AWSTask *task) {
XCTAssertNil(task.error);
if (task.result) {
XCTAssertTrue([task.result isKindOfClass:[AWSEC2DescribeInstancesResult class]]);
AWSEC2DescribeInstancesResult *describeInstancesResult = task.result;
XCTAssertNotNil(describeInstancesResult.reservations);
for (AWSEC2Reservation *reservation in describeInstancesResult.reservations) {
for (AWSEC2Instance * instance in reservation.instances) {
if ([instance.instanceId isEqualToString:@"i-dcd2a937"]) {
XCTAssertEqual(AWSEC2PlatformValuesWindows, instance.platform);
}
}
}
}
return nil;
}] waitUntilFinished];
}
/* Commenting out this test until, we can get time to fix it.
- (void)testDescribeImages {
AWSEC2 *ec2 = [AWSEC2 defaultEC2];
AWSEC2DescribeImagesRequest *describeImagesRequest = [AWSEC2DescribeImagesRequest new];
// amazon-eks-arm64-node-1.12-v20200228
describeImagesRequest.imageIds = @[@"ami-06e3f2ff72d194f27"];
[[[ec2 describeImages:describeImagesRequest] continueWithBlock:^id(AWSTask *task) {
XCTAssertNil(task.error);
if (task.result) {
XCTAssertTrue([task.result isKindOfClass:[AWSEC2DescribeImagesResult class]]);
AWSEC2DescribeImagesResult *describeImagesResult = task.result;
XCTAssertNotNil(describeImagesResult.images);
BOOL imageExist = NO;
for (AWSEC2Image *image in describeImagesResult.images) {
if ([image.imageId isEqualToString:@"ami-06e3f2ff72d194f27"]) {
imageExist = YES;
XCTAssertEqual(AWSEC2PlatformValuesUnknown, image.platform);
}
}
XCTAssertTrue(imageExist);
}
return nil;
}] waitUntilFinished ];
}
*/
- (void)testAttachVolumeFailed {
AWSEC2 *ec2 = [AWSEC2 defaultEC2];
AWSEC2AttachVolumeRequest *volumeRequest = [AWSEC2AttachVolumeRequest new];
volumeRequest.volumeId = @"invalidVolumeId"; //VolumeId is empty
[[[ec2 attachVolume:volumeRequest] continueWithBlock:^id(AWSTask *task) {
XCTAssertNotNil(task.error, @"expected InvalidParameterValue error but got nil");
return nil;
}] waitUntilFinished];
}
- (void)testEC2Serializer {
NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"ec2-input" ofType:@"json"];
NSError *error = nil;
NSMutableArray *ec2TestPackage = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:filePath]
options:NSJSONReadingMutableContainers
error:&error];
XCTAssertNil(error);
for (int i=0; i<[ec2TestPackage count]; i++) {
NSMutableDictionary *aTestPak = [ec2TestPackage objectAtIndex:i];
NSArray *testCases = [aTestPak objectForKey:@"cases"];
for(int j=0; j<[testCases count]; j++) {
NSDictionary *aTest = [testCases objectAtIndex:j];
//create mockRequest
NSMutableURLRequest *mockRequest = [NSMutableURLRequest new];
mockRequest.URL = [NSURL URLWithString:@"/"];
mockRequest.HTTPMethod = @"POST";
//create user input parameters
NSDictionary *testParameters = aTest[@"params"];
//construct serviceDefinition dictionary
if (aTestPak[@"operations"] == nil) {
aTestPak[@"operations"] = [NSMutableDictionary new];
}
if (aTestPak[@"operations"][@"OperationName"]) {
[aTestPak[@"operations"] removeObjectForKey:@"OperationName"];
}
aTestPak[@"operations"][@"OperationName"] = aTest[@"given"];
//create mock ServiceDefinitionJSON
NSDictionary *mockServiceDefinitionJSON = aTestPak;
// ------------ Perform Serialization ---------------------
// --------------------------------------------------------
AWSEC2RequestSerializer *testEC2Serializer = [AWSEC2RequestSerializer new];
[testEC2Serializer setValue:mockServiceDefinitionJSON forKey:@"serviceDefinitionJSON"];
[testEC2Serializer setValue:@"OperationName" forKey:@"actionName"];
AWSTask *resultTask = [testEC2Serializer serializeRequest:mockRequest
headers:@{}
parameters:testParameters];
if (resultTask.error) {
XCTFail(@"(TestPak %d TestCase %d) Serialization Error:%@",i,j,resultTask.error);
return;
}
// ------------Validate Result------------------------------
//validate result
NSDictionary *resultDic = aTest[@"serialized"];
//validate HTTP URL
XCTAssertEqualObjects(resultDic[@"uri"], [mockRequest.URL absoluteString], @"(TestPak %d TestCase %d) wrong HTTP URI, expect:%@, but got:%@",i,j,resultDic[@"uri"],[mockRequest.URL absoluteString]);
//validate HTTP Body
NSString* resultBodyStr = [[NSString alloc] initWithData:mockRequest.HTTPBody encoding:NSUTF8StringEncoding];
NSString* expectedBodyStr = resultDic[@"body"];
NSCountedSet *resultBodySet = [NSCountedSet setWithArray:[resultBodyStr componentsSeparatedByString:@"&"]];
NSCountedSet *expectedBodySet = [NSCountedSet setWithArray:[expectedBodyStr componentsSeparatedByString:@"&"]];
if ([expectedBodySet count] == 0) {
XCTAssertEqualObjects(expectedBodyStr,resultBodyStr , @"(TestPak %d TestCase %d) wrong HTTP Body, expect:\n%@, but got:\n%@",i,j,expectedBodyStr, resultBodyStr);
} else {
XCTAssertEqualObjects(expectedBodySet,resultBodySet , @"(TestPak %d TestCase %d) wrong HTTP Body, expect:\n%@, but got:\n%@",i,j,expectedBodySet, resultBodySet);
}
}
}
}
- (void)testEC2Deserializer {
NSString *filePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"ec2-output" ofType:@"json"];
NSError *error = nil;
NSMutableArray *ec2TestPackge = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:filePath]
options:NSJSONReadingMutableContainers
error:&error];
XCTAssertNil(error);
for (int i=0; i<[ec2TestPackge count]; i++) {
NSMutableDictionary *aTestPak = [ec2TestPackge objectAtIndex:i];
NSArray *testCases = [aTestPak objectForKey:@"cases"];
for(int j=0; j<[testCases count]; j++) {
NSDictionary *aTest = [testCases objectAtIndex:j];
//create mockResponse
NSDictionary *responseHeaders = aTest[@"response"][@"headers"];
NSInteger statusCode = [aTest[@"response"][@"status_code"] integerValue];
NSHTTPURLResponse *mockResponse = [[NSHTTPURLResponse alloc] initWithURL:[NSURL URLWithString:@"/"] statusCode:statusCode HTTPVersion:@"HTTP/1.1" headerFields:responseHeaders];
//construct serviceDefinition dictionary
if (aTestPak[@"operations"] == nil) {
aTestPak[@"operations"] = [NSMutableDictionary new];
}
if (aTestPak[@"operations"][@"OperationName"]) {
[aTestPak[@"operations"] removeObjectForKey:@"OperationName"];
}
aTestPak[@"operations"][@"OperationName"] = aTest[@"given"];
//create mock ServiceDefinitionJSON
NSDictionary *mockServiceDefinitionJSON = aTestPak;
// ------------ Perform Serialization ---------------------
// --------------------------------------------------------
AWSXMLResponseSerializer *testXmlResponseSerializer = [AWSXMLResponseSerializer new];
[testXmlResponseSerializer setValue:mockServiceDefinitionJSON forKey:@"serviceDefinitionJSON"];
[testXmlResponseSerializer setValue:@"OperationName" forKey:@"actionName"];
NSString *responseBodyStr = aTest[@"response"][@"body"];
NSData *responseBodyData = [responseBodyStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *parseError = nil;
id responseResult = [[testXmlResponseSerializer responseObjectForResponse:mockResponse originalRequest:nil currentRequest:nil data:responseBodyData error:&parseError] mutableCopy];
if (parseError) {
XCTFail(@"(TestPak %d TestCase %d) Serialization Error:%@",i,j,parseError);
return;
}
// ------------Validate Result------------------------------
if ([responseResult isKindOfClass:[NSDictionary class]] && [responseResult objectForKey:@"Stream"]) {
NSMutableDictionary *tempResult = [responseResult mutableCopy];
[tempResult setObject:[[NSString alloc] initWithData:responseResult[@"Stream"] encoding:NSUTF8StringEncoding] forKey:@"Stream"];
responseResult = tempResult;
}
//validate result
NSDictionary *expectedResult = aTest[@"result"];
[self replaceNSData2NSString:responseResult];
XCTAssertEqualObjects(expectedResult,responseResult , @"(TestPak %d TestCase %d) wrong HTTP Body, expect:\n%@, but got:\n%@",i,j,expectedResult, responseResult);
}
}
}
- (void) replaceNSData2NSString:(id)jsonObject
{
if ([jsonObject isKindOfClass:[NSArray class]]) {
for (int i = 0 ; i < [(NSArray *)jsonObject count] ; i++) {
id object = jsonObject[i];
if ([object isKindOfClass:[NSArray class]] || [object isKindOfClass:[NSDictionary class]]) {
[self replaceNSData2NSString:object];
}
if ([object isKindOfClass:[NSData class]]) {
[jsonObject replaceObjectAtIndex:i withObject:[[NSString alloc] initWithData:object encoding:NSUTF8StringEncoding]];
}
}
}
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
for (NSString *key in [jsonObject allKeys]) {
if ([jsonObject[key] isKindOfClass:[NSDictionary class]] || [jsonObject[key] isKindOfClass:[NSArray class]]) {
[self replaceNSData2NSString:jsonObject[key]];
}
if ([jsonObject[key] isKindOfClass:[NSData class]]) {
jsonObject[key] = [[NSString alloc] initWithData:jsonObject[key] encoding:NSUTF8StringEncoding];
}
}
}
}
@end