Exemple #1
0
def test_bind(db):
    InfraDownstream.bind('base.foo', 'redis', 'mycache', 'idcs', 'alta1',
                         'url', 'redis.foo')
    InfraDownstream.bind('base.bar', 'redis', 'mycache', 'idcs', 'alta1',
                         'url', 'redis.foo')
    InfraDownstream.bind('base.bar', 'redis', 'mycache', 'idcs', 'altb1',
                         'url', 'redis.foo')
    InfraDownstream.bind('base.bar', 'redis', u'缓存', 'idcs', 'alta1', 'url',
                         'redis.foo')
    result = list_infra_downstream_by_application_name(db, 'redis.foo')
    assert len(result) == 4
    assert result[0].user_application_name == 'base.foo'
    assert result[0].user_scope_name == 'alta1'
    assert result[1].user_application_name == 'base.bar'
    assert result[1].user_scope_name == 'alta1'
    assert result[2].user_application_name == 'base.bar'
    assert result[2].user_scope_name == 'altb1'
    assert result[3].user_application_name == 'base.bar'
    assert result[3].user_scope_name == 'alta1'
    assert result[3].user_infra_name == u'缓存'
    result = list_infra_downstream_by_application_name(db, 'redis.bar')
    assert len(result) == 0

    InfraDownstream.bind('base.bar', 'redis', 'mycache', 'idcs', 'alta1',
                         'url', 'redis.bar')
    result = list_infra_downstream_by_application_name(db, 'redis.foo')
    assert len(result) == 3
    assert result[0].user_application_name == 'base.foo'
    assert result[0].user_scope_name == 'alta1'
    assert result[1].user_application_name == 'base.bar'
    assert result[1].user_scope_name == 'altb1'
    result = list_infra_downstream_by_application_name(db, 'redis.bar')
    assert len(result) == 1
    assert result[0].user_application_name == 'base.bar'
    assert result[0].user_scope_name == 'alta1'
Exemple #2
0
    def _update(self, application_name, request, infra_type, infra_name):
        check_application_auth(application_name, Authority.WRITE)
        check_infra_type(infra_type)

        scope_type = request.args['scope_type']
        scope_name = request.args['scope_name']
        check_scope(scope_type, scope_name)

        value = request.get_json()
        if not value or not isinstance(value, dict):
            abort(400, 'Unacceptable content type or content body')

        infra_info = InfraInfo(huskar_client.client, application_name,
                               infra_type)
        infra_info.load()
        old_value = infra_info.get_by_name(infra_name, scope_type, scope_name)

        yield application_name, infra_info, scope_type, scope_name, value

        infra_urls = infra_info.extract_urls(value)
        infra_application_names = extract_application_names(infra_urls)
        new_value = infra_info.get_by_name(infra_name, scope_type, scope_name)

        with audit_log(audit_log.types.UPDATE_INFRA_CONFIG,
                       application_name=application_name,
                       infra_type=infra_type,
                       infra_name=infra_name,
                       scope_type=scope_type,
                       scope_name=scope_name,
                       old_value=old_value,
                       new_value=new_value):
            for infra_application_name in infra_application_names:
                check_application(infra_application_name)

            infra_info.save()

            with suppress_exceptions('infra config updating'):
                infra_urls = infra_info.extract_urls(value, as_dict=True)
                infra_applications = extract_application_names(infra_urls)
                for field_name, infra_application_name in \
                        infra_applications.iteritems():
                    InfraDownstream.bind(application_name, infra_type,
                                         infra_name, scope_type, scope_name,
                                         field_name, infra_application_name)
Exemple #3
0
def test_unbind(db):
    InfraDownstream.bind('base.foo', 'redis', 'mycache', 'idcs', 'alta1',
                         'url', 'redis.foo')
    InfraDownstream.bind('base.foo', 'redis', 'mycache', 'idcs', 'altb1',
                         'url', 'redis.foo')
    InfraDownstream.bind('base.bar', 'redis', 'mycache', 'idcs', 'alta1',
                         'url', 'redis.foo')
    result = list_infra_downstream_by_application_name(db, 'redis.foo')
    assert len(result) == 3

    InfraDownstream.unbind('base.bar', 'redis', 'mycache', 'idcs', 'alta1',
                           'url')
    result = list_infra_downstream_by_application_name(db, 'redis.foo')
    assert len(result) == 2
    assert result[0].user_application_name == 'base.foo'
    assert result[0].user_scope_name == 'alta1'
    assert result[1].user_application_name == 'base.foo'
    assert result[1].user_scope_name == 'altb1'
Exemple #4
0
def test_get_infra_downstream(client, test_token):
    InfraDownstream.bindmany() \
        .bind('base.foo', 'redis', 'cache-1', 'idcs', 'alta1', 'url',
              'redis.100010') \
        .bind('base.foo', 'redis', 'cache-1', 'idcs', 'altb1', 'url',
              'redis.100010') \
        .bind('base.bar', 'redis', 'cache-1', 'idcs', 'alta1', 'url',
              'redis.100010') \
        .bind('base.bar', 'redis', 'cache-2', 'idcs', 'alta1', 'url',
              'redis.100011') \
        .commit()

    r = client.get('/api/infra-config-downstream/redis.100010',
                   headers={'Authorization': test_token})
    assert_response_ok(r)
    downstream = r.json['data']['downstream']
    assert len(downstream) == 3
    assert downstream[0]['user_application_name'] == 'base.foo'
    assert downstream[0]['user_infra_type'] == 'redis'
    assert downstream[0]['user_infra_name'] == 'cache-1'
    assert downstream[0]['user_scope_pair'] == {
        'type': 'idcs',
        'name': 'alta1'
    }
    assert downstream[0]['user_field_name'] == 'url'
    assert downstream[1]['user_application_name'] == 'base.foo'
    assert downstream[1]['user_infra_type'] == 'redis'
    assert downstream[1]['user_infra_name'] == 'cache-1'
    assert downstream[1]['user_scope_pair'] == {
        'type': 'idcs',
        'name': 'altb1'
    }
    assert downstream[1]['user_field_name'] == 'url'
    assert downstream[2]['user_application_name'] == 'base.bar'
    assert downstream[2]['user_infra_type'] == 'redis'
    assert downstream[2]['user_infra_name'] == 'cache-1'
    assert downstream[2]['user_scope_pair'] == {
        'type': 'idcs',
        'name': 'alta1'
    }
    assert downstream[2]['user_field_name'] == 'url'

    r = client.get('/api/infra-config-downstream/redis.100011',
                   headers={'Authorization': test_token})
    assert_response_ok(r)
    downstream = r.json['data']['downstream']
    assert len(downstream) == 1
    assert downstream[0]['user_application_name'] == 'base.bar'
    assert downstream[0]['user_infra_type'] == 'redis'
    assert downstream[0]['user_infra_name'] == 'cache-2'
    assert downstream[0]['user_scope_pair'] == {
        'type': 'idcs',
        'name': 'alta1'
    }
    assert downstream[0]['user_field_name'] == 'url'

    InfraDownstream.bind('base.baz', 'redis', 'cache-1', 'idcs', 'alta1',
                         'url', 'redis.100011')
    InfraDownstream.unbind('base.bar', 'redis', 'cache-2', 'idcs', 'alta1',
                           'url')

    # Stale data
    r = client.get('/api/infra-config-downstream/redis.100011',
                   headers={'Authorization': test_token})
    assert_response_ok(r)
    downstream = r.json['data']['downstream']
    assert len(downstream) == 1
    assert downstream[0]['user_application_name'] == 'base.bar'
    assert downstream[0]['user_infra_name'] == 'cache-2'
    assert downstream[0]['user_scope_pair'] == {
        'type': 'idcs',
        'name': 'alta1'
    }
    assert downstream[0]['user_field_name'] == 'url'

    # Fresh data
    r = client.post('/api/infra-config-downstream/redis.100011',
                    headers={'Authorization': test_token})
    assert_response_ok(r)
    downstream = r.json['data']['downstream']
    assert len(downstream) == 1
    assert downstream[0]['user_application_name'] == 'base.baz'
    assert downstream[0]['user_infra_name'] == 'cache-1'
    assert downstream[0]['user_scope_pair'] == {
        'type': 'idcs',
        'name': 'alta1'
    }
    assert downstream[0]['user_field_name'] == 'url'