Ejemplo n.º 1
0
 def _send_server_action_request(self, url, body):
     app = compute.APIRouter(init_only=('servers', ))
     request = webob.Request.blank(url)
     request.method = 'POST'
     request.content_type = 'application/json'
     request.body = jsonutils.dump_as_bytes(body)
     response = request.get_response(app)
     return response
Ejemplo n.º 2
0
 def test_invalid_extensions(self):
     # Don't need the serialization middleware here because we're
     # not testing any serialization
     compute.APIRouter()
     ext_mgr = compute_extensions.ExtensionManager()
     ext_mgr.register(InvalidExtension())
     self.assertTrue(ext_mgr.is_loaded('FOXNSOX'))
     self.assertFalse(ext_mgr.is_loaded('THIRD'))
Ejemplo n.º 3
0
 def test_get_resources_with_controller(self):
     res_ext = base_extensions.ResourceExtension(
         'tweedles', StubController(response_body))
     manager = StubExtensionManager(res_ext)
     app = compute.APIRouter(manager)
     request = webob.Request.blank("/fake/tweedles")
     response = request.get_response(app)
     self.assertEqual(200, response.status_int)
     self.assertEqual(response_body, response.body)
Ejemplo n.º 4
0
    def _bounce_id(self, test_id):
        class BounceController(object):
            def show(self, req, id):
                return id

        res_ext = base_extensions.ResourceExtension('bounce',
                                                    BounceController())
        manager = StubExtensionManager(res_ext)
        app = compute.APIRouter(manager)
        request = webob.Request.blank("/fake/bounce/%s" % test_id)
        response = request.get_response(app)
        return response.body
Ejemplo n.º 5
0
 def test_controller_extension_early(self):
     controller = StubController(response_body)
     res_ext = base_extensions.ResourceExtension('tweedles', controller)
     ext_controller = StubEarlyExtensionController(extension_body)
     extension = StubControllerExtension()
     cont_ext = base_extensions.ControllerExtension(extension, 'tweedles',
                                                    ext_controller)
     manager = StubExtensionManager(resource_ext=res_ext,
                                    controller_ext=cont_ext)
     app = compute.APIRouter(manager)
     request = webob.Request.blank("/fake/tweedles")
     response = request.get_response(app)
     self.assertEqual(200, response.status_int)
     self.assertEqual(extension_body, response.body)
Ejemplo n.º 6
0
    def test_get_extension_json(self):
        app = compute.APIRouter(init_only=('extensions', ))
        request = webob.Request.blank("/fake/extensions/FOXNSOX")
        response = request.get_response(app)
        self.assertEqual(200, response.status_int)

        data = jsonutils.loads(response.body)
        self.assertEqual(
            {
                "namespace": "http://www.fox.in.socks/api/ext/pie/v1.0",
                "name": "Fox In Socks",
                "updated": "2011-01-22T13:25:27-06:00",
                "description": "The Fox In Socks Extension.",
                "alias": "FOXNSOX",
                "links": []
            }, data['extension'])
Ejemplo n.º 7
0
    def test_controller_extension_late(self):
        # Need a dict for the body to convert to a ResponseObject
        controller = StubController(dict(foo=response_body))
        res_ext = base_extensions.ResourceExtension('tweedles', controller)

        ext_controller = StubLateExtensionController(extension_body)
        extension = StubControllerExtension()
        cont_ext = base_extensions.ControllerExtension(extension, 'tweedles',
                                                       ext_controller)

        manager = StubExtensionManager(resource_ext=res_ext,
                                       controller_ext=cont_ext)
        app = compute.APIRouter(manager)
        request = webob.Request.blank("/fake/tweedles")
        response = request.get_response(app)
        self.assertEqual(200, response.status_int)
        self.assertEqual(extension_body, response.body)
Ejemplo n.º 8
0
 def test_non_exist_resource(self):
     res_ext = base_extensions.ResourceExtension(
         'tweedles', StubController(response_body))
     manager = StubExtensionManager(res_ext)
     app = compute.APIRouter(manager)
     request = webob.Request.blank("/fake/tweedles/1")
     response = request.get_response(app)
     self.assertEqual(404, response.status_int)
     self.assertEqual('application/json', response.content_type)
     body = jsonutils.loads(response.body)
     expected = {
         "itemNotFound": {
             "message": "The resource could not be found.",
             "code": 404
         }
     }
     self.assertThat(expected, matchers.DictMatches(body))
Ejemplo n.º 9
0
 def test_bad_request(self):
     res_ext = base_extensions.ResourceExtension(
         'tweedles', StubController(response_body))
     manager = StubExtensionManager(res_ext)
     app = compute.APIRouter(manager)
     request = webob.Request.blank("/fake/tweedles")
     request.method = "POST"
     response = request.get_response(app)
     self.assertEqual(400, response.status_int)
     self.assertEqual('application/json', response.content_type)
     body = jsonutils.loads(response.body)
     expected = {
         "badRequest": {
             "message": "All aboard the fail train!",
             "code": 400
         }
     }
     self.assertThat(expected, matchers.DictMatches(body))
Ejemplo n.º 10
0
 def test_controller_action_extension_early(self):
     controller = StubActionController(response_body)
     actions = dict(action='POST')
     res_ext = base_extensions.ResourceExtension('tweedles',
                                                 controller,
                                                 member_actions=actions)
     ext_controller = StubEarlyExtensionController(extension_body)
     extension = StubControllerExtension()
     cont_ext = base_extensions.ControllerExtension(extension, 'tweedles',
                                                    ext_controller)
     manager = StubExtensionManager(resource_ext=res_ext,
                                    controller_ext=cont_ext)
     app = compute.APIRouter(manager)
     request = webob.Request.blank("/fake/tweedles/foo/action")
     request.method = 'POST'
     request.headers['Content-Type'] = 'application/json'
     request.body = jsonutils.dump_as_bytes(dict(fooAction=True))
     response = request.get_response(app)
     self.assertEqual(200, response.status_int)
     self.assertEqual(extension_body, response.body)
Ejemplo n.º 11
0
    def test_list_extensions_json(self):
        app = compute.APIRouter(init_only=('extensions', ))
        request = webob.Request.blank("/fake/extensions")
        response = request.get_response(app)
        self.assertEqual(200, response.status_int)

        # Make sure we have all the extensions, extra extensions being OK.
        data = jsonutils.loads(response.body)
        names = [
            str(x['name']) for x in data['extensions']
            if str(x['name']) in self.ext_list
        ]
        names.sort()
        self.assertEqual(names, self.ext_list)

        # Ensure all the timestamps are valid according to iso8601
        for ext in data['extensions']:
            iso8601.parse_date(ext['updated'])

        # Make sure that at least Fox in Sox is correct.
        (fox_ext, ) = [
            x for x in data['extensions'] if x['alias'] == 'FOXNSOX'
        ]
        self.assertEqual(
            {
                'namespace': 'http://www.fox.in.socks/api/ext/pie/v1.0',
                'name': 'Fox In Socks',
                'updated': '2011-01-22T13:25:27-06:00',
                'description': 'The Fox In Socks Extension.',
                'alias': 'FOXNSOX',
                'links': []
            }, fox_ext)

        for ext in data['extensions']:
            url = '/fake/extensions/%s' % ext['alias']
            request = webob.Request.blank(url)
            response = request.get_response(app)
            output = jsonutils.loads(response.body)
            self.assertEqual(output['extension']['alias'], ext['alias'])
Ejemplo n.º 12
0
 def test_get_resources(self):
     app = compute.APIRouter()
     request = webob.Request.blank("/fake/foxnsocks")
     response = request.get_response(app)
     self.assertEqual(200, response.status_int)
     self.assertEqual(response_body, response.body)
Ejemplo n.º 13
0
 def test_no_extension_present(self):
     manager = StubExtensionManager(None)
     app = compute.APIRouter(manager)
     request = webob.Request.blank("/blah")
     response = request.get_response(app)
     self.assertEqual(404, response.status_int)
Ejemplo n.º 14
0
 def test_get_non_existing_extension_json(self):
     app = compute.APIRouter(init_only=('extensions', ))
     request = webob.Request.blank("/fake/extensions/4")
     response = request.get_response(app)
     self.assertEqual(404, response.status_int)