Best Python code snippet using localstack_python
test_deregister.py
Source:test_deregister.py
1# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License"). You4# may not use this file except in compliance with the License. A copy of5# the License is located at6#7# http://aws.amazon.com/apache2.0/8#9# or in the "license" file accompanying this file. This file is10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF11# ANY KIND, either express or implied. See the License for the specific12# language governing permissions and limitations under the License.13from argparse import Namespace14from mock import MagicMock, call15from awscli.customizations.codedeploy.deregister import Deregister16from awscli.testutils import unittest17class TestDeregister(unittest.TestCase):18 def setUp(self):19 self.instance_name = 'instance-name'20 self.tags = [{'Key': 'k1', 'Value': 'v1'}]21 self.iam_user_arn = 'arn:aws:iam::012345678912:user/{0}'.format(22 self.instance_name23 )24 self.access_key_id = 'ACCESSKEYID'25 self.region = 'us-east-1'26 self.policy_name = 'codedeploy-agent'27 self.endpoint_url = 'https://codedeploy.aws.amazon.com'28 self.args = Namespace()29 self.args.instance_name = self.instance_name30 self.args.no_delete_iam_user = False31 self.globals = Namespace()32 self.globals.region = self.region33 self.globals.endpoint_url = self.endpoint_url34 self.globals.verify_ssl = False35 self.codedeploy = MagicMock()36 self.codedeploy.get_on_premises_instance.return_value = {37 'instanceInfo': {38 'iamUserArn': self.iam_user_arn,39 'tags': None40 }41 }42 self.iam = MagicMock()43 self.list_user_policies = MagicMock()44 self.list_user_policies.paginate.return_value = [45 {'PolicyNames': [self.policy_name]}46 ]47 self.list_access_keys = MagicMock()48 self.list_access_keys.paginate.return_value = [49 {'AccessKeyMetadata': [{'AccessKeyId': self.access_key_id}]}50 ]51 self.iam.get_paginator.side_effect = [52 self.list_user_policies, self.list_access_keys53 ]54 self.session = MagicMock()55 self.session.create_client.side_effect = [self.codedeploy, self.iam]56 self.deregister = Deregister(self.session)57 def test_deregister_throws_on_invalid_region(self):58 self.globals.region = None59 self.session.get_config_variable.return_value = None60 with self.assertRaisesRegexp(RuntimeError, 'Region not specified.'):61 self.deregister._run_main(self.args, self.globals)62 def test_deregister_throws_on_invalid_instance_name(self):63 self.args.instance_name = 'invalid%@^&%#&'64 with self.assertRaisesRegexp(65 ValueError, 'Instance name contains invalid characters.'):66 self.deregister._run_main(self.args, self.globals)67 def test_deregister_creates_clients(self):68 self.deregister._run_main(self.args, self.globals)69 self.session.create_client.assert_has_calls([70 call(71 'codedeploy',72 region_name=self.region,73 endpoint_url=self.endpoint_url,74 verify=self.globals.verify_ssl75 ),76 call('iam', region_name=self.region)77 ])78 def test_deregister_with_tags(self):79 self.codedeploy.get_on_premises_instance.return_value = {80 'instanceInfo': {81 'iamUserArn': self.iam_user_arn,82 'tags': self.tags83 }84 }85 self.deregister._run_main(self.args, self.globals)86 self.codedeploy.get_on_premises_instance.assert_called_with(87 instanceName=self.instance_name88 )89 self.assertIn('iam_user_arn', self.args)90 self.assertEquals(self.iam_user_arn, self.args.iam_user_arn)91 self.assertIn('user_name', self.args)92 self.assertEquals(self.instance_name, self.args.user_name)93 self.assertIn('tags', self.args)94 self.assertEquals(self.tags, self.args.tags)95 self.codedeploy.remove_tags_from_on_premises_instances.\96 assert_called_with(97 tags=self.tags,98 instanceNames=[self.instance_name]99 )100 self.codedeploy.deregister_on_premises_instance.\101 assert_called_with(102 instanceName=self.instance_name103 )104 def test_deregister_with_no_tags(self):105 self.codedeploy.get_on_premises_instance.return_value = {106 'instanceInfo': {107 'iamUserArn': self.iam_user_arn,108 'tags': None109 }110 }111 self.deregister._run_main(self.args, self.globals)112 self.codedeploy.get_on_premises_instance.assert_called_with(113 instanceName=self.instance_name114 )115 self.assertIn('iam_user_arn', self.args)116 self.assertEquals(self.iam_user_arn, self.args.iam_user_arn)117 self.assertIn('user_name', self.args)118 self.assertEquals(self.instance_name, self.args.user_name)119 self.assertIn('tags', self.args)120 self.assertEquals(None, self.args.tags)121 self.assertFalse(122 self.codedeploy.remove_tags_from_on_premises_instances.called123 )124 self.codedeploy.deregister_on_premises_instance.\125 assert_called_with(126 instanceName=self.instance_name127 )128 def test_deregister_with_delete_iam_user(self):129 self.args.no_delete_iam_user = False130 self.deregister._run_main(self.args, self.globals)131 self.codedeploy.get_on_premises_instance.assert_called_with(132 instanceName=self.instance_name133 )134 self.codedeploy.deregister_on_premises_instance.\135 assert_called_with(136 instanceName=self.instance_name137 )138 self.iam.get_paginator.assert_has_calls([139 call('list_user_policies'),140 call('list_access_keys')141 ])142 self.list_user_policies.paginate.assert_called_with(143 UserName=self.instance_name144 )145 self.iam.delete_user_policy.assert_called_with(146 UserName=self.instance_name,147 PolicyName=self.policy_name148 )149 self.list_access_keys.paginate.assert_called_with(150 UserName=self.instance_name151 )152 self.iam.delete_access_key.assert_called_with(153 UserName=self.instance_name,154 AccessKeyId=self.access_key_id155 )156 self.iam.delete_user.assert_called_with(157 UserName=self.instance_name158 )159 def test_deregister_with_no_delete_iam_user(self):160 self.args.no_delete_iam_user = True161 self.deregister._run_main(self.args, self.globals)162 self.codedeploy.get_on_premises_instance.assert_called_with(163 instanceName=self.instance_name164 )165 self.codedeploy.deregister_on_premises_instance.\166 assert_called_with(167 instanceName=self.instance_name168 )169 self.assertFalse(self.iam.get_paginator.called)170 self.assertFalse(self.list_user_policies.paginate.called)171 self.assertFalse(self.iam.delete_user_policy.called)172 self.assertFalse(self.list_access_keys.paginate.called)173 self.assertFalse(self.iam.delete_access_key.called)174 self.assertFalse(self.iam.delete_user.called)175if __name__ == "__main__":...
user.py
Source:user.py
...12 self._iam_client = self.account_ctx.get_aws_client('iam', self.aws_region)13 return self._iam_client14 def disable_access_keys(self):15 "Set all User's access keys to Inactive"16 keys_meta = self.iam_client.list_access_keys(UserName=self.username)17 for key_meta in keys_meta['AccessKeyMetadata']:18 if key_meta['Status'] == 'Active':19 print(f"{self.username}: Modifying Access Key Status to: Inactive: {key_meta['AccessKeyId']}")20 self.iam_client.update_access_key(21 UserName=self.username,22 AccessKeyId=key_meta['AccessKeyId'],23 Status='Inactive'24 )25 def enable_access_keys(self):26 keys_meta = self.iam_client.list_access_keys(UserName=self.username)27 for key_meta in keys_meta['AccessKeyMetadata']:28 if key_meta['Status'] == 'Inactive':29 print(f"{self.username}: Modifying Access Key Status to: Active: {key_meta['AccessKeyId']}")30 self.iam_client.update_access_key(31 UserName=self.username,32 AccessKeyId=key_meta['AccessKeyId'],33 Status='Active'34 )35 def list_access_keys(self):36 return self.iam_client.list_access_keys(UserName=self.username)37 def create_access_key(self, key_num):38 "Create an IAM User Access Key"39 access_key_meta = self.iam_client.create_access_key(UserName=self.username)40 access_key_id = access_key_meta['AccessKey']['AccessKeyId']41 secret_key = access_key_meta['AccessKey']['SecretAccessKey']42 print(f"{self.username}: Created Access Key {key_num}: Key Id : {access_key_id}")43 print(f"{self.username}: {key_num}: Secret Key: {secret_key}")44 return access_key_id45 def delete_access_key(self, key_num, access_key_id):46 "Delete an IAM User Access Key"47 self.iam_client.delete_access_key(48 UserName=self.username,49 AccessKeyId=access_key_id,50 )...
list_access_keys.py
Source:list_access_keys.py
2# encoding: utf-83from list_profiles import get_list_profiles_output4from aws_api import aws_connection_context5from datetime import datetime, timezone6def get_list_access_keys(conn_context):7 now = datetime.now(timezone.utc)8 list_access_keys = conn_context.list_access_keys().get('AccessKeyMetadata')9 for key in list_access_keys:10 username = key.get('UserName')11 access_key_id = key.get('AccessKeyId')12 created_date = key.get('CreateDate')13 print(f'[INFO] - AccessKey: Username: {username}, AccessKeyId: {access_key_id} Created: {created_date}, Age: {now - created_date}')14 return list_access_keys15if __name__ == '__main__':16 print(get_list_profiles_output())17 profile_name = str(input('Enter profile name to be used in AWS context: '))18 iam = aws_connection_context(profile_name)19 # print list of current access keys...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!