Esempio n. 1
0
    def setUp(self):
        self.registry.set_hostname('http://127.0.0.1')
        self.app = Application([(r"(.*)/", CollectionWrapper)])

        self.registry.set_renderer('text/plain',
                                   TextPlainRendering(self.registry))
        self.registry.set_renderer('text/occi',
                                   TextOcciRendering(self.registry))
        self.registry.set_renderer('text/uri-list',
                                   TextUriListRendering(self.registry))

        self.mixin = Mixin('foo', 'mystuff')

        self.compute = Resource('/compute/1', COMPUTE, [])
        self.compute.attributes = {'foo2': 'bar2'}
        self.network = Resource('/network/1', NETWORK, [IPNETWORK])
        self.network_interface = Link('/network/interface/1', NETWORKINTERFACE,
                                      [IPNETWORKINTERFACE], self.compute,
                                      self.network)

        self.registry.set_backend(COMPUTE, SimpleComputeBackend())
        self.registry.set_backend(NETWORK, KindBackend())
        self.registry.set_backend(self.mixin, MixinBackend())
        self.registry.set_backend(START, SimpleComputeBackend())

        self.registry.add_resource(self.compute.identifier, self.compute)
        self.registry.add_resource(self.network.identifier, self.network)
        self.registry.add_resource(self.network_interface.identifier,
                                   self.network_interface)
Esempio n. 2
0
    def __init__(self, registry=None):

        if registry is None:
            self.registry = NonePersistentRegistry()
        elif isinstance(registry, Registry):
            self.registry = registry
        else:
            raise AttributeError('Registry needs to derive from abstract' \
                                 ' class \'Registry\'')

        self.registry.set_renderer('text/occi',
                                   TextOcciRendering(self.registry))
        self.registry.set_renderer('text/plain',
                                   TextPlainRendering(self.registry))
        self.registry.set_renderer('text/uri-list',
                                   TextUriListRendering(self.registry))
        self.registry.set_renderer('text/html', HTMLRendering(self.registry))

        application = tornado.web.Application([
            (r"/-/", QueryHandler, dict(registry=self.registry)),
            (r"/.well-known/org/ogf/occi/-/", QueryHandler,
             dict(registry=self.registry)),
            (r"(.*)/", CollectionHandler, dict(registry=self.registry)),
            (r"(.*)", ResourceHandler, dict(registry=self.registry)),
        ])

        self.http_server = tornado.httpserver.HTTPServer(application)
Esempio n. 3
0
    def setUp(self):
        self.app = Application([(r"(.*)", ResourceWrapper)])
        self.registry.set_renderer('text/occi',
                                   TextOcciRendering(self.registry))

        self.registry.set_backend(COMPUTE, SimpleComputeBackend())
        self.registry.set_backend(NETWORK, KindBackend())
        self.registry.set_backend(NETWORKINTERFACE, KindBackend())
        self.registry.set_backend(START, SimpleComputeBackend())
Esempio n. 4
0
    def setUp(self):
        self.app = Application([(r"/-/", QueryWrapper)])
        self.registry.set_renderer('text/occi',
                                   TextOcciRendering(self.registry))

        backend = KindBackend()

        self.registry.set_backend(COMPUTE, backend)
        self.registry.set_backend(STORAGE, backend)
        self.registry.set_backend(NETWORK, backend)
Esempio n. 5
0
    def setUp(self):
        self.rendering = TextOcciRendering(self.registry)
        # type system...
        self.kind = Kind('http://example.com#', 'foo', related=[Resource.kind])
        self.invalid_kind = Kind('http://example.com#', 'invalid')
        self.link = Kind('http://example.com#', 'link', related=[Link.kind])
        self.mixin = Mixin('http://example.com#', 'mixin')
        self.action = Action('http://example.com#', 'action')

        self.registry.set_backend(self.kind, KindBackend())
        self.registry.set_backend(self.invalid_kind, KindBackend())
        self.registry.set_backend(self.link, KindBackend())
        self.registry.set_backend(self.mixin, MixinBackend())
        self.registry.set_backend(self.action, ActionBackend())

        # 2 linked entities
        self.entity = Resource('/foo/1', self.kind, [self.mixin])
        trg = Resource('/foo/2', self.kind, [], [])
        self.link1 = Link('/link/1', self.link, [], self.entity, trg)
        self.entity.links = [self.link1]

        self.registry.add_resource('/foo/2', trg)
        self.registry.add_resource('/link/1', self.link1)
        self.registry.add_resource('/foo/1', self.entity)
Esempio n. 6
0
    def setUp(self):
        self.app = Application([(r"(.*)", ResourceWrapper)])
        self.registry.set_renderer('text/plain',
                                   TextPlainRendering(self.registry))
        self.registry.set_renderer('text/occi',
                                   TextOcciRendering(self.registry))

        self.registry.set_backend(COMPUTE, SimpleComputeBackend())
        self.registry.set_backend(NETWORK, KindBackend())
        self.registry.set_backend(NETWORKINTERFACE, KindBackend())
        self.registry.set_backend(START, SimpleComputeBackend())

        self.compute = Resource('/compute/1', COMPUTE, [])
        self.network = Resource('/network/1', NETWORK, [IPNETWORK])

        self.registry.add_resource('/compute/1', self.compute)
        self.registry.add_resource('/network/1', self.network)
Esempio n. 7
0
class TestTextOcciRendering(unittest.TestCase):
    '''
    Simple sanity checks...
    '''

    # disable 'Unused attr' pylint check (not needing body here)
    # disable 'Too many instance attributes' pyling check (It's a test :))
    # pylint: disable=W0612,R0902

    registry = NonePersistentRegistry()

    def setUp(self):
        self.rendering = TextOcciRendering(self.registry)
        # type system...
        self.kind = Kind('http://example.com#', 'foo', related=[Resource.kind])
        self.invalid_kind = Kind('http://example.com#', 'invalid')
        self.link = Kind('http://example.com#', 'link', related=[Link.kind])
        self.mixin = Mixin('http://example.com#', 'mixin')
        self.action = Action('http://example.com#', 'action')

        self.registry.set_backend(self.kind, KindBackend())
        self.registry.set_backend(self.invalid_kind, KindBackend())
        self.registry.set_backend(self.link, KindBackend())
        self.registry.set_backend(self.mixin, MixinBackend())
        self.registry.set_backend(self.action, ActionBackend())

        # 2 linked entities
        self.entity = Resource('/foo/1', self.kind, [self.mixin])
        trg = Resource('/foo/2', self.kind, [], [])
        self.link1 = Link('/link/1', self.link, [], self.entity, trg)
        self.entity.links = [self.link1]

        self.registry.add_resource('/foo/2', trg)
        self.registry.add_resource('/link/1', self.link1)
        self.registry.add_resource('/foo/1', self.entity)

    def tearDown(self):
        for res in self.registry.get_resources():
            self.registry.delete_resource(res.identifier)
        for item in self.registry.get_categories():
            self.registry.delete_mixin(item)

    #==========================================================================
    # Failure
    #==========================================================================

    def test_resource_for_failure(self):
        '''
        Check if the correct exceptions are thrown.
        '''
        # no kind available...
        res = Resource('/foo/1', self.mixin, [], links=[])
        headers, body = self.rendering.from_entity(res)
        self.assertRaises(AttributeError, self.rendering.to_entity, headers,
                          body, None)

        # kind does not relate to link or resource...
        res.kind = self.invalid_kind
        headers, body = self.rendering.from_entity(res)
        self.assertRaises(AttributeError, self.rendering.to_entity, headers,
                          body, None)

    def test_resources_for_failure(self):
        '''
        Tests is a set of resource can be set and retrieved.
        '''
        heads = {'X-OCCI-Location': '/bla/bla/2'}
        self.assertRaises(AttributeError, self.rendering.to_entities, heads,
                          '')

    def test_link_for_failure(self):
        '''
        Test link...
        '''
        # call creation of entity with non existing trg resource.
        trg = Resource('/bar/1', self.kind, [], [])
        link = Link('/bar/1', self.link, [], self.entity, trg)
        headers, body = self.rendering.from_entity(link)
        self.assertRaises(AttributeError, self.rendering.to_entity, headers,
                          body, None)

    #==========================================================================
    # Sanity
    #==========================================================================

    def test_resource_for_sanity(self):
        '''
        Test is a resource can be rendered and retrieved.
        '''
        # basic check
        headers, body = self.rendering.from_entity(self.entity)
        new = self.rendering.to_entity(headers, body, None)
        self.assertEqual(self.entity.kind, new.kind)
        self.assertEqual(len(self.entity.links), len(new.links))

        # verify that provided kind is taken
        kind = Kind('foo', 'bar', related=[Resource.kind])
        headers, body = self.rendering.from_entity(self.entity)
        new = self.rendering.to_entity(headers, body, kind)
        self.assertEqual(new.kind, kind)
        self.assertEqual(len(self.entity.links), len(new.links))

        # verify that actions get added
        self.entity.actions = [self.action]
        headers, body = self.rendering.from_entity(self.entity)
        self.assertTrue('?action' in headers['Link'])

    def test_resources_for_sanity(self):
        '''
        Tests is a set of resource can be set and retrieved for sanity.
        '''
        res = self.registry.get_resources()
        heads, body = self.rendering.from_entities(res, '/')
        entities = self.rendering.to_entities(heads, body)
        self.assertEqual(self.registry.get_resources(), entities)

    def test_link_for_sanity(self):
        '''
        Test is a link can be rendered and retrieved.
        '''
        headers, body = self.rendering.from_entity(self.link1)
        tmp = 'occi.core.target=' + self.link1.target.identifier
        tmp += ', occi.core.source=' + self.link1.source.identifier
        headers['X-OCCI-Attribute'] = tmp
        new = self.rendering.to_entity(headers, body, None)
        self.assertEqual(self.link1.kind, new.kind)
        # do not alter the source entity link list!
        self.assertTrue(len(self.entity.links) == 1)

    def test_qi_categories_for_sanity(self):
        '''
        Tests QI interface rendering...
        '''
        heads = {
            'Category':
            'foo; scheme="http://example.com#";' + ' location="/foo/"'
        }
        mixins = self.rendering.to_mixins(heads, '')
        headers, body = self.rendering.from_categories(mixins)
        self.assertTrue('foo' in headers['Category'])
        self.assertTrue('scheme="http://example.com#"' in headers['Category'])
        self.assertTrue('location="/foo/"' in headers['Category'])

    def test_action_for_sanity(self):
        '''
        Test the to actions function...
        '''
        heads = {
            'Category':
            self.action.term + '; scheme="' + self.action.scheme + '"'
        }
        action = self.rendering.to_action(heads, None)
        self.assertEqual(action, self.action)

    def test_get_filters_for_sanity(self):
        '''
        Test if filters can be retrieved...
        '''
        headers, body = self.rendering.from_categories([self.kind])
        cats, attrs = self.rendering.get_filters(headers, '')
        self.assertTrue(cats == [self.kind])
        self.assertTrue(attrs == {})

        headers['X-OCCI-Attribute'] = 'foo="bar"'
        cats, attrs = self.rendering.get_filters(headers, '')
        self.assertTrue(cats == [self.kind])
        self.assertTrue(attrs['foo'] == 'bar')