Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/emr/test_describe_cluster.py
1569 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
import json
15
16
from awscli.testutils import mock
17
from tests.unit.customizations.emr import EMRBaseAWSCommandParamsTest as \
18
BaseAWSCommandParamsTest
19
20
describe_cluster_result_mock_ig = {
21
"Cluster": {
22
"Status": {
23
"Timeline": {
24
"ReadyDateTime": 1398376089.0,
25
"EndDateTime": 1398376477.0,
26
"CreationDateTime": 1398375871.0
27
},
28
"State": "TERMINATED",
29
"StateChangeReason": {
30
"Message": "Terminated by user request",
31
"Code": "USER_REQUEST"
32
}
33
},
34
"Ec2InstanceAttributes": {
35
"IamInstanceProfile": "EMR_EC2_DefaultRole",
36
"Ec2AvailabilityZone": "us-east-1b"
37
},
38
"InstanceCollectionType": "INSTANCE_GROUP",
39
"Name": "ABCD",
40
"Tags": [],
41
"TerminationProtected": "false",
42
"RunningAmiVersion": "2.4.2",
43
"Applications": [
44
{
45
"Version": "1.0.3",
46
"Name": "hadoop"
47
}
48
],
49
"VisibleToAllUsers": "true",
50
"RequestedAmiVersion": "2.4.2",
51
"LogUri": "s3n://abc/logs/",
52
"AutoTerminate": "false",
53
"Id": "j-ABCD",
54
"MasterPublicDnsName": "ec2-01-01-1-188.compute-1.amazonaws.com"
55
}
56
}
57
58
describe_cluster_result_mock_if = {
59
"Cluster": {
60
"Status": {
61
"Timeline": {
62
"ReadyDateTime": 1398376089.0,
63
"EndDateTime": 1398376477.0,
64
"CreationDateTime": 1398375871.0
65
},
66
"State": "TERMINATED",
67
"StateChangeReason": {
68
"Message": "Terminated by user request",
69
"Code": "USER_REQUEST"
70
}
71
},
72
"Ec2InstanceAttributes": {
73
"IamInstanceProfile": "EMR_EC2_DefaultRole",
74
"Ec2AvailabilityZone": "us-east-1b"
75
},
76
"InstanceCollectionType": "INSTANCE_FLEET",
77
"Name": "ABCD",
78
"Tags": [],
79
"TerminationProtected": "false",
80
"RunningAmiVersion": "2.4.2",
81
"Applications": [
82
{
83
"Version": "1.0.3",
84
"Name": "hadoop"
85
}
86
],
87
"VisibleToAllUsers": "true",
88
"RequestedAmiVersion": "2.4.2",
89
"LogUri": "s3n://abc/logs/",
90
"AutoTerminate": "false",
91
"Id": "j-ABCD",
92
"MasterPublicDnsName": "ec2-01-01-1-188.compute-1.amazonaws.com"
93
}
94
}
95
96
list_instance_groups_result_mock = {
97
"ResponseMetadata": {
98
"RequestId": '12ec323c-50b1-11e4-b653-afa4228d882f'
99
},
100
"InstanceGroups": [
101
{
102
"RequestedInstanceCount": 1,
103
"Status": {
104
"Timeline": {
105
"ReadyDateTime": 1398376083.0,
106
"EndDateTime": 1398376476.0,
107
"CreationDateTime": 1398375871.0
108
},
109
"State": "TERMINATED",
110
"StateChangeReason": {
111
"Message": "Job flow terminated",
112
"Code": "CLUSTER_TERMINATED"
113
}
114
},
115
"Name": "Master instance group",
116
"InstanceGroupType": "MASTER",
117
"InstanceType": "m1.large",
118
"CustomAmiId": "ami-deadbeef",
119
"Id": "ig-ABCD",
120
"Market": "ON_DEMAND",
121
"RunningInstanceCount": 0
122
},
123
{
124
"RequestedInstanceCount": 2,
125
"Status": {
126
"Timeline": {
127
"ReadyDateTime": 1398376089.0,
128
"EndDateTime": 1398376476.0,
129
"CreationDateTime": 1398375871.0
130
},
131
"State": "TERMINATED",
132
"StateChangeReason": {
133
"Message": "Job flow terminated",
134
"Code": "CLUSTER_TERMINATED"
135
}
136
},
137
"Name": "Core instance group",
138
"InstanceGroupType": "CORE",
139
"InstanceType": "m1.large",
140
"CustomAmiId": "ami-deadpork",
141
"Id": "ig-DEF",
142
"Market": "ON_DEMAND",
143
"RunningInstanceCount": 0
144
}
145
]
146
}
147
148
list_instance_fleets_result_mock = {
149
"ResponseMetadata": {
150
"RequestId": '12ec323c-50b1-11e4-b653-afa4228d882f'
151
},
152
"InstanceFleets": [
153
{
154
"TargetSpotCapacity": 0,
155
"TargetOnDemandCapacity": 1,
156
"ProvisionedOnDemandCapacity": 0,
157
"ProvisionedSpotCapacity": 0,
158
"Status": {
159
"Timeline": {
160
"ReadyDateTime": 1398376083.0,
161
"EndDateTime": 1398376476.0,
162
"CreationDateTime": 1398375871.0
163
},
164
"State": "TERMINATED",
165
"StateChangeReason": {
166
"Message": "Job flow terminated",
167
"Code": "CLUSTER_TERMINATED"
168
}
169
},
170
"InstanceTypeSpecifications": [
171
{
172
"WeightedCapacity": 1,
173
"BidPrice": "1",
174
"InstanceType": "m3.xlarge",
175
"CustomAmiId": "ami-deadbeef",
176
"BidPriceAsPercentageOfOnDemandPrice": 0.0
177
}
178
],
179
"LaunchSpecifications" : {
180
"SpotSpecification": {"TimeoutDurationMinutes": 77, "TimeoutAction": "TERMINATE_CLUSTER",
181
"AllocationStrategy": "capacity-optimized-prioritized"},
182
"OnDemandSpecification": {"AllocationStrategy": "lowest-price"}
183
},
184
"ResizeSpecifications": {
185
"OnDemandResizeSpecification": {"AllocationStrategy": "lowest-price",
186
"CapacityReservationOptions": {
187
"CapacityReservationPreference": "open",
188
"UsageStrategy": "use-capacity-reservations-first"
189
}
190
},
191
"SpotResizeSpecification": {"AllocationStrategy": "capacity-optimized"}
192
},
193
"Name": "Master instance group",
194
"InstanceFleetType": "MASTER",
195
"InstanceType": "m1.large",
196
"Id": "if-ABCD",
197
"Context": "testContext"
198
}
199
]
200
}
201
202
list_bootstrap_actions_result_mock = {
203
"ResponseMetadata": {
204
"RequestId": '12ec323c-50b1-11e4-b653-afa4228d882f'
205
},
206
"BootstrapActions": [
207
{
208
"Args": [],
209
"Name": "Install HBase",
210
"ScriptPath": "s3://elasticmapreduce/bootstrap-actions/setup-hbase"
211
}
212
]
213
}
214
215
EXPECTED_RESULT_IG = {
216
"Cluster": {
217
"Ec2InstanceAttributes": {
218
"IamInstanceProfile": "EMR_EC2_DefaultRole",
219
"Ec2AvailabilityZone": "us-east-1b"
220
},
221
"InstanceCollectionType": "INSTANCE_GROUP",
222
"Name": "ABCD",
223
"TerminationProtected": "false",
224
"RunningAmiVersion": "2.4.2",
225
"MasterPublicDnsName": "ec2-01-01-1-188.compute-1.amazonaws.com",
226
"InstanceGroups": [
227
{
228
"RequestedInstanceCount": 1,
229
"Status": {
230
"Timeline": {
231
"ReadyDateTime": 1398376083.0,
232
"CreationDateTime": 1398375871.0,
233
"EndDateTime": 1398376476.0
234
},
235
"State": "TERMINATED",
236
"StateChangeReason": {
237
"Message": "Job flow terminated",
238
"Code": "CLUSTER_TERMINATED"
239
}
240
},
241
"RunningInstanceCount": 0,
242
"Name": "Master instance group",
243
"InstanceGroupType": "MASTER",
244
"InstanceType": "m1.large",
245
"CustomAmiId": "ami-deadbeef",
246
"Market": "ON_DEMAND",
247
"Id": "ig-ABCD"
248
},
249
{
250
"RequestedInstanceCount": 2,
251
"Status": {
252
"Timeline": {
253
"ReadyDateTime": 1398376089.0,
254
"CreationDateTime": 1398375871.0,
255
"EndDateTime": 1398376476.0
256
},
257
"State": "TERMINATED",
258
"StateChangeReason": {
259
"Message": "Job flow terminated",
260
"Code": "CLUSTER_TERMINATED"
261
}
262
},
263
"RunningInstanceCount": 0,
264
"Name": "Core instance group",
265
"InstanceGroupType": "CORE",
266
"InstanceType": "m1.large",
267
"CustomAmiId": "ami-deadpork",
268
"Market": "ON_DEMAND",
269
"Id": "ig-DEF"
270
}
271
],
272
"RequestedAmiVersion": "2.4.2",
273
"AutoTerminate": "false",
274
"LogUri": "s3n://abc/logs/",
275
"Status": {
276
"Timeline": {
277
"ReadyDateTime": 1398376089.0,
278
"CreationDateTime": 1398375871.0,
279
"EndDateTime": 1398376477.0
280
},
281
"State": "TERMINATED",
282
"StateChangeReason": {
283
"Message": "Terminated by user request",
284
"Code": "USER_REQUEST"
285
}
286
},
287
"Tags": [],
288
"Applications": [
289
{
290
"Version": "1.0.3",
291
"Name": "hadoop"
292
}
293
],
294
"VisibleToAllUsers": "true",
295
"BootstrapActions": [
296
{
297
"Args": [],
298
"Name": "Install HBase",
299
"ScriptPath": "s3://elasticmapreduce/bootstrap-actions/"
300
"setup-hbase"
301
}
302
],
303
"Id": "j-ABCD"
304
}
305
}
306
307
EXPECTED_RESULT_IF = {
308
"Cluster": {
309
"Ec2InstanceAttributes": {
310
"IamInstanceProfile": "EMR_EC2_DefaultRole",
311
"Ec2AvailabilityZone": "us-east-1b"
312
},
313
"InstanceCollectionType": "INSTANCE_FLEET",
314
"Name": "ABCD",
315
"TerminationProtected": "false",
316
"RunningAmiVersion": "2.4.2",
317
"MasterPublicDnsName": "ec2-01-01-1-188.compute-1.amazonaws.com",
318
"InstanceFleets": [
319
{
320
"TargetSpotCapacity": 0,
321
"TargetOnDemandCapacity": 1,
322
"ProvisionedOnDemandCapacity": 0,
323
"ProvisionedSpotCapacity": 0,
324
"Status": {
325
"Timeline": {
326
"ReadyDateTime": 1398376083.0,
327
"EndDateTime": 1398376476.0,
328
"CreationDateTime": 1398375871.0
329
},
330
"State": "TERMINATED",
331
"StateChangeReason": {
332
"Message": "Job flow terminated",
333
"Code": "CLUSTER_TERMINATED"
334
}
335
},
336
"InstanceTypeSpecifications": [
337
{
338
"WeightedCapacity": 1,
339
"BidPrice": "1",
340
"InstanceType": "m3.xlarge",
341
"CustomAmiId": "ami-deadbeef",
342
"BidPriceAsPercentageOfOnDemandPrice": 0.0
343
}
344
],
345
"LaunchSpecifications" : {
346
"SpotSpecification": {"TimeoutDurationMinutes": 77, "TimeoutAction": "TERMINATE_CLUSTER",
347
"AllocationStrategy": "capacity-optimized-prioritized"},
348
"OnDemandSpecification": {"AllocationStrategy": "lowest-price"}
349
},
350
"ResizeSpecifications": {
351
"OnDemandResizeSpecification": {"AllocationStrategy": "lowest-price",
352
"CapacityReservationOptions": {
353
"CapacityReservationPreference": "open",
354
"UsageStrategy": "use-capacity-reservations-first"
355
}
356
},
357
"SpotResizeSpecification": {"AllocationStrategy": "capacity-optimized"}
358
},
359
"Name": "Master instance group",
360
"InstanceFleetType": "MASTER",
361
"InstanceType": "m1.large",
362
"Id": "if-ABCD",
363
"Context": "testContext"
364
}
365
],
366
"RequestedAmiVersion": "2.4.2",
367
"AutoTerminate": "false",
368
"LogUri": "s3n://abc/logs/",
369
"Status": {
370
"Timeline": {
371
"ReadyDateTime": 1398376089.0,
372
"CreationDateTime": 1398375871.0,
373
"EndDateTime": 1398376477.0
374
},
375
"State": "TERMINATED",
376
"StateChangeReason": {
377
"Message": "Terminated by user request",
378
"Code": "USER_REQUEST"
379
}
380
},
381
"Tags": [],
382
"Applications": [
383
{
384
"Version": "1.0.3",
385
"Name": "hadoop"
386
}
387
],
388
"VisibleToAllUsers": "true",
389
"BootstrapActions": [
390
{
391
"Args": [],
392
"Name": "Install HBase",
393
"ScriptPath": "s3://elasticmapreduce/bootstrap-actions/"
394
"setup-hbase"
395
}
396
],
397
"Id": "j-ABCD"
398
}
399
}
400
401
class TestDescribeCluster(BaseAWSCommandParamsTest):
402
prefix = 'emr describe-cluster'
403
404
@mock.patch('awscli.customizations.emr.emr.DescribeCluster._construct_result')
405
def test_operations_called(self, construct_result_patch):
406
construct_result_patch.return_value = dict()
407
408
args = ' --cluster-id j-ABCD'
409
cmdline = self.prefix + args
410
411
self.run_cmd(cmdline, expected_rc=0)
412
413
self.assertEqual(len(self.operations_called), 3)
414
self.assertEqual(self.operations_called[0][0].name,
415
'DescribeCluster')
416
self.assertEqual(self.operations_called[0][1]['ClusterId'],
417
'j-ABCD')
418
self.assertEqual(self.operations_called[1][0].name,
419
'ListInstanceGroups')
420
self.assertEqual(self.operations_called[1][1]['ClusterId'],
421
'j-ABCD')
422
423
self.assertEqual(self.operations_called[2][0].name,
424
'ListBootstrapActions')
425
self.assertEqual(self.operations_called[2][1]['ClusterId'],
426
'j-ABCD')
427
428
@mock.patch('awscli.customizations.emr.emr.DescribeCluster._call')
429
def test_constructed_result_ig(self, call_patch):
430
call_patch.side_effect = side_effect_of_call_ig
431
432
args = ' --cluster-id j-ABCD'
433
cmdline = self.prefix + args
434
result = self.run_cmd(cmdline, expected_rc=0)
435
result_json = json.loads(result[0])
436
self.assertEqual(result_json, EXPECTED_RESULT_IG)
437
438
@mock.patch('awscli.customizations.emr.emr.DescribeCluster._call')
439
def test_constructed_result_if(self, call_patch):
440
call_patch.side_effect = side_effect_of_call_if
441
442
args = ' --cluster-id j-ABCD'
443
cmdline = self.prefix + args
444
result = self.run_cmd(cmdline, expected_rc=0)
445
result_json = json.loads(result[0])
446
self.assertEqual(result_json, EXPECTED_RESULT_IF)
447
448
def side_effect_of_call_ig(*args, **kwargs):
449
if args[1] == 'describe_cluster':
450
return describe_cluster_result_mock_ig
451
elif args[1] == 'list_instance_fleets':
452
return list_instance_fleets_result_mock
453
elif args[1] == 'list_instance_groups':
454
return list_instance_groups_result_mock
455
elif args[1] == 'list_bootstrap_actions':
456
return list_bootstrap_actions_result_mock
457
458
def side_effect_of_call_if(*args, **kwargs):
459
if args[1] == 'describe_cluster':
460
return describe_cluster_result_mock_if
461
elif args[1] == 'list_instance_fleets':
462
return list_instance_fleets_result_mock
463
elif args[1] == 'list_instance_groups':
464
return list_instance_groups_result_mock
465
elif args[1] == 'list_bootstrap_actions':
466
return list_bootstrap_actions_result_mock
467
468
if __name__ == "__main__":
469
unittest.main()
470
471