Exemple #1
0
    def test_http_get_redirect(self):
        # Add two layers of redirects to the response stack, which will
        # return the default 200 OK with the expected data after resolving
        # both redirects.
        self._mock_httplib()
        redirect1 = {"location": "http://example.com/teapot.img"}
        redirect2 = {"location": "http://example.com/teapot_real.img"}
        responses = [
            utils.FakeHTTPResponse(status=302, headers=redirect1),
            utils.FakeHTTPResponse(status=301, headers=redirect2),
            utils.FakeHTTPResponse()
        ]

        def getresponse():
            return responses.pop()

        self.response.side_effect = getresponse

        uri = "http://netloc/path/to/file.tar.gz"
        expected_returns = [
            'I ', 'am', ' a', ' t', 'ea', 'po', 't,', ' s', 'ho', 'rt', ' a',
            'nd', ' s', 'to', 'ut', '\n'
        ]

        loc = location.get_location_from_uri(uri, conf=self.conf)
        (image_file, image_size) = self.store.get(loc)
        self.assertEqual(image_size, 31)

        chunks = [c for c in image_file]
        self.assertEqual(chunks, expected_returns)
    def test_http_get_not_found(self):
        fake = utils.FakeHTTPResponse(status=404, data="404 Not Found")
        self.response.return_value = fake

        uri = "http://netloc/path/to/file.tar.gz"
        loc = location.get_location_from_uri(uri, conf=self.conf)
        self.assertRaises(exceptions.NotFound, self.store.get, loc)
Exemple #3
0
    def test_http_get_size_with_non_existent_image_raises_Not_Found(self):
        self._mock_httplib()
        fake = utils.FakeHTTPResponse(status=404, data="404 Not Found")
        self.response.return_value = fake

        uri = "http://netloc/path/to/file.tar.gz"
        loc = location.get_location_from_uri(uri, conf=self.conf)
        self.assertRaises(exceptions.NotFound, self.store.get_size, loc)
    def test_http_get_redirect_invalid(self):
        redirect = {"location": "http://example.com/teapot.img"}
        redirect_resp = utils.FakeHTTPResponse(status=307, headers=redirect)
        self.response.return_value = redirect_resp

        uri = "http://netloc/path/to/file.tar.gz"
        loc = location.get_location_from_uri(uri, conf=self.conf)
        self.assertRaises(exceptions.BadStoreUri, self.store.get, loc)
Exemple #5
0
    def test_http_get_max_redirects(self):
        redirect = {"location": "http://example.com/teapot.img"}
        responses = ([utils.FakeHTTPResponse(status=302, headers=redirect)]
                     * (http.MAX_REDIRECTS + 2))

        def getresponse():
            return responses.pop()
        self.response.side_effect = getresponse

        uri = "http://netloc/path/to/file.tar.gz"
        loc = get_location_from_uri(uri)
        self.assertRaises(exceptions.MaxRedirectsExceeded, self.store.get, loc)
Exemple #6
0
    def _mock_httplib(self):
        """Mock httplib connection object.

        Should be called when need to mock httplib response and request
        objects.
        """
        response = mock.patch('httplib.HTTPConnection.getresponse')
        self.response = response.start()
        self.response.return_value = utils.FakeHTTPResponse()
        self.addCleanup(response.stop)

        request = mock.patch('httplib.HTTPConnection.request')
        self.request = request.start()
        self.request.side_effect = lambda w, x, y, z: None
        self.addCleanup(request.stop)
    def setUp(self):
        super(TestHttpStore, self).setUp()
        self.config(default_store='http', group='glance_store')
        http.Store.READ_CHUNKSIZE = 2
        self.store = http.Store(self.conf)

        response = mock.patch('httplib.HTTPConnection.getresponse')
        self.response = response.start()
        self.response.return_value = utils.FakeHTTPResponse()
        self.addCleanup(response.stop)

        request = mock.patch('httplib.HTTPConnection.request')
        self.request = request.start()
        self.request.side_effect = lambda w, x, y, z: None
        self.addCleanup(request.stop)
Exemple #8
0
 def getresponse(self):
     return utils.FakeHTTPResponse(status=self.status)
 def getresponse(self):
     return utils.FakeHTTPResponse(status=self.status,
                                   no_response_body=self.no_response_body)