Path: blob/develop/tests/unit/customizations/datapipeline/test_create_default_role.py
1569 views
import pytest12import awscli.customizations.datapipeline.createdefaultroles \3as createdefaultroles4from awscli.customizations.datapipeline.constants\5import DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME,\6DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME,\7DATAPIPELINE_DEFAULT_SERVICE_ROLE_ASSUME_POLICY,\8DATAPIPELINE_DEFAULT_RESOURCE_ROLE_ASSUME_POLICY910from awscli.testutils import BaseAWSCommandParamsTest,\11mock, unittest12from awscli.customizations.datapipeline.translator import dict_to_string13from botocore.compat import json141516@pytest.mark.filterwarnings('ignore::UserWarning')17class TestCreateDefaultRole(BaseAWSCommandParamsTest):18prefix = 'datapipeline create-default-roles'1920DATAPIPELINE_ROLE_POLICY = {21"Statement": [22{23"Action": [24"cloudwatch:*",25"dynamodb:*",26"ec2:Describe*",27"elasticmapreduce:Describe*",28"rds:Describe*",29"s3:*",30"sdb:*",31"sns:*",32"sqs:*"33],34"Effect": "Allow",35"Resource": ["*"]36}37]38}3940CREATE_DATAPIPELINE_ROLE_RESULT = {41"Role": {42"AssumeRolePolicyDocument": {43"Version": "2008-10-17",44"Statement": [45{46"Action": "sts:AssumeRole",47"Sid": "",48"Effect": "Allow",49"Principal": {50"Service": "ec2.amazonaws.com"51}52}53]54},55"RoleId": "AROAJG7O4RNNSRINMF6DI",56"CreateDate": "2014-05-01T23:47:14.552Z",57"RoleName": DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME,58"Path": "/",59"Arn": "arn:aws:iam::176430881729:role/" +60DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME61}62}6364CONSTRUCTED_RESULT_OUTPUT = [65{66"Role": CREATE_DATAPIPELINE_ROLE_RESULT['Role'],67"RolePolicy": DATAPIPELINE_ROLE_POLICY68}69]7071# Use case: Default roles exists72# Expected results: No Operation performed for creation, except calls made73# for verifying existence of roles74def test_default_roles_exist(self):75cmdline = self.prefix7677self.run_cmd(cmdline, expected_rc=0)78self.assertEqual(len(self.operations_called), 3)7980self.assertEqual(self.operations_called[0][0].name, 'GetRole')81self.assertEqual(self.operations_called[0][1]['RoleName'],82DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME)8384# Use case: Default roles do not exist85# Expected results: Operations are performed by the client to verify86# existence of roles and then creation of roles (Service role,87# resource role and instance profile)88@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'89'CreateDefaultRoles._construct_result')90@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'91'CreateDefaultRoles._check_if_role_exists')92@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'93'CreateDefaultRoles._check_if_instance_profile_exists')94@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'95'CreateDefaultRoles._get_role_policy')96def test_default_roles_not_exist(self, get_rp_patch,97role_exists_patch,98instance_profile_exists_patch,99construct_result_patch):100get_rp_patch.return_value = False101instance_profile_exists_patch.return_value = False102role_exists_patch.return_value = False103construct_result_patch.return_value = []104105self.run_cmd(self.prefix, expected_rc=0)106self.assertEqual(len(self.operations_called), 6)107108self.assertEqual(self.operations_called[0][0].name, 'CreateRole')109self.assertEqual(self.operations_called[0][1]['RoleName'],110DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME)111self.assertEqual(112self.operations_called[0][1]['AssumeRolePolicyDocument'],113dict_to_string(DATAPIPELINE_DEFAULT_SERVICE_ROLE_ASSUME_POLICY))114115self.assertEqual(self.operations_called[1][0].name,116'AttachRolePolicy')117self.assertEqual(self.operations_called[1][1]['PolicyArn'],118(createdefaultroles.119DATAPIPELINE_DEFAULT_SERVICE_ROLE_ARN))120self.assertEqual(self.operations_called[1][1]['RoleName'],121DATAPIPELINE_DEFAULT_SERVICE_ROLE_NAME)122123self.assertEqual(self.operations_called[2][0].name, 'CreateRole')124self.assertEqual(self.operations_called[2][1]['RoleName'],125DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME)126self.assertEqual(127self.operations_called[2][1]['AssumeRolePolicyDocument'],128dict_to_string(DATAPIPELINE_DEFAULT_RESOURCE_ROLE_ASSUME_POLICY))129130self.assertEqual(self.operations_called[3][0].name, 'AttachRolePolicy')131self.assertEqual(self.operations_called[3][1]['PolicyArn'],132(createdefaultroles.133DATAPIPELINE_DEFAULT_RESOURCE_ROLE_ARN))134self.assertEqual(self.operations_called[3][1]['RoleName'],135DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME)136137self.assertEqual(self.operations_called[4][0].name,138'CreateInstanceProfile')139self.assertEqual(self.operations_called[4][1]['InstanceProfileName'],140DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME)141142self.assertEqual(self.operations_called[5][0].name,143'AddRoleToInstanceProfile')144self.assertEqual(self.operations_called[5][1]['InstanceProfileName'],145DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME)146self.assertEqual(self.operations_called[5][1]['RoleName'],147DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME)148149# Use case: Creating only DataPipeline service role150# Expected output: The service role is created displaying a message151# to the customer that a particular role with a policy has been created152@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'153'CreateDefaultRoles._get_role_policy')154@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'155'CreateDefaultRoles._create_role_with_role_policy')156@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'157'CreateDefaultRoles._check_if_instance_profile_exists')158@mock.patch('awscli.customizations.datapipeline.createdefaultroles.'159'CreateDefaultRoles._check_if_role_exists')160def test_constructed_result(self, role_exists_patch,161instance_profile_exists_patch,162create_role_patch,163get_role_policy_patch):164role_exists_patch.side_effect = self.toggle_for_check_if_exists165instance_profile_exists_patch.return_value = True166create_role_patch.return_value = self.CREATE_DATAPIPELINE_ROLE_RESULT167get_role_policy_patch.return_value = self.DATAPIPELINE_ROLE_POLICY168169result = self.run_cmd(self.prefix, 0)170expected_output = json.dumps(self.CONSTRUCTED_RESULT_OUTPUT,171indent=4) + '\n'172self.assertEqual(result[0], expected_output)173174def toggle_for_check_if_exists(self, *args):175if args[0] == DATAPIPELINE_DEFAULT_RESOURCE_ROLE_NAME:176return False177else:178return True179180181if __name__ == "__main__":182unittest.main()183184185