How to use list_tags_of_resource method in localstack

Best Python code snippet using localstack_python

test_tag.py

Source:test_tag.py Github

copy

Full Screen

...24import re25import time26from util import multiset, create_test_table, test_table_name27def delete_tags(table, arn):28 got = table.meta.client.list_tags_of_resource(ResourceArn=arn)29 print(got['Tags'])30 if len(got['Tags']):31 table.meta.client.untag_resource(ResourceArn=arn, TagKeys=[tag['Key'] for tag in got['Tags']])32# Test checking that tagging and untagging is correctly handled33def test_tag_resource_basic(test_table):34 got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']35 arn = got['TableArn']36 tags = [37 {38 'Key': 'string',39 'Value': 'string'40 },41 {42 'Key': 'string2',43 'Value': 'string4'44 },45 {46 'Key': '7',47 'Value': ' '48 },49 {50 'Key': ' ',51 'Value': '9'52 },53 ]54 delete_tags(test_table, arn)55 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)56 assert len(got['Tags']) == 057 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=tags)58 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)59 assert 'Tags' in got60 assert multiset(got['Tags']) == multiset(tags)61 # Removing non-existent tags is legal62 test_table.meta.client.untag_resource(ResourceArn=arn, TagKeys=['string2', 'non-nexistent', 'zzz2'])63 tags.remove({'Key': 'string2', 'Value': 'string4'})64 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)65 assert 'Tags' in got66 assert multiset(got['Tags']) == multiset(tags)67 delete_tags(test_table, arn)68 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)69 assert len(got['Tags']) == 070def test_tag_resource_overwrite(test_table):71 got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']72 arn = got['TableArn']73 tags = [74 {75 'Key': 'string',76 'Value': 'string'77 },78 ]79 delete_tags(test_table, arn)80 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=tags)81 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)82 assert 'Tags' in got83 assert multiset(got['Tags']) == multiset(tags)84 tags = [85 {86 'Key': 'string',87 'Value': 'different_string_value'88 },89 ]90 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=tags)91 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)92 assert 'Tags' in got93 assert multiset(got['Tags']) == multiset(tags)94PREDEFINED_TAGS = [{'Key': 'str1', 'Value': 'str2'}, {'Key': 'kkk', 'Value': 'vv'}, {'Key': 'keykey', 'Value': 'valvalvalval'}]95@pytest.fixture(scope="session")96def test_table_tags(dynamodb):97 # The feature of creating a table already with tags was only added to98 # DynamoDB in April 2019, and to the botocore library in version 1.12.13699 # https://aws.amazon.com/about-aws/whats-new/2019/04/now-you-can-tag-amazon-dynamodb-tables-when-you-create-them/100 # so older versions of the library cannot run this test.101 import botocore102 from distutils.version import LooseVersion103 if (LooseVersion(botocore.__version__) < LooseVersion('1.12.136')):104 pytest.skip("Botocore version 1.12.136 or above required to run this test")105 table = create_test_table(dynamodb,106 KeySchema=[ { 'AttributeName': 'p', 'KeyType': 'HASH' }, { 'AttributeName': 'c', 'KeyType': 'RANGE' } ],107 AttributeDefinitions=[ { 'AttributeName': 'p', 'AttributeType': 'S' }, { 'AttributeName': 'c', 'AttributeType': 'N' } ],108 Tags=PREDEFINED_TAGS)109 yield table110 table.delete()111# Test checking that tagging works during table creation112def test_list_tags_from_creation(test_table_tags):113 got = test_table_tags.meta.client.describe_table(TableName=test_table_tags.name)['Table']114 arn = got['TableArn']115 got = test_table_tags.meta.client.list_tags_of_resource(ResourceArn=arn)116 assert multiset(got['Tags']) == multiset(PREDEFINED_TAGS)117# Test checking that incorrect parameters return proper error codes118def test_tag_resource_incorrect(test_table):119 got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']120 arn = got['TableArn']121 # Note: Tags must have two entries in the map: Key and Value, and their values122 # must be at least 1 character long, but these are validated on boto3 level123 with pytest.raises(ClientError, match='AccessDeniedException'):124 test_table.meta.client.tag_resource(ResourceArn='I_do_not_exist', Tags=[{'Key': '7', 'Value': '8'}])125 with pytest.raises(ClientError, match='ValidationException'):126 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[])127 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[{'Key': str(i), 'Value': str(i)} for i in range(30)])128 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[{'Key': str(i), 'Value': str(i)} for i in range(20, 40)])129 with pytest.raises(ClientError, match='ValidationException'):130 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[{'Key': str(i), 'Value': str(i)} for i in range(40, 60)])131 for incorrect_arn in ['arn:not/a/good/format', 'x'*125, 'arn:'+'scylla/'*15, ':/'*30, ' ', 'незаконные буквы']:132 with pytest.raises(ClientError, match='.*Exception'):133 test_table.meta.client.tag_resource(ResourceArn=incorrect_arn, Tags=[{'Key':'x', 'Value':'y'}])134 for incorrect_tag in [('ok', '#!%%^$$&'), ('->>;-)])', 'ok'), ('!!!\\|','<><')]:135 with pytest.raises(ClientError, match='ValidationException'):136 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[{'Key':incorrect_tag[0],'Value':incorrect_tag[1]}])137# Test that only specific values are allowed for write isolation (system:write_isolation tag)138def test_tag_resource_write_isolation_values(scylla_only, test_table):139 got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']140 arn = got['TableArn']141 for i in ['f', 'forbid', 'forbid_rmw', 'a', 'always', 'always_use_lwt', 'o', 'only_rmw_uses_lwt', 'u', 'unsafe', 'unsafe_rmw']:142 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[{'Key':'system:write_isolation', 'Value':i}])143 with pytest.raises(ClientError, match='ValidationException'):144 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=[{'Key':'system:write_isolation', 'Value':'bah'}])145# Test that if trying to create a table with forbidden tags (in this test,146# a list of tags longer than the maximum allowed of 50 tags), the table147# is not created at all.148def test_too_long_tags_from_creation(dynamodb):149 # The feature of creating a table already with tags was only added to150 # DynamoDB in April 2019, and to the botocore library in version 1.12.136151 # so older versions of the library cannot run this test.152 import botocore153 from distutils.version import LooseVersion154 if (LooseVersion(botocore.__version__) < LooseVersion('1.12.136')):155 pytest.skip("Botocore version 1.12.136 or above required to run this test")156 name = test_table_name()157 # Setting 100 tags is not allowed, the following table creation should fail:158 with pytest.raises(ClientError, match='ValidationException'):159 dynamodb.create_table(TableName=name,160 BillingMode='PAY_PER_REQUEST',161 KeySchema=[{ 'AttributeName': 'p', 'KeyType': 'HASH' }],162 AttributeDefinitions=[{ 'AttributeName': 'p', 'AttributeType': 'S' }],163 Tags=[{'Key': str(i), 'Value': str(i)} for i in range(100)])164 # After the table creation failed, the table should not exist.165 with pytest.raises(ClientError, match='ResourceNotFoundException'):166 dynamodb.meta.client.describe_table(TableName=name)167# This test is similar to the above, but uses another case of forbidden tags -168# here an illegal value for the system::write_isolation tag. This is a169# scylla_only test because only Alternator checks the validity of the170# system::write_isolation tag.171# Reproduces issue #6809, where the table creation appeared to fail, but it172# was actually created (without the tag).173def test_forbidden_tags_from_creation(scylla_only, dynamodb):174 # The feature of creating a table already with tags was only added to175 # DynamoDB in April 2019, and to the botocore library in version 1.12.136176 # so older versions of the library cannot run this test.177 import botocore178 from distutils.version import LooseVersion179 if (LooseVersion(botocore.__version__) < LooseVersion('1.12.136')):180 pytest.skip("Botocore version 1.12.136 or above required to run this test")181 name = test_table_name()182 # It is not allowed to set the system:write_isolation to "dog", so the183 # following table creation should fail:184 with pytest.raises(ClientError, match='ValidationException'):185 dynamodb.create_table(TableName=name,186 BillingMode='PAY_PER_REQUEST',187 KeySchema=[{ 'AttributeName': 'p', 'KeyType': 'HASH' }],188 AttributeDefinitions=[{ 'AttributeName': 'p', 'AttributeType': 'S' }],189 Tags=[{'Key': 'system:write_isolation', 'Value': 'dog'}])190 # After the table creation failed, the table should not exist.191 with pytest.raises(ClientError, match='ResourceNotFoundException'):192 dynamodb.meta.client.describe_table(TableName=name)193# Test checking that unicode tags are allowed194@pytest.mark.xfail(reason="unicode tags not yet supported")195def test_tag_resource_unicode(test_table):196 got = test_table.meta.client.describe_table(TableName=test_table.name)['Table']197 arn = got['TableArn']198 tags = [199 {200 'Key': 'законные буквы',201 'Value': 'string'202 },203 {204 'Key': 'ѮѮ Ѯ',205 'Value': 'string4'206 },207 {208 'Key': 'ѮѮ',209 'Value': 'ѮѮѮѮѮѮѮѮѮѮѮѮѮѮ'210 },211 {212 'Key': 'keyѮѮѮ',213 'Value': 'ѮѮѮvalue'214 },215 ]216 delete_tags(test_table, arn)217 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)218 assert len(got['Tags']) == 0219 test_table.meta.client.tag_resource(ResourceArn=arn, Tags=tags)220 got = test_table.meta.client.list_tags_of_resource(ResourceArn=arn)221 assert 'Tags' in got...

Full Screen

Full Screen

10594_test_dynamodb.py

Source:10594_test_dynamodb.py Github

copy

Full Screen

...62 session_factory=session_factory)63 resources = p.run()64 self.assertEqual(len(resources), 1)65 arn = resources[0]['TableArn']66 tags = client.list_tags_of_resource(ResourceArn=arn)67 tag_map = {t['Key']: t['Value'] for t in tags['Tags']}68 self.assertTrue('test_key' in tag_map)69 def test_dynamodb_mark(self):70 session_factory = self.replay_flight_data(71 'test_dynamodb_mark')72 client = session_factory().client('dynamodb')73 p = self.load_policy({74 'name': 'dynamodb-mark',75 'resource': 'dynamodb-table',76 'filters': [77 {'TableName': 'rolltop'}],78 'actions': [79 {'type': 'mark-for-op', 'days': 4,80 'op': 'delete', 'tag': 'test_tag'}]},81 session_factory=session_factory)82 resources = p.run()83 arn = resources[0]['TableArn']84 self.assertEqual(len(resources), 1)85 tags = client.list_tags_of_resource(ResourceArn=arn)86 tag_map = {t['Key']: t['Value'] for t in tags['Tags']}87 self.assertTrue('test_key' in tag_map)88 def test_dynamodb_tag(self):89 session_factory = self.replay_flight_data('test_dynamodb_tag')90 client = session_factory().client('dynamodb')91 p = self.load_policy({92 'name': 'dynamodb-tag-table',93 'resource': 'dynamodb-table',94 'filters': [{'TableName': 'rolltop'}],95 'actions': [{96 'type': 'tag',97 'tags': {'new_tag_key': 'new_tag_value'}98 }]99 },100 session_factory=session_factory)101 resources = p.run()102 arn = resources[0]['TableArn']103 tags = client.list_tags_of_resource(ResourceArn=arn)104 tag_map = {t['Key']: t['Value'] for t in tags['Tags']}105 self.assertEqual({106 'test_key': 'test_value',107 'new_tag_key': 'new_tag_value'108 },109 tag_map)110 def test_dynamodb_unmark(self):111 session_factory = self.replay_flight_data(112 'test_dynamodb_unmark')113 client = session_factory().client('dynamodb')114 p = self.load_policy({115 'name': 'dynamodb-unmark',116 'resource': 'dynamodb-table',117 'filters': [118 {'TableName': 'rolltop'}],119 'actions': [120 {'type': 'remove-tag',121 'tags': ['test_key']}]},122 session_factory=session_factory)123 resources = p.run()124 arn = resources[0]['TableArn']125 self.assertEqual(len(resources), 1)126 tags = client.list_tags_of_resource(ResourceArn=arn)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful