コード例 #1
0
    def test_pending_to_cloning(self, executor, etcd_client):
        volume_data = [
            """
            {
                "state": "RANDOM",
                "node": "1",
                "requested": {
                    "reserved_size": 1,
                    "constraints": []
                },
                "actual": {},
                "control": {
                    "parent_id": "",
                    "updated": 0
                }
            }
        """
        ]

        machine_data = [
            """
            {
                "name": "1",
                "labels": [],
                "available": 10
            }
        """
        ]

        parent_volume = self._create_entries('volumes', volume_data,
                                             etcd_client)[0]
        self._create_entries('machines', machine_data, etcd_client)

        volume_data = json.loads(parent_volume.value)
        volume_data['state'] = 'pending'
        volume_data['control']['parent_id'] = VolumeManager(
            etcd_client).get_id_from_key(parent_volume.key)

        volume = self._create_entries('volumes', [json.dumps(volume_data)],
                                      etcd_client)[0]

        executor.tick()

        test_volume = etcd_client.read(volume.key)

        test_volume_from_json = json.loads(test_volume.value)
        volume_from_json = json.loads(volume.value)

        test_volume_state = test_volume_from_json.pop('state')
        volume_from_json.pop('node')

        assert test_volume.modifiedIndex != volume.createdIndex
        assert test_volume_state != volume_from_json.pop('state')
        assert test_volume_state == 'cloning'
        assert test_volume_from_json['control'].pop(
            'updated') != volume_from_json['control'].pop('updated')
        assert test_volume_from_json.pop('node') == '1'
コード例 #2
0
ファイル: cobalt.py プロジェクト: shaotao1988/cobalt
    def _create_volume_manager(etcd_client):
        """Factory method for creating the VolumeManger to be used inside the app.

        Args:
            etcd_client (etcd.Client): The client responsible for communicating with the database

        Returns:
            VolumeManager: The volume manager to be used by the APP
        """
        return VolumeManager(etcd_client)
コード例 #3
0
    def test_post_clone(self, etcd_client, flask_app):
        expected_result = {
            'meta': {},
            'name': '',
            'requested': {'constraints': [], 'reserved_size': 1},
            'actual': {},
            'node': '',
            'state': 'pending',
            'control': {
                'error': '',
                'error_count': 0,
                'parent_id': ''
            }
        }

        parent_data = """{
            "name": "",
            "meta": {},
            "requested": {
                "constraints": [],
                "reserved_size": 1
            },
            "actual": {
                "constraints": [],
                "reserved_size": 1
            },
            "control": {
                "error": "",
                "error_count": 0,
                "parent_id": ""
            },
            "state": "ready"
        }"""

        with flask_app.test_client() as c:
            parent = etcd_client.write('/cobalt/volumes', parent_data, append=True)
            parent.value = json.loads(parent.value)

            id = VolumeManager(etcd_client).get_id_from_key(parent.key)
            expected_result['control']['parent_id'] = str(id)

            # create clone
            request = {'id': str(id),
                       'requested': {'reserved_size': 1, 'constraints': []}}
            response = c.post('/volumes', data=json.dumps(request),
                              content_type='application/json')
            result = json.loads(response.data.decode())
            id = result.pop('id')

            assert id
            assert response.status_code == 202
            assert expected_result == result
            assert response.headers[
                       'Location'] == 'http://localhost/volumes/{}'.format(id)
コード例 #4
0
    def test_post_clone_scheduling(self, etcd_client, flask_app, parent_state):
        expected_result = {
            'message': 'Parent can\'t have state {} '
                       'in order to clone'.format(parent_state)
        }

        parent_data = """{
            "name": "",
            "meta": {},
            "requested": {
                "constraints": [],
                "reserved_size": 1
            },
            "actual": {
                "constraints": [],
                "reserved_size": 1
            },
            "control": {
                "error": "",
                "error_count": 0,
                "parent_id": ""
            }""" + ', "state": "{}"'.format(parent_state) + '}'

        with flask_app.test_client() as c:
            parent = etcd_client.write('/cobalt/volumes', parent_data, append=True)
            parent.value = json.loads(parent.value)

            id = VolumeManager(etcd_client).get_id_from_key(parent.key)

            # create clone
            request = {'id': str(id),
                       'requested': {'reserved_size': 1, 'constraints': []}}
            response = c.post('/volumes', data=json.dumps(request),
                              content_type='application/json')
            result = json.loads(response.data.decode())

            assert response.status_code == 400
            assert expected_result == result
コード例 #5
0
def volume_manager(etcd_client):
    return VolumeManager(etcd_client)
コード例 #6
0
    def test_volume_filter_states(self, volumes, filter, expected_result):
        result = VolumeManager.filter_states(volumes, filter)

        assert expected_result == result
コード例 #7
0
    def test_volume_filter_states(self, volumes, filter, expected_result):
        result = VolumeManager.filter_states(volumes, filter)

        assert expected_result == result
コード例 #8
0
    def test_volume_get_id_from_key(self, mocker):
        key = '/cobalt/volumes/1'

        assert VolumeManager(mocker.MagicMock()).get_id_from_key(key) == '1'
コード例 #9
0
    def test_volume_get_id_from_key_invalid_input(self, mocker):
        key = 'cobalt/volumes'

        assert VolumeManager(mocker.MagicMock()).get_id_from_key(key) == ''