def test_update_qos(self): """ Test update qos """ LOG.debug("test_update_qos - START") req_body = jsonutils.dumps(self.test_qos_data) index_response = self.test_app.post(self.qos_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) rename_req_body = jsonutils.dumps({ 'qos': { 'qos_name': 'cisco_rename_qos', 'qos_desc': { 'PPS': 50, 'TTL': 5, }, }, }) rename_path_temp = (self.qos_second_path + resp_body['qoss']['qos']['id']) rename_path = str(rename_path_temp) rename_response = self.test_app.put(rename_path, rename_req_body, content_type=self.contenttype) self.assertEqual(200, rename_response.status_int) rename_resp_dict = wsgi.Serializer().deserialize( rename_response.body, self.contenttype) self.assertEqual(rename_resp_dict['qoss']['qos']['name'], 'cisco_rename_qos') self.tearDownQos(rename_path) LOG.debug("test_update_qos - END")
def test_update_credential(self): """ Test update credential """ LOG.debug("test_update_credential - START") req_body = jsonutils.dumps(self.test_credential_data) index_response = self.test_app.post(self.credential_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) rename_req_body = jsonutils.dumps({ 'credential': { 'credential_name': 'cred3', 'user_name': 'RenamedUser', 'password': '******', }, }) rename_path_temp = (self.cred_second_path + resp_body['credentials']['credential']['id']) rename_path = str(rename_path_temp) rename_response = self.test_app.put(rename_path, rename_req_body, content_type=self.contenttype) rename_resp_dict = wsgi.Serializer().deserialize( rename_response.body, self.contenttype) self.assertEqual(rename_resp_dict['credentials']['credential']['name'], 'cred3') self.assertEqual( rename_resp_dict['credentials']['credential']['password'], self.test_credential_data['credential']['password']) self.assertEqual(200, rename_response.status_int) # Clean Up - Delete the Credentials self.tearDownCredential(rename_path) LOG.debug("test_update_credential - END")
def _create_port(self, network_id, port_state): """ Test create port""" LOG.debug("Creating port for network %s - START", network_id) port_path = "/tenants/tt/networks/%s/ports" % network_id port_req_data = {'port': {'state': '%s' % port_state}} req_body = wsgi.Serializer().serialize(port_req_data, self.contenttype) port_req = self.create_request(port_path, req_body, self.contenttype, 'POST') port_res = port_req.get_response(self.api) port_data = wsgi.Serializer().deserialize(port_res.body, self.contenttype) LOG.debug("Creating port for network - END") return port_data['port']['id']
def test_get_deserialize_handler_unknown_content_type(self): """Verify that exception InvalidContentType is raised.""" content_type = 'application/unknown' serializer = wsgi.Serializer() self.assertRaises(exception.InvalidContentType, serializer.get_deserialize_handler, content_type)
def __call__(self, req): metadata = { 'application/xml': { 'attributes': { 'resource': ['name', 'collection'], 'link': ['href', 'rel'] } } } layout = [] for name, collection in self.resources.iteritems(): href = urlparse.urljoin(req.path_url, collection) resource = { 'name': name, 'collection': collection, 'links': [{ 'rel': 'self', 'href': href }] } layout.append(resource) response = dict(resources=layout) content_type = req.best_match_content_type() body = wsgi.Serializer(metadata=metadata).serialize( response, content_type) return webob.Response(body=body, content_type=content_type)
def __call__(self, req): """Respond to a request for all Quantum API versions.""" version_objs = [ { "id": "v1.0", "status": "CURRENT", }, { "id": "v1.1", "status": "PROPOSED", }, ] builder = versions_view.get_view_builder(req) versions = [builder.build(version) for version in version_objs] response = dict(versions=versions) metadata = { "application/xml": { "attributes": { "version": ["status", "id"], "link": ["rel", "href"], } } } content_type = req.best_match_content_type() body = wsgi.Serializer(metadata=metadata). \ serialize(response, content_type) response = webob.Response() response.content_type = content_type response.body = body return response
def test_serialize_content_type_json(self): """Test serialize with content type json.""" input_data = {'servers': ['test=pass']} content_type = 'application/json' serializer = wsgi.Serializer(default_xmlns="fake") result = serializer.serialize(input_data, content_type) self.assertEqual('{"servers": ["test=pass"]}', result)
def test_deserialize_json_content_type(self): """Test Serializer.deserialize with content type json.""" content_type = 'application/json' data_string = '{"servers": ["test=pass"]}' serializer = wsgi.Serializer(default_xmlns="fake") result = serializer.deserialize(data_string, content_type) self.assertEqual({'body': {u'servers': [u'test=pass']}}, result)
def test_serialize_unknown_content_type(self): """Verify that exception InvalidContentType is raised.""" input_dict = {'servers': {'test': 'pass'}} content_type = 'application/unknown' serializer = wsgi.Serializer() self.assertRaises(exception.InvalidContentType, serializer.serialize, input_dict, content_type)
def test_deserialize_raise_bad_request(self): """Test serialize verifies that exception is raises.""" content_type = 'application/unknown' data_string = 'test' serializer = wsgi.Serializer(default_xmlns="fake") self.assertRaises(webob.exc.HTTPBadRequest, serializer.deserialize, data_string, content_type)
def test_list_credentials(self): """ Test list credentials """ #Create Credential before listing LOG.debug("test_list_credentials - START") req_body1 = jsonutils.dumps(self.test_credential_data) create_response1 = self.test_app.post(self.credential_path, req_body1, content_type=self.contenttype) req_body2 = jsonutils.dumps({ 'credential': { 'credential_name': 'cred9', 'user_name': 'newUser2', 'password': '******', }, }) create_response2 = self.test_app.post(self.credential_path, req_body2, content_type=self.contenttype) index_response = self.test_app.get(self.credential_path) index_resp_body = wsgi.Serializer().deserialize( index_response.body, self.contenttype) self.assertEqual(200, index_response.status_int) #CLean Up - Deletion of the Credentials resp_body1 = wsgi.Serializer().deserialize(create_response1.body, self.contenttype) delete_path1_temp = (self.cred_second_path + resp_body1['credentials']['credential']['id']) delete_path1 = str(delete_path1_temp) resp_body2 = wsgi.Serializer().deserialize(create_response2.body, self.contenttype) list_all_credential = [ resp_body1['credentials']['credential'], resp_body2['credentials']['credential'] ] self.assertTrue( index_resp_body['credentials'][0] in list_all_credential) self.assertTrue( index_resp_body['credentials'][1] in list_all_credential) delete_path2_temp = (self.cred_second_path + resp_body2['credentials']['credential']['id']) delete_path2 = str(delete_path2_temp) self.tearDownCredential(delete_path1) self.tearDownCredential(delete_path2) LOG.debug("test_list_credentials - END")
def _create_network(self, name=None): """ Test create network""" LOG.debug("Creating network - START") if name: net_name = name else: net_name = self.network_name net_path = "/tenants/tt/networks" net_data = {'network': {'name': '%s' % net_name}} req_body = wsgi.Serializer().serialize(net_data, self.contenttype) network_req = self.create_request(net_path, req_body, self.contenttype, 'POST') network_res = network_req.get_response(self.api) network_data = wsgi.Serializer().deserialize(network_res.body, self.contenttype) LOG.debug("Creating network - END") return network_data['network']['id']
def test_deserialize_xml_content_type(self): """Test deserialize with content type xml.""" content_type = 'application/xml' data_string = ('<servers xmlns="fake">' '<server>test=pass</server>' '</servers>') serializer = wsgi.Serializer(default_xmlns="fake", metadata={'xmlns': 'fake'}) result = serializer.deserialize(data_string, content_type) expected = {'body': {'servers': {'server': 'test=pass'}}} self.assertEqual(expected, result)
def test_list_qoss(self): """ Test list qoss """ LOG.debug("test_list_qoss - START") req_body1 = jsonutils.dumps(self.test_qos_data) create_resp1 = self.test_app.post(self.qos_path, req_body1, content_type=self.contenttype) req_body2 = jsonutils.dumps({ 'qos': { 'qos_name': 'cisco_test_qos2', 'qos_desc': { 'PPS': 50, 'TTL': 5, }, }, }) create_resp2 = self.test_app.post(self.qos_path, req_body2, content_type=self.contenttype) index_response = self.test_app.get(self.qos_path) index_resp_body = wsgi.Serializer().deserialize( index_response.body, self.contenttype) self.assertEqual(200, index_response.status_int) # Clean Up - Delete the qos's resp_body1 = wsgi.Serializer().deserialize(create_resp1.body, self.contenttype) qos_path1_temp = self.qos_second_path + resp_body1['qoss']['qos']['id'] qos_path1 = str(qos_path1_temp) resp_body2 = wsgi.Serializer().deserialize(create_resp2.body, self.contenttype) list_all_qos = [resp_body1['qoss']['qos'], resp_body2['qoss']['qos']] self.assertTrue(index_resp_body['qoss'][0] in list_all_qos) self.assertTrue(index_resp_body['qoss'][1] in list_all_qos) qos_path2_temp = self.qos_second_path + resp_body2['qoss']['qos']['id'] qos_path2 = str(qos_path2_temp) self.tearDownQos(qos_path1) self.tearDownQos(qos_path2) LOG.debug("test_list_qoss - END")
def test_show_credential(self): """ Test show credential """ LOG.debug("test_show_credential - START") req_body = jsonutils.dumps(self.test_credential_data) index_response = self.test_app.post(self.credential_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) show_path_temp = (self.cred_second_path + resp_body['credentials']['credential']['id']) show_cred_path = str(show_path_temp) show_response = self.test_app.get(show_cred_path) show_resp_dict = wsgi.Serializer().deserialize(show_response.body, self.contenttype) self.assertEqual(show_resp_dict['credentials']['credential']['name'], self.test_credential_data['credential']['user_name']) self.assertEqual( show_resp_dict['credentials']['credential']['password'], self.test_credential_data['credential']['password']) self.assertEqual(200, show_response.status_int) LOG.debug("test_show_credential - END")
def test_serialize_content_type_xml(self): """Test serialize with content type xml.""" input_data = {'servers': ['test=pass']} content_type = 'application/xml' serializer = wsgi.Serializer(default_xmlns="fake") result = serializer.serialize(input_data, content_type) expected = ('<?xml version=\'1.0\'' ' encoding=\'UTF-8\'?>\n' '<servers xmlns="http://openstack.org/quantum/api/v2.0" ' 'xmlns:quantum="http://openstack.org/quantum/api/v2.0" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' '<server>test=pass</server></servers>') self.assertEqual(expected, result)
def _do_request(self, method, path, data=None, params=None, action=None): content_type = 'application/json' body = None if data is not None: # empty dict is valid body = wsgi.Serializer().serialize(data, content_type) req = testlib_api.create_request( path, body, content_type, method, query_string=params) res = req.get_response(self._api) if res.status_code >= 400: raise webexc.HTTPClientError(detail=res.body, code=res.status_code) if res.status_code != webexc.HTTPNoContent.code: return res.json
def test_show_qos(self): """ Test show qos """ LOG.debug("test_show_qos - START") req_body = jsonutils.dumps(self.test_qos_data) index_response = self.test_app.post(self.qos_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) show_path_temp = self.qos_second_path + resp_body['qoss']['qos']['id'] show_qos_path = str(show_path_temp) show_response = self.test_app.get(show_qos_path) show_resp_dict = wsgi.Serializer().deserialize(show_response.body, self.contenttype) self.assertEqual(show_resp_dict['qoss']['qos']['name'], self.test_qos_data['qos']['qos_name']) self.assertEqual(200, show_response.status_int) # Clean Up - Delete the qos self.tearDownQos(show_qos_path) LOG.debug("test_show_qos - END")
def test_serialize_xml_root_is_None(self): input_dict = {'test': 'pass'} content_type = 'application/xml' serializer = wsgi.Serializer(default_xmlns="fake") result = serializer.serialize(input_dict, content_type) result = result.replace('\n', '').replace(' ', '') expected = ('<?xmlversion=\'1.0\'' 'encoding=\'UTF-8\'?>' '<testxmlns="http://openstack.org/quantum/api/v2.0"' 'xmlns:quantum="http://openstack.org/quantum/api/v2.0"' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' 'pass</test>') self.assertEqual(result, expected)
def test_serialize_xml_root_key_is_dict(self): """Test Serializer.serialize with content type xml with meta dict.""" content_type = 'application/xml' data = {'servers': {'network': (2, 3)}} metadata = {'xmlns': 'fake'} serializer = wsgi.Serializer(default_xmlns="fake", metadata=metadata) result = serializer.serialize(data, content_type) result = result.replace('\n', '') expected = ('<?xml version=\'1.0\' encoding=\'UTF-8\'?>' '<servers xmlns="fake" xmlns:quantum="fake" ' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' '<network>(2, 3)</network></servers>') self.assertEqual(result, expected)
def test_deserialize_xml_content_type_with_meta(self): """Test deserialize with content type xml with meta.""" content_type = 'application/xml' data_string = ('<servers>' '<server name="s1">' '<test test="a">passed</test>' '</server>' '</servers>') metadata = {'plurals': {'servers': 'server'}, 'xmlns': 'fake'} serializer = wsgi.Serializer(default_xmlns="fake", metadata=metadata) result = serializer.deserialize(data_string, content_type) expected = {'body': {'servers': [{'name': 's1', 'test': 'passed'}]}} self.assertEqual(expected, result)
def test_serialize_xml_root_key_is_list(self): """Test serialize with content type xml with meta list.""" input_dict = {'servers': ['test=pass']} content_type = 'application/xml' metadata = {'application/xml': {'xmlns': 'fake'}} serializer = wsgi.Serializer(default_xmlns="fake", metadata=metadata) result = serializer.serialize(input_dict, content_type) result = result.replace('\n', '').replace(' ', '') expected = ('<?xmlversion=\'1.0\'' 'encoding=\'UTF-8\'?>' '<serversxmlns="http://openstack.org/quantum/api/v2.0"' 'xmlns:quantum="http://openstack.org/quantum/api/v2.0"' 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' '<server>test=pass</server></servers>') self.assertEqual(result, expected)
def test_delete_credential(self): """ Test delete credential """ LOG.debug("test_delete_credential - START") req_body = jsonutils.dumps(self.test_credential_data) index_response = self.test_app.post(self.credential_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) delete_path_temp = (self.cred_second_path + resp_body['credentials']['credential']['id']) delete_path = str(delete_path_temp) delete_response = self.test_app.delete(delete_path) self.assertEqual(200, delete_response.status_int) LOG.debug("test_delete_credential - END")
def test_create_qos(self): """ Test create qos """ LOG.debug("test_create_qos - START") req_body = jsonutils.dumps(self.test_qos_data) index_response = self.test_app.post(self.qos_path, req_body, content_type=self.contenttype) self.assertEqual(200, index_response.status_int) # Clean Up - Delete the qos resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) qos_path_temp = self.qos_second_path + resp_body['qoss']['qos']['id'] qos_path = str(qos_path_temp) self.tearDownQos(qos_path) LOG.debug("test_create_qos - END")
def test_update_credBADReq(self): """ Test update credential bad request """ LOG.debug("test_update_credBADReq - START") req_body = jsonutils.dumps(self.test_credential_data) index_response = self.test_app.post(self.credential_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) rename_path_temp = (self.cred_second_path + resp_body['credentials']['credential']['id']) rename_path = str(rename_path_temp) rename_response = self.test_app.put(rename_path, 'BAD_REQUEST', status='*') self.assertEqual(400, rename_response.status_int) LOG.debug("test_update_credBADReq - END")
def __call__(self, req): """Generate a WSGI response based on the exception passed to constructor.""" # Replace the body with fault details. code = self.wrapped_exc.status_int fault_name = self._fault_names.get(code, "quantumServiceFault") fault_data = { fault_name: { 'code': code, 'message': self.wrapped_exc.explanation } } # 'code' is an attribute on the fault tag itself content_type = req.best_match_content_type() self.wrapped_exc.body = wsgi.Serializer().\ serialize(fault_data, content_type) self.wrapped_exc.content_type = content_type return self.wrapped_exc
def test_update_qosBADRequest(self): """ Test update qos bad request """ LOG.debug("test_update_qosBADRequest - START") req_body = jsonutils.dumps(self.test_qos_data) index_response = self.test_app.post(self.qos_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) rename_path_temp = (self.qos_second_path + resp_body['qoss']['qos']['id']) rename_path = str(rename_path_temp) rename_response = self.test_app.put(rename_path, 'BAD_REQUEST', status="*") self.assertEqual(400, rename_response.status_int) # Clean Up - Delete the Port Profile self.tearDownQos(rename_path) LOG.debug("test_update_qosBADRequest - END")
def test_delete_qos(self): """ Test delte qos """ LOG.debug("test_delete_qos - START") req_body = jsonutils.dumps({ 'qos': { 'qos_name': 'cisco_test_qos', 'qos_desc': { 'PPS': 50, 'TTL': 5, }, }, }) index_response = self.test_app.post(self.qos_path, req_body, content_type=self.contenttype) resp_body = wsgi.Serializer().deserialize(index_response.body, self.contenttype) delete_path_temp = (self.qos_second_path + resp_body['qoss']['qos']['id']) delete_path = str(delete_path_temp) delete_response = self.test_app.delete(delete_path) self.assertEqual(200, delete_response.status_int) LOG.debug("test_delete_qos - END")
def serialize(self, data): ctype = 'application/%s' % self.fmt result = wsgi.Serializer(attributes.get_attr_metadata()).serialize( data, ctype) return result