Run black on moto & test directories.
This commit is contained in:
parent
c820395dbf
commit
96e5b1993d
507 changed files with 52541 additions and 47814 deletions
|
|
@ -13,7 +13,7 @@ from moto import mock_s3
|
|||
@mock_resourcegroupstaggingapi
|
||||
def test_get_resources_s3():
|
||||
# Tests pagination
|
||||
s3_client = boto3.client('s3', region_name='eu-central-1')
|
||||
s3_client = boto3.client("s3", region_name="eu-central-1")
|
||||
|
||||
# Will end up having key1,key2,key3,key4
|
||||
response_keys = set()
|
||||
|
|
@ -21,26 +21,25 @@ def test_get_resources_s3():
|
|||
# Create 4 buckets
|
||||
for i in range(1, 5):
|
||||
i_str = str(i)
|
||||
s3_client.create_bucket(Bucket='test_bucket' + i_str)
|
||||
s3_client.create_bucket(Bucket="test_bucket" + i_str)
|
||||
s3_client.put_bucket_tagging(
|
||||
Bucket='test_bucket' + i_str,
|
||||
Tagging={'TagSet': [{'Key': 'key' + i_str, 'Value': 'value' + i_str}]}
|
||||
Bucket="test_bucket" + i_str,
|
||||
Tagging={"TagSet": [{"Key": "key" + i_str, "Value": "value" + i_str}]},
|
||||
)
|
||||
response_keys.add('key' + i_str)
|
||||
response_keys.add("key" + i_str)
|
||||
|
||||
rtapi = boto3.client('resourcegroupstaggingapi', region_name='eu-central-1')
|
||||
rtapi = boto3.client("resourcegroupstaggingapi", region_name="eu-central-1")
|
||||
resp = rtapi.get_resources(ResourcesPerPage=2)
|
||||
for resource in resp['ResourceTagMappingList']:
|
||||
response_keys.remove(resource['Tags'][0]['Key'])
|
||||
for resource in resp["ResourceTagMappingList"]:
|
||||
response_keys.remove(resource["Tags"][0]["Key"])
|
||||
|
||||
response_keys.should.have.length_of(2)
|
||||
|
||||
resp = rtapi.get_resources(
|
||||
ResourcesPerPage=2,
|
||||
PaginationToken=resp['PaginationToken']
|
||||
ResourcesPerPage=2, PaginationToken=resp["PaginationToken"]
|
||||
)
|
||||
for resource in resp['ResourceTagMappingList']:
|
||||
response_keys.remove(resource['Tags'][0]['Key'])
|
||||
for resource in resp["ResourceTagMappingList"]:
|
||||
response_keys.remove(resource["Tags"][0]["Key"])
|
||||
|
||||
response_keys.should.have.length_of(0)
|
||||
|
||||
|
|
@ -48,109 +47,86 @@ def test_get_resources_s3():
|
|||
@mock_ec2
|
||||
@mock_resourcegroupstaggingapi
|
||||
def test_get_resources_ec2():
|
||||
client = boto3.client('ec2', region_name='eu-central-1')
|
||||
client = boto3.client("ec2", region_name="eu-central-1")
|
||||
|
||||
instances = client.run_instances(
|
||||
ImageId='ami-123',
|
||||
ImageId="ami-123",
|
||||
MinCount=1,
|
||||
MaxCount=1,
|
||||
InstanceType='t2.micro',
|
||||
InstanceType="t2.micro",
|
||||
TagSpecifications=[
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG1',
|
||||
'Value': 'MY_VALUE1',
|
||||
},
|
||||
{
|
||||
'Key': 'MY_TAG2',
|
||||
'Value': 'MY_VALUE2',
|
||||
},
|
||||
"ResourceType": "instance",
|
||||
"Tags": [
|
||||
{"Key": "MY_TAG1", "Value": "MY_VALUE1"},
|
||||
{"Key": "MY_TAG2", "Value": "MY_VALUE2"},
|
||||
],
|
||||
},
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG3',
|
||||
'Value': 'MY_VALUE3',
|
||||
},
|
||||
]
|
||||
"ResourceType": "instance",
|
||||
"Tags": [{"Key": "MY_TAG3", "Value": "MY_VALUE3"}],
|
||||
},
|
||||
],
|
||||
)
|
||||
instance_id = instances['Instances'][0]['InstanceId']
|
||||
image_id = client.create_image(Name='testami', InstanceId=instance_id)['ImageId']
|
||||
instance_id = instances["Instances"][0]["InstanceId"]
|
||||
image_id = client.create_image(Name="testami", InstanceId=instance_id)["ImageId"]
|
||||
|
||||
client.create_tags(
|
||||
Resources=[image_id],
|
||||
Tags=[{'Key': 'ami', 'Value': 'test'}]
|
||||
)
|
||||
client.create_tags(Resources=[image_id], Tags=[{"Key": "ami", "Value": "test"}])
|
||||
|
||||
rtapi = boto3.client('resourcegroupstaggingapi', region_name='eu-central-1')
|
||||
rtapi = boto3.client("resourcegroupstaggingapi", region_name="eu-central-1")
|
||||
resp = rtapi.get_resources()
|
||||
# Check we have 1 entry for Instance, 1 Entry for AMI
|
||||
resp['ResourceTagMappingList'].should.have.length_of(2)
|
||||
resp["ResourceTagMappingList"].should.have.length_of(2)
|
||||
|
||||
# 1 Entry for AMI
|
||||
resp = rtapi.get_resources(ResourceTypeFilters=['ec2:image'])
|
||||
resp['ResourceTagMappingList'].should.have.length_of(1)
|
||||
resp['ResourceTagMappingList'][0]['ResourceARN'].should.contain('image/')
|
||||
resp = rtapi.get_resources(ResourceTypeFilters=["ec2:image"])
|
||||
resp["ResourceTagMappingList"].should.have.length_of(1)
|
||||
resp["ResourceTagMappingList"][0]["ResourceARN"].should.contain("image/")
|
||||
|
||||
# As were iterating the same data, this rules out that the test above was a fluke
|
||||
resp = rtapi.get_resources(ResourceTypeFilters=['ec2:instance'])
|
||||
resp['ResourceTagMappingList'].should.have.length_of(1)
|
||||
resp['ResourceTagMappingList'][0]['ResourceARN'].should.contain('instance/')
|
||||
resp = rtapi.get_resources(ResourceTypeFilters=["ec2:instance"])
|
||||
resp["ResourceTagMappingList"].should.have.length_of(1)
|
||||
resp["ResourceTagMappingList"][0]["ResourceARN"].should.contain("instance/")
|
||||
|
||||
# Basic test of tag filters
|
||||
resp = rtapi.get_resources(TagFilters=[{'Key': 'MY_TAG1', 'Values': ['MY_VALUE1', 'some_other_value']}])
|
||||
resp['ResourceTagMappingList'].should.have.length_of(1)
|
||||
resp['ResourceTagMappingList'][0]['ResourceARN'].should.contain('instance/')
|
||||
resp = rtapi.get_resources(
|
||||
TagFilters=[{"Key": "MY_TAG1", "Values": ["MY_VALUE1", "some_other_value"]}]
|
||||
)
|
||||
resp["ResourceTagMappingList"].should.have.length_of(1)
|
||||
resp["ResourceTagMappingList"][0]["ResourceARN"].should.contain("instance/")
|
||||
|
||||
|
||||
@mock_ec2
|
||||
@mock_resourcegroupstaggingapi
|
||||
def test_get_tag_keys_ec2():
|
||||
client = boto3.client('ec2', region_name='eu-central-1')
|
||||
client = boto3.client("ec2", region_name="eu-central-1")
|
||||
|
||||
client.run_instances(
|
||||
ImageId='ami-123',
|
||||
ImageId="ami-123",
|
||||
MinCount=1,
|
||||
MaxCount=1,
|
||||
InstanceType='t2.micro',
|
||||
InstanceType="t2.micro",
|
||||
TagSpecifications=[
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG1',
|
||||
'Value': 'MY_VALUE1',
|
||||
},
|
||||
{
|
||||
'Key': 'MY_TAG2',
|
||||
'Value': 'MY_VALUE2',
|
||||
},
|
||||
"ResourceType": "instance",
|
||||
"Tags": [
|
||||
{"Key": "MY_TAG1", "Value": "MY_VALUE1"},
|
||||
{"Key": "MY_TAG2", "Value": "MY_VALUE2"},
|
||||
],
|
||||
},
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG3',
|
||||
'Value': 'MY_VALUE3',
|
||||
},
|
||||
]
|
||||
"ResourceType": "instance",
|
||||
"Tags": [{"Key": "MY_TAG3", "Value": "MY_VALUE3"}],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
rtapi = boto3.client('resourcegroupstaggingapi', region_name='eu-central-1')
|
||||
rtapi = boto3.client("resourcegroupstaggingapi", region_name="eu-central-1")
|
||||
resp = rtapi.get_tag_keys()
|
||||
|
||||
resp['TagKeys'].should.contain('MY_TAG1')
|
||||
resp['TagKeys'].should.contain('MY_TAG2')
|
||||
resp['TagKeys'].should.contain('MY_TAG3')
|
||||
resp["TagKeys"].should.contain("MY_TAG1")
|
||||
resp["TagKeys"].should.contain("MY_TAG2")
|
||||
resp["TagKeys"].should.contain("MY_TAG3")
|
||||
|
||||
# TODO test pagenation
|
||||
|
||||
|
|
@ -158,148 +134,114 @@ def test_get_tag_keys_ec2():
|
|||
@mock_ec2
|
||||
@mock_resourcegroupstaggingapi
|
||||
def test_get_tag_values_ec2():
|
||||
client = boto3.client('ec2', region_name='eu-central-1')
|
||||
client = boto3.client("ec2", region_name="eu-central-1")
|
||||
|
||||
client.run_instances(
|
||||
ImageId='ami-123',
|
||||
ImageId="ami-123",
|
||||
MinCount=1,
|
||||
MaxCount=1,
|
||||
InstanceType='t2.micro',
|
||||
InstanceType="t2.micro",
|
||||
TagSpecifications=[
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG1',
|
||||
'Value': 'MY_VALUE1',
|
||||
},
|
||||
{
|
||||
'Key': 'MY_TAG2',
|
||||
'Value': 'MY_VALUE2',
|
||||
},
|
||||
"ResourceType": "instance",
|
||||
"Tags": [
|
||||
{"Key": "MY_TAG1", "Value": "MY_VALUE1"},
|
||||
{"Key": "MY_TAG2", "Value": "MY_VALUE2"},
|
||||
],
|
||||
},
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG3',
|
||||
'Value': 'MY_VALUE3',
|
||||
},
|
||||
]
|
||||
"ResourceType": "instance",
|
||||
"Tags": [{"Key": "MY_TAG3", "Value": "MY_VALUE3"}],
|
||||
},
|
||||
],
|
||||
)
|
||||
client.run_instances(
|
||||
ImageId='ami-123',
|
||||
ImageId="ami-123",
|
||||
MinCount=1,
|
||||
MaxCount=1,
|
||||
InstanceType='t2.micro',
|
||||
InstanceType="t2.micro",
|
||||
TagSpecifications=[
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG1',
|
||||
'Value': 'MY_VALUE4',
|
||||
},
|
||||
{
|
||||
'Key': 'MY_TAG2',
|
||||
'Value': 'MY_VALUE5',
|
||||
},
|
||||
"ResourceType": "instance",
|
||||
"Tags": [
|
||||
{"Key": "MY_TAG1", "Value": "MY_VALUE4"},
|
||||
{"Key": "MY_TAG2", "Value": "MY_VALUE5"},
|
||||
],
|
||||
},
|
||||
{
|
||||
'ResourceType': 'instance',
|
||||
'Tags': [
|
||||
{
|
||||
'Key': 'MY_TAG3',
|
||||
'Value': 'MY_VALUE6',
|
||||
},
|
||||
]
|
||||
"ResourceType": "instance",
|
||||
"Tags": [{"Key": "MY_TAG3", "Value": "MY_VALUE6"}],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
rtapi = boto3.client('resourcegroupstaggingapi', region_name='eu-central-1')
|
||||
resp = rtapi.get_tag_values(Key='MY_TAG1')
|
||||
rtapi = boto3.client("resourcegroupstaggingapi", region_name="eu-central-1")
|
||||
resp = rtapi.get_tag_values(Key="MY_TAG1")
|
||||
|
||||
resp["TagValues"].should.contain("MY_VALUE1")
|
||||
resp["TagValues"].should.contain("MY_VALUE4")
|
||||
|
||||
resp['TagValues'].should.contain('MY_VALUE1')
|
||||
resp['TagValues'].should.contain('MY_VALUE4')
|
||||
|
||||
@mock_ec2
|
||||
@mock_elbv2
|
||||
@mock_kms
|
||||
@mock_resourcegroupstaggingapi
|
||||
def test_get_many_resources():
|
||||
elbv2 = boto3.client('elbv2', region_name='us-east-1')
|
||||
ec2 = boto3.resource('ec2', region_name='us-east-1')
|
||||
kms = boto3.client('kms', region_name='us-east-1')
|
||||
elbv2 = boto3.client("elbv2", region_name="us-east-1")
|
||||
ec2 = boto3.resource("ec2", region_name="us-east-1")
|
||||
kms = boto3.client("kms", region_name="us-east-1")
|
||||
|
||||
security_group = ec2.create_security_group(
|
||||
GroupName='a-security-group', Description='First One')
|
||||
vpc = ec2.create_vpc(CidrBlock='172.28.7.0/24', InstanceTenancy='default')
|
||||
GroupName="a-security-group", Description="First One"
|
||||
)
|
||||
vpc = ec2.create_vpc(CidrBlock="172.28.7.0/24", InstanceTenancy="default")
|
||||
subnet1 = ec2.create_subnet(
|
||||
VpcId=vpc.id,
|
||||
CidrBlock='172.28.7.192/26',
|
||||
AvailabilityZone='us-east-1a')
|
||||
VpcId=vpc.id, CidrBlock="172.28.7.192/26", AvailabilityZone="us-east-1a"
|
||||
)
|
||||
subnet2 = ec2.create_subnet(
|
||||
VpcId=vpc.id,
|
||||
CidrBlock='172.28.7.0/26',
|
||||
AvailabilityZone='us-east-1b')
|
||||
VpcId=vpc.id, CidrBlock="172.28.7.0/26", AvailabilityZone="us-east-1b"
|
||||
)
|
||||
|
||||
elbv2.create_load_balancer(
|
||||
Name='my-lb',
|
||||
Name="my-lb",
|
||||
Subnets=[subnet1.id, subnet2.id],
|
||||
SecurityGroups=[security_group.id],
|
||||
Scheme='internal',
|
||||
Scheme="internal",
|
||||
Tags=[
|
||||
{
|
||||
'Key': 'key_name',
|
||||
'Value': 'a_value'
|
||||
},
|
||||
{
|
||||
'Key': 'key_2',
|
||||
'Value': 'val2'
|
||||
}
|
||||
]
|
||||
)
|
||||
{"Key": "key_name", "Value": "a_value"},
|
||||
{"Key": "key_2", "Value": "val2"},
|
||||
],
|
||||
)
|
||||
|
||||
elbv2.create_load_balancer(
|
||||
Name='my-other-lb',
|
||||
Name="my-other-lb",
|
||||
Subnets=[subnet1.id, subnet2.id],
|
||||
SecurityGroups=[security_group.id],
|
||||
Scheme='internal',
|
||||
)
|
||||
Scheme="internal",
|
||||
)
|
||||
|
||||
kms.create_key(
|
||||
KeyUsage='ENCRYPT_DECRYPT',
|
||||
KeyUsage="ENCRYPT_DECRYPT",
|
||||
Tags=[
|
||||
{
|
||||
'TagKey': 'key_name',
|
||||
'TagValue': 'a_value'
|
||||
},
|
||||
{
|
||||
'TagKey': 'key_2',
|
||||
'TagValue': 'val2'
|
||||
}
|
||||
]
|
||||
)
|
||||
{"TagKey": "key_name", "TagValue": "a_value"},
|
||||
{"TagKey": "key_2", "TagValue": "val2"},
|
||||
],
|
||||
)
|
||||
|
||||
rtapi = boto3.client('resourcegroupstaggingapi', region_name='us-east-1')
|
||||
rtapi = boto3.client("resourcegroupstaggingapi", region_name="us-east-1")
|
||||
|
||||
resp = rtapi.get_resources(ResourceTypeFilters=['elasticloadbalancer:loadbalancer'])
|
||||
resp = rtapi.get_resources(ResourceTypeFilters=["elasticloadbalancer:loadbalancer"])
|
||||
|
||||
resp['ResourceTagMappingList'].should.have.length_of(2)
|
||||
resp['ResourceTagMappingList'][0]['ResourceARN'].should.contain('loadbalancer/')
|
||||
resp["ResourceTagMappingList"].should.have.length_of(2)
|
||||
resp["ResourceTagMappingList"][0]["ResourceARN"].should.contain("loadbalancer/")
|
||||
resp = rtapi.get_resources(
|
||||
ResourceTypeFilters=['elasticloadbalancer:loadbalancer'],
|
||||
TagFilters=[{
|
||||
'Key': 'key_name'
|
||||
}]
|
||||
)
|
||||
ResourceTypeFilters=["elasticloadbalancer:loadbalancer"],
|
||||
TagFilters=[{"Key": "key_name"}],
|
||||
)
|
||||
|
||||
resp['ResourceTagMappingList'].should.have.length_of(1)
|
||||
resp['ResourceTagMappingList'][0]['Tags'].should.contain({'Key': 'key_name', 'Value': 'a_value'})
|
||||
resp["ResourceTagMappingList"].should.have.length_of(1)
|
||||
resp["ResourceTagMappingList"][0]["Tags"].should.contain(
|
||||
{"Key": "key_name", "Value": "a_value"}
|
||||
)
|
||||
|
||||
# TODO test pagenation
|
||||
|
|
|
|||
|
|
@ -4,9 +4,9 @@ import sure # noqa
|
|||
|
||||
import moto.server as server
|
||||
|
||||
'''
|
||||
"""
|
||||
Test the different server responses
|
||||
'''
|
||||
"""
|
||||
|
||||
|
||||
def test_resourcegroupstaggingapi_list():
|
||||
|
|
@ -15,10 +15,10 @@ def test_resourcegroupstaggingapi_list():
|
|||
# do test
|
||||
|
||||
headers = {
|
||||
'X-Amz-Target': 'ResourceGroupsTaggingAPI_20170126.GetResources',
|
||||
'X-Amz-Date': '20171114T234623Z'
|
||||
"X-Amz-Target": "ResourceGroupsTaggingAPI_20170126.GetResources",
|
||||
"X-Amz-Date": "20171114T234623Z",
|
||||
}
|
||||
resp = test_client.post('/', headers=headers, data='{}')
|
||||
resp = test_client.post("/", headers=headers, data="{}")
|
||||
|
||||
assert resp.status_code == 200
|
||||
assert b'ResourceTagMappingList' in resp.data
|
||||
assert b"ResourceTagMappingList" in resp.data
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue