Esempio n. 1
0
def test_create_application(db, zk, test_team, faker):
    application_name = faker.uuid4()[:8]
    stat = zk.exists('/huskar/service/%s' % application_name)
    assert stat is None

    application = Application.create(application_name, test_team.id)
    assert zk.exists('/huskar/service/%s' % application_name)

    assert application.id > 0
    assert application.application_name == application_name
    assert application.domain_name == application_name
    assert application.team_id == test_team.id
    assert application.team.team_name == test_team.team_name

    user = User.get_by_name(application_name)
    assert user is not None
    assert user.is_application
    assert not user.is_admin
    assert application.check_auth(Authority.WRITE, user.id)
    assert application.check_auth(Authority.READ, user.id)

    with raises(NameOccupiedError):
        Application.create(application_name, test_team.id)  # name conflicts

    application = Application.create('baz', test_team.id)
    assert application.application_name == 'baz'
Esempio n. 2
0
def test_set_route_failed_dest_linked_empty_cluster(client, zk, test_team,
                                                    test_application,
                                                    test_application_token,
                                                    exist):
    dest_application_name = '%s_dest' % test_application.application_name
    Application.create(dest_application_name, test_team.id)
    test_application_name = test_application.application_name
    cluster_name = 'stable'
    dest_cluster_name = 'channel-stable-1'
    physical_cluster = 'foo.bar'
    path = '/huskar/service/%s/%s' % (dest_application_name, dest_cluster_name)
    data = json.dumps({'link': [physical_cluster]})
    zk.create(path, data, makepath=True)
    zk.ensure_path('/huskar/service/%s/%s' %
                   (test_application_name, cluster_name))
    if exist:
        zk.ensure_path('/huskar/service/%s/%s' %
                       (dest_application_name, physical_cluster))

    url = '/api/serviceroute/%s/%s/%s' % (test_application_name, cluster_name,
                                          dest_application_name)
    r = client.put(url,
                   data={
                       'cluster_name': dest_cluster_name,
                       'intent': 'direct'
                   },
                   headers={'Authorization': test_application_token})
    assert r.status_code == 400, r.data
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == \
        'The target cluster %s is empty.' % dest_cluster_name
Esempio n. 3
0
def test_list_applications_under_team(client, zk, test_token, fake_team,
                                      minimal_mode, present, expected):
    for name in present:
        if minimal_mode:
            zk.ensure_path('/huskar/config/%s' % name)
            sleep(0.1)
        else:
            Application.create(name, fake_team.id)

    if minimal_mode:
        r = client.get('/api/team/%s+1s' % Team.DEFAULT_NAME,
                       headers={'Authorization': test_token})
        assert r.status_code == 404
        assert r.json['status'] == 'NotFound'

        r = client.get('/api/team/%s' % Team.DEFAULT_NAME,
                       headers={'Authorization': test_token})
        assert_response_ok(r)
        assert set(expected).issubset(r.json['data'])
    else:
        r = client.get('/api/team/%s+1s' % fake_team.team_name,
                       headers={'Authorization': test_token})
        assert r.status_code == 404
        assert r.json['status'] == 'NotFound'

        r = client.get('/api/team/%s' % fake_team.team_name,
                       headers={'Authorization': test_token})
        assert_response_ok(r)
        assert r.json['data'] == expected
Esempio n. 4
0
def test_delete_route_noauth(client, zk, test_application, test_team,
                             test_token):
    Application.create('base.foo', test_team.id)
    url = '/api/serviceroute/%s/stable/base.foo' % (
        test_application.application_name)
    r = client.delete(url, headers={'Authorization': test_token})
    assert r.status_code == 400, r.data
    assert r.json['status'] == 'NoAuthError'
Esempio n. 5
0
def test_set_route_noauth(client, zk, test_application, test_team, test_token):
    Application.create('base.foo', test_team.id)
    url = '/api/serviceroute/%s/stable/base.foo' % (
        test_application.application_name)
    r = client.put(url,
                   data={'cluster_name': 'channel-stable-1'},
                   headers={'Authorization': test_token})
    assert r.status_code == 400, r.data
    assert r.json['status'] == 'NoAuthError'
Esempio n. 6
0
def test_create_application_cache_invalidation(db, test_team):
    assert Application.get_by_name('bar') is None
    assert len(Application.get_multi_by_team(test_team.id)) == 0
    assert len(Application.get_all()) == 0

    Application.create('bar', test_team.id)

    assert Application.get_by_name('bar') is not None
    assert len(Application.get_multi_by_team(test_team.id)) == 1
    assert len(Application.get_all()) == 1
Esempio n. 7
0
def test_set_route_badintent(client, zk, test_team, test_application,
                             test_application_token):
    Application.create('base.foo', test_team.id)
    url = '/api/serviceroute/%s/stable/base.foo' % (
        test_application.application_name)
    r = client.put(url,
                   data={
                       'cluster_name': 'channel-stable-1',
                       'intent': 'freestyle'
                   },
                   headers={'Authorization': test_application_token})
    assert r.status_code == 400, r.data
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == 'intent must be one of direct'
Esempio n. 8
0
    def _create(self, ignore_existed=False):
        team_name = request.values['team'].strip()
        application_name = request.values['application'].strip()
        validate_fields(application_schema,
                        {'name': application_name, 'team_name': team_name})

        team = Team.get_by_name(team_name)
        if team is None:
            abort(400, 'team "%s" does not exist' % team_name)

        require_team_admin_or_site_admin(team)

        application = Application.get_by_name(application_name)
        if application is not None:
            if ignore_existed:
                return api_response()
            raise ApplicationExistedError(
                'application: {} has existed, application is globally '
                'unique'.format(application))

        try:
            application = Application.create(application_name, team.id)
        except NameOccupiedError:
            abort(400, 'The application name {0} has been occupied.'.format(
                application_name))
        audit_log.emit(
            audit_log.types.CREATE_APPLICATION, application=application,
            team=team)
        return api_response()
Esempio n. 9
0
def test_set_route_with_dest_linked_cluster(client, zk, test_team,
                                            test_application, check_tree,
                                            test_application_token):
    dest_application_name = '%s_dest' % test_application.application_name
    Application.create(dest_application_name, test_team.id)
    test_application_name = test_application.application_name
    cluster_name = 'stable'
    dest_cluster_name = 'channel-stable-1'
    physical_cluster = 'foo.bar'
    path = '/huskar/service/%s/%s' % (dest_application_name, dest_cluster_name)
    data = json.dumps({'link': [physical_cluster]})
    zk.create(path, data, makepath=True)
    zk.ensure_path('/huskar/service/%s/%s' %
                   (test_application_name, cluster_name))
    zk.ensure_path('/huskar/service/%s/%s' %
                   (dest_application_name, physical_cluster))
    zk.ensure_path('/huskar/service/%s/%s/key' %
                   (dest_application_name, physical_cluster))

    url = '/api/serviceroute/%s/%s/%s' % (test_application_name, cluster_name,
                                          dest_application_name)
    r = client.put(url,
                   data={
                       'cluster_name': dest_cluster_name,
                       'intent': 'direct'
                   },
                   headers={'Authorization': test_application_token})
    assert_response_ok(r)
    result = [
        {
            'path':
            '/huskar/service/%s' % test_application_name,
            'data':
            '{"dependency":{"%s":["%s"]},"_version":"1"}' %
            (dest_application_name, cluster_name),
        },
        {
            'path':
            '/huskar/service/%s/%s' % (dest_application_name, cluster_name),
            'data':
            '{"route":{"%s":"%s"},"_version":"1"}' %
            (test_application_name, dest_cluster_name),
        },
    ]
    check_tree(result)
Esempio n. 10
0
def test_create_application_setup_default_zpath_oos(mocker, zk, test_team,
                                                    faker):
    application_name = faker.uuid4()[:8]
    mocker.patch.object(ServiceInfo, 'save', side_effect=OutOfSyncError())

    application = Application.create(application_name, test_team.id)
    assert application.id > 0
    stat = zk.exists('/huskar/service/%s' % application_name)
    assert stat is None
Esempio n. 11
0
def test_create_application_setup_default_zpath_bypass(mocker, zk, test_team,
                                                       faker):
    application_name = faker.uuid4()[:8]
    path = '/huskar/service/%s' % application_name
    zk.create(path, b'{}', makepath=True)

    application = Application.create(application_name, test_team.id)
    assert application.id > 0
    data, _ = zk.get(path)
    assert data == b'{}'
Esempio n. 12
0
def test_delete_team(team_foo, team_bar, user_foo):
    Application.create('biu', team_bar.id)
    TeamAdmin.ensure(team_foo.id, user_foo.id)

    # fill cache
    assert Team.get_by_name(team_foo.team_name) is team_foo
    assert Team.get_by_name(team_bar.team_name) is team_bar
    assert TeamAdmin.get_user_ids(team_foo.id) == [user_foo.id]

    Team.delete(team_foo.id)
    with raises(TeamNotEmptyError):
        Team.delete(team_bar.id)

    assert Team.get_by_name(team_foo.team_name) is None
    assert Team.get_by_name(team_bar.team_name) is not None
    assert Team.get(team_foo.id) is None
    assert Team.get(team_bar.id) is not None
    assert TeamAdmin.get_user_ids(team_foo.id) == []
    Team.delete(team_foo.id)
    assert Team.get(team_foo.id) is None
Esempio n. 13
0
def test_table_cache_expiration(faker, monkeypatch, db, test_team, ttl,
                                result_expected):
    monkeypatch.setattr(Application, 'TABLE_CACHE_EXPIRATION_TIME', ttl)
    application_name = faker.uuid4()[:8]
    application = Application.create(application_name, test_team.id)
    assert application

    cache_client = Application._cache_client
    raw_key = Application.gen_raw_key(application.id)
    key = cache_client._keygen(raw_key)
    assert int(cache_client.client.ttl(key)) == result_expected
Esempio n. 14
0
def test_get_data_from_public_domain_application(client, zk, faker,
                                                 test_application,
                                                 test_application_token,
                                                 data_type):
    name = 'public.%s' % faker.uuid4()
    application = Application.create(name, test_application.team_id)
    path = '/huskar/%s/%s/foo/bar' % (data_type, application.application_name)
    url = '/api/%s/%s/foo?key=bar' % (data_type, application.application_name)
    zk.create(path, b'1', makepath=True)

    r = client.get(url, headers={'Authorization': test_application_token})
    assert_response_ok(r)
Esempio n. 15
0
def test_set_route_failed_dest_empty_cluster(client, zk, test_team,
                                             test_application,
                                             test_application_token, exist):
    dest_application_name = '%s_dest' % test_application.application_name
    Application.create(dest_application_name, test_team.id)
    dest_cluster_name = 'channel-stable-1'
    if exist:
        zk.ensure_path('/huskar/service/%s/%s' %
                       (dest_application_name, dest_cluster_name))

    url = '/api/serviceroute/%s/stable/%s' % (
        test_application.application_name, dest_application_name)
    r = client.put(url,
                   data={
                       'cluster_name': dest_cluster_name,
                       'intent': 'direct'
                   },
                   headers={'Authorization': test_application_token})
    assert r.status_code == 400, r.data
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == \
        'The target cluster %s is empty.' % dest_cluster_name
Esempio n. 16
0
def test_archive_team(db, team_foo, user_foo, user_bar):
    team_foo.grant_admin(user_foo.id)
    assert team_foo.is_active is True

    application = Application.create('biu', team_foo.id)
    assert application.is_active is True
    with raises(TeamNotEmptyError):
        team_foo.archive()

    application.archive()
    team_foo.archive()
    assert Team.get_by_name(team_foo.team_name) is None
    assert db.query(TeamAdmin.id).filter_by(
        team_id=team_foo.id, user_id=user_foo.id).first() is not None
Esempio n. 17
0
def test_get_team_application_token_with_malformed_application(
        client, faker, capsys, db, test_team, admin_user, admin_token):
    _application = Application.create(faker.uuid4()[:8], test_team.id)
    _user = User.get_by_name(_application.application_name)
    _user.is_app = False
    db.add(_user)
    db.commit()

    r = client.post('/api/team/%s/application/%s/token' %
                    (test_team.team_name, _application.application_name),
                    data={'owner_email': admin_user.username + '@foo.bar'},
                    headers={'Authorization': admin_token})

    assert r.status_code == 400
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == ('malformed application: %s' %
                                 _application.application_name)
    assert r.json['data'] is None
Esempio n. 18
0
def test_get_application_token_with_malformed_application(
        client, faker, capsys, db, test_team, admin_token):
    _application = Application.create(faker.uuid4()[:8], test_team.id)
    _user = User.get_by_name(_application.application_name)
    with DBSession() as session:
        session.delete(_user)
        session.commit()
    _user = User.create_normal(_application.application_name,
                               '-',
                               is_active=True)

    r = client.post('/api/application/%s/token' %
                    _application.application_name,
                    headers={'Authorization': admin_token})
    assert r.status_code == 400
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == ('malformed application: %s' %
                                 _application.application_name)
    assert r.json['data'] is None
Esempio n. 19
0
def test_get_application_token_with_orphan_application(client, faker, db,
                                                       test_team, admin_token):
    _application = Application.create(faker.uuid4()[:8], test_team.id)
    _user = User.get_by_name(_application.application_name)
    _user.archive()

    user = User.get_by_name(_application.application_name)
    assert db.query(User).filter_by(username=_application.application_name,
                                    is_active=True).count() == 0

    r = client.post('/api/application/%s/token' %
                    _application.application_name,
                    headers={'Authorization': admin_token})
    assert_response_ok(r)
    user = User.get_by_token(settings.SECRET_KEY, r.json['data']['token'])
    assert user and user.username == _application.application_name

    assert User.get_by_name(_application.application_name) is not None
    assert db.query(User).filter_by(
        username=_application.application_name).count() == 1
Esempio n. 20
0
def self_application(db, faker, test_team):
    return Application.create(SELF_APPLICATION_NAME, test_team.id)
Esempio n. 21
0
def stolen_application(db, test_team, test_application):
    application_name = 'stolen-%s' % test_application.application_name
    return Application.create(application_name, test_team.id)
Esempio n. 22
0
 def make(data):
     peer = Application.create(data['application_name'], test_team.id)
     if request.param:
         return peer.setup_default_auth().generate_token(secret_key)
     return test_application_token
Esempio n. 23
0
def application(team):
    return Application.create('base.foo', team.id)
Esempio n. 24
0
 def ensure_application(self):
     return (Application.get_by_name(self.application_name)
             or Application.create(self.application_name,
                                   self.ensure_team().id))
Esempio n. 25
0
def test_oss(test_team):
    application = Application.create(u'oss.hello_test', test_team.id)
    return application.application_name
Esempio n. 26
0
def test_redis(test_team):
    application = Application.create(u'redis.test', test_team.id)
    return application.application_name
Esempio n. 27
0
def test_application(db, faker, test_team):
    application_name = faker.uuid4()[:8]
    return Application.create(application_name, test_team.id)
Esempio n. 28
0
def test_create_application_but_name_occupied(db, test_user, test_team):
    with raises(NameOccupiedError):
        Application.create(test_user.username, test_team.id)
    db.refresh(test_user)
    assert not test_user.is_application
Esempio n. 29
0
def test_collect_infra_config(zk, db, faker, test_team):
    prefix = faker.uuid4()[:8]
    for x in 'foo', 'bar':
        x = '%s_%s' % (prefix, x)
        Application.create(x, test_team.id)
        InfraDownstream.bindmany()\
            .bind(x, 'database', 'stale-db', 'idcs', 'altb1', 'master',
                  'dal.foo.master') \
            .bind(x, 'database', 'stale-db', 'idcs', 'altb1', 'slave',
                  'dal.foo.auto') \
            .bind(x, 'redis', 'stale-cache', 'idcs', 'altb1', 'url',
                  'redis.foo') \
            .commit()
        mysql_url = {
            'master': 'sam+mysql://root:[email protected]/%s_db' % x,
            'slave': 'sam+mysql://root:[email protected]/%s_db' % x}
        redis_url = {'url': 'sam+redis://redis.foo'}
        infra_info = InfraInfo(zk, x, 'database')
        infra_info.load()
        infra_info.set_by_name('db', 'idcs', 'altb1', mysql_url)
        infra_info.save()
        infra_info = InfraInfo(zk, x, 'redis')
        infra_info.load()
        infra_info.set_by_name('cache', 'idcs', 'altb1', redis_url)
        infra_info.save()

    InfraDownstream.flush_cache_by_application('dal.foo.auto')
    ds = InfraDownstream.get_multi_by_application('dal.foo.auto')
    assert all(d.user_infra_name == 'stale-db' for d in ds)

    InfraDownstream.flush_cache_by_application('redis.auto')
    ds = InfraDownstream.get_multi_by_application('redis.foo')
    assert all(d.user_infra_name == 'stale-cache' for d in ds)

    list(_collect_infra_upstream(zk, u'error\u00a0'))

    collect_infra_config()

    InfraDownstream.flush_cache_by_application('dal.foo.auto')
    ds = InfraDownstream.get_multi_by_application('dal.foo.auto')
    assert len(ds) == 2
    assert ds[0].user_application_name == '%s_foo' % prefix
    assert ds[0].user_infra_type == 'database'
    assert ds[0].user_infra_name == 'db'
    assert ds[0].user_scope_pair == ('idcs', 'altb1')
    assert ds[0].user_field_name == 'slave'
    assert ds[1].user_application_name == '%s_bar' % prefix
    assert ds[1].user_infra_type == 'database'
    assert ds[1].user_infra_name == 'db'
    assert ds[1].user_scope_pair == ('idcs', 'altb1')
    assert ds[1].user_field_name == 'slave'

    InfraDownstream.flush_cache_by_application('redis.auto')
    ds = InfraDownstream.get_multi_by_application('redis.foo')
    assert len(ds) == 2
    assert ds[0].user_application_name == '%s_foo' % prefix
    assert ds[0].user_infra_type == 'redis'
    assert ds[0].user_infra_name == 'cache'
    assert ds[0].user_scope_pair == ('idcs', 'altb1')
    assert ds[0].user_field_name == 'url'
    assert ds[1].user_application_name == '%s_bar' % prefix
    assert ds[1].user_infra_type == 'redis'
    assert ds[1].user_infra_name == 'cache'
    assert ds[1].user_scope_pair == ('idcs', 'altb1')
    assert ds[1].user_field_name == 'url'
Esempio n. 30
0
def _setup_apps(present_apps, present_auth, team_id, user_id):
    apps = {name: Application.create(name, team_id) for name in present_apps}
    for auth in present_auth:
        authority = Authority(auth['auth'])
        apps[auth['name']].ensure_auth(authority, user_id)
    return apps