Path: blob/develop/tests/unit/customizations/dlm/test_create_default_role.py
1569 views
# Copyright 2018 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.12from awscli.testutils import BaseAWSCommandParamsTest, mock, unittest13from botocore.compat import json14import botocore.session15from awscli.customizations.dlm.iam import IAM1617from awscli.customizations.dlm.constants \18import LIFECYCLE_DEFAULT_ROLE_NAME, \19LIFECYCLE_DEFAULT_ROLE_ASSUME_POLICY, \20LIFECYCLE_DEFAULT_ROLE_NAME_AMI, \21LIFECYCLE_DEFAULT_MANAGED_POLICY_NAME, \22LIFECYCLE_DEFAULT_MANAGED_POLICY_NAME_AMI, \23RESOURCE_TYPE_SNAPSHOT, \24RESOURCE_TYPE_IMAGE252627class TestCreateDefaultRole(BaseAWSCommandParamsTest):28prefix = 'dlm create-default-role'29LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN = \30"arn:aws:iam::aws:policy/service-role/%s" \31% (LIFECYCLE_DEFAULT_MANAGED_POLICY_NAME)32LIFECYCLE_DEFAULT_MANAGED_POLICY_AMI_ARN = \33"arn:aws:iam::aws:policy/service-role/%s" \34% (LIFECYCLE_DEFAULT_MANAGED_POLICY_NAME_AMI)3536# Call to attach policy to role37def assert_attached_policy_to_role(self, expected_policy_arn,38expected_role):39self.assertEqual(self.operations_called[2][0].name, 'AttachRolePolicy')40self.assertEqual(self.operations_called[2][1]['PolicyArn'],41expected_policy_arn)42self.assertEqual(self.operations_called[2][1]['RoleName'],43expected_role)4445# Call to create default role46def assert_create_default_role(self, role, assume_policy):47self.assertEqual(self.operations_called[1][0].name, 'CreateRole')48self.assertEqual(49self.operations_called[1][1]['RoleName'],50role51)52self.assertEqual(53self.operations_called[1][1]['AssumeRolePolicyDocument'],54json.dumps(assume_policy)55)5657# Use case: Default role exists58# Expected results: No Operation performed for creation,59# only call made for verifying existence of role60# create-default-role is executed without any resource type parameter,61# should default to snapshot62def test_default_role_exists(self):63cmdline = self.prefix6465self.run_cmd(cmdline, expected_rc=0)66self.assertEqual(len(self.operations_called), 1)6768# Call to check if default lifecycle role exists69self.assertEqual(self.operations_called[0][0].name, 'GetRole')70self.assertEqual(self.operations_called[0][1]['RoleName'],71LIFECYCLE_DEFAULT_ROLE_NAME)7273# Use case: Default role does not exist.74# Managed Policy exists.75# Expected results: Operations are performed by the client to verify76# existence of policy, creation of role and then77# attaching policy to role78# create-default-role is executed without any resource type parameter,79# should default to snapshot80@mock.patch('awscli.customizations.dlm.'81'iam.IAM.check_if_role_exists')82def test_default_role_not_exist(self, role_exists_patch):8384role_exists_patch.return_value = False8586self.run_cmd(self.prefix, expected_rc=0)87self.assertEqual(len(self.operations_called), 5)8889# Call to check if managed policy exists.90self.assertEqual(self.operations_called[0][0].name, 'GetPolicy')91self.assertEqual(self.operations_called[0][1]['PolicyArn'],92self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN)9394self.assert_create_default_role(LIFECYCLE_DEFAULT_ROLE_NAME,95LIFECYCLE_DEFAULT_ROLE_ASSUME_POLICY)96self.assert_attached_policy_to_role(97self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN,98LIFECYCLE_DEFAULT_ROLE_NAME)99100# Call to get policy's default version id101self.assertEqual(self.operations_called[3][0].name, 'GetPolicy')102self.assertEqual(self.operations_called[3][1]['PolicyArn'],103self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN)104105# Call to get detailed policy to106# construct result with policy permissions107self.assertEqual(self.operations_called[4][0].name, 'GetPolicyVersion')108self.assertEqual(self.operations_called[4][1]['PolicyArn'],109self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN)110111# Use case: Default role exists112# Expected results: No Operation performed for creation,113# only call made for verifying existence of role114# create-default-role is executed with resource type = snapshot115def test_default_role_exists_snapshot(self):116cmdline = self.prefix + " --resource-type=" + RESOURCE_TYPE_SNAPSHOT117118self.run_cmd(cmdline, expected_rc=0)119self.assertEqual(len(self.operations_called), 1)120121# Call to check if default lifecycle role exists122self.assertEqual(self.operations_called[0][0].name, 'GetRole')123self.assertEqual(self.operations_called[0][1]['RoleName'],124LIFECYCLE_DEFAULT_ROLE_NAME)125126# Use case: Default role does not exist.127# Managed Policy exists.128# Expected results: Operations are performed by the client to verify129# existence of policy, creation of role and then130# attaching policy to role131# create-default-role is executed resource type = snapshot132@mock.patch('awscli.customizations.dlm.'133'iam.IAM.check_if_role_exists')134def test_default_role_not_exist_snapshot(self, role_exists_patch):135136role_exists_patch.return_value = False137138self.run_cmd(self.prefix + " --resource-type=%s"139% (RESOURCE_TYPE_SNAPSHOT),140expected_rc=0)141self.assertEqual(len(self.operations_called), 5)142143# Call to check if managed policy exists.144self.assertEqual(self.operations_called[0][0].name, 'GetPolicy')145self.assertEqual(self.operations_called[0][1]['PolicyArn'],146self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN)147148self.assert_create_default_role(LIFECYCLE_DEFAULT_ROLE_NAME,149LIFECYCLE_DEFAULT_ROLE_ASSUME_POLICY)150self.assert_attached_policy_to_role(151self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN,152LIFECYCLE_DEFAULT_ROLE_NAME)153154# Call to get policy's default version id155self.assertEqual(self.operations_called[3][0].name, 'GetPolicy')156self.assertEqual(self.operations_called[3][1]['PolicyArn'],157self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN)158159# Call to get detailed policy to160# construct result with policy permissions161self.assertEqual(self.operations_called[4][0].name, 'GetPolicyVersion')162self.assertEqual(self.operations_called[4][1]['PolicyArn'],163self.LIFECYCLE_DEFAULT_MANAGED_POLICY_ARN)164165# Use case: Default role exists for AMI166# Expected results: No Operation performed for creation,167# only call made for verifying existence of role168# create-default-role is executed with resource type = image169def test_default_role_exists_ami(self):170cmdline = self.prefix + " --resource-type=" + RESOURCE_TYPE_IMAGE171172self.run_cmd(cmdline, expected_rc=0)173self.assertEqual(len(self.operations_called), 1)174175# Call to check if default lifecycle role exists176self.assertEqual(self.operations_called[0][0].name, 'GetRole')177self.assertEqual(self.operations_called[0][1]['RoleName'],178LIFECYCLE_DEFAULT_ROLE_NAME_AMI)179180# Use case: Default role does not exist for AMI.181# AMI Managed Policy exists.182# Expected results: Operations are performed by the client to verify183# existence of policy, creation of role and then184# attaching policy to role185# create-default-role is executed with resource type = image186@mock.patch('awscli.customizations.dlm.'187'iam.IAM.check_if_role_exists')188def test_default_role_not_exist_ami(self, role_exists_patch):189190role_exists_patch.return_value = False191192self.run_cmd(self.prefix + " --resource-type=%s"193% (RESOURCE_TYPE_IMAGE),194expected_rc=0)195self.assertEqual(len(self.operations_called), 5)196197# Call to check if managed policy exists.198self.assertEqual(self.operations_called[0][0].name, 'GetPolicy')199self.assertEqual(self.operations_called[0][1]['PolicyArn'],200self.LIFECYCLE_DEFAULT_MANAGED_POLICY_AMI_ARN)201202self.assert_create_default_role(LIFECYCLE_DEFAULT_ROLE_NAME_AMI,203LIFECYCLE_DEFAULT_ROLE_ASSUME_POLICY)204self.assert_attached_policy_to_role(205self.LIFECYCLE_DEFAULT_MANAGED_POLICY_AMI_ARN,206LIFECYCLE_DEFAULT_ROLE_NAME_AMI)207208# Call to get policy's default version id209self.assertEqual(self.operations_called[3][0].name, 'GetPolicy')210self.assertEqual(self.operations_called[3][1]['PolicyArn'],211self.LIFECYCLE_DEFAULT_MANAGED_POLICY_AMI_ARN)212213# Call to get detailed policy to214# construct result with policy permissions215self.assertEqual(self.operations_called[4][0].name, 'GetPolicyVersion')216self.assertEqual(self.operations_called[4][1]['PolicyArn'],217self.LIFECYCLE_DEFAULT_MANAGED_POLICY_AMI_ARN)218219220class TestCreateDefaultRoleUnitTest(unittest.TestCase):221222def setUp(self):223self.iam_client = mock.Mock()224self.iam_client.exceptions.NoSuchEntityException = \225botocore.session\226.get_session()\227.create_client('iam', region_name="us-east-1")\228.exceptions.NoSuchEntityException229self.iam = IAM(self.iam_client)230231def test_check_if_role_exists_raises_client_error(self):232self.iam_client.get_role.side_effect = \233self.iam_client.exceptions.NoSuchEntityException(234error_response={'Error': {'Code': 'NoSuchEntityError'}},235operation_name='GetRole',236)237238self.assertFalse(self.iam.check_if_role_exists('role'))239240def test_check_if_policy_exists_raises_client_error(self):241self.iam_client.get_policy.side_effect = \242self.iam_client.exceptions.NoSuchEntityException(243error_response={'Error': {'Code': 'NoSuchEntityError'}},244operation_name='GetPolicy',245)246self.assertFalse(self.iam.check_if_policy_exists('policy'))247248249if __name__ == "__main__":250unittest.main()251252253