Python moto.mock_ec2() Examples

The following are 22 code examples of moto.mock_ec2(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module moto , or try the search function .
Example #1
Source File: environment.py    From clusterman with Apache License 2.0 6 votes vote down vote up
def boto_patches(context):
    mock_sqs_obj = mock_sqs()
    mock_sqs_obj.start()
    mock_ec2_obj = mock_ec2()
    mock_ec2_obj.start()
    mock_autoscaling_obj = mock_autoscaling()
    mock_autoscaling_obj.start()
    vpc_response = ec2.create_vpc(CidrBlock='10.0.0.0/24')
    subnet_response = ec2.create_subnet(
        CidrBlock='10.0.0.0/24',
        VpcId=vpc_response['Vpc']['VpcId'],
        AvailabilityZone='us-west-2a'
    )
    context.subnet_id = subnet_response['Subnet']['SubnetId']
    yield
    mock_sqs_obj.stop()
    mock_ec2_obj.stop()
    mock_autoscaling_obj.stop() 
Example #2
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {"ec2": {"security_group": ["test-security-group"]}}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"security_groups": {"clean": True, "ttl": -1}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #3
Source File: conftest.py    From jungle with MIT License 5 votes vote down vote up
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() 
Example #4
Source File: conftest.py    From diffy with Apache License 2.0 5 votes vote down vote up
def ec2():
    with mock_ec2():
        yield boto3.client("ec2", region_name="us-east-1") 
Example #5
Source File: conftest.py    From clusterman with Apache License 2.0 5 votes vote down vote up
def setup_ec2():
    mock_ec2_obj = moto.mock_ec2()
    mock_ec2_obj.start()
    yield
    mock_ec2_obj.stop() 
Example #6
Source File: test_keypair.py    From stacker with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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 
Example #7
Source File: test_aws.py    From flambe with MIT License 5 votes vote down vote up
def ec2_mock():
    mock = mock_ec2()
    mock_cw = mock_cloudwatch()

    mock.start()
    mock_cw.start()

    yield

    mock.stop()
    mock_cw.stop()

# Create a mock subnet_id 
Example #8
Source File: test_keypair.py    From runway with Apache License 2.0 5 votes vote down vote up
def ec2(ssh_key):
    """Mock EC2."""
    # 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 
Example #9
Source File: conftest.py    From cloudaux with Apache License 2.0 5 votes vote down vote up
def ec2(sts, conn_dict):
    with mock_ec2():
        yield boto3_cached_conn("ec2", **conn_dict) 
Example #10
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"volumes": {"clean": True, "ttl": -1}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #11
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"volumes": {"clean": True, "ttl": 7}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #12
Source File: test_securityhub_iam.py    From aws-auto-remediate with GNU General Public License v3.0 5 votes vote down vote up
def sh(self):
        with moto.mock_ec2(), moto.mock_s3(), moto.mock_iam(), moto.mock_kms():
            sh = security_hub_rules.SecurityHubRules(logging)
            yield sh 
Example #13
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"security_groups": {"clean": True, "ttl": -1}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #14
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"instances": {"clean": True, "ttl": -1}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #15
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"instances": {"clean": True, "ttl": 7}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #16
Source File: test_ec2.py    From aws-auto-cleanup with GNU General Public License v3.0 5 votes vote down vote up
def test_class(self):
        with moto.mock_ec2():
            whitelist = {}
            settings = {
                "general": {"dry_run": False},
                "services": {"ec2": {"instances": {"clean": True, "ttl": -1}}},
            }
            resource_tree = {"AWS": {}}

            test_class = ec2_cleanup.EC2Cleanup(
                logging, whitelist, settings, resource_tree, "ap-southeast-2"
            )
            yield test_class 
Example #17
Source File: test_batch_waiters.py    From airflow with Apache License 2.0 5 votes vote down vote up
def ec2_client(aws_region):
    with mock_ec2():
        yield boto3.client("ec2", region_name=aws_region) 
Example #18
Source File: test_moto.py    From aws-data-wrangler with Apache License 2.0 5 votes vote down vote up
def moto_subnet():
    with moto.mock_ec2():
        ec2 = boto3.resource("ec2", region_name="us-west-1")
        vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")
        subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock="10.0.0.0/24", AvailabilityZone="us-west-1a")
        yield subnet.id 
Example #19
Source File: test_securityhub_ec2.py    From aws-auto-remediate with GNU General Public License v3.0 5 votes vote down vote up
def sh(self):
        with moto.mock_ec2(), moto.mock_sts():
            sh = security_hub_rules.SecurityHubRules(logging)
            yield sh 
Example #20
Source File: test_securityhub_ec2.py    From aws-auto-remediate with GNU General Public License v3.0 5 votes vote down vote up
def sh(self):
        with moto.mock_ec2():
            sh = security_hub_rules.SecurityHubRules(logging)
            yield sh 
Example #21
Source File: test_securityhub_ec2.py    From aws-auto-remediate with GNU General Public License v3.0 5 votes vote down vote up
def sh(self):
        with moto.mock_ec2():
            sh = security_hub_rules.SecurityHubRules(logging)
            yield sh 
Example #22
Source File: test_securityhub_ec2.py    From aws-auto-remediate with GNU General Public License v3.0 5 votes vote down vote up
def sh(self):
        with moto.mock_ec2():
            sh = security_hub_rules.SecurityHubRules(logging)
            yield sh