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_get(self): headers = [('Accept', 'text/occi')] request = HttpRequest(headers, '') response = self.handler.get(request) self.assertEqual(response.status, 200) self.assertEqual(response.body, '') self.assertEqual(response.headers[0], ('Content-Type', 'text/occi; charset=utf-8')) self.assertEqual(len(response.headers), len(self.backend.registry.all()) + 1) expected_headers = [] expected_headers.append(( 'Category', 'entity; scheme="http://schemas.ogf.org/occi/core#"; class="kind"; title="Entity type"; attributes="occi.core.id{immutable} occi.core.title"' )) expected_headers.append(( 'Category', 'resource; scheme="http://schemas.ogf.org/occi/core#"; class="kind"; title="Resource type"; rel="http://schemas.ogf.org/occi/core#entity"; attributes="occi.core.summary"' )) expected_headers.append(( 'Category', 'link; scheme="http://schemas.ogf.org/occi/core#"; class="kind"; title="Link type"; rel="http://schemas.ogf.org/occi/core#entity"; attributes="occi.core.source{required} occi.core.target{required}"' )) expected_headers.append(( 'Category', 'compute; scheme="http://schemas.ogf.org/occi/infrastructure#"; class="kind"; title="Compute Resource"; rel="http://schemas.ogf.org/occi/core#resource"; attributes="%s"; actions="%s"; location="/api/compute/"' % ('occi.compute.architecture occi.compute.cores occi.compute.hostname occi.compute.speed occi.compute.memory occi.compute.state{immutable}', ' '.join([str(cat) for cat in ComputeKind.actions])))) self._verify_headers(response.headers[1:5], expected_headers)
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 _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_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(self): self.test_post() headers = [('Content-Type', 'text/occi; charset=utf-8')] headers.append( ('Category', 'taggy; scheme="http://example.com/occi/custom#"')) request = HttpRequest(headers, '') response = self.handler.delete(request) self.assertEqual(response.body, 'OK') self.assertEqual(response.status, 200) self.assertEqual(self.backend.registry.lookup_location('my_stuff/'), 'http://example.com/occi/custom#my_stuff') self.assertEqual(self.backend.registry.lookup_location('taggy/'), None)
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 _request(self, verb='get', path='', content_type=None, headers=[], body='', query_args=None): request = HttpRequest(headers, body, content_type=content_type, query_args=query_args) response = getattr(self.handler, verb)(request, path) return response
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(self): location = 'my_stuff/' path = self.BASE_URL + '/' + location headers = [('Content-Type', 'text/occi; charset=utf-8')] headers.append(( 'Category', 'my_stuff; scheme="http://example.com/occi/custom#"; class=mixin; location=%s' % path)) headers.append(( 'Category', 'taggy; scheme="http://example.com/occi/custom#"; class=mixin; location=taggy/' )) request = HttpRequest(headers, '') response = self.handler.post(request) self.assertEqual(response.body, 'OK') self.assertEqual(response.status, 200) self.assertEqual(self.backend.registry.lookup_location(location), 'http://example.com/occi/custom#my_stuff') self.assertEqual(self.backend.registry.lookup_location('taggy/'), 'http://example.com/occi/custom#taggy')
def test_get_filter(self): request_headers = [('Accept', 'text/plain')] request_headers.append(('Content-Type', 'text/occi')) request_headers.append(( 'Category', 'storage; scheme="http://schemas.ogf.org/occi/infrastructure#"; class="kind"' )) request = HttpRequest(request_headers, '') response = self.handler.get(request) self.assertEqual(response.status, 200) self.assertEqual(response.headers[0], ('Content-Type', 'text/plain; charset=utf-8')) expected_body = [] expected_body.append( self._category_header(StorageKind) + '; rel="http://schemas.ogf.org/occi/core#resource"' + '; attributes="%s"; actions="%s"; location="%s"' % ('occi.storage.size{required} occi.storage.state{immutable}', ' '.join([str(cat) for cat in StorageKind.actions]), '/api/storage/')) self._verify_body(response.body, expected_body)
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 _handle_request(self, verb, *args): request = HttpRequest( self.request.headers.iteritems(), self.request.body, content_type=self.request.headers.get('Content-Type'), query_args=self.request.arguments) if self.args: args = self.args response = getattr(self.handler, verb)(request, *args) # Debugging if self.logger.isEnabledFor(logging.DEBUG): request_str = '%s %s HTTP/x.x\n' % (self.request.method, self.request.uri) request_str += '\n'.join([ '%s: %s' % (h, v) for h, v in self.request.headers.iteritems() ]) request_str += '\n' if self.request.body: request_str += '\n' request_str += self.request.body.rstrip() response_str = 'HTTP/x.x %d\n' % response.status response_str += '\n'.join( ['%s: %s' % (h, v) for h, v in response.headers]) response_str += '\n' if response.body: response_str += '\n' response_str += response.body.rstrip() logging.debug("""%s.%s(%s): status=%d' -------- Request --------- %s -------- Response -------- %s --------------------------""" % ( self.handler.__class__.__name__, verb, args, response.status, request_str, response_str, )) # Status code of response self.set_status(response.status) # Response Headers headers = {} for name, value in response.headers: values = headers.get(name) if values: values += ', ' + value else: values = value headers[name] = values for name, value in headers.iteritems(): try: self.set_header(name, value) except ValueError: value = tornado.web._utf8(value) value = re.sub(r"[\x00-\x1f]", " ", value) self._headers[name] = value # Set Server header self.set_header('Server', occi.http.version_string) # Response Body self.write(response.body)
def test_delete_nonexisting(self): request = HttpRequest([], '') response = self.handler.delete(request, 'blah/not/found') self.assertEqual(response.status, 404)
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)