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