本文整理汇总了Python中moto.mock_ec2函数的典型用法代码示例。如果您正苦于以下问题:Python mock_ec2函数的具体用法?Python mock_ec2怎么用?Python mock_ec2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mock_ec2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
def setUp(self):
mock_ec2().start()
self.disco_vpc1 = DiscoVPC('mock-vpc-1', 'sandbox')
self.disco_vpc2 = DiscoVPC('mock-vpc-2', 'sandbox')
self.disco_vpc3 = DiscoVPC('mock-vpc-3', 'sandbox')
self.client = boto3.client('ec2')
self.disco_vpc_peerings = DiscoVPCPeerings()
示例2: test_delete_vpc
def test_delete_vpc(self):
ctx = {
'name': 'vpc01',
'cidr_block': '10.0.10.0/24'
}
filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]
def _add_wrapper(base_classes, **kwargs):
base_classes.insert(0, ec2_vpc.VpcWrapper)
with mock_ec2():
event = 'creating-resource-class.ec2.Vpc'
session = Session(**self.credentials)
session.events.register(event, _add_wrapper)
ec2 = session.resource('ec2')
# Create the VPC
h = ec2_vpc.create_handler(ctx, self.credentials)
h.create_resource()
vpcs = list(ec2.vpcs.filter(Filters=filters))
self.assertEqual(len(vpcs), 1)
# We clear the resource cache to simulate a new
# program execution with the 'delete' option
base.BaseHandler._cache.clear()
# Delete the VPC
h.delete_resource()
vpcs = list(ec2.vpcs.filter(Filters=filters))
self.assertEqual(len(vpcs), 0)
示例3: test_delete_internet_gateway
def test_delete_internet_gateway(self):
ctx = {
'name': 'igw01'
}
filters = [{'Name': 'tag:Name', 'Values': ['igw01']}]
def _add_wrapper(base_classes, **kwargs):
base_classes.insert(0, ec2_igw.InternetGatewayWrapper)
with mock_ec2():
event = 'creating-resource-class.ec2.InternetGateway'
session = Session(**self.credentials)
session.events.register(event, _add_wrapper)
ec2 = session.resource('ec2')
# Create the internet gateway
h = ec2_igw.create_handler(ctx, self.credentials)
h.create_resource()
gateways = list(ec2.internet_gateways.filter(Filters=filters))
self.assertEqual(len(gateways), 1)
# We clear the resource cache to simulate a new
# program execution with the 'delete' option
base.BaseHandler._cache.clear()
# Delete the internet gateway
h.delete_resource()
gateways = list(ec2.internet_gateways.filter(Filters=filters))
self.assertEqual(len(gateways), 0)
示例4: test_keypair_missing_create
def test_keypair_missing_create(self, mocked_input):
mocked_input.side_effect = ["create", "./"]
with mock_ec2():
logger = "stacker.hooks.keypair"
client = boto3.client("ec2", region_name=REGION)
with LogCapture(logger) as logs:
value = ensure_keypair_exists(provider=self.provider,
context=self.context,
keypair=KEY_PAIR_NAME)
response = client.describe_key_pairs()
print(response)
keypair = find(response["KeyPairs"], "KeyName", KEY_PAIR_NAME)
message = "keypair: " + KEY_PAIR_NAME + \
" (" + keypair["KeyFingerprint"] + ") created"
logs.check(
(
logger,
"INFO",
"keypair: \"%s\" not found" % KEY_PAIR_NAME
),
(
logger,
"INFO",
message
)
)
tmp_file_path = "/home/circleci/project/" + KEY_PAIR_NAME + ".pem"
self.assertEqual(value["status"], "created")
self.assertEqual(value["key_name"], KEY_PAIR_NAME)
self.assertEqual(value["file_path"], tmp_file_path)
示例5: test_keypair_exists
def test_keypair_exists(self):
with mock_ec2():
logger = "stacker.hooks.keypair"
client = boto3.client("ec2", region_name=REGION)
client.create_key_pair(KeyName=KEY_PAIR_NAME)
response = client.describe_key_pairs()
# check that one keypair was created
self.assertEqual(len(response["KeyPairs"]), 1)
keypair = find(response["KeyPairs"], "KeyName", KEY_PAIR_NAME)
with LogCapture(logger) as logs:
value = ensure_keypair_exists(provider=self.provider,
context=self.context,
keypair=KEY_PAIR_NAME)
message = "keypair: " + KEY_PAIR_NAME + \
" (" + keypair["KeyFingerprint"] + ") exists"
logs.check(
(
logger,
"INFO",
message
)
)
self.assertEqual(value["status"], "exists")
self.assertEqual(value["key_name"], KEY_PAIR_NAME)
self.assertEqual(value["fingerprint"],
keypair["KeyFingerprint"])
示例6: test_keypair_missing_cancel_input
def test_keypair_missing_cancel_input(self, mocked_input):
mocked_input.side_effect = ["Cancel"]
with mock_ec2():
logger = "stacker.hooks.keypair"
client = boto3.client("ec2", region_name=REGION)
response = client.describe_key_pairs()
# initially no key pairs created
self.assertEqual(len(response["KeyPairs"]), 0)
with LogCapture(logger) as logs:
self.assertFalse(ensure_keypair_exists(provider=self.provider,
context=self.context,
keypair=KEY_PAIR_NAME))
logs.check(
(
logger,
"INFO",
"keypair: \"%s\" not found" % KEY_PAIR_NAME
),
(
logger,
"WARNING",
"no action to find keypair, failing"
)
)
示例7: ec2
def ec2():
"""EC2 mock service"""
mock = mock_ec2()
mock.start()
ec2 = boto3.resource('ec2')
ssh_server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1)
for s in ssh_server:
ec2.create_tags(
Resources=[s.id],
Tags=[{'Key': 'Name', 'Value': 'ssh_server'}])
gateway_server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1)
for s in gateway_server:
ec2.create_tags(
Resources=[s.id],
Tags=[{'Key': 'Name', 'Value': 'gateway_server'}])
server = ec2.create_instances(ImageId='ami-xxxxx', MinCount=1, MaxCount=1)
servers = ec2.create_instances(ImageId='ami-xxxxx', MinCount=2, MaxCount=2)
for i, s in enumerate(servers):
ec2.create_tags(
Resources=[s.id],
Tags=[{'Key': 'Name', 'Value': 'server{:0>2d}'.format(i)}])
yield dict(
ec2=ec2,
servers=servers,
server=server[0],
ssh_target_server=ssh_server[0],
gateway_target_server=gateway_server[0],
)
mock.stop()
示例8: test_create_subnet
def test_create_subnet(self):
vpc_ctx = {
'name': 'vpc01',
'cidr_block': '10.0.10.0/24'
}
subnet_ctx = {
'name': 'subnet01a',
'cidr_block': '10.0.10.0/25',
'zone': 'us-west-2a',
'vpc': 'vpc01',
'tags': {
'description': 'Test subnet (zone a) for VPC vpc01'
}
}
tags = [
{
'Key': 'Name',
'Value': 'subnet01a'
},
{
'Key': 'Description',
'Value': 'Test subnet (zone a) for VPC vpc01'
}
]
vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]
subnet_filters = [{'Name': 'tag:Name', 'Values': ['subnet01a']}]
def _add_wrapper(base_classes, **kwargs):
base_classes.insert(0, ec2_subnet.SubnetWrapper)
with mock_ec2():
event = 'creating-resource-class.ec2.Subnet'
session = Session(**self.credentials)
session.events.register(event, _add_wrapper)
ec2 = session.resource('ec2')
# Create the VPC
h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
h.create_resource()
vpcs = list(ec2.vpcs.filter(Filters=vpc_filters))
vpc = vpcs[0]
# Create the subnet
h = ec2_subnet.create_handler(subnet_ctx, self.credentials)
h.create_resource()
subnets = list(ec2.subnets.filter(Filters=subnet_filters))
subnet = subnets[0]
self.assertEqual(len(subnets), 1)
self.assertEqual(subnet.name, 'subnet01a')
self.assertEqual(subnet.cidr_block, '10.0.10.0/25')
self.assertEqual(subnet.availability_zone, 'us-west-2a')
self.assertEqual(subnet.vpc_id, vpc.id)
self.assertEqual(subnet.map_public_ip_on_launch, False)
self.assertCountEqual(subnet.tags, tags)
示例9: sec_groups
def sec_groups():
""" Mock security groups """
mock = mock_ec2()
mock.start()
client = session.client('ec2')
client.create_security_group(GroupName='test', Description='test')
yield client.describe_security_groups()
mock.stop()
示例10: setUp
def setUp(self):
log = logging.getLogger()
if not log.handlers:
log.addHandler(logging.NullHandler())
self.ec2_mock = mock_ec2()
self.ec2_mock.start()
示例11: test_delete_route_table_with_association
def test_delete_route_table_with_association(self):
vpc_ctx = {
'name': 'vpc01',
'cidr_block': '10.0.10.0/24'
}
subnet_ctx = {
'name': 'subnet01a',
'cidr_block': '10.0.10.0/25',
'zone': 'us-west-2a',
'vpc': 'vpc01'
}
rt_ctx = {
'name': 'rt01',
'vpc': 'vpc01',
'subnets': [
'subnet01a'
]
}
filters = [{'Name': 'tag:Name', 'Values': ['rt01']}]
def _add_wrapper(base_classes, **kwargs):
base_classes.insert(0, ec2_rt.RouteTableWrapper)
with mock_ec2():
event = 'creating-resource-class.ec2.RouteTable'
session = Session(**self.credentials)
session.events.register(event, _add_wrapper)
ec2 = session.resource('ec2')
# Create the VPC
h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
h.create_resource()
# Create the subnet
h = ec2_subnet.create_handler(subnet_ctx, self.credentials)
h.create_resource()
# Create the route table
h = ec2_rt.create_handler(rt_ctx, self.credentials)
h.create_resource()
route_tables = list(ec2.route_tables.filter(Filters=filters))
self.assertEqual(len(route_tables), 1)
# We clear the resource cache to simulate a new
# program execution with the 'delete' option
base.BaseHandler._cache.clear()
# Delete the route table
h.delete_resource()
route_tables = list(ec2.route_tables.filter(Filters=filters))
self.assertEqual(len(route_tables), 0)
示例12: test_create_dhcp_options
def test_create_dhcp_options(self):
ctx = {
'name': 'dhcp01',
'domain_name': [
'test01.us-west-2.aws'
],
'domain_name_servers': [
'10.0.10.2'
],
'tags': {
'description': 'DHCP options set for VPC vpc01'
}
}
tags = [
{
'Key': 'Name',
'Value': 'dhcp01'
},
{
'Key': 'Description',
'Value': 'DHCP options set for VPC vpc01'
}
]
dhcp_configurations = [
{
'Key': 'domain-name',
'Values': [{'Value': 'test01.us-west-2.aws'}]
},
{
'Key': 'domain-name-servers',
'Values': [{'Value': '10.0.10.2'}]
}
]
filters = [{'Name': 'tag:Name', 'Values': ['dhcp01']}]
def _add_wrapper(base_classes, **kwargs):
base_classes.insert(0, ec2_dhcp.DhcpOptionsWrapper)
with mock_ec2():
event = 'creating-resource-class.ec2.DhcpOptions'
session = Session(**self.credentials)
session.events.register(event, _add_wrapper)
ec2 = session.resource('ec2')
# Create the DHCP options set
h = ec2_dhcp.create_handler(ctx, self.credentials)
h.create_resource()
dhcp_options_sets = list(ec2.dhcp_options_sets.filter(Filters=filters))
dhcp = dhcp_options_sets[0]
self.assertEqual(len(dhcp_options_sets), 1)
self.assertEqual(dhcp.name, 'dhcp01')
self.assertCountEqual(dhcp.dhcp_configurations, dhcp_configurations)
self.assertCountEqual(dhcp.tags, tags)
示例13: test_context_manager
def test_context_manager():
conn = boto.connect_ec2('the_key', 'the_secret')
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
with mock_ec2():
conn = boto.connect_ec2('the_key', 'the_secret')
list(conn.get_all_instances()).should.equal([])
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
示例14: test_delete_attached_internet_gateway
def test_delete_attached_internet_gateway(self):
igw_ctx = {
'name': 'igw01'
}
vpc_ctx = {
'name': 'vpc01',
'cidr_block': '10.0.10.0/24',
'internet_gateway': 'igw01'
}
igw_filters = [{'Name': 'tag:Name', 'Values': ['igw01']}]
vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]
def _add_wrapper(base_classes, **kwargs):
base_classes.insert(0, ec2_igw.InternetGatewayWrapper)
with mock_ec2():
event = 'creating-resource-class.ec2.InternetGateway'
session = Session(**self.credentials)
session.events.register(event, _add_wrapper)
ec2 = session.resource('ec2')
# Create the internet gateway
h = ec2_igw.create_handler(igw_ctx, self.credentials)
h.create_resource()
gateways = list(ec2.internet_gateways.filter(Filters=igw_filters))
igw = gateways[0]
self.assertCountEqual(igw.attachments, [])
# Create the VPC
h = ec2_vpc.create_handler(vpc_ctx, self.credentials)
h.create_resource()
vpcs = list(ec2.vpcs.filter(Filters=vpc_filters))
vpc = vpcs[0]
# Test that the internet gateway has been attached
igw.reload()
attachments = [{'VpcId': vpc.id, 'State': 'available'}]
self.assertCountEqual(igw.attachments, attachments)
# We clear the resource cache to simulate a new
# program execution with the 'delete' option
base.BaseHandler._cache.clear()
# Delete the internet gateway
h = ec2_igw.create_handler(igw_ctx, self.credentials)
h.delete_resource()
gateways = list(ec2.internet_gateways.filter(Filters=igw_filters))
# The gateway was not deleted
self.assertEqual(len(gateways), 1)
示例15: fake_vpcs
def fake_vpcs():
"""VPC mock service"""
mock = mock_ec2()
mock.start()
ec2_client = get_client(client_type='ec2', config=config)
vpc = ec2_client.create_vpc(CidrBlock='10.0.0.0/16')
print(vpc)
ec2_client.create_subnet(VpcId=vpc['Vpc']['VpcId'], CidrBlock="10.0.0.0/18")
yield ec2_client.describe_vpcs()
mock.stop()