本文整理汇总了Python中moto.mock_ec2函数的典型用法代码示例。如果您正苦于以下问题:Python mock_ec2函数的具体用法?Python mock_ec2怎么用?Python mock_ec2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mock_ec2函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:amplifylitco,项目名称:asiaq,代码行数:10,代码来源:test_disco_vpc_peerings.py
示例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)
开发者ID:yannlambret,项目名称:smartaws,代码行数:35,代码来源:test_vpc.py
示例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)
开发者ID:yannlambret,项目名称:smartaws,代码行数:34,代码来源:test_internet_gateway.py
示例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)
开发者ID:acmcelwee,项目名称:stacker,代码行数:30,代码来源:test_keypair.py
示例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"])
开发者ID:acmcelwee,项目名称:stacker,代码行数:27,代码来源:test_keypair.py
示例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"
)
)
开发者ID:acmcelwee,项目名称:stacker,代码行数:25,代码来源:test_keypair.py
示例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()
开发者ID:geass,项目名称:jungle,代码行数:34,代码来源:conftest.py
示例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)
开发者ID:yannlambret,项目名称:smartaws,代码行数:60,代码来源:test_subnet.py
示例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()
开发者ID:jonhadfield,项目名称:acli,代码行数:8,代码来源:test_ec2.py
示例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()
开发者ID:harvard-dce,项目名称:ec2-management,代码行数:8,代码来源:test_ec2_controller.py
示例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)
开发者ID:yannlambret,项目名称:smartaws,代码行数:58,代码来源:test_route_table.py
示例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)
开发者ID:yannlambret,项目名称:smartaws,代码行数:58,代码来源:test_dhcp_options_set.py
示例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)
开发者ID:Katafalkas,项目名称:moto,代码行数:9,代码来源:test_decorator_calls.py
示例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)
开发者ID:yannlambret,项目名称:smartaws,代码行数:56,代码来源:test_internet_gateway.py
示例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()
开发者ID:jonhadfield,项目名称:acli,代码行数:10,代码来源:test_vpc.py
示例16: ec2
def ec2(ssh_key):
# Force moto to generate a deterministic key pair on creation.
# Can be replaced by something more sensible when
# https://github.com/spulec/moto/pull/2108 is merged
key_pair = {'fingerprint': ssh_key.fingerprint,
'material': ssh_key.private_key.decode('ascii')}
with mock.patch('moto.ec2.models.random_key_pair', side_effect=[key_pair]):
with mock_ec2():
yield
开发者ID:troyready,项目名称:stacker,代码行数:10,代码来源:test_keypair.py
示例17: test_create_route_table
def test_create_route_table(self):
vpc_ctx = {
'name': 'vpc01',
'cidr_block': '10.0.10.0/24'
}
rt_ctx = {
'name': 'rt01',
'vpc': 'vpc01',
'tags': {
'description': 'Replace the default route table for VPC vpc01'
}
}
tags = [
{
'Key': 'Name',
'Value': 'rt01'
},
{
'Key': 'Description',
'Value': 'Replace the default route table for VPC vpc01'
}
]
vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]
rt_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()
vpcs = list(ec2.vpcs.filter(Filters=vpc_filters))
vpc = vpcs[0]
# Create the route table
h = ec2_rt.create_handler(rt_ctx, self.credentials)
h.create_resource()
route_tables = list(ec2.route_tables.filter(Filters=rt_filters))
rt = route_tables[0]
self.assertEqual(len(route_tables), 1)
self.assertEqual(rt.name, 'rt01')
self.assertEqual(rt.vpc_id, vpc.id)
self.assertCountEqual(rt.tags, tags)
开发者ID:yannlambret,项目名称:smartaws,代码行数:55,代码来源:test_route_table.py
示例18: test_decorator_start_and_stop
def test_decorator_start_and_stop():
conn = boto.connect_ec2('the_key', 'the_secret')
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
mock = mock_ec2()
mock.start()
conn = boto.connect_ec2('the_key', 'the_secret')
list(conn.get_all_instances()).should.equal([])
mock.stop()
conn.get_all_instances.when.called_with().should.throw(EC2ResponseError)
开发者ID:Katafalkas,项目名称:moto,代码行数:11,代码来源:test_decorator_calls.py
示例19: test_context_manager
def test_context_manager():
conn = boto.connect_ec2('the_key', 'the_secret')
with assert_raises(EC2ResponseError):
conn.get_all_instances()
with mock_ec2():
conn = boto.connect_ec2('the_key', 'the_secret')
list(conn.get_all_instances()).should.equal([])
with assert_raises(EC2ResponseError):
conn.get_all_instances()
开发者ID:DreadPirateShawn,项目名称:moto,代码行数:11,代码来源:test_decorator_calls.py
示例20: amis
def amis():
"""AMI mock service"""
mock = mock_ec2()
mock.start()
client = session.client('ec2')
reservation = client.run_instances(ImageId='ami-1234abcd', MinCount=1, MaxCount=1)
instance = reservation.get('Instances')[0]
image_id = client.create_image(InstanceId=instance.get('InstanceId'),
Name="test-ami",
Description="this is a test ami")
yield client.describe_images()
mock.stop()
开发者ID:jonhadfield,项目名称:acli,代码行数:12,代码来源:test_ec2.py
注:本文中的moto.mock_ec2函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论