'Verify that partial download requests for a fully cached image
succeeds; we do not serve it from cache.'

@skip_if_disabled
def test_partial_download_of_cached_images_v2_api(self):

    self.cleanup()
    self.start_servers(**self.__dict__.copy())
    path = ('http://%s:%d/v2/images' % ('0.0.0.0', self.api_port))
    http = httplib2.Http()
    headers = {'content-type': 'application/json'}
    image_entity = {'name': 'Image1', 'visibility': 'public', 'container_format': 'bare', 'disk_format': 'raw'}
    (response, content) = http.request(path, 'POST', headers=headers, body=jsonutils.dumps(image_entity))
    self.assertEqual(http_client.CREATED, response.status)
    data = jsonutils.loads(content)
    image_id = data['id']
    path = ('http://%s:%d/v2/images/%s/file' % ('0.0.0.0', self.api_port, image_id))
    headers = {'content-type': 'application/octet-stream'}
    image_data = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    (response, content) = http.request(path, 'PUT', headers=headers, body=image_data)
    self.assertEqual(http_client.NO_CONTENT, response.status)
    image_cached_path = os.path.join(self.api_server.image_cache_dir, image_id)
    self.assertFalse(os.path.exists(image_cached_path))
    http = httplib2.Http()
    (response, content) = http.request(path, 'GET')
    self.assertEqual(http_client.OK, response.status)
    self.assertEqual('ABCDEFGHIJKLMNOPQRSTUVWXYZ', content)
    image_cached_path = os.path.join(self.api_server.image_cache_dir, image_id)
    self.assertTrue(os.path.exists(image_cached_path))
    with open(image_cached_path, 'w') as cache_file:
        cache_file.write('0123456789')
    range_ = 'bytes=3-5'
    headers = {'X-Identity-Status': 'Confirmed', 'X-Auth-Token': '932c5c84-02ac-4fe5-a9ba-620af0e2bb96', 'X-User-Id': 'f9a41d13-0c13-47e9-bee2-ce4e8bfe958e', 'X-Tenant-Id': str(uuid.uuid4()), 'X-Roles': 'member', 'Range': range_, 'content-type': 'application/json'}
    (response, content) = http.request(path, 'GET', headers=headers)
    self.assertEqual(http_client.PARTIAL_CONTENT, response.status)
    self.assertEqual('DEF', content)
    self.assertNotEqual('345', content)
    self.assertNotEqual(image_data, content)
    content_range = 'bytes    3-5/*'
    headers = {'X-Identity-Status': 'Confirmed', 'X-Auth-Token': '932c5c84-02ac-4fe5-a9ba-620af0e2bb96', 'X-User-Id': 'f9a41d13-0c13-47e9-bee2-ce4e8bfe958e', 'X-Tenant-Id': str(uuid.uuid4()), 'X-Roles': 'member', 'Content-Range': content_range, 'content-type': 'application/json'}
    (response, content) = http.request(path, 'GET', headers=headers)
    self.assertEqual(http_client.PARTIAL_CONTENT, response.status)
    self.assertEqual('DEF', content)
    self.assertNotEqual('345', content)
    self.assertNotEqual(image_data, content)
    self.stop_servers()
