Ejemplo n.º 1
0
def fake_request(stubs, mdinst, relpath, address="127.0.0.1",
                 fake_get_metadata=None, headers=None,
                 fake_get_metadata_by_instance_id=None, app=None):

    def get_metadata_by_remote_address(address):
        return mdinst

    if app is None:
        app = handler.MetadataRequestHandler()

    if fake_get_metadata is None:
        fake_get_metadata = get_metadata_by_remote_address

    if stubs:
        stubs.Set(app, 'get_metadata_by_remote_address', fake_get_metadata)

        if fake_get_metadata_by_instance_id:
            stubs.Set(app, 'get_metadata_by_instance_id',
                      fake_get_metadata_by_instance_id)

    request = webob.Request.blank(relpath)
    request.remote_addr = address

    if headers is not None:
        request.headers.update(headers)

    response = request.get_response(app)
    return response
Ejemplo n.º 2
0
 def test_metadata_handler_with_remote_address_no_cache(self, get_by_uuid):
     # test twice to ensure that disabling the cache works
     get_by_uuid.return_value = self.mdinst
     self.flags(metadata_cache_expiration=0)
     hnd = handler.MetadataRequestHandler()
     self._metadata_handler_with_remote_address(hnd)
     self._metadata_handler_with_remote_address(hnd)
     self.assertEqual(2, get_by_uuid.call_count)
Ejemplo n.º 3
0
 def test_metadata_handler_with_instance_id(self, get_by_uuid):
     # test twice to ensure that the cache works
     get_by_uuid.return_value = self.mdinst
     self.flags(metadata_cache_expiration=15)
     hnd = handler.MetadataRequestHandler()
     self._metadata_handler_with_instance_id(hnd)
     self._metadata_handler_with_instance_id(hnd)
     self.assertEqual(1, get_by_uuid.call_count)
Ejemplo n.º 4
0
    def test_by_instance_id_uses_constant_time_compare(self, mock_compare):
        mock_compare.side_effect = test.TestingException

        req = webob.Request.blank('/')
        hnd = handler.MetadataRequestHandler()

        req.headers['X-Instance-ID'] = 'fake-inst'
        req.headers['X-Instance-ID-Signature'] = 'fake-sig'
        req.headers['X-Tenant-ID'] = 'fake-proj'

        self.assertRaises(test.TestingException,
                          hnd._handle_instance_id_request, req)

        self.assertEqual(1, mock_compare.call_count)