def _is_good_response(self, response, prefix): ''' Check for errors in a response from a GET or POST request. The response argument contains a response object from a GET or POST request. The prefix argument contains the prefix to put into the error message. Raises: CvpApiError: A CvpApiError is raised if there was a JSON error. CvpRequestError: A CvpRequestError is raised if the request is not properly constructed. CvpSessionLogOutError: A CvpSessionLogOutError is raised if response from server indicates session was logged out. ''' if not response.ok: if 'Unauthorized' in response.reason: # Check for 'Unauthorized' User error because this is how # CVP responds to a logged out users requests in 2018.x. msg = '%s: Request Error: %s' % (prefix, response.reason) self.log.error(msg) raise CvpApiError(msg) if 'User is unauthorized' in response.text: # Check for 'User is unauthorized' response text because this # is how CVP responds to a logged out users requests in 2019.x. msg = '%s: Request Error: User is unauthorized' % prefix self.log.error(msg) raise CvpApiError(msg) else: msg = '%s: Request Error: %s - %s' % (prefix, response.reason, response.text) self.log.error(msg) raise CvpRequestError(msg) if 'LOG OUT MESSAGE' in response.text: msg = ('%s: Request Error: session logged out' % prefix) raise CvpSessionLogOutError(msg) joutput = json_decoder(response.text) err_code_val = self._finditem(joutput, 'errorCode') if err_code_val: if 'errorMessage' in joutput: err_msg = joutput['errorMessage'] else: if 'errors' in joutput: error_list = joutput['errors'] else: error_list = [joutput['errorCode']] # Build the error message from all the errors. err_msg = error_list[0] for idx in range(1, len(error_list)): err_msg = '%s\n%s' % (err_msg, error_list[idx]) msg = ('%s: Request Error: %s' % (prefix, err_msg)) self.log.error(msg) raise CvpApiError(msg)
def test_main_module_args(mock_module, mock_connect, mock_info, mock_comp, mock_server_conf): ''' Test main module args. ''' mock_module_object = mock.Mock() mock_module_object.params = dict(action='show', switch_name='eos') mock_module_object.fail_json.side_effect = SystemExit('Exiting') mock_module.return_value = mock_module_object mock_connect.return_value = 'Client' mock_info.side_effect = CvpApiError('Error Getting Info') argument_spec = dict( host=dict(required=True), port=dict(required=False, default=None), protocol=dict(default='https', choices=['http', 'https']), username=dict(required=True), password=dict(required=True, no_log=True), server_name=dict(required=True), switch_name=dict(required=True), switch_port=dict(required=True), port_vlan=dict(required=False, default=None), template=dict(require=True), action=dict(default='show', choices=['show', 'add', 'remove']), auto_run=dict(type='bool', default=False), ) assert_raises(SystemExit, cv_server_provision.main) mock_module.assert_called_with(argument_spec=argument_spec, supports_check_mode=False) mock_connect.assert_called_once() mock_info.assert_called_once() mock_comp.assert_not_called() mock_server_conf.assert_not_called() mock_module_object.fail_json.assert_called_with(msg='Error Getting Info')
def test_make_request_response_error(self): """ Test request exception raised from CVP response data. """ self.clnt.session = Mock() self.clnt.session.return_value = True self.clnt.session.get.return_value = Mock() self.clnt._create_session = Mock() self.clnt.NUM_RETRY_REQUESTS = 2 self.clnt.connect_timeout = 2 self.clnt.node_cnt = 2 self.clnt.url_prefix = 'https://1.1.1.1:7777/web' self.clnt._is_good_response = Mock() self.clnt._is_good_response.side_effect = CvpApiError('CvpApiError') self.assertIsNone(self.clnt.last_used_node) with self.assertRaises(CvpApiError): self.clnt._make_request('GET', 'url', 2, {'data': 'data'}) self.assertEqual(self.clnt.last_used_node, '1.1.1.1')
def _is_good_response(self, response, prefix): ''' Check for errors in a response from a GET or POST request. The response argument contains a response object from a GET or POST request. The prefix argument contains the prefix to put into the error message. Raises: CvpApiError: A CvpApiError is raised if there was a JSON error. CvpRequestError: A CvpRequestError is raised if the request is not properly constructed. CvpSessionLogOutError: A CvpSessionLogOutError is raised if response from server indicates session was logged out. ''' if not response.ok: msg = '%s: Request Error: %s' % (prefix, response.reason) self.log.error(msg) raise CvpRequestError(msg) if 'LOG OUT MESSAGE' in response.text: msg = ('%s: Request Error: session logged out' % prefix) raise CvpSessionLogOutError(msg) if 'errorCode' in response.text: joutput = response.json() if 'errorMessage' in joutput: err_msg = joutput['errorMessage'] else: if 'errors' in joutput: error_list = joutput['errors'] else: error_list = [joutput['errorCode']] # Build the error message from all the errors. err_msg = error_list[0] for idx in range(1, len(error_list)): err_msg = '%s\n%s' % (err_msg, error_list[idx]) msg = ('%s: Request Error: %s' % (prefix, err_msg)) self.log.error(msg) raise CvpApiError(msg)