Esempio n. 1
0
def cluster():
    boto3.setup_default_session(region_name='eu-west-1')

    moto.mock_ecs().start()
    moto.mock_ec2().start()

    ec2 = boto3.resource('ec2', region_name='eu-west-1')
    ecs = boto3.client('ecs', region_name='eu-west-1')

    test_instance = ec2.create_instances(
        ImageId="ami-1234abcd",
        MinCount=1,
        MaxCount=1,
    )[0]

    instance_id_document = json.dumps(
        ec2_utils.generate_instance_identity_document(test_instance))

    cluster = ecs.create_cluster(clusterName='default')
    ecs.register_container_instance(
        cluster='default',
        instanceIdentityDocument=instance_id_document)

    yield cluster

    moto.mock_ecs().stop()
    moto.mock_ec2().stop()
Esempio n. 2
0
def create_ec2():
    moto.mock_ec2().start()
    client = boto3.client('ec2', region_name=default_region)
    r = client.run_instances(ImageId=ami_id,
                             MinCount=min_count,
                             MaxCount=min_count)
    instance_id = r['Instances'][0]['InstanceId']
    yield instance_id
    moto.mock_ec2().stop()
Esempio n. 3
0
    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()
    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()
Esempio n. 5
0
def moto_start(set_region):
    mock_autoscaling().start()
    mock_ec2().start()
    mock_ecs().start()
    mock_sns().start()
    mock_sqs().start()
    yield
    mock_autoscaling().stop()
    mock_ec2().stop()
    mock_ecs().stop()
    mock_sns().stop()
    mock_sqs().stop()
Esempio n. 6
0
def mock_aws_config():
    mock = mock_ec2()
    mock.start()

    return {
        "region": "ap-southeast-2",
    }
    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)
Esempio n. 8
0
    def setUp(self):
        # load dummy kube specs
        dir_path = os.path.dirname(os.path.realpath(__file__))
        with open(os.path.join(dir_path, 'data/busybox.yaml'), 'r') as f:
            self.dummy_pod = yaml.load(f.read())
        with open(os.path.join(dir_path, 'data/ds-pod.yaml'), 'r') as f:
            self.dummy_ds_pod = yaml.load(f.read())
        with open(os.path.join(dir_path, 'data/rc-pod.yaml'), 'r') as f:
            self.dummy_rc_pod = yaml.load(f.read())
        with open(os.path.join(dir_path, 'data/node.yaml'), 'r') as f:
            self.dummy_node = yaml.load(f.read())

        # this isn't actually used here
        # only needed to create the KubePod object...
        self.api = pykube.HTTPClient(
            pykube.KubeConfig.from_file('~/.kube/config'))

        # start creating our mock ec2 environment
        self.mocks = [moto.mock_ec2(), moto.mock_autoscaling()]
        for moto_mock in self.mocks:
            moto_mock.start()

        client = boto3.client('autoscaling', region_name='us-west-2')
        self.asg_client = client

        client.create_launch_configuration(LaunchConfigurationName='dummy-lc',
                                           ImageId='ami-deadbeef',
                                           KeyName='dummy-key',
                                           SecurityGroups=[
                                               'sg-cafebeef',
                                           ],
                                           InstanceType='t2.medium')

        client.create_auto_scaling_group(AutoScalingGroupName='dummy-asg',
                                         LaunchConfigurationName='dummy-lc',
                                         MinSize=0,
                                         MaxSize=10,
                                         VPCZoneIdentifier='subnet-beefbeef',
                                         Tags=[{
                                             'Key': 'KubernetesCluster',
                                             'Value': 'dummy-cluster',
                                             'PropagateAtLaunch': True
                                         }, {
                                             'Key': 'KubernetesRole',
                                             'Value': 'worker',
                                             'PropagateAtLaunch': True
                                         }])

        # finally our cluster
        self.cluster = Cluster(aws_access_key='',
                               aws_secret_key='',
                               regions=['us-west-2', 'us-east-1', 'us-west-1'],
                               kubeconfig='~/.kube/config',
                               pod_namespace=None,
                               idle_threshold=60,
                               instance_init_time=60,
                               type_idle_threshold=60,
                               cluster_name='dummy-cluster',
                               notifier=Notifier(),
                               dry_run=False)
Esempio n. 9
0
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'
                        }])

    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_server=ssh_server[0],
    )

    mock.stop()
Esempio n. 10
0
    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)
Esempio n. 11
0
 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)
Esempio n. 12
0
    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"])
Esempio n. 13
0
    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"
                    )
                )
Esempio n. 14
0
    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)
    def test_is_dns_hostnames_enabled(self):

        mock = moto.mock_ec2()

        mock.start()
        client = boto3.client('ec2', region_name='us-east-1')
        dhcp_options = client.create_dhcp_options(
            DhcpConfigurations=[{
                'Key': 'example.com',
                'Values': ['10.0.0.6', '10.0.0.7']
            }])
        print('dhcp options: ' + str(dhcp_options))

        vpc1 = client.create_vpc(CidrBlock="10.0.0.0/16")
        print('vpc1: ' + str(vpc1))

        response = client.modify_vpc_attribute(
            EnableDnsHostnames={'Value': True}, VpcId=vpc1['Vpc']['VpcId'])

        print('response: ' + str(response))

        results = is_dns_hostnames_enabled(client, vpc1['Vpc']['VpcId'])
        print('results: ' + str(results))
        assert results == True
        mock.stop()
Esempio n. 16
0
def subnet_id(aws_credentials):
    with mock_ec2():
        ec2_client = server.create_backend_app("ec2").test_client()
        resp = ec2_client.get("/?Action=DescribeSubnets")
        subnet_ids = re.findall("<subnetId>(.*?)</subnetId>",
                                resp.data.decode("utf-8"))
        yield subnet_ids[0]
Esempio n. 17
0
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()
    def test_get_dhcp_configurations(self):

        mock = moto.mock_ec2()

        mock.start()
        client = boto3.client('ec2', region_name='us-east-1')
        dhcp_options = client.create_dhcp_options(
            DhcpConfigurations=[{
                'Key': 'example.com',
                'Values': ['10.0.0.6', '10.0.0.7']
            }])
        print('dhcp options: ' + str(dhcp_options))

        vpc1 = client.create_vpc(CidrBlock="10.0.0.0/16")
        print('vpc1: ' + str(vpc1))
        vpc2 = client.create_vpc(CidrBlock="10.0.0.0/16")
        print('vpc2: ' + str(vpc2))

        vpc3 = client.create_vpc(CidrBlock="10.0.0.0/24")
        print('vpc3: ' + str(vpc3))

        client.associate_dhcp_options(
            DhcpOptionsId=dhcp_options['DhcpOptions']['DhcpOptionsId'],
            VpcId=vpc1['Vpc']['VpcId'])
        client.associate_dhcp_options(
            DhcpOptionsId=dhcp_options['DhcpOptions']['DhcpOptionsId'],
            VpcId=vpc2['Vpc']['VpcId'])

        results = get_dhcp_configurations(
            client, dhcp_options['DhcpOptions']['DhcpOptionsId'])
        # Returning nothing now because moto needs fixed
        assert results == []
        mock.stop()
    def setUp(self):

        log = logging.getLogger()
        if not log.handlers:
            log.addHandler(logging.NullHandler())

        self.ec2_mock = mock_ec2()
        self.ec2_mock.start()
Esempio n. 20
0
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
Esempio n. 21
0
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()
Esempio n. 22
0
def start():
    """
    Entrypoint for mocking EC2.
    :return: nothing
    """
    # start EC2 mocking with moto
    mock = mock_ec2()
    mock.start()
Esempio n. 23
0
def test_ecs():
    cl = EcsCluster(cluster_name="name", task_definitions=["one", "two"])
    with mock_ecs(), mock_ec2():
        resources.ecs_cluster(cl)
        ecs = boto3.client("ecs")
        ran = ecs.run_task(cluster=cl.cluster_name,
                           taskDefinition=cl.task_definitions[0])
        print(ran)
Esempio n. 24
0
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()
    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)
Esempio n. 26
0
    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)
Esempio n. 27
0
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)
Esempio n. 28
0
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)
    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)
Esempio n. 30
0
 def setUpMock(self):
     """
     Let Moto take over all socket communications
     """
     self.ec2mock = mock_ec2()
     self.ec2mock.start()
     self.s3mock = mock_s3()
     self.s3mock.start()
     self.route53mock = mock_route53()
     self.route53mock.start()
Esempio n. 31
0
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()
Esempio n. 32
0
def security_group_fixture():
    mock = mock_ec2()
    mock.start()
    ec2 = boto3.resource('ec2')
    sg = ec2.create_security_group(
        Description='Security Group for SSH Whitelisting',
        GroupName='SSH Whitelist',
        VpcId='vpc-123')
    yield sg
    mock.stop()
Esempio n. 33
0
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
Esempio n. 34
0
    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)
Esempio n. 35
0
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()
Esempio n. 36
0
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)
Esempio n. 37
0
def ec2_mock():
    mock = mock_ec2()
    mock_cw = mock_cloudwatch()

    mock.start()
    mock_cw.start()

    yield

    mock.stop()
    mock_cw.stop()
Esempio n. 38
0
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)
Esempio n. 39
0
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()
Esempio n. 40
0
def vpc():
    mock = moto.mock_ec2()
    mock.start()
    ec2 = boto3.resource("ec2", region_name='us-east-1')
    vpc = ec2.create_vpc(CidrBlock="172.16.0.0/16")
    vpc.wait_until_available()
    sub = ec2.create_subnet(CidrBlock="172.16.0.0/24", VpcId=vpc.id)
    sg = ec2.create_security_group(Description="Test security group",
                                   GroupName="sg1",
                                   VpcId=vpc.id)
    yield sg, sub
    mock.stop()
Esempio n. 41
0
def test_context_manager(aws_credentials):
    client = boto3.client("ec2", region_name="us-west-1")
    with pytest.raises(ClientError) as exc:
        client.describe_addresses()
    err = exc.value.response["Error"]
    err["Code"].should.equal("AuthFailure")
    err["Message"].should.equal(
        "AWS was not able to validate the provided access credentials")

    with mock_ec2():
        client = boto3.client("ec2", region_name="us-west-1")
        client.describe_addresses()["Addresses"].should.equal([])
Esempio n. 42
0
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()
Esempio n. 43
0
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
Esempio n. 44
0
def ec2(ssh_key: SSHKey) -> Iterator[None]:
    """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
Esempio n. 45
0
def test_decorator_start_and_stop():
    conn = boto.connect_ec2('the_key', 'the_secret')
    with assert_raises(EC2ResponseError):
        conn.get_all_instances()

    mock = mock_ec2()
    mock.start()
    conn = boto.connect_ec2('the_key', 'the_secret')
    list(conn.get_all_instances()).should.equal([])
    mock.stop()

    with assert_raises(EC2ResponseError):
        conn.get_all_instances()
Esempio n. 46
0
def test_decorator_start_and_stop():
    conn = boto.connect_ec2('the_key', 'the_secret')
    with assert_raises(EC2ResponseError):
        conn.get_all_instances()

    mock = mock_ec2()
    mock.start()
    conn = boto.connect_ec2('the_key', 'the_secret')
    list(conn.get_all_instances()).should.equal([])
    mock.stop()

    with assert_raises(EC2ResponseError):
        conn.get_all_instances()
Esempio n. 47
0
def ec2_instances():
    """EC2 mock service"""
    mock = mock_ec2()
    mock.start()
    client = session.client('ec2')
    client.create_security_group(GroupName='group1', Description='my first sec group')
    instances = client.run_instances(ImageId='ami-12345', MinCount=2, MaxCount=2, SecurityGroups=['group1']).get('Instances')
    for i, s in enumerate(instances):
        client.create_tags(
            Resources=[s.get('InstanceId')],
            Tags=[{'Key': 'Name', 'Value': 'Bob'}])
    yield instances
    mock.stop()
Esempio n. 48
0
def ec2():
    """EC2 mock service"""
    mock = mock_ec2()
    mock.start()

    ec2 = boto3.resource('ec2')
    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 ec2
    mock.stop()
Esempio n. 49
0
def pytest_funcarg__mock_groups(request):
    def end():
        mock.stop()

    mock = moto.mock_ec2()
    mock.start()
    con = boto.connect_ec2()
    group = con.create_security_group("mock-group", "hoge")
    group2 = con.create_security_group("mock-group2", "description")
    group.authorize(ip_protocol="tcp", from_port=22, to_port=22, cidr_ip="192.168.1.0/32")
    group.authorize(ip_protocol="tcp", from_port=22, to_port=22, src_group=group2)
    request.addfinalizer(end)
    return [group, group2]
Esempio n. 50
0
    def test_create_security_group(self):
        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24'
        }

        sg_ctx = {
            'name': 'sg01a',
            'description': 'Test security group sg01a',
            'vpc': 'vpc01'
        }

        tags = [
            {
                'Key': 'Name',
                'Value': 'sg01a'
            }
        ]

        vpc_filters = [{'Name': 'tag:Name', 'Values': ['vpc01']}]
        sg_filters = [{'Name': 'tag:Name', 'Values': ['sg01a']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_sg.SecurityGroupWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.SecurityGroup'
            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 security group
            h = ec2_sg.create_handler(sg_ctx, self.credentials)
            h.create_resource()

            security_groups = list(ec2.security_groups.filter(Filters=sg_filters))
            sg = security_groups[0]

            self.assertEqual(len(security_groups), 1)
            self.assertEqual(sg.name, 'sg01a')
            # Security groups have a dedicated attribute for their name
            self.assertEqual(sg.name, sg.group_name)
            self.assertEqual(sg.vpc_id, vpc.id)
            self.assertCountEqual(sg.tags, tags)
Esempio n. 51
0
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()
Esempio n. 52
0
    def setUp(self):
        os.environ["AWS_DEFAULT_REGION"] = "us-east-1"

        # Start moto environment
        self.mock = mock_ec2()
        self.mock.start()

        # Create mock unused images
        self.ec2 = boto3.resource("ec2")
        instances = self.ec2.create_instances(ImageId="ami-inuse", MinCount=5, MaxCount=5)

        # Mock images
        self.unused = [i.create_image(Name="unused") for i in instances]
        [i.terminate() for i in instances]
Esempio n. 53
0
    def setup_class(klass):
        # setup bucket to test providing an s3 bucket to Case class
        klass.m = mock_s3()

        klass.m.start()

        klass.e = mock_ec2()
        klass.e.start()

        klass.created_buckets = []
        klass.s3_resource = boto3.resource(
             service_name='s3',
             region_name='us-west-2'
             #endpoint_url='http://localhost:5000',
        )

        klass.existing_bucket_name = "case-lib-test-{0}".format(
                ''.join(random.choice(string.ascii_lowercase +
                                      string.digits) for _ in range(10))
                )
        klass.s3_resource.Bucket(klass.existing_bucket_name).create()
        klass.created_buckets.append(klass.existing_bucket_name)

        # create a Case object for testing
        klass.generic_case = case.Case()
        klass.created_buckets.append(klass.generic_case.case_bucket)

        #setup default info for log collection
        klass.log_base = os.path.dirname(os.path.abspath(__file__))

        #create test files
        klass.test_log = "{0}/{1}-aws_ir.log".format(klass.log_base,
                                                     klass.generic_case.case_number)
        klass.renamed_test_log = "{0}/{1}-{2}-aws_ir.log".format(
                klass.log_base,
                klass.generic_case.case_number,
                'i-12345678'
            )

        with open(klass.test_log, 'w') as f:
            f.write('test log data')
            f.close()

        #create test screenshot
        klass.test_jpg = "{0}/{1}-console.jpg".format(klass.log_base,
                                                     klass.generic_case.case_number)
        with open(klass.test_jpg, 'w') as f:
            f.write('test jpg data')
            f.close()
Esempio n. 54
0
    def test_delete_security_group(self):
        vpc_ctx = {
            'name': 'vpc01',
            'cidr_block': '10.0.10.0/24'
        }

        sg_ctx = {
            'name': 'sg01a',
            'description': 'Test security group sg01a',
            'vpc': 'vpc01'
        }

        filters = [{'Name': 'tag:Name', 'Values': ['sg01a']}]

        def _add_wrapper(base_classes, **kwargs):
            base_classes.insert(0, ec2_sg.SecurityGroupWrapper)

        with mock_ec2():
            event = 'creating-resource-class.ec2.SecurityGroup'
            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 security group
            h = ec2_sg.create_handler(sg_ctx, self.credentials)
            h.create_resource()

            security_groups = list(ec2.security_groups.filter(Filters=filters))

            self.assertEqual(len(security_groups), 1)

            # We clear the resource cache to simulate a new
            # program execution with the 'delete' option
            base.BaseHandler._cache.clear()

            # Delete the security group
            h.delete_resource()

            security_groups = list(ec2.security_groups.filter(Filters=filters))

            self.assertEqual(len(security_groups), 0)
Esempio n. 55
0
	def setUp(self):
		self.ec2_mock = mock_ec2()
		self.ec2_mock.start()

		self.ec2_connect = boto.connect_ec2(config.EC2_SECRET, config.EC2_ACCESS)
		self._copy_mocked_ec2_conn()
		ScannerType.objects.create_all_valid_scanner_db_entries()
		self.instance_image_tags = ScannerTypeWorkerImage.objects.get_worker_image_set()
		# have to make sure we have valid scanner types

		# have to add each image type to the mocked back end before we try the individual scanner type
		for image_tag in self.instance_image_tags:
			reservation = self.ec2_connect.run_instances(image_tag)
			instances = reservation.instances
			i = self.ec2_connect.create_image(instances[0].id, image_tag, description="test image")
			# got this syntax from the mock tests
			self.ec2_connect.terminate_instances(map(attrgetter('id'), instances))
		# even though the instances are terminated Ec2 will still have them there in shutting down state
		self.assertNotEqual(0, ScannerType.objects.count())
Esempio n. 56
0
    def test_attach_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_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 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)