コード例 #1
0
ファイル: test_application.py プロジェクト: zhoudaqing/huskar
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
コード例 #2
0
def test_add_blacklist_oos(mocker, client, admin_token, minimal_mode):
    mocker.patch.object(Instance, 'save', side_effect=OutOfSyncError())

    r = client.post('/api/_internal/ops/blacklist',
                    data={'remote_addr': '169.254.1.1'},
                    headers={'Authorization': admin_token})
    assert r.status_code == 409, r.data
    assert r.json['status'] == 'Conflict'
    assert r.json['message'] == 'resource is modified by another request'
コード例 #3
0
def test_add_list_oos(mocker, client, test_application, test_application_token,
                      hijack_url):
    mocker.patch.object(Instance, 'save', side_effect=OutOfSyncError())

    r = client.post(hijack_url,
                    data={
                        'application': test_application.application_name,
                        'stage': 'D',
                    },
                    headers={'Authorization': test_application_token})
    assert r.status_code == 409, r.data
    assert r.json['status'] == 'Conflict'
    assert r.json['message'] == 'resource is modified by another request'
コード例 #4
0
 def set(self, application, cluster, key, value, version=None):
     value = str(value) if value else ''
     path = self.get_path(application, cluster, key)
     try:
         if version is None:
             self.raw_client.set(path, value)
         else:
             self.raw_client.set(path, value, version)
         zk_payload(payload_data=value, payload_type='set')
     except NoNodeError:
         self.raw_client.create(path, value, makepath=True)
         zk_payload(payload_data=value, payload_type='create')
     except BadVersionError as e:
         raise OutOfSyncError(e)
コード例 #5
0
def test_set_default_route_outofsyncerror(client, zk, test_application,
                                          test_application_token, mocker):
    url = '/api/serviceroute/default/%s' % test_application.application_name
    headers = {'Authorization': test_application_token}
    mocked_route_management = mocker.MagicMock()
    mocker.patch('huskar_api.api.service_route.RouteManagement',
                 return_value=mocked_route_management)
    mocked_route_management.set_default_route.side_effect = OutOfSyncError()

    with raises(OutOfSyncError):
        client.put(url,
                   data={
                       'ezone': 'overall',
                       'intent': 'direct',
                       'cluster_name': 'channel-stable-1',
                   },
                   headers=headers)
    assert mocked_route_management.set_default_route.call_count == 3
コード例 #6
0
ファイル: test_service.py プロジェクト: zhoudaqing/huskar
def test_set_weight(
        client, zk, mocker, test_application_name, test_application_token):
    key = '169.254.1.2_5000'
    value = '{"ip": "169.254.1.2", "port":{"main": 5000},"state":"up"}'
    args = (test_application_name, key)
    zkpath = '/huskar/service/%s/alpha_stable/%s' % args
    url = '/api/service/%s/alpha_stable/%s/weight' % args
    headers = {'Authorization': test_application_token}

    # create
    zk.create(zkpath, value, makepath=True)
    r = client.post(url, headers=headers, data={
        'weight': '10',
        'ephemeral': '1',
    })
    assert_response_ok(r)

    data, stat = zk.get(zkpath)
    assert json.loads(data)['meta']['weight'] == '10'
    assert stat.version == 1

    # update
    r = client.post(url, headers=headers, data={
        'weight': '200',
        'ephemeral': '1',
    })
    assert_response_ok(r)

    data, stat = zk.get(zkpath)
    assert json.loads(data)['meta']['weight'] == '200'
    assert stat.version == 2

    r = client.post(url, headers=headers, data={
        'weight': '-1',
        'ephemeral': '1',
    })
    assert r.status_code == 400
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == 'weight must be a positive integer'

    r = client.post(url, headers=headers, data={
        'ephemeral': '1',
    })
    assert r.status_code == 400
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == 'weight must be a positive integer'

    r = client.post(url, headers=headers, data={
        'weight': '1',
    })
    assert r.status_code == 400
    assert r.json['status'] == 'BadRequest'
    assert r.json['message'] == 'ephemeral must be "1" for now'

    zk.set(zkpath, 'broken')
    r = client.post(url, headers=headers, data={
        'weight': '1',
        'ephemeral': '1',
    })
    assert r.status_code == 500
    assert r.json['status'] == 'InternalServerError'

    zk.set(zkpath, value)
    with mocker.patch.object(Instance, 'save', side_effect=OutOfSyncError()):
        r = client.post(url, headers=headers, data={
            'weight': '1',
            'ephemeral': '1',
        })
    assert r.status_code == 409
    assert r.json['status'] == 'Conflict'
    assert r.json['message'] == (
        'service %s/alpha_stable/%s has been modified by another request' % (
            test_application_name, key)
    )

    url = '/api/service/%s/alpha_dev/%s/weight' % args
    r = client.post(url, headers=headers, data={
        'weight': '1',
        'ephemeral': '1',
    })
    assert r.status_code == 404
    assert r.json['status'] == 'NotFound'
    assert r.json['message'] == 'service %s/alpha_dev/%s does not exist' % (
        test_application_name, key)
コード例 #7
0
ファイル: service.py プロジェクト: zhoudaqing/huskar
    def save(cls,
             application,
             cluster,
             key,
             value=None,
             runtime=None,
             version=None):
        """Register a service instance.

        :param str application: The name of application (appid).
        :param str cluster: The name of cluster.
        :param str key: The fingerprint of service instance.
        :param dict value: The information of service instance.
        :param dict runtime: The overlay information of service instance.
        :param int version: The version of service instance.
        """
        cls.check_cluster_name_in_creation(application, cluster)
        cluster_path = cls.client.get_path(application, cluster)
        service_path = cls.client.get_path(application, cluster,
                                           encode_key(key))

        raw_client = cls.client.raw_client
        raw_client.ensure_path(cluster_path)

        merged_value = {}
        merged_value.update(value or {})
        merged_value.update(runtime or {})

        try:
            remote_value, stat = raw_client.get(service_path)
        except NoNodeError:
            if not value:
                raise ServiceValueError(
                    '`value` should be provided while creating service.')
            json_merged_value = json.dumps(merged_value)
            try:
                raw_client.create(service_path, json_merged_value)
                zk_payload(payload_data=json_merged_value,
                           payload_type='create')
            except NodeExistsError as e:
                raise OutOfSyncError(e)
            stat = raw_client.exists(service_path)
            if stat is None:
                raise OutOfSyncError()
            return cls.info_class(merged_value, stat)
        else:
            if version is None:
                version = stat.version
            try:
                remote_value = json.loads(remote_value)
            except (ValueError, TypeError):
                logger.warning('Failed to parse %r', service_path)
                remote_value = {}
            new_value = dict(remote_value)
            new_value.update(value or {})
            new_value.update(runtime or {})
            json_new_value = json.dumps(new_value)
            try:
                stat = raw_client.set(service_path,
                                      json_new_value,
                                      version=version)
                zk_payload(payload_data=json_new_value, payload_type='set')
            except BadVersionError as e:
                raise OutOfSyncError(e)
            return cls.info_class(new_value, stat)