Best Python code snippet using localstack_python
test_aws_tags_resolver.py
Source:test_aws_tags_resolver.py
1import unittest2from ec2_metadata import EC2Metadata3from mock import call, Mock, MagicMock4import mock5import logging6from configbutler.resolvers import AWSTagResolver7logger = logging.getLogger("configbutler")8logging.basicConfig()9class TestAWSTagsResolver(unittest.TestCase):10 @mock.patch("configbutler.resolvers.logger")11 def test_no_tags_retries(self, mock_logger=None):12 undertest = AWSTagResolver()13 undertest.RETRY_COUNT = 214 undertest.metadata = mock.create_autospec(EC2Metadata)15 undertest.metadata.instance_id = "i-12345"16 mock_tags = {17 "Tags": []18 }19 undertest.client = Mock()20 undertest.client.describe_tags = MagicMock(return_value=mock_tags)21 self.assertEqual(None, undertest.resolve("blart", dict))22 self.assertEqual([call(Filters=[{'Values': ['i-12345'], 'Name': 'resource-id'}]),23 call(Filters=[{'Values': ['i-12345'], 'Name': 'resource-id'}])],24 undertest.client.describe_tags.mock_calls)25 self.assertEqual([call.error('No AWS::tag values found, waiting 1sec to retry.'),26 call.error('No AWS::tag values found, waiting 2sec to retry.'),27 call.error('No AWS::tag values found, continuing with no tags.'),28 call.error("Unable to find AWS::tag named 'blart'")],29 mock_logger.mock_calls)30 @mock.patch("configbutler.resolvers.logger")31 def test_no_tags_multiple_tags_cache_results(self, mock_logger=None):32 undertest = AWSTagResolver()33 undertest.RETRY_COUNT = 234 undertest.metadata = mock.create_autospec(EC2Metadata)35 undertest.metadata.instance_id = "i-12345"36 mock_tags = {37 "Tags": []38 }39 undertest.client = Mock()40 undertest.client.describe_tags = MagicMock(return_value=mock_tags)41 self.assertEqual(None, undertest.resolve("blart", dict))42 self.assertEqual(None, undertest.resolve("blerg", dict))43 self.assertEqual(None, undertest.resolve("bling", dict))44 self.assertEqual([call(Filters=[{'Values': ['i-12345'], 'Name': 'resource-id'}]),45 call(Filters=[{'Values': ['i-12345'], 'Name': 'resource-id'}])],46 undertest.client.describe_tags.mock_calls)47 self.assertEqual([call.error('No AWS::tag values found, waiting 1sec to retry.'),48 call.error('No AWS::tag values found, waiting 2sec to retry.'),49 call.error('No AWS::tag values found, continuing with no tags.'),50 call.error("Unable to find AWS::tag named 'blart'"),51 call.error("Unable to find AWS::tag named 'blerg'"),52 call.error("Unable to find AWS::tag named 'bling'")],53 mock_logger.mock_calls)54 @mock.patch("configbutler.resolvers.logger")55 def test_with_second_retries(self, mock_logger):56 no_tags = {57 "Tags": []58 }59 some_tags = {60 "Tags": [{"Key": "blart", "Value": "bling"}]61 }62 incr_return_values = [no_tags, some_tags]63 def mock_tags_lookup(**args):64 return incr_return_values.pop(0)65 undertest = AWSTagResolver()66 undertest.RETRY_COUNT = 267 undertest.metadata = mock.create_autospec(EC2Metadata)68 undertest.metadata.instance_id = "i-12345"69 undertest.client = Mock()70 undertest.client.describe_tags = mock_tags_lookup71 self.assertEqual("bling", undertest.resolve("blart", dict))72 # Should be no values left in the array73 self.assertEqual(0, len(incr_return_values))74 self.assertEqual([call.error('No AWS::tag values found, waiting 1sec to retry.')],75 mock_logger.mock_calls)76 @mock.patch("configbutler.resolvers.logger")77 def test_with_missing_tag(self, mock_logger):78 undertest = AWSTagResolver()79 undertest.RETRY_COUNT = 280 undertest.metadata = mock.create_autospec(EC2Metadata)81 undertest.metadata.instance_id = "i-12345"82 mock_tags = {83 "Tags": [{"Key": "aKey", "Value": "aValue"}]84 }85 undertest.client = Mock()86 undertest.client.describe_tags = MagicMock(return_value=mock_tags)87 self.assertEqual(None, undertest.resolve("blart", dict))88 self.assertEqual([call(Filters=[{'Values': ['i-12345'], 'Name': 'resource-id'}])],89 undertest.client.describe_tags.mock_calls)90 self.assertEqual([call.error("Unable to find AWS::tag named 'blart'")],91 mock_logger.mock_calls)92 @mock.patch("configbutler.resolvers.logger")93 def test_with_actual_tag(self, mock_logger):94 undertest = AWSTagResolver()95 undertest.RETRY_COUNT = 296 undertest.metadata = mock.create_autospec(EC2Metadata)97 undertest.metadata.instance_id = "i-12345"98 mock_tags = {99 "Tags": [{"Key": "blart", "Value": "bling"}]100 }101 undertest.client = Mock()102 undertest.client.describe_tags = MagicMock(return_value=mock_tags)103 self.assertEqual("bling", undertest.resolve("blart", dict))104 self.assertEqual([call(Filters=[{'Values': ['i-12345'], 'Name': 'resource-id'}])],105 undertest.client.describe_tags.mock_calls)106 self.assertEqual([],...
untaggedEC2.py
Source:untaggedEC2.py
...26272829#looking for untagged Ec2 with key Name30response = client.describe_tags(31 Filters=[32 {33 'Name':'tag:'+ 'Name',34 'Values': ['']35 },36 ],37)38pprint(response)39#looking for untagged Ec2 with key Owner40response = client.describe_tags(41 Filters=[42 {43 'Name':'tag:'+ 'Owner',44 'Values': ['']45 },46 ],47)48pprint(response)49#looking for untagged Ec2 with key LOB50response = client.describe_tags(51 Filters=[52 {53 'Name':'tag:'+ 'LOB',54 'Values': ['']55 },56 ],57)58pprint(response)59#looking for untagged Ec2 with key Application60response = client.describe_tags(61 Filters=[62 {63 'Name':'tag:'+ 'Application',64 'Values': ['']65 },66 ],67)68pprint(response)69#looking for untagged Ec2 with key Portfolio70response = client.describe_tags(71 Filters=[72 {73 'Name':'tag:'+ 'Portfolio',74 'Values': ['']75 },76 ],77)78pprint(response)79#looking for untagged Ec2 with key Product80response = client.describe_tags(81 Filters=[82 {83 'Name':'tag:'+ 'product',84 'Values': ['']85 },86 ],87)88pprint(response)89#looking for untagged Ec2 with key Usage90response = client.describe_tags(91 Filters=[92 {93 'Name':'tag:'+ 'Usage',94 'Values': ['']95 },96 ],97)
...
tag_checking.py
Source:tag_checking.py
...4import re5client = boto3.client('ec2')6ec2 = boto3.resource('ec2')7response = client.describe_instances()8def describe_tags(resource_id):9 search_tags = client.describe_tags(10 Filters=[{11 'Name': 'resource-id',12 'Values': [13 "{}".format(resource_id),14 ], }, ],15 )16 if search_tags:17 create_tag(resource_id)18 for tags in search_tags['Tags']:19 if 'BillingID' not in tags['Key']:20 create_tag(resource_id)21 return search_tags22def create_tag(resource_id):23 client.create_tags(24 Resources=[25 '{}'.format(resource_id)26 ],27 Tags=[28 {29 'Key': 'BillingID',30 'Value': 'FDB',31 },32 ],33 )34for r in response['Reservations']:35 for i in r['Instances']:36 if i['State']['Name'] != 'terminated':37 describe_tags(i['InstanceId'])38 describe_tags(i['VpcId'])39 describe_tags(i['SubnetId'])40 for sg in i['SecurityGroups']:41 describe_tags(sg['GroupId'])42 for blockdevice in i['BlockDeviceMappings']:43 describe_tags(blockdevice['Ebs']['VolumeId'])44 for interfaces in i['NetworkInterfaces']:...
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!!