Exemplo n.º 1
0
    def test_fetch_all_resources_two_items(self):
        with self.app.test_client() as http_client:
            self.manager.list_resources.return_value = [
                Resource(uuid='14', resource_type=self.server_resource_type, attributes=dict(ironic_driver='yes')),
                Resource(uuid='15', resource_type=self.server_resource_type, attributes=dict(ironic_driver='no'),
                         relations={"port1": Resource("uuid1")})
            ]

            result = http_client.get("{}/resources/".format(API_ROOT),
                                     headers=json_content_type)
            self.assertEqual(200, result.status_code)
            self.assertEqual('application/json', result.content_type)
            self.assertEqual({"resources": [
                {
                    'uuid': '14',
                    'type': 'server',
                    'attributes': {'ironic_driver': 'yes'},
                    'relations': {}
                },
                {
                    'uuid': '15',
                    'type': 'server',
                    'attributes': {'ironic_driver': 'no'},
                    'relations': {"port1": "uuid1"}
                }
            ]}, json.loads(result.data.decode(result.charset)))
Exemplo n.º 2
0
    def test_fetching_a_resource(self):
        with self.app.test_client() as http_client:
            uuid = "cecc2a85-3d6b-461c-a8f3-f4a370f3b10c"

            self.manager.get_resource.return_value = Resource(
                uuid=uuid,
                resource_type=self.server_resource_type,
                attributes=dict(ironic_driver='wow'),
                relations={
                    "port1": Resource("uuid1"),
                    "port2": Resource("uuid2"),
                }
            )

            result = http_client.get("{}/resources/{}".format(API_ROOT, uuid),
                                     headers=json_content_type)
            self.assertEqual(200, result.status_code)
            self.assertEqual('application/json', result.content_type)
            self.assertEqual({
                'uuid': uuid,
                'type': 'server',
                'attributes': {'ironic_driver': 'wow'},
                'relations': {
                    "port1": "uuid1",
                    "port2": "uuid2",
                }}, json.loads(result.data.decode(result.charset)))

            self.manager.get_resource.assert_called_with('%s' % uuid)
Exemplo n.º 3
0
    def test_fetching_all_resources_returns_it_from_the_datastore(self):
        resourceA = Resource(uuid=mock.sentinel.a_uuid)
        resourceB = Resource(uuid=mock.sentinel.another_uuid)
        self.datastore.load_all.return_value = [resourceA, resourceB]

        loaded_resources = self.manager.list_resources()

        self.assertEqual([resourceA, resourceB], loaded_resources)
        self.datastore.load_all.assert_called_with()
Exemplo n.º 4
0
    def test_fetch_all_accept_html(self):
        with self.app.test_client() as http_client:
            self.manager.list_resources.return_value = [
                Resource(uuid='14', attributes=dict(ironic_driver='yes')),
                Resource(uuid='15', attributes=dict(ironic_driver='no'))
            ]

            result = http_client.get("{}/resources".format(API_ROOT),
                                     headers={'Accept': 'text/html'})

            self.assertEqual(200, result.status_code)
            self.assertIn('text/html', result.content_type)
Exemplo n.º 5
0
    def test_update_resource_with_relations_sync_the_relations(self):
        self.manager.synchronize_resource = mock.Mock()
        origin_resource = Resource(uuid='my-uuid',
                                   relations={"port1": Resource("your-uuid")})
        self.datastore.load.return_value = origin_resource

        change = mock.Mock()

        self.manager.update_resource('my-uuid', [change])
        self.datastore.save.assert_called_with(origin_resource)
        self.manager.synchronize_resource.assert_has_calls([
            mock.call('my-uuid'),
            mock.call('your-uuid'),
        ],
                                                           any_order=True)
Exemplo n.º 6
0
 def setUp(self):
     super().setUp()
     self.resource = Resource(
         uuid='1234', attributes={'one': {
             'one-one': {}, 'one-two': {'one-two-tree': ''}}},
         foreign_tracking={}
     )
Exemplo n.º 7
0
    def test_creating_one_resource_with_relations_sync_the_relations(
            self, uuid4_mock):
        uuid4_mock.return_value = 'new-uuid'
        origin_resource = Resource(uuid=None,
                                   relations={"port 1": Resource("uuid1")})

        self.manager.synchronize_resource = mock.Mock()

        created_resource = self.manager.create_resource(origin_resource)

        self.datastore.save.assert_called_with(created_resource)
        self.manager.synchronize_resource.assert_has_calls([
            mock.call('new-uuid'),
            mock.call('uuid1'),
        ],
                                                           any_order=True)
Exemplo n.º 8
0
    def test_fetching_one_resource_returns_it_from_the_datastore(self):
        resource = Resource(uuid=mock.sentinel.a_uuid)
        self.datastore.load.return_value = resource

        loaded_resource = self.manager.get_resource(mock.sentinel.a_uuid)

        self.assertEqual(resource, loaded_resource)
        self.datastore.load.assert_called_with(mock.sentinel.a_uuid)
Exemplo n.º 9
0
    def test_synchronizing_one_resource_saves_changes_by_the_synchronizer(
            self):
        origin_resource = Resource(uuid='my-uuid')
        self.datastore.load.return_value = origin_resource
        self.resource_synchronizer1.synchronize.side_effect = \
            lambda r: r.foreign_tracking.update({'ironic': 'ironic-uuid'})
        self.resource_synchronizer2.synchronize.side_effect = \
            lambda r: r.attributes.update({'shizzle': 'whizzle'})

        self.manager.synchronize_resource('my-uuid')

        self.resource_synchronizer1.synchronize.assert_called_with(
            origin_resource)
        self.resource_synchronizer2.synchronize.assert_called_with(
            origin_resource)
        self.datastore.save.assert_called_with(
            Resource(uuid='my-uuid',
                     attributes={'shizzle': 'whizzle'},
                     foreign_tracking={'ironic': 'ironic-uuid'}))
Exemplo n.º 10
0
    def test_creating_one_resource_returns_it_with_a_uuid_and_saves_it(
            self, uuid4_mock):
        uuid4_mock.return_value = 'new-uuid'
        self.manager.synchronize_resource = mock.Mock()

        created_resource = self.manager.create_resource(Resource())

        self.assertEqual('new-uuid', created_resource.uuid)
        self.datastore.save.assert_called_with(created_resource)
        self.manager.synchronize_resource.assert_called_with('new-uuid')
Exemplo n.º 11
0
    def test_removing_an_existent_dictionary(self):
        operation = Remove(['attributes', 'one', 'one-two'])

        operation.apply(self.resource)

        self.assertEqual(Resource(
            uuid='1234', attributes={'one': {
                'one-one': {}}},
            foreign_tracking={}
        ), self.resource)
Exemplo n.º 12
0
    def test_adding_to_new_dictionary(self):
        operation = Create(['foreign_tracking', 'one', 'one-tree', 'one-tree-one', 'new-field'], 'my-value')

        operation.apply(self.resource)

        self.assertEqual(Resource(
            uuid='1234', attributes={'one': {
                'one-one': {}, 'one-two': {'one-two-tree': ''}}},
            foreign_tracking={'one': {'one-tree': {
                'one-tree-one': {'new-field': 'my-value'}}}}
        ), self.resource)
Exemplo n.º 13
0
    def test_replacing_to_existent_dictionary(self):
        operation = Replace(['attributes', 'one', 'one-one', 'new-field'], 'my-value')

        operation.apply(self.resource)

        self.assertEqual(Resource(
            uuid='1234', attributes={'one': {
                'one-one': {'new-field': 'my-value'},
                'one-two': {'one-two-tree': ''}}},
            foreign_tracking={}
        ), self.resource)
Exemplo n.º 14
0
    def test_update_resource(self):
        origin_resource = Resource(uuid='my-uuid')
        self.datastore.load.return_value = origin_resource

        change = mock.Mock()

        self.manager.update_resource('my-uuid', [change])
        self.datastore.save.assert_called_with(origin_resource)
        self.resource_synchronizer1.synchronize.assert_called_with(
            origin_resource)
        self.resource_synchronizer2.synchronize.assert_called_with(
            origin_resource)
Exemplo n.º 15
0
    def create_resource(self):
        request_data = request.json

        resource = self.manager.create_resource(
            Resource(resource_type=self.resource_type_factory.get(
                request_data['type']),
                     attributes=request_data['attributes'],
                     relations=self._to_relations(
                         request_data.get('relations', {}))))
        response = make_response(json.dumps(resource_to_api(resource)), 201)
        response.headers['Content-type'] = 'application/json'
        response.headers['Location'] = '/v1/resources/{}'.format(resource.uuid)

        return response
Exemplo n.º 16
0
    def test_creating_a_resource_returns_a_location(self):
        with self.app.test_client() as http_client:
            resource = Resource(uuid='some-uuid',
                                resource_type=self.server_resource_type,
                                attributes={'ironic_driver': 'hello'},
                                relations={
                                    "port1": Resource("uuid1"),
                                    "port2": Resource("uuid2"),
                                })
            self.manager.create_resource.return_value = resource

            self.manager.get_resource.side_effect = lambda uuid: Resource(uuid)

            result = http_client.post("{}/resources".format(API_ROOT),
                                      headers=json_content_type,
                                      data=json.dumps({
                                          'type': 'server',
                                          'attributes': {'ironic_driver': 'hello'},
                                          'relations': {
                                              "port1": "uuid1",
                                              "port2": "uuid2",
                                          }
                                      }))
            self.assertEqual(201, result.status_code)
            self.assertEqual('application/json', result.headers['Content-Type'])

            self.assertDictEqual(
                json.loads(result.data.decode(result.charset)),
                {
                    "uuid": "some-uuid",
                    "type": "server",
                    "attributes": {"ironic_driver": "hello"},
                    "relations": {
                        "port1": "uuid1",
                        "port2": "uuid2",
                    }
                }
            )
            self.assertIn('Location', result.headers)
            self.assertIn('{}/resources/some-uuid'.format(API_ROOT),
                          result.headers['Location'])

            self.manager.create_resource.assert_called_with(
                Resource(uuid=None,
                         resource_type=self.server_resource_type,
                         attributes={'ironic_driver': 'hello'},
                         relations={
                             "port1": Resource("uuid1"),
                             "port2": Resource("uuid2"),
                         }))
Exemplo n.º 17
0
    def test_update_resource_fails_if_the_patching_returns_an_error(self):
        origin_resource = Resource(uuid='my-uuid')
        self.datastore.load.return_value = origin_resource

        change = mock.Mock()

        change.apply.side_effect = AttributeError
        self.assertRaises(InvalidUpdate, self.manager.update_resource,
                          'my-uuid', [change])

        change.apply.side_effect = TypeError
        self.assertRaises(InvalidUpdate, self.manager.update_resource,
                          'my-uuid', [change])

        change.apply.side_effect = KeyError
        self.assertRaises(InvalidUpdate, self.manager.update_resource,
                          'my-uuid', [change])

        self.assertFalse(self.datastore.save.called)
Exemplo n.º 18
0
    def test_updating_a_resource_returns_the_updated_resource(self):
        with self.app.test_client() as http_client:
            resource = Resource(uuid='some-uuid',
                                attributes={'ironic_driver': 'changed'})

            self.manager.update_resource.return_value = resource

            result = http_client.patch(
                "{}/resources/some-uuid".format(API_ROOT),
                headers=json_content_type,
                data=json.dumps(
                    [{"value": "changed",
                      "path": "/attributes/ironic_driver",
                      "op": "replace"}
                     ]
                )
            )

            self.assertEqual(200, result.status_code)
            self.assertEqual('application/json', result.headers['Content-Type'])
            self.assertDictEqual(
                json.loads(result.data.decode(result.charset)),
                {
                    "uuid": "some-uuid",
                    "type": "",
                    "attributes": {"ironic_driver": "changed"},
                    "relations": {}
                }
            )
            self.assertIn('Location', result.headers)
            self.assertIn('{}/resources/some-uuid'.format(API_ROOT),
                          result.headers['Location'])

            self.manager.update_resource.assert_called_with(
                'some-uuid',
                changes=[
                    Replace(["attributes", "ironic_driver"], "changed")
                ]
            )