def test_crud_operations(self): user = randomword() prefs = self.create_random_dict() # GET preferences for a non-existent user with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = self.handle('GET', user) self.assertEqual('error', reply[api.STATUS]) try: reply = self.handle('POST', user, prefs) self.assertEqual('complete', reply[api.STATUS]) reply = self.handle('GET', user) self.assertEqual('complete', reply[api.STATUS]) self.assertEqual(prefs, reply[api.DATA]) prefs = self.create_random_dict() reply = self.handle('PUT', user, prefs) self.assertEqual('complete', reply[api.STATUS]) reply = self.handle('GET', user) self.assertEqual('complete', reply[api.STATUS]) self.assertEqual(prefs, reply[api.DATA]) finally: reply = self.handle('DELETE', user) self.assertEqual('complete', reply[api.STATUS]) with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = self.handle('GET', user) self.assertEqual('error', reply[api.STATUS])
def testMissingOp(self): request = {api.TARGET: 'expose', api.DATA: {api.OPERATION: 'foo'}} # Suppress exception log with util.log_level(logging.CRITICAL, 'bll'): reply = SvcBase.spawn_service(BllRequest(request)) self.assertEqual(reply[api.STATUS], api.STATUS_ERROR) self.assertIn('Unsupported', reply[api.DATA][0][api.DATA])
def test_post_request_fail(self, _mock_request, _mock_response): _mock_request.body = json.dumps({api.TARGET: 'general', api.DATA: {api.OPERATION: 'failhandle'}}) # Suppress the expected exception message with log_level(logging.CRITICAL, 'bll'): reply = app_controller.AppController().post() self.assertEqual(reply[api.STATUS], api.STATUS_ERROR) self.assertTrue(_mock_response.status, 400)
def test_post_complete_fail(self, _mock_request, _mock_response): _mock_request.body = json.dumps({api.TARGET: 'general', api.DATA: {api.OPERATION: 'failcomplete'}}) # Suppress the expected exception message from service with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = app_controller.AppController().post() time.sleep(0.1) txn_id = reply.get(api.TXN_ID) reply = get_job_status(txn_id) self.assertEqual(reply[api.STATUS], 'error')
def test_service_delete_bad_input(self, _mock_nova_client, _mock_endpoints, _mock_serv_end, _mock_ks_client): _mock_nova_client.return_value = self.mock_novaclient _mock_serv_end.return_value = None _mock_ks_client.return_value = self.mock_ksclient _mock_endpoints.return_value = self.mock_get_endpoints with log_level(logging.CRITICAL, 'bll'): svc = NovaSvc( BllRequest(auth_token=get_mock_token(), operation='service-delete', data={'hostname': 'badhost'})) with self.assertRaises(Exception): svc.handle()
def test_status_update_no_txn_id(self, _mock_request, _mock_response): _mock_request.body = json.dumps({ api.TARGET: 'general', api.DATA: { api.OPERATION: 'null', }, api.JOB_STATUS_REQUEST: True }) with log_level(logging.CRITICAL, 'bll'): reply = app_controller.AppController().post() self.assertEqual(reply[api.STATUS], api.STATUS_ERROR) self.assertIn("No txn_id", reply[api.DATA][0][api.DATA]) self.assertTrue(_mock_response.status, 400)
def testSlowOp(self): request = {api.TARGET: 'expose', api.DATA: {api.OPERATION: 'slow_op'}} # Suppress exception log txn_id = None with util.log_level(logging.CRITICAL, 'bll'): reply = SvcBase.spawn_service(BllRequest(request)) txn_id = reply.get(api.TXN_ID) self.assertEqual(reply[api.PROGRESS]['percentComplete'], 0) self.assertEqual(api.STATUS_INPROGRESS, reply[api.STATUS]) time.sleep(0.1) reply = get_job_status(txn_id) self.assertEqual(reply[api.PROGRESS]['percentComplete'], 100)
def test_service_delete_no_input(self, _mock_nova_client, _mock_serv_end, _mock_ks_client): _mock_nova_client.return_value = self.mock_novaclient _mock_serv_end.return_value = None _mock_ks_client.return_value = self.mock_ksclient with log_level(logging.CRITICAL, 'bll'): svc = NovaSvc( BllRequest(auth_token=get_mock_token(), data={api.OPERATION: 'service-delete'})) # In production the service is called via the handle method of the # base, SvcBase, which uses pykka and catches exceptions and # returns an appropriate error response to the caller. But using # pykka in unit tests conflicts with the mock library causing any # such tests to hang as both libraries are attempting to # automagically wrap function calls in proxies. Therefore we have # to directly expect and handle the exception ourselves. with self.assertRaises(Exception): svc.handle()
def test_put_to_missing_user(self): # PUT preferences for a non-existent user with log_level(logging.CRITICAL, 'bll.plugins.service'): reply = self.handle('PUT', randomword(), randomword()) self.assertEqual('error', reply[api.STATUS])