示例#1
0
    def __init__(self, *args, **kwargs):
        super(TornadoHttpServer, self).__init__(*args, **kwargs)

        self.application = tornado.web.Application([
            (self.base_path + r'/*/-/', TornadoRequestHandler,
             dict(handler=DiscoveryHandler(self.backend,
                                           translator=self.translator))),
            (self.base_path + r'/.well-known/org/ogf/occi/-/',
             TornadoRequestHandler,
             dict(handler=DiscoveryHandler(self.backend,
                                           translator=self.translator))),
            (self.base_path + r'/', TornadoRequestHandler,
             dict(handler=CollectionHandler(self.backend,
                                            translator=self.translator),
                  args=[''])),
            (self.base_path + r'/(.+/)', TornadoRequestHandler,
             dict(handler=CollectionHandler(self.backend,
                                            translator=self.translator))),
            (self.base_path + r'/(.+[^/])', TornadoRequestHandler,
             dict(handler=EntityHandler(self.backend,
                                        translator=self.translator))),
        ])
示例#2
0
 def setUp(self):
     super(EntityHandlerTestCase, self).setUp()
     self.handler = EntityHandler(self.backend, translator=self.translator)
示例#3
0
 def setUp(self):
     super(EntityHandlerTestCase, self).setUp()
     self.handler = EntityHandler(self.backend, translator=self.translator)
示例#4
0
class EntityHandlerTestCase(HandlerTestCaseBase):
    def setUp(self):
        super(EntityHandlerTestCase, self).setUp()
        self.handler = EntityHandler(self.backend, translator=self.translator)

    def _get(self, entity_id=None, accept_header=None):
        entity_id = entity_id or self.computes[0].id
        request_headers = []
        if accept_header:
            request_headers.append(('accept', accept_header))
        request = HttpRequest(request_headers, '')
        response = self.handler.get(request, str(entity_id))
        return response

    def test_get(self):
        response = self._get()
        self.assertEqual(response.status, 200)
        self.assertEqual(response.headers[0], ('Content-Type', 'text/plain; charset=utf-8'))

    def test_get__text_occi(self):
        response = self._get(accept_header='text/*, text/occi')
        self.assertEqual(response.body, '')
        self.assertEqual(response.headers[0], ('Content-Type', 'text/occi; charset=utf-8'))
        self.assertEqual(len(response.headers), 9)

    def test_get__text_plain(self):
        response = self._get(accept_header='text/occi;q=0.5, text/plain;q=0.8')
        self.assertEqual(response.headers, [('Content-Type', 'text/plain; charset=utf-8')])
        self.assertNotEqual(response.body, '')

    def test_get__text_urilist(self):
        response = self._get(accept_header='text/plain;q=0.9, text/uri-list')
        self.assertEqual(response.headers, [('Content-Type', 'text/uri-list; charset=utf-8')])
        self.assertEqual(response.body[:44+len(self.BASE_URL)+1], self._loc(self.computes[0]))

    def test_get__text_any(self):
        response = self._get(accept_header='text/*, */*;q=0.1')
        self.assertEqual(response.headers, [('Content-Type', 'text/plain; charset=utf-8')])
        expected_body = []
        expected_body.append(self._category_header(ComputeKind))
        expected_body.append('Link: <%s>; rel="http://schemas.ogf.org/occi/infrastructure#network http://schemas.ogf.org/occi/infrastructure/network#ipnetwork"; title="Internet"; self="%s"; category="%s"; occi.core.title="Primary Interface"; occi.networkinterface.interface="eth0"; occi.networkinterface.mac="00:11:22:33:44:55"; occi.networkinterface.state="active"; occi.networkinterface.ip="11.12.13.14"; occi.networkinterface.allocation="static"' % (
            self._loc(self.networks[0]), self._loc(self.links[0]), "%s %s" % (str(NetworkInterfaceKind), str(IPNetworkInterfaceMixin))))
        expected_body.append('Link: <%s>; rel="http://schemas.ogf.org/occi/infrastructure#storage"; title=""; self="%s"; category="%s"; occi.core.title="Boot drive"; occi.storagelink.deviceid="ide:0:0"; occi.storagelink.state="active"' % (
            self._loc(self.storages[0]), self._loc(self.links[1]), str(StorageLinkKind)))
        expected_body.append('Link: <%s?action=start>; rel="http://schemas.ogf.org/occi/infrastructure/compute/action#start"; title="Start Compute Resource"' % self._loc(self.computes[0]))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' % self.computes[0].id)
        expected_body.append('X-OCCI-Attribute: occi.core.title="A \\"little\\" VM"')
        expected_body.append('X-OCCI-Attribute: occi.compute.memory=1.67')
        expected_body.append('X-OCCI-Attribute: occi.compute.state="inactive"')
        self._verify_body(response.body, expected_body)

    def test_get_link(self):
        response = self._get(entity_id=self.links[1].id,
                accept_header='text/plain')
        self.assertEqual(response.headers, [('Content-Type', 'text/plain; charset=utf-8')])
        expected_body = []
        expected_body.append(self._category_header(StorageLinkKind))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' % self.links[1].id)
        expected_body.append('X-OCCI-Attribute: occi.core.title="Boot drive"')
        expected_body.append('X-OCCI-Attribute: occi.core.source="%s"' % self._loc(self.computes[0]))
        expected_body.append('X-OCCI-Attribute: occi.core.target="%s"' % self._loc(self.storages[0]))
        expected_body.append('X-OCCI-Attribute: occi.storagelink.deviceid="ide:0:0"')
        expected_body.append('X-OCCI-Attribute: occi.storagelink.state="active"')
        self._verify_body(response.body, expected_body)

    def test_post_action(self):
        request_headers = []
        request_headers.append(('Category', 'stop; scheme="http://schemas.ogf.org/occi/infrastructure/compute/action#'))
        request_headers.append(('x-occi-attribute', 'method="acpioff"'))
        request = HttpRequest(request_headers, '', query_args={'action': ['stop']})
        response = self.handler.post(request, 'compute/' + str(self.computes[1].id))
        self.assertEqual(response.body, 'OK')
        self.assertEqual(response.status, 200)

    def test_post_action_not_found(self):
        request = HttpRequest([], '', query_args={'action': 'stop'})
        response = self.handler.post(request, 'blah/not/found')
        self.assertEqual(response.status, 404)

    def test_post_action_not_applicable(self):
        request_headers = []
        request_headers.append(('Category', 'start; scheme="http://schemas.ogf.org/occi/infrastructure/compute/action#'))
        request = HttpRequest(request_headers, '', query_args={'action': ['start']})
        response = self.handler.post(request, str(self.computes[1].id))
        self.assertEqual(response.status, 400)
        self.assertEqual(response.body, 'start: action not applicable')

    def test_post_update(self):
        entity = self.computes[1]
        request_headers = [('accept', 'text/*;q=0.8, text/uri-list')]
        request_body = ''
        request_body += 'x-occi-attribute: occi.compute.cores=3\n'
        request_body += 'x-occi-attribute: occi.compute.speed=3.26, occi.compute.memory=2.0\n'
        request = HttpRequest(request_headers, request_body, content_type='text/plain')
        response = self.handler.post(request, str(entity.id))
        self.assertEqual(response.body, self._loc(entity) + '\r\n')
        self.assertEqual(response.status, 200)
        expected_headers = []
        expected_headers.append(('Content-Type', 'text/uri-list; charset=utf-8'))
        expected_headers.append(('Location', self._loc(entity)))
        self._verify_headers(response.headers, expected_headers)

        get_response = self._get(entity_id=entity.id, accept_header='text/plain')
        expected_body = []
        expected_body.append(self._category_header(ComputeKind))
        expected_body.append('Link: <%s?action=stop>; rel="http://schemas.ogf.org/occi/infrastructure/compute/action#stop"; title="Stop Compute Resource"' % self._loc(self.computes[1]))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' % self.computes[1].id)
        expected_body.append('X-OCCI-Attribute: occi.core.title="Another \\" VM"')
        expected_body.append('X-OCCI-Attribute: occi.compute.cores=3')
        expected_body.append('X-OCCI-Attribute: occi.compute.speed=3.26')
        expected_body.append('X-OCCI-Attribute: occi.compute.memory=2.00')
        expected_body.append('X-OCCI-Attribute: occi.compute.state="active"')
        self._verify_body(get_response.body, expected_body)

    def test_post_update_nonexisting(self):
        request = HttpRequest([], '')
        response = self.handler.post(request, 'blah/not/found')
        self.assertEqual(response.status, 404)

    def test_put_new(self):
        entity_id = uuid.uuid4()
        path = 'network/%s' % entity_id
        request_headers = [('accept', 'text/plain')]
        request_headers.append(('Category', 'network; scheme=http://schemas.ogf.org/occi/infrastructure#'))
        request_headers.append(('Category', 'ipnetwork; scheme=http://schemas.ogf.org/occi/infrastructure/network#'))
        request_headers.append(('x-occi-attribute', 'occi.core.title="My VLAN"'))
        request_headers.append(('x-occi-attribute', 'occi.network.vlan=91'))
        request_headers.append(('x-occi-attribute', 'occi.network.address=192.168.1.100'))
        request_headers.append(('x-occi-attribute', 'occi.network.gateway=192.168.1.1'))
        request = HttpRequest(request_headers, '', content_type='text/occi')
        response = self.handler.put(request, self.BASE_URL + '/' + path)

        # Assume success
        self.assertEqual(response.body, 'OK')
        self.assertEqual(response.status, 200)

        return entity_id

    def test_put_existing(self):
        entity_id = self.test_put_new()

        request_headers = [('accept', 'text/plain')]
        request_headers.append(('Category', 'network; scheme=http://schemas.ogf.org/occi/infrastructure#'))
        request_headers.append(('x-occi-attribute', 'occi.network.vlan=123'))
        request = HttpRequest(request_headers, '', content_type='text/occi')
        response = self.handler.put(request, str(entity_id))

        # Assume success
        self.assertEqual(response.body, 'OK')
        self.assertEqual(response.status, 200)

        get_response = self._get(entity_id=entity_id, accept_header='text/plain')
        expected_body = []
        expected_body.append(self._category_header(NetworkKind))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' % entity_id)
        expected_body.append('X-OCCI-Attribute: occi.network.vlan=123')
        self._verify_body(get_response.body, expected_body)

    def test_delete(self):
        entity_id = 'compute/%s' % self.computes[0].id
        request = HttpRequest([], '')
        response = self.handler.delete(request, entity_id)
        self.assertEqual(response.status, 200)
        self.assertEqual(response.headers, [('Content-Type', 'text/plain; charset=utf-8')])
        self.assertEqual(response.body, 'OK')

        get_response = self._get(entity_id=entity_id)
        self.assertEqual(get_response.status, 404)

    def test_delete_nonexisting(self):
        request = HttpRequest([], '')
        response = self.handler.delete(request, 'blah/not/found')
        self.assertEqual(response.status, 404)
示例#5
0
class EntityHandlerTestCase(HandlerTestCaseBase):
    def setUp(self):
        super(EntityHandlerTestCase, self).setUp()
        self.handler = EntityHandler(self.backend, translator=self.translator)

    def _get(self, entity_id=None, accept_header=None):
        entity_id = entity_id or self.computes[0].id
        request_headers = []
        if accept_header:
            request_headers.append(('accept', accept_header))
        request = HttpRequest(request_headers, '')
        response = self.handler.get(request, str(entity_id))
        return response

    def test_get(self):
        response = self._get()
        self.assertEqual(response.status, 200)
        self.assertEqual(response.headers[0],
                         ('Content-Type', 'text/plain; charset=utf-8'))

    def test_get__text_occi(self):
        response = self._get(accept_header='text/*, text/occi')
        self.assertEqual(response.body, '')
        self.assertEqual(response.headers[0],
                         ('Content-Type', 'text/occi; charset=utf-8'))
        self.assertEqual(len(response.headers), 9)

    def test_get__text_plain(self):
        response = self._get(accept_header='text/occi;q=0.5, text/plain;q=0.8')
        self.assertEqual(response.headers,
                         [('Content-Type', 'text/plain; charset=utf-8')])
        self.assertNotEqual(response.body, '')

    def test_get__text_urilist(self):
        response = self._get(accept_header='text/plain;q=0.9, text/uri-list')
        self.assertEqual(response.headers,
                         [('Content-Type', 'text/uri-list; charset=utf-8')])
        self.assertEqual(response.body[:44 + len(self.BASE_URL) + 1],
                         self._loc(self.computes[0]))

    def test_get__text_any(self):
        response = self._get(accept_header='text/*, */*;q=0.1')
        self.assertEqual(response.headers,
                         [('Content-Type', 'text/plain; charset=utf-8')])
        expected_body = []
        expected_body.append(self._category_header(ComputeKind))
        expected_body.append(
            'Link: <%s>; rel="http://schemas.ogf.org/occi/infrastructure#network http://schemas.ogf.org/occi/infrastructure/network#ipnetwork"; title="Internet"; self="%s"; category="%s"; occi.core.title="Primary Interface"; occi.networkinterface.interface="eth0"; occi.networkinterface.mac="00:11:22:33:44:55"; occi.networkinterface.state="active"; occi.networkinterface.ip="11.12.13.14"; occi.networkinterface.allocation="static"'
            % (self._loc(self.networks[0]), self._loc(self.links[0]), "%s %s" %
               (str(NetworkInterfaceKind), str(IPNetworkInterfaceMixin))))
        expected_body.append(
            'Link: <%s>; rel="http://schemas.ogf.org/occi/infrastructure#storage"; title=""; self="%s"; category="%s"; occi.core.title="Boot drive"; occi.storagelink.deviceid="ide:0:0"; occi.storagelink.state="active"'
            % (self._loc(self.storages[0]), self._loc(
                self.links[1]), str(StorageLinkKind)))
        expected_body.append(
            'Link: <%s?action=start>; rel="http://schemas.ogf.org/occi/infrastructure/compute/action#start"; title="Start Compute Resource"'
            % self._loc(self.computes[0]))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' %
                             self.computes[0].id)
        expected_body.append(
            'X-OCCI-Attribute: occi.core.title="A \\"little\\" VM"')
        expected_body.append('X-OCCI-Attribute: occi.compute.memory=1.67')
        expected_body.append('X-OCCI-Attribute: occi.compute.state="inactive"')
        self._verify_body(response.body, expected_body)

    def test_get_link(self):
        response = self._get(entity_id=self.links[1].id,
                             accept_header='text/plain')
        self.assertEqual(response.headers,
                         [('Content-Type', 'text/plain; charset=utf-8')])
        expected_body = []
        expected_body.append(self._category_header(StorageLinkKind))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' %
                             self.links[1].id)
        expected_body.append('X-OCCI-Attribute: occi.core.title="Boot drive"')
        expected_body.append('X-OCCI-Attribute: occi.core.source="%s"' %
                             self._loc(self.computes[0]))
        expected_body.append('X-OCCI-Attribute: occi.core.target="%s"' %
                             self._loc(self.storages[0]))
        expected_body.append(
            'X-OCCI-Attribute: occi.storagelink.deviceid="ide:0:0"')
        expected_body.append(
            'X-OCCI-Attribute: occi.storagelink.state="active"')
        self._verify_body(response.body, expected_body)

    def test_post_action(self):
        request_headers = []
        request_headers.append((
            'Category',
            'stop; scheme="http://schemas.ogf.org/occi/infrastructure/compute/action#'
        ))
        request_headers.append(('x-occi-attribute', 'method="acpioff"'))
        request = HttpRequest(request_headers,
                              '',
                              query_args={'action': ['stop']})
        response = self.handler.post(request,
                                     'compute/' + str(self.computes[1].id))
        self.assertEqual(response.body, 'OK')
        self.assertEqual(response.status, 200)

    def test_post_action_not_found(self):
        request = HttpRequest([], '', query_args={'action': 'stop'})
        response = self.handler.post(request, 'blah/not/found')
        self.assertEqual(response.status, 404)

    def test_post_action_not_applicable(self):
        request_headers = []
        request_headers.append((
            'Category',
            'start; scheme="http://schemas.ogf.org/occi/infrastructure/compute/action#'
        ))
        request = HttpRequest(request_headers,
                              '',
                              query_args={'action': ['start']})
        response = self.handler.post(request, str(self.computes[1].id))
        self.assertEqual(response.status, 400)
        self.assertEqual(response.body, 'start: action not applicable')

    def test_post_update(self):
        entity = self.computes[1]
        request_headers = [('accept', 'text/*;q=0.8, text/uri-list')]
        request_body = ''
        request_body += 'x-occi-attribute: occi.compute.cores=3\n'
        request_body += 'x-occi-attribute: occi.compute.speed=3.26, occi.compute.memory=2.0\n'
        request = HttpRequest(request_headers,
                              request_body,
                              content_type='text/plain')
        response = self.handler.post(request, str(entity.id))
        self.assertEqual(response.body, self._loc(entity) + '\r\n')
        self.assertEqual(response.status, 200)
        expected_headers = []
        expected_headers.append(
            ('Content-Type', 'text/uri-list; charset=utf-8'))
        expected_headers.append(('Location', self._loc(entity)))
        self._verify_headers(response.headers, expected_headers)

        get_response = self._get(entity_id=entity.id,
                                 accept_header='text/plain')
        expected_body = []
        expected_body.append(self._category_header(ComputeKind))
        expected_body.append(
            'Link: <%s?action=stop>; rel="http://schemas.ogf.org/occi/infrastructure/compute/action#stop"; title="Stop Compute Resource"'
            % self._loc(self.computes[1]))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' %
                             self.computes[1].id)
        expected_body.append(
            'X-OCCI-Attribute: occi.core.title="Another \\" VM"')
        expected_body.append('X-OCCI-Attribute: occi.compute.cores=3')
        expected_body.append('X-OCCI-Attribute: occi.compute.speed=3.26')
        expected_body.append('X-OCCI-Attribute: occi.compute.memory=2.00')
        expected_body.append('X-OCCI-Attribute: occi.compute.state="active"')
        self._verify_body(get_response.body, expected_body)

    def test_post_update_nonexisting(self):
        request = HttpRequest([], '')
        response = self.handler.post(request, 'blah/not/found')
        self.assertEqual(response.status, 404)

    def test_put_new(self):
        entity_id = uuid.uuid4()
        path = 'network/%s' % entity_id
        request_headers = [('accept', 'text/plain')]
        request_headers.append(
            ('Category',
             'network; scheme=http://schemas.ogf.org/occi/infrastructure#'))
        request_headers.append((
            'Category',
            'ipnetwork; scheme=http://schemas.ogf.org/occi/infrastructure/network#'
        ))
        request_headers.append(
            ('x-occi-attribute', 'occi.core.title="My VLAN"'))
        request_headers.append(('x-occi-attribute', 'occi.network.vlan=91'))
        request_headers.append(
            ('x-occi-attribute', 'occi.network.address=192.168.1.100'))
        request_headers.append(
            ('x-occi-attribute', 'occi.network.gateway=192.168.1.1'))
        request = HttpRequest(request_headers, '', content_type='text/occi')
        response = self.handler.put(request, self.BASE_URL + '/' + path)

        # Assume success
        self.assertEqual(response.body, 'OK')
        self.assertEqual(response.status, 200)

        return entity_id

    def test_put_existing(self):
        entity_id = self.test_put_new()

        request_headers = [('accept', 'text/plain')]
        request_headers.append(
            ('Category',
             'network; scheme=http://schemas.ogf.org/occi/infrastructure#'))
        request_headers.append(('x-occi-attribute', 'occi.network.vlan=123'))
        request = HttpRequest(request_headers, '', content_type='text/occi')
        response = self.handler.put(request, str(entity_id))

        # Assume success
        self.assertEqual(response.body, 'OK')
        self.assertEqual(response.status, 200)

        get_response = self._get(entity_id=entity_id,
                                 accept_header='text/plain')
        expected_body = []
        expected_body.append(self._category_header(NetworkKind))
        expected_body.append('X-OCCI-Attribute: occi.core.id="%s"' % entity_id)
        expected_body.append('X-OCCI-Attribute: occi.network.vlan=123')
        self._verify_body(get_response.body, expected_body)

    def test_delete(self):
        entity_id = 'compute/%s' % self.computes[0].id
        request = HttpRequest([], '')
        response = self.handler.delete(request, entity_id)
        self.assertEqual(response.status, 200)
        self.assertEqual(response.headers,
                         [('Content-Type', 'text/plain; charset=utf-8')])
        self.assertEqual(response.body, 'OK')

        get_response = self._get(entity_id=entity_id)
        self.assertEqual(get_response.status, 404)

    def test_delete_nonexisting(self):
        request = HttpRequest([], '')
        response = self.handler.delete(request, 'blah/not/found')
        self.assertEqual(response.status, 404)