Example #1
0
    def test_list_extensions_xml(self):
        app = v2.APIRouter()
        ext_midware = extensions.ExtensionMiddleware(app)
        ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
        request = webob.Request.blank("/123/extensions")
        request.accept = "application/xml"
        response = request.get_response(ser_midware)
        self.assertEqual(200, response.status_int)
        print response.body

        root = etree.XML(response.body)
        self.assertEqual(root.tag.split('extensions')[0], NS)

        # Make sure we have all the extensions.
        exts = root.findall('{0}extension'.format(NS))
        self.assertEqual(len(exts), len(self.ext_list))

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

        xmlutil.validate_schema(root, 'extensions')
Example #2
0
    def test_list_extensions_json(self):
        app = v2.APIRouter()
        ext_midware = extensions.ExtensionMiddleware(app)
        ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
        request = webob.Request.blank("/123/extensions")
        response = request.get_response(ser_midware)
        self.assertEqual(200, response.status_int)

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

        # Make sure that at least Fox in Sox is correct.
        (fox_ext, ) = [
            x for x in data['extensions'] if x['alias'] == 'FOXNSOX']
        self.assertEqual(fox_ext, {
                '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': []
            },
        )
Example #3
0
 def test_get_resources(self):
     app = v2.APIRouter()
     ext_midware = extensions.ExtensionMiddleware(app)
     ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
     request = webob.Request.blank("/123/foxnsocks")
     response = request.get_response(ser_midware)
     self.assertEqual(200, response.status_int)
     self.assertEqual(response_body, response.body)
Example #4
0
 def test_admin_extensions_no_admin_api(self):
     self.flags(allow_admin_api=False)
     app = v2.APIRouter()
     ext_midware = extensions.ExtensionMiddleware(app)
     ext_mgr = ext_midware.ext_mgr
     ext_mgr.register(AdminExtension())
     self.assertTrue('FOXNSOX' in ext_mgr.extensions)
     self.assertTrue('ADMIN' not in ext_mgr.extensions)
Example #5
0
 def test_no_extension_present(self):
     manager = StubExtensionManager(None)
     app = v2.APIRouter(manager)
     ext_midware = extensions.ExtensionMiddleware(app, manager)
     ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
     request = webob.Request.blank("/blah")
     response = request.get_response(ser_midware)
     self.assertEqual(404, response.status_int)
Example #6
0
 def test_invalid_extensions(self):
     # Don't need the serialization middleware here because we're
     # not testing any serialization
     app = v2.APIRouter()
     ext_midware = extensions.ExtensionMiddleware(app)
     ext_mgr = ext_midware.ext_mgr
     ext_mgr.register(InvalidExtension())
     self.assertTrue('FOXNSOX' in ext_mgr.extensions)
     self.assertTrue('THIRD' not in ext_mgr.extensions)
Example #7
0
 def _send_server_action_request(self, url, body):
     app = v2.APIRouter()
     ext_midware = extensions.ExtensionMiddleware(app)
     ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
     request = webob.Request.blank(url)
     request.method = 'POST'
     request.content_type = 'application/json'
     request.body = json.dumps(body)
     response = request.get_response(ser_midware)
     return response
Example #8
0
    def setUp(self):
        super(ServerDiagnosticsTest, self).setUp()
        self.flags(allow_admin_api=True)
        self.flags(verbose=True)
        self.stubs.Set(nova.compute.API, 'get_actions', fake_get_actions)
        self.stubs.Set(nova.compute.API, 'get', fake_instance_get)
        self.compute_api = nova.compute.API()

        self.router = v2.APIRouter()
        ext_middleware = extensions.ExtensionMiddleware(self.router)
        self.app = wsgi.LazySerializationMiddleware(ext_middleware)
Example #9
0
 def test_get_resources_with_controller(self):
     res_ext = extensions.ResourceExtension('tweedles',
                                            StubController(response_body))
     manager = StubExtensionManager(res_ext)
     app = v2.APIRouter(manager)
     ext_midware = extensions.ExtensionMiddleware(app, manager)
     ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
     request = webob.Request.blank("/123/tweedles")
     response = request.get_response(ser_midware)
     self.assertEqual(200, response.status_int)
     self.assertEqual(response_body, response.body)
Example #10
0
    def setUp(self):
        super(CreateBackupTests, self).setUp()

        self.stubs.Set(compute.API, 'get', fake_compute_api_get)
        self.backup_stubs = fakes.stub_out_compute_api_backup(self.stubs)

        self.flags(allow_admin_api=True)
        router = v2.APIRouter()
        ext_middleware = extensions.ExtensionMiddleware(router)
        self.app = wsgi.LazySerializationMiddleware(ext_middleware)

        self.uuid = utils.gen_uuid()
Example #11
0
    def test_get_resources_with_mgr(self):

        app = fakes.wsgi_app(serialization=base_wsgi.Middleware)
        ext_midware = extensions.ExtensionMiddleware(app)
        ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
        request = webob.Request.blank("/v2/123/flavors/1?chewing=newblue")
        request.environ['api.version'] = '2'
        response = request.get_response(ser_midware)
        self.assertEqual(200, response.status_int)
        response_data = json.loads(response.body)
        print response_data
        self.assertEqual('newblue', response_data['flavor']['googoose'])
        self.assertEqual("Pig Bands!", response_data['big_bands'])
Example #12
0
    def test_get_extension_json(self):
        app = v2.APIRouter()
        ext_midware = extensions.ExtensionMiddleware(app)
        ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
        request = webob.Request.blank("/123/extensions/FOXNSOX")
        response = request.get_response(ser_midware)
        self.assertEqual(200, response.status_int)

        data = json.loads(response.body)
        self.assertEqual(data['extension'], {
                "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": []})
Example #13
0
    def test_get_resources_with_stub_mgr(self):
        def _req_handler(req, res, body):
            # only handle JSON responses
            body['flavor']['googoose'] = req.GET.get('chewing')
            return res

        req_ext = extensions.RequestExtension('GET', '/v2/123/flavors/:(id)',
                                              _req_handler)

        manager = StubExtensionManager(None, None, req_ext)
        app = fakes.wsgi_app(serialization=base_wsgi.Middleware)
        ext_midware = extensions.ExtensionMiddleware(app, manager)
        ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
        request = webob.Request.blank("/v2/123/flavors/1?chewing=bluegoo")
        request.environ['api.version'] = '2'
        response = request.get_response(ser_midware)
        self.assertEqual(200, response.status_int)
        response_data = json.loads(response.body)
        self.assertEqual('bluegoo', response_data['flavor']['googoose'])
Example #14
0
 def test_non_exist_resource(self):
     res_ext = extensions.ResourceExtension('tweedles',
                                            StubController(response_body))
     manager = StubExtensionManager(res_ext)
     app = v2.APIRouter(manager)
     ext_midware = extensions.ExtensionMiddleware(app, manager)
     ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
     request = webob.Request.blank("/123/tweedles/1")
     response = request.get_response(ser_midware)
     self.assertEqual(404, response.status_int)
     self.assertEqual('application/json', response.content_type)
     body = json.loads(response.body)
     expected = {
         "itemNotFound": {
             "message": "The resource could not be found.",
             "code": 404
         }
     }
     self.assertDictMatch(expected, body)
Example #15
0
 def test_bad_request(self):
     res_ext = extensions.ResourceExtension('tweedles',
                                            StubController(response_body))
     manager = StubExtensionManager(res_ext)
     app = v2.APIRouter(manager)
     ext_midware = extensions.ExtensionMiddleware(app, manager)
     ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
     request = webob.Request.blank("/123/tweedles")
     request.method = "POST"
     response = request.get_response(ser_midware)
     self.assertEqual(400, response.status_int)
     self.assertEqual('application/json', response.content_type)
     body = json.loads(response.body)
     expected = {
         "badRequest": {
             "message": "All aboard the fail train!",
             "code": 400
         }
     }
     self.assertDictMatch(expected, body)
Example #16
0
    def test_get_extension_xml(self):
        app = v2.APIRouter()
        ext_midware = extensions.ExtensionMiddleware(app)
        ser_midware = wsgi.LazySerializationMiddleware(ext_midware)
        request = webob.Request.blank("/123/extensions/FOXNSOX")
        request.accept = "application/xml"
        response = request.get_response(ser_midware)
        self.assertEqual(200, response.status_int)
        xml = response.body
        print xml

        root = etree.XML(xml)
        self.assertEqual(root.tag.split('extension')[0], NS)
        self.assertEqual(root.get('alias'), 'FOXNSOX')
        self.assertEqual(root.get('name'), 'Fox In Socks')
        self.assertEqual(root.get('namespace'),
            'http://www.fox.in.socks/api/ext/pie/v1.0')
        self.assertEqual(root.get('updated'), '2011-01-22T13:25:27-06:00')
        self.assertEqual(root.findtext('{0}description'.format(NS)),
            'The Fox In Socks Extension')

        xmlutil.validate_schema(root, 'extension')
Example #17
0
    def setUp(self):
        super(DiskConfigTestCase, self).setUp()
        self.flags(verbose=True)
        fakes.stub_out_nw_api(self.stubs)

        FAKE_INSTANCES = [
            fakes.stub_instance(1,
                                uuid=MANUAL_INSTANCE_UUID,
                                auto_disk_config=False),
            fakes.stub_instance(2,
                                uuid=AUTO_INSTANCE_UUID,
                                auto_disk_config=True)
        ]

        def fake_instance_get(context, id_):
            for instance in FAKE_INSTANCES:
                if id_ == instance['id']:
                    return instance

        self.stubs.Set(nova.db.api, 'instance_get', fake_instance_get)
        self.stubs.Set(nova.db, 'instance_get', fake_instance_get)

        def fake_instance_get_by_uuid(context, uuid):
            for instance in FAKE_INSTANCES:
                if uuid == instance['uuid']:
                    return instance

        self.stubs.Set(nova.db, 'instance_get_by_uuid',
                       fake_instance_get_by_uuid)

        def fake_instance_get_all(context, *args, **kwargs):
            return FAKE_INSTANCES

        self.stubs.Set(nova.db, 'instance_get_all', fake_instance_get_all)
        self.stubs.Set(nova.db.api, 'instance_get_all_by_filters',
                       fake_instance_get_all)

        def fake_instance_create(context, inst_, session=None):
            class FakeModel(dict):
                def save(self, session=None):
                    pass

            inst = FakeModel(**inst_)
            inst['id'] = 1
            inst['uuid'] = AUTO_INSTANCE_UUID
            inst['created_at'] = datetime.datetime(2010, 10, 10, 12, 0, 0)
            inst['updated_at'] = datetime.datetime(2010, 10, 10, 12, 0, 0)
            inst['progress'] = 0
            inst['name'] = 'instance-1'  # this is a property

            def fake_instance_get_for_create(context, id_, session=None):
                return inst

            self.stubs.Set(nova.db, 'instance_get',
                           fake_instance_get_for_create)
            self.stubs.Set(nova.db.api, 'instance_get',
                           fake_instance_get_for_create)
            self.stubs.Set(nova.db.sqlalchemy.api, 'instance_get',
                           fake_instance_get_for_create)

            def fake_instance_add_security_group(context, instance_id,
                                                 security_group_id):
                pass

            self.stubs.Set(nova.db.sqlalchemy.api,
                           'instance_add_security_group',
                           fake_instance_add_security_group)

            return inst

        self.stubs.Set(nova.db, 'instance_create', fake_instance_create)

        app = v2.APIRouter()
        app = extensions.ExtensionMiddleware(app)
        app = wsgi.LazySerializationMiddleware(app)
        self.app = app
Example #18
0
 def test_get_non_existing_extension_json(self):
     app = v2.APIRouter()
     ext_midware = extensions.ExtensionMiddleware(app)
     request = webob.Request.blank("/123/extensions/4")
     response = request.get_response(ext_midware)
     self.assertEqual(404, response.status_int)