Exemplo n.º 1
0
def test_weighted_dns_load_balancer_with_different_domains(monkeypatch,  # noqa: F811
                                                           boto_client,
                                                           boto_resource):
    senza.traffic.DNS_ZONE_CACHE = {}

    boto_client['route53'].list_hosted_zones.return_value = {
        'HostedZones': [HOSTED_ZONE_ZO_NE_DEV,
                        HOSTED_ZONE_ZO_NE_COM],
        'IsTruncated': False,
        'MaxItems': '100'}

    configuration = {
        "Name": "test_lb",
        "SecurityGroups": "",
        "HTTPPort": "9999",
        'MainDomain': 'great.api.zo.ne.com',
        'VersionDomain': 'version.api.zo.ne.dev'
    }
    info = {'StackName': 'foobar', 'StackVersion': '0.1'}
    definition = {"Resources": {}}

    args = MagicMock()
    args.region = "foo"

    mock_string_result = MagicMock()
    mock_string_result.return_value = "foo"
    monkeypatch.setattr('senza.components.elastic_load_balancer.resolve_security_groups', mock_string_result)

    m_acm = MagicMock()
    m_acm_certificate = MagicMock()
    m_acm_certificate.arn = "foo"
    m_acm.get_certificates.return_value = iter([m_acm_certificate])
    monkeypatch.setattr('senza.components.elastic_load_balancer.ACM', m_acm)

    result = component_weighted_dns_elastic_load_balancer(definition,
                                                          configuration,
                                                          args,
                                                          info,
                                                          False,
                                                          AccountArguments('dummyregion'))

    assert 'zo.ne.com.' == result["Resources"]["test_lbMainDomain"]["Properties"]['HostedZoneName']
    assert 'zo.ne.dev.' == result["Resources"]["test_lbVersionDomain"]["Properties"]['HostedZoneName']

    configuration = {
        "Name": "test_lb",
        "SecurityGroups": "",
        "HTTPPort": "9999",
        'MainDomain': 'this.does.not.exists.com',
        'VersionDomain': 'this.does.not.exists.com'
    }
    senza.traffic.DNS_ZONE_CACHE = {}
    with pytest.raises(AttributeError):
        result = component_weighted_dns_elastic_load_balancer(definition,
                                                              configuration,
                                                              args,
                                                              info,
                                                              False,
                                                              AccountArguments('dummyregion'))
def component_weighted_dns_elastic_load_balancer(
        definition,
        configuration: Dict,
        args,
        info,
        force,
        account_info: AccountArguments,
        lb_component=component_elastic_load_balancer):
    if 'Domains' not in configuration:
        if 'MainDomain' in configuration:
            main_domain = configuration['MainDomain']
            main_subdomain, fall_back_hz = account_info.split_domain(
                main_domain)
            try:
                hosted_zone = next(
                    Route53.get_hosted_zones(domain_name=main_domain))
                main_zone = hosted_zone.name
            except StopIteration:
                main_zone = fall_back_hz
            del configuration['MainDomain']
        else:
            main_zone = account_info.Domain
            main_subdomain = info['StackName']

        if 'VersionDomain' in configuration:
            version_domain = configuration['VersionDomain']
            version_subdomain, fall_back_hz = account_info.split_domain(
                version_domain)
            try:
                hosted_zone = next(
                    Route53.get_hosted_zones(domain_name=version_domain))
                version_zone = hosted_zone.name
            except StopIteration:
                version_zone = fall_back_hz
            del configuration['VersionDomain']
        else:
            version_zone = account_info.Domain
            version_subdomain = '{}-{}'.format(info['StackName'],
                                               info['StackVersion'])

        configuration['Domains'] = {
            'MainDomain': {
                'Type': 'weighted',
                'Zone': '{}.'.format(main_zone.rstrip('.')),
                'Subdomain': main_subdomain
            },
            'VersionDomain': {
                'Type': 'standalone',
                'Zone': '{}.'.format(version_zone.rstrip('.')),
                'Subdomain': version_subdomain
            }
        }
    return lb_component(definition, configuration, args, info, force,
                        account_info)
Exemplo n.º 3
0
def test_weighted_dns_load_balancer(monkeypatch, boto_client, boto_resource):  # noqa: F811
    senza.traffic.DNS_ZONE_CACHE = {}

    configuration = {
        "Name": "test_lb",
        "SecurityGroups": "",
        "HTTPPort": "9999",
        'MainDomain': 'great.api.zo.ne',
        'VersionDomain': 'version.api.zo.ne'
    }
    info = {'StackName': 'foobar', 'StackVersion': '0.1'}
    definition = {"Resources": {}}

    args = MagicMock()
    args.region = "foo"

    mock_string_result = MagicMock()
    mock_string_result.return_value = "foo"
    monkeypatch.setattr('senza.components.elastic_load_balancer.resolve_security_groups', mock_string_result)

    m_acm = MagicMock()
    m_acm_certificate = MagicMock()
    m_acm_certificate.arn = "foo"
    m_acm.get_certificates.return_value = iter([m_acm_certificate])
    monkeypatch.setattr('senza.components.elastic_load_balancer.ACM', m_acm)

    result = component_weighted_dns_elastic_load_balancer(definition,
                                                          configuration,
                                                          args,
                                                          info,
                                                          False,
                                                          AccountArguments('dummyregion'))

    assert 'MainDomain' not in result["Resources"]["test_lb"]["Properties"]
Exemplo n.º 4
0
def test_template_parameters():
    definition = {}
    configuration = {'DefineParameters': False}
    args = MagicMock()
    args.__dict__ = {'Param1': 'my param value', 'SecondParam': ('1234567890' * 100)}
    info = {'StackName': 'My-Stack', 'Parameters': []}
    component_configuration(definition, configuration, args, info, False, AccountArguments('dummyregion'))
    assert definition.get('Parameters') == None
Exemplo n.º 5
0
def test_max_description_length():
    definition = {}
    configuration = {}
    args = MagicMock()
    args.__dict__ = {'Param1': 'my param value', 'SecondParam': ('1234567890' * 100)}
    info = {'StackName': 'My-Stack'}
    component_configuration(definition, configuration, args, info, False, AccountArguments('dummyregion'))
    assert definition['Description'].startswith('My Stack (Param1: my param value, SecondParam: 1234567890')
    assert 0 < len(definition['Description']) <= 1024
Exemplo n.º 6
0
def test_weighted_dns_load_balancer_v2(monkeypatch, boto_client, boto_resource):  # noqa: F811
    senza.traffic.DNS_ZONE_CACHE = {}

    configuration = {
        "Name": "MyLB",
        "SecurityGroups": "",
        "HTTPPort": "9999",
        'MainDomain': 'great.api.zo.ne',
        'VersionDomain': 'version.api.zo.ne',
        # test overwritting specific properties in one of the resources
        'TargetGroupAttributes': [{'Key': 'deregistration_delay.timeout_seconds', 'Value': '123'}],
        # test that Security Groups are resolved
        'SecurityGroups': ['foo-security-group']
    }
    info = {'StackName': 'foobar', 'StackVersion': '0.1'}
    definition = {"Resources": {}}

    args = MagicMock()
    args.region = "foo"

    mock_string_result = MagicMock()
    mock_string_result.return_value = ['sg-foo']
    monkeypatch.setattr('senza.components.elastic_load_balancer_v2.resolve_security_groups', mock_string_result)

    get_ssl_cert = MagicMock()
    get_ssl_cert.return_value = 'arn:aws:42'
    monkeypatch.setattr('senza.components.elastic_load_balancer_v2.get_ssl_cert', get_ssl_cert)

    result = component_weighted_dns_elastic_load_balancer_v2(definition,
                                                             configuration,
                                                             args,
                                                             info,
                                                             False,
                                                             AccountArguments('dummyregion'))

    assert 'MyLB' in result["Resources"]
    assert 'MyLBListener' in result["Resources"]
    assert 'MyLBTargetGroup' in result["Resources"]

    target_group = result['Resources']['MyLBTargetGroup']
    lb_listener = result['Resources']['MyLBListener']

    assert target_group['Properties']['HealthCheckPort'] == '9999'
    assert lb_listener['Properties']['Certificates'] == [
        {'CertificateArn': 'arn:aws:42'}
    ]
    # test that our custom drain setting works
    assert target_group['Properties']['TargetGroupAttributes'] == [
        {'Key': 'deregistration_delay.timeout_seconds',
         'Value': '123'}
    ]
    assert result['Resources']['MyLB']['Properties']['SecurityGroups'] == ['sg-foo']