<s> from argparse import Namespace <EOL> from mock import MagicMock , call <EOL> from awscli . customizations . codedeploy . deregister import Deregister <EOL> from awscli . testutils import unittest <EOL> class TestDeregister ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . instance_name = '<STR_LIT>' <EOL> self . tags = [ { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ] <EOL> self . iam_user_arn = '<STR_LIT>' . format ( <EOL> self . instance_name <EOL> ) <EOL> self . access_key_id = '<STR_LIT>' <EOL> self . region = '<STR_LIT>' <EOL> self . policy_name = '<STR_LIT>' <EOL> self . endpoint_url = '<STR_LIT>' <EOL> self . args = Namespace ( ) <EOL> self . args . instance_name = self . instance_name <EOL> self . args . no_delete_iam_user = False <EOL> self . globals = Namespace ( ) <EOL> self . globals . region = self . region <EOL> self . globals . endpoint_url = self . endpoint_url <EOL> self . globals . verify_ssl = False <EOL> self . codedeploy = MagicMock ( ) <EOL> self . codedeploy . get_on_premises_instance . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . iam_user_arn , <EOL> '<STR_LIT>' : None <EOL> } <EOL> } <EOL> self . iam = MagicMock ( ) <EOL> self . list_user_policies = MagicMock ( ) <EOL> self . list_user_policies . paginate . return_value = [ <EOL> { '<STR_LIT>' : [ self . policy_name ] } <EOL> ] <EOL> self . list_access_keys = MagicMock ( ) <EOL> self . list_access_keys . paginate . return_value = [ <EOL> { '<STR_LIT>' : [ { '<STR_LIT>' : self . access_key_id } ] } <EOL> ] <EOL> self . iam . get_paginator . side_effect = [ <EOL> self . list_user_policies , self . list_access_keys <EOL> ] <EOL> self . session = MagicMock ( ) <EOL> self . session . create_client . side_effect = [ self . codedeploy , self . iam ] <EOL> self . deregister = Deregister ( self . session ) <EOL> def test_deregister_throws_on_invalid_region ( self ) : <EOL> self . globals . region = None <EOL> self . session . get_config_variable . return_value = None <EOL> with self . assertRaisesRegexp ( RuntimeError , '<STR_LIT>' ) : <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> def test_deregister_throws_on_invalid_instance_name ( self ) : <EOL> self . args . instance_name = '<STR_LIT>' <EOL> with self . assertRaisesRegexp ( <EOL> ValueError , '<STR_LIT>' ) : <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> def test_deregister_creates_clients ( self ) : <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> self . session . create_client . assert_has_calls ( [ <EOL> call ( <EOL> '<STR_LIT>' , <EOL> region_name = self . region , <EOL> endpoint_url = self . endpoint_url , <EOL> verify = self . globals . verify_ssl <EOL> ) , <EOL> call ( '<STR_LIT>' , region_name = self . region ) <EOL> ] ) <EOL> def test_deregister_with_tags ( self ) : <EOL> self . codedeploy . get_on_premises_instance . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . iam_user_arn , <EOL> '<STR_LIT>' : self . tags <EOL> } <EOL> } <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> self . codedeploy . get_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> self . assertIn ( '<STR_LIT>' , self . args ) <EOL> self . assertEquals ( self . iam_user_arn , self . args . iam_user_arn ) <EOL> self . assertIn ( '<STR_LIT>' , self . args ) <EOL> self . assertEquals ( self . instance_name , self . args . user_name ) <EOL> self . assertIn ( '<STR_LIT>' , self . args ) <EOL> self . assertEquals ( self . tags , self . args . tags ) <EOL> self . codedeploy . remove_tags_from_on_premises_instances . assert_called_with ( <EOL> tags = self . tags , <EOL> instanceNames = [ self . instance_name ] <EOL> ) <EOL> self . codedeploy . deregister_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> def test_deregister_with_no_tags ( self ) : <EOL> self . codedeploy . get_on_premises_instance . return_value = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . iam_user_arn , <EOL> '<STR_LIT>' : None <EOL> } <EOL> } <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> self . codedeploy . get_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> self . assertIn ( '<STR_LIT>' , self . args ) <EOL> self . assertEquals ( self . iam_user_arn , self . args . iam_user_arn ) <EOL> self . assertIn ( '<STR_LIT>' , self . args ) <EOL> self . assertEquals ( self . instance_name , self . args . user_name ) <EOL> self . assertIn ( '<STR_LIT>' , self . args ) <EOL> self . assertEquals ( None , self . args . tags ) <EOL> self . assertFalse ( <EOL> self . codedeploy . remove_tags_from_on_premises_instances . called <EOL> ) <EOL> self . codedeploy . deregister_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> def test_deregister_with_delete_iam_user ( self ) : <EOL> self . args . no_delete_iam_user = False <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> self . codedeploy . get_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> self . codedeploy . deregister_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> self . iam . get_paginator . assert_has_calls ( [ <EOL> call ( '<STR_LIT>' ) , <EOL> call ( '<STR_LIT>' ) <EOL> ] ) <EOL> self . list_user_policies . paginate . assert_called_with ( <EOL> UserName = self . instance_name <EOL> ) <EOL> self . iam . delete_user_policy . assert_called_with ( <EOL> UserName = self . instance_name , <EOL> PolicyName = self . policy_name <EOL> ) <EOL> self . list_access_keys . paginate . assert_called_with ( <EOL> UserName = self . instance_name <EOL> ) <EOL> self . iam . delete_access_key . assert_called_with ( <EOL> UserName = self . instance_name , <EOL> AccessKeyId = self . access_key_id <EOL> ) <EOL> self . iam . delete_user . assert_called_with ( <EOL> UserName = self . instance_name <EOL> ) <EOL> def test_deregister_with_no_delete_iam_user ( self ) : <EOL> self . args . no_delete_iam_user = True <EOL> self . deregister . _run_main ( self . args , self . globals ) <EOL> self . codedeploy . get_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> self . codedeploy . deregister_on_premises_instance . assert_called_with ( <EOL> instanceName = self . instance_name <EOL> ) <EOL> self . assertFalse ( self . iam . get_paginator . called ) <EOL> self . assertFalse ( self . list_user_policies . paginate . called ) <EOL> self . assertFalse ( self . iam . delete_user_policy . called ) <EOL> self . assertFalse ( self . list_access_keys . paginate . called ) <EOL> self . assertFalse ( self . iam . delete_access_key . called ) <EOL> self . assertFalse ( self . iam . delete_user . called ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
