本文整理汇总了Python中unittest.mock.MagicMock.tags方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.tags方法的具体用法?Python MagicMock.tags怎么用?Python MagicMock.tags使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.tags方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_all_vpc
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_get_all_vpc(monkeypatch):
mock_vpc1 = MagicMock()
mock_vpc1.vpc_id = 'vpc-id1'
mock_vpc1.is_default = True
mock_vpc1.tags = []
mock_vpc2 = MagicMock()
mock_vpc2.vpc_id = 'vpc-id2'
mock_vpc2.is_default = False
mock_vpc2.tags = []
mock_vpc3 = MagicMock()
mock_vpc3.vpc_id = 'vpc-id3'
mock_vpc3.is_default = False
mock_vpc3.tags = []
m_resource = MagicMock()
m_resource.return_value = m_resource
monkeypatch.setattr('boto3.resource', m_resource)
ec2 = EC2('eu-test-1')
m_resource.vpcs.all.return_value = [mock_vpc1, mock_vpc2, mock_vpc3]
vpcs = list(ec2.get_all_vpcs())
assert len(vpcs) == 3
示例2: test_component_stups_auto_configuration
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_component_stups_auto_configuration(monkeypatch):
args = MagicMock()
args.region = 'myregion'
configuration = {
'Name': 'Config',
'AvailabilityZones': ['az-1']
}
sn1 = MagicMock()
sn1.id = 'sn-1'
sn1.tags = [{'Key': 'Name', 'Value': 'dmz-1'}]
sn1.availability_zone = 'az-1'
sn2 = MagicMock()
sn2.id = 'sn-2'
sn2.tags = [{'Key': 'Name', 'Value': 'dmz-2'}]
sn2.availability_zone = 'az-2'
sn3 = MagicMock()
sn3.id = 'sn-3'
sn3.tags = [{'Key': 'Name', 'Value': 'internal-3'}]
sn3.availability_zone = 'az-1'
ec2 = MagicMock()
ec2.subnets.filter.return_value = [sn1, sn2, sn3]
image = MagicMock()
ec2.images.filter.return_value = [image]
monkeypatch.setattr('boto3.resource', lambda x, y: ec2)
result = component_stups_auto_configuration({}, configuration, args, MagicMock(), False, MagicMock())
assert {'myregion': {'Subnets': ['sn-1']}} == result['Mappings']['LoadBalancerSubnets']
assert {'myregion': {'Subnets': ['sn-3']}} == result['Mappings']['ServerSubnets']
示例3: test_get_default_vpc
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_get_default_vpc(monkeypatch):
mock_vpc1 = MagicMock()
mock_vpc1.vpc_id = 'vpc-id1'
mock_vpc1.is_default = True
mock_vpc1.tags = []
mock_vpc2 = MagicMock()
mock_vpc2.vpc_id = 'vpc-id2'
mock_vpc2.is_default = False
mock_vpc2.tags = []
mock_vpc3 = MagicMock()
mock_vpc3.vpc_id = 'vpc-id3'
mock_vpc3.is_default = False
mock_vpc3.tags = []
mock_vpc4 = MagicMock()
mock_vpc4.vpc_id = 'vpc-id4'
mock_vpc4.is_default = True
mock_vpc4.tags = None
m_resource = MagicMock()
m_resource.return_value = m_resource
monkeypatch.setattr('boto3.resource', m_resource)
ec2 = EC2('eu-test-1')
# return default vpc
m_resource.vpcs.all.return_value = [mock_vpc1, mock_vpc2]
vpc1 = ec2.get_default_vpc()
assert vpc1.vpc_id == 'vpc-id1'
# ony one, non default
m_resource.vpcs.all.return_value = [mock_vpc2]
vpc2 = ec2.get_default_vpc()
assert vpc2.vpc_id == 'vpc-id2'
# no vpcs
m_resource.vpcs.all.return_value = []
with pytest.raises(VPCError) as exc_info:
ec2.get_default_vpc()
assert str(exc_info.value) == "Can't find any VPC!"
# multiple vpcs
m_resource.vpcs.all.return_value = [mock_vpc2, mock_vpc3]
with pytest.raises(VPCError) as exc_info:
ec2.get_default_vpc()
# no tags in vpc return default vpc
m_resource.vpcs.all.return_value = [mock_vpc4, mock_vpc2]
vpc3 = ec2.get_default_vpc()
assert vpc3.vpc_id == 'vpc-id4'
assert str(exc_info.value) == ("Multiple VPCs are only supported if one "
"VPC is the default VPC (IsDefault=true)!")
示例4: test_images
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_images(monkeypatch):
image = MagicMock()
image.id = 'ami-123'
image.name = 'BrandNewImage'
image.creationDate = datetime.datetime.utcnow().isoformat('T') + 'Z'
old_image_still_used = MagicMock()
old_image_still_used.id = 'ami-456'
old_image_still_used.name = 'OldImage'
old_image_still_used.creationDate = (datetime.datetime.utcnow() - datetime.timedelta(days=30)).isoformat('T') + 'Z'
instance = MagicMock()
instance.id = 'i-777'
instance.image_id = 'ami-456'
instance.tags = {'aws:cloudformation:stack-name': 'mystack'}
ec2 = MagicMock()
ec2.get_all_images.return_value = [image, old_image_still_used]
ec2.get_only_instances.return_value = [instance]
monkeypatch.setattr('boto.cloudformation.connect_to_region', lambda x: MagicMock())
monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: ec2)
monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())
runner = CliRunner()
with runner.isolated_filesystem():
result = runner.invoke(cli, ['images', '--region=myregion'], catch_exceptions=False)
assert 'ami-123' in result.output
assert 'ami-456' in result.output
assert 'mystack' in result.output
示例5: test_console
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_console(monkeypatch):
stack = MagicMock(stack_name='test-1')
inst = MagicMock()
inst.tags = {'aws:cloudformation:stack-name': 'test-1'}
ec2 = MagicMock()
ec2.get_only_instances.return_value = [inst]
ec2.get_console_output.return_value.output = b'**MAGIC-CONSOLE-OUTPUT**'
monkeypatch.setattr('boto.ec2.connect_to_region', lambda x: ec2)
monkeypatch.setattr('boto.cloudformation.connect_to_region',
lambda x: MagicMock(list_stacks=lambda stack_status_filters: [stack]))
monkeypatch.setattr('boto.iam.connect_to_region', lambda x: MagicMock())
runner = CliRunner()
data = {'SenzaInfo': {'StackName': 'test'}}
with runner.isolated_filesystem():
with open('myapp.yaml', 'w') as fd:
yaml.dump(data, fd)
result = runner.invoke(cli, ['console', 'myapp.yaml', '--region=myregion', '1'],
catch_exceptions=False)
assert '**MAGIC-CONSOLE-OUTPUT**' in result.output
result = runner.invoke(cli, ['console', 'foobar', '--region=myregion'],
catch_exceptions=False)
assert '' == result.output
result = runner.invoke(cli, ['console', '172.31.1.2', '--region=myregion'],
catch_exceptions=False)
assert '**MAGIC-CONSOLE-OUTPUT**' in result.output
result = runner.invoke(cli, ['console', 'i-123', '--region=myregion'],
catch_exceptions=False)
assert '**MAGIC-CONSOLE-OUTPUT**' in result.output
示例6: test_console
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_console(monkeypatch):
stack = MagicMock(stack_name="test-1")
inst = MagicMock()
inst.tags = {"aws:cloudformation:stack-name": "test-1"}
ec2 = MagicMock()
ec2.get_only_instances.return_value = [inst]
ec2.get_console_output.return_value.output = b"**MAGIC-CONSOLE-OUTPUT**"
monkeypatch.setattr("boto.ec2.connect_to_region", lambda x: ec2)
monkeypatch.setattr(
"boto.cloudformation.connect_to_region", lambda x: MagicMock(list_stacks=lambda stack_status_filters: [stack])
)
monkeypatch.setattr("boto.iam.connect_to_region", lambda x: MagicMock())
runner = CliRunner()
data = {"SenzaInfo": {"StackName": "test"}}
with runner.isolated_filesystem():
with open("myapp.yaml", "w") as fd:
yaml.dump(data, fd)
result = runner.invoke(cli, ["console", "myapp.yaml", "--region=myregion", "1"], catch_exceptions=False)
assert "**MAGIC-CONSOLE-OUTPUT**" in result.output
result = runner.invoke(cli, ["console", "foobar", "--region=myregion"], catch_exceptions=False)
assert "" == result.output
result = runner.invoke(cli, ["console", "172.31.1.2", "--region=myregion"], catch_exceptions=False)
assert "**MAGIC-CONSOLE-OUTPUT**" in result.output
result = runner.invoke(cli, ["console", "i-123", "--region=myregion"], catch_exceptions=False)
assert "**MAGIC-CONSOLE-OUTPUT**" in result.output
示例7: my_resource
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def my_resource(rtype, *args):
if rtype == "ec2":
ec2 = MagicMock()
instance = MagicMock()
instance.id = "inst-123"
instance.private_ip_address = "10.0.0.1"
instance.tags = [{"Key": "aws:cloudformation:stack-name", "Value": "test-1"}]
instance.console_output.return_value = {"Output": "**MAGIC-CONSOLE-OUTPUT**"}
ec2.instances.filter.return_value = [instance]
return ec2
return MagicMock()
示例8: test_from_boto_vpc
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_from_boto_vpc():
mock_vpc = MagicMock()
mock_vpc.vpc_id = 'vpc-id'
mock_vpc.is_default = True
mock_vpc.tags = [{'Key': 'mykey', 'Value': 'myvalue'},
{'Key': 'theanswer', 'Value': '42'},
{'Key': 'Name', 'Value': 'my-vpc'}]
vpc = EC2VPC.from_boto_vpc(mock_vpc)
assert vpc.vpc_id == 'vpc-id'
assert vpc.is_default
assert vpc.tags['mykey'] == 'myvalue'
assert vpc.tags['theanswer'] == '42'
assert vpc.name == 'my-vpc'
示例9: test_get_instances
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_get_instances(monkeypatch):
conn = MagicMock(name='conn')
instance = MagicMock(name='instance')
instance.tags = {'Name': 'app-myapp-123'}
instance.vpc_id = 'myvpc'
instance.id = 'myid'
conn.get_only_instances.return_value = [instance]
monkeypatch.setattr('boto.ec2.connect_to_region', MagicMock(name='connect_to_region', return_value=conn))
ctx = Context({'region': 'someregion', 'domain': 'apps.example.com', 'vpc': 'myvpc'})
instances = ctx.get_instances()
assert len(instances) == 1
instance = ctx.get_instance_by_id('myid')
assert instance.tags == instances[0].tags
assert ctx.get_instance_by_id('000') is None
示例10: test_get_applications
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import tags [as 别名]
def test_get_applications(monkeypatch):
conn = MagicMock(name='conn')
sg1 = MagicMock(name='invalid sg')
sg2 = MagicMock(name='valid sg')
sg2.name = 'app-myapp'
sg2.tags = {'Manifest': 'a: b'}
sg2.vpc_id = 'myvpc'
sg3 = MagicMock(name='another invalid sg')
sg3.name = 'app-myapp-db'
sg3.vpc_id = 'myvpc'
conn.get_all_security_groups.return_value = [sg1, sg2, sg3]
monkeypatch.setattr('boto.ec2.connect_to_region', MagicMock(name='connect_to_region', return_value=conn))
ctx = Context({'region': 'someregion', 'domain': 'apps.example.com', 'vpc': 'myvpc'})
apps = ctx.get_applications()
assert len(apps) == 1
app = ctx.get_application('myapp')
assert app == apps[0]