def add_image(self, image_meta=None, image_data=None): """ Tells Tank about an image's metadata as well as optionally the image_data itself :param image_meta: Optional Mapping of information about the image :param image_data: Optional string of raw image data or file-like object that can be used to read the image data :retval The newly-stored image's metadata. """ headers = utils.image_meta_to_http_headers(image_meta or {}) if image_data: body = image_data headers['content-type'] = 'application/octet-stream' image_size = self._get_image_size(image_data) if image_size: headers['x-image-meta-size'] = image_size headers['content-length'] = image_size else: body = None res = self.do_request("POST", "/images", body, headers) data = json.loads(res.read()) return data['image']
def test_boolean_header_values(self): """ Tests that boolean headers like is_public can be set to True if any of ('True', 'On', 1) is provided, case-insensitive """ fixtures = [{'is_public': True}, {'is_public': 'True'}, {'is_public': 'true'}] expected = {'is_public': True} class FakeResponse(): pass for fixture in fixtures: headers = utils.image_meta_to_http_headers(fixture) response = FakeResponse() response.headers = headers result = utils.get_image_meta_from_headers(response) for k, v in expected.items(): self.assertEqual(v, result[k]) # Ensure False for other values... fixtures = [{'is_public': False}, {'is_public': 'Off'}, {'is_public': 'on'}, {'is_public': '1'}, {'is_public': 'False'}] expected = {'is_public': False} for fixture in fixtures: headers = utils.image_meta_to_http_headers(fixture) response = FakeResponse() response.headers = headers result = utils.get_image_meta_from_headers(response) for k, v in expected.items(): self.assertEqual(v, result[k])
def _inject_image_meta_headers(self, response, image_meta): """ Given a response and mapping of image metadata, injects the Response with a set of HTTP headers for the image metadata. Each main image metadata field is injected as a HTTP header with key 'x-image-meta-<FIELD>' except for the properties field, which is further broken out into a set of 'x-image-meta-property-<KEY>' headers :param response: The Webob Response object :param image_meta: Mapping of image metadata """ headers = utils.image_meta_to_http_headers(image_meta) for k, v in headers.items(): response.headers[k] = v
def test_headers_are_unicode(self): """ Verifies that the headers returned by conversion code are unicode. Headers are passed via http in non-testing mode, which automatically converts them to unicode. Verifying that the method does the conversion proves that we aren't passing data that works in tests but will fail in production. """ fixture = {'name': 'fake public image', 'is_public': True, 'type': 'kernel', 'size': 19, 'location': "file:///tmp/tank-tests/2", 'properties': {'distro': 'Ubuntu 10.04 LTS'}} headers = utils.image_meta_to_http_headers(fixture) for k, v in headers.iteritems(): self.assert_(isinstance(v, unicode), "%s is not unicode" % v)
def update_image(self, image_id, image_meta=None, image_data=None): """ Updates Tank's information about an image """ if image_meta is None: image_meta = {} headers = utils.image_meta_to_http_headers(image_meta) if image_data: body = image_data headers['content-type'] = 'application/octet-stream' image_size = self._get_image_size(image_data) if image_size: headers['x-image-meta-size'] = image_size headers['content-length'] = image_size else: body = None res = self.do_request("PUT", "/images/%s" % image_id, body, headers) data = json.loads(res.read()) return data['image']
def test_data_passed_properly_through_headers(self): """ Verifies that data is the same after being passed through headers """ fixture = {'name': 'fake public image', 'is_public': True, 'deleted': False, 'type': 'kernel', 'name': None, 'size': 19, 'location': "file:///tmp/tank-tests/2", 'properties': {'distro': 'Ubuntu 10.04 LTS'}} headers = utils.image_meta_to_http_headers(fixture) class FakeResponse(): pass response = FakeResponse() response.headers = headers result = utils.get_image_meta_from_headers(response) for k, v in fixture.iteritems(): self.assertEqual(v, result[k])