'Ensure we can copy from an external image in HTTP.'

@skip_if_disabled
def test_copy_from_http_exists(self):

    self.cleanup()
    self.start_servers(**self.__dict__.copy())
    setup_http(self)
    copy_from = get_http_uri(self, 'foobar')
    headers = {'X-Image-Meta-Name': 'copied', 'X-Image-Meta-disk_format': 'raw', 'X-Image-Meta-container_format': 'ovf', 'X-Image-Meta-Is-Public': 'True', 'X-Glance-API-Copy-From': copy_from}
    path = ('http://%s:%d/v1/images' % ('127.0.0.1', self.api_port))
    http = httplib2.Http()
    (response, content) = http.request(path, 'POST', headers=headers)
    self.assertEqual(http_client.CREATED, response.status, content)
    data = jsonutils.loads(content)
    copy_image_id = data['image']['id']
    self.assertEqual('queued', data['image']['status'], content)
    path = ('http://%s:%d/v1/images/%s' % ('127.0.0.1', self.api_port, copy_image_id))
    def _await_status(expected_status):
        for i in range(100):
            time.sleep(0.01)
            http = httplib2.Http()
            (response, content) = http.request(path, 'HEAD')
            self.assertEqual(http_client.OK, response.status)
            if (response['x-image-meta-status'] == expected_status):
                return
        self.fail(('unexpected    image    status    %s' % response['x-image-meta-status']))
    _await_status('active')
    http = httplib2.Http()
    (response, content) = http.request(path, 'GET')
    self.assertEqual(http_client.OK, response.status)
    self.assertEqual(str(FIVE_KB), response['content-length'])
    self.assertEqual(('*' * FIVE_KB), content)
    self.assertEqual(hashlib.md5(('*' * FIVE_KB)).hexdigest(), hashlib.md5(content).hexdigest())
    http = httplib2.Http()
    (response, content) = http.request(path, 'DELETE')
    self.assertEqual(http_client.OK, response.status)
    self.stop_servers()
