Ejemplo n.º 1
0
def test_boto3_validate_launch_args():
    """Test validation of launch args for boto3 connection"""
    configDict = {
        'awsaccesskey': 'the_key',
        'awssecretkey': 'the_secret',
        'region': 'us-east-1',
        'placementgroup': 'testgroup',
    }

    # Set up adapter and process configDict
    adapter = Aws()
    adapter.process_config(configDict)

    # Get boto3 connection
    conn3 = adapter.getEC2Connection3(configDict)

    # We have to mock this directly since moto doesn't implement
    # a mock of create_placement_group at present (Oct. 2019)
    with mock.patch.object(conn3, 'create_placement_group') as mock_cpg:
        adapter._validate_ec2_launch_args(conn3, configDict)

    # Check results
    call_kwargs = mock_cpg.call_args[1]
    assert len(call_kwargs) == 2
    assert call_kwargs['GroupName'] == configDict['placementgroup']
    assert call_kwargs['Strategy'] == 'cluster'
Ejemplo n.º 2
0
def test_boto3_conn_setup(proxy_host, proxy_port, proxy_user, proxy_pass):
    """Test setup of boto3 connection"""

    # Construct configDict
    configDict = {
        'awsaccesskey': 'the_key',
        'awssecretkey': 'the_secret',
        'region': 'us-east-1',
    }
    if proxy_host:
        configDict['proxy_host'] = proxy_host
        configDict['proxy_port'] = proxy_port
        if proxy_user:
            configDict['proxy_user'] = proxy_user
            configDict['proxy_pass'] = proxy_pass

    # Set up adapter and process configDict
    adapter = Aws()
    adapter.process_config(configDict)

    # Get boto3 connection
    session_cls = 'tortuga.resourceAdapter.aws.aws.boto3.Session'
    config_cls = 'tortuga.resourceAdapter.aws.aws.Config'
    with mock.patch(session_cls) as boto3_session_mock, \
         mock.patch(config_cls) as botocore_config_mock:
        conn3 = adapter.getEC2Connection3(configDict)

    # Test session call args/kwargs
    session_call_kwargs = boto3_session_mock.call_args[1]
    assert len(session_call_kwargs) == 3
    assert session_call_kwargs['aws_access_key_id'] == \
        configDict['awsaccesskey']
    assert session_call_kwargs['aws_secret_access_key'] == \
        configDict['awssecretkey']
    assert session_call_kwargs['region_name'] == configDict['region']

    # Test config call args/kwargs if proxy used
    if proxy_host is not None:
        config_call_kwargs = botocore_config_mock.call_args[1]
        proxy_dict = config_call_kwargs['proxies']
        assert len(proxy_dict) == 1
        assert 'http' in proxy_dict
        proxy_url = f'{proxy_host}:{proxy_port}'
        if proxy_user is not None:
            proxy_url = f'{proxy_user}:{proxy_pass}@{proxy_url}'
        assert proxy_dict['http'] == proxy_url
Ejemplo n.º 3
0
def test_scale_set_tags(name_tag):
    """Test scale set tag generation"""
    configDict = {
        'installer_ip': '123.456.7.89',
        'use_instance_hostname': True,
        'tags': {
            'other tag': 'value'
        }
    }
    if name_tag:
        configDict['tags']['Name'] = name_tag
    group_name = 'fake_group'
    hardware_profile = 'hwp'
    software_profile = 'swp'
    adapter = Aws()
    adapter.process_config(configDict)

    # Get tags
    tags = adapter._get_scale_set_tags(group_name, configDict,
                                       hardware_profile, software_profile)

    # Check basic tag properties
    assert len(tags) == 6
    for tag in tags:
        assert tag.resource_id == group_name
        assert tag.resource_type == 'auto-scaling-group'
        assert tag.propagate_at_launch

    # Convert to dict for more specific testing
    tag_dict = {tag.key: tag for tag in tags}
    assert tag_dict['tortuga-hardwareprofile'].value == hardware_profile
    assert tag_dict['tortuga-softwareprofile'].value == software_profile
    assert tag_dict['tortuga-installer_hostname'].value == \
        adapter._sanitze_tag_value(adapter.installer_public_hostname)
    assert tag_dict['tortuga-installer_ipaddress'].value == \
        adapter._sanitze_tag_value(configDict['installer_ip'])
    assert tag_dict['other tag'].value == configDict['tags']['other tag']

    # Check name
    expected_name = name_tag if name_tag else 'Tortuga compute node'
    assert tag_dict['Name'].value == expected_name
Ejemplo n.º 4
0
def test_launch_EC2(valid_ami):
    """Test full EC2 launch process"""
    # Set up configDict
    configDict = {
        'keypair': 'keypair_name',
        'instancetype': 't2.large',
        'region': 'us-east-1',
        'awsaccesskey': 'the_key',
        'awssecretkey': 'the_secret',
        'zone': 'fake_zone',
        'use_instance_hostname': False,
        'block_device_map': '/dev/sda1=:30:true:io1:500:encrypted',
        'ami': valid_ami,
        'aki': 'fake_kernel_id',
        'ari': 'fake_ramdisk_id',
        'ebs_optimized': True,
        'monitoring_enabled': False,
        'iam_instance_profile_name': 'fake_profile_name',
        'subnet_id': None,
        'securitygroup': ['sg-1234'],
        'associate_public_ip_address': True,
        'tags': {'key1': 'value1', 'key2': 'value2'},
    }

    # Set up adapter and process configDict
    adapter = Aws()
    adapter.process_config(configDict)

    # Set up a mock node
    node = mock.Mock(spec=Node)
    node.hardwareprofile.name = 'hwp_name'
    node.softwareprofile.name = 'swp_name'
    node.name = 'node_name'

    # Run __launchEC2
    with mock_ec2():
        # Get boto3 connection
        conn3 = adapter.getEC2Connection3(configDict)

        # Create mock vpc and subnet
        vpc = conn3.create_vpc(CidrBlock='10.0.0.0/16')
        subnet = conn3.create_subnet(CidrBlock='10.0.0.0/18', VpcId=vpc.id)
        configDict['subnet_id'] = subnet.id

        # Launch instances
        result = adapter._Aws__launchEC2(conn3, configDict, count=1, node=node)

    # Check results
    assert len(result) == 1
    instance = result[0]
    assert instance.image.id == configDict['ami']
    assert instance.key_pair.key_name == configDict['keypair']
    assert instance.instance_type == configDict['instancetype']
    assert instance.vpc_id == vpc.id
    assert instance.subnet_id == subnet.id
    assert len(instance.network_interfaces) == 1
    assert instance.network_interfaces[0].groups[0]['GroupName'] == \
        configDict['securitygroup'][0]
    # NOTE: moto does not give the correct result for these two. I have tested
    # manually on AWS and confirmed that they work as expected.
    #assert instance.ebs_optimized == configDict['ebs_optimized']
    #assert instance.monitoring['state'] == 'disabled'

    # Check tags
    instance_tags = {d['Key']: d['Value'] for d in instance.tags}
    assert instance_tags['Name'] == node.name
    assert instance_tags['key1'] == 'value1'
    assert instance_tags['key2'] == 'value2'
    assert instance_tags['tortuga-softwareprofile'] == \
        node.softwareprofile.name
    assert instance_tags['tortuga-hardwareprofile'] == \
        node.hardwareprofile.name
    assert instance_tags['tortuga-installer_hostname'] == \
        adapter.installer_public_hostname
    # Have to sanitize this one
    assert instance_tags['tortuga-installer_ipaddress'] == \
        adapter._sanitze_tag_value(adapter.installer_public_ipaddress)