def test_check_syntax_of_an_invalid_file(self, session_send_mock): invalid_jenkinsfile_content = """node { "pwd" } """ invalid_response = { "status": "ok", "data": { "result": "failure", "errors": [{ "error": [ "Unknown stage section \"stexps\". Starting with version 0.5, steps in a stage must be" + " in a \"steps\" block. @ line 5, column 9.", "Expected one of \"steps\", \"stages\", or \"parallel\" for stage \"blah\" @ line 5, column 9." ] }] } } session_send_mock.side_effect = iter([ build_response_mock(200, self.crumb_data), # crumb build_response_mock(200, invalid_response) # request ]) response = self.j.check_jenkinsfile_syntax(invalid_jenkinsfile_content) self.assertEqual(response, invalid_response["data"]["errors"])
def test_return_none(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) ret = self.j.get_build_test_report(u'TestJob', number=52) self.assertIsNone(ret)
def test_create_new_folder_that_already_exists_ignoring_errors(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(200, self.crumb_data), # crumb build_response_mock(400), # request ]) self.j.create_folder(u'Folder-that-already-exists', ignore_failures=True)
def test_in_folder_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_build_info(u'a Folder/TestJob', number=52) self.assertEqual(str(context_manager.exception), 'job[a Folder/TestJob] number[52] does not exist')
def test_in_folder_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_build_info(u'a Folder/TestJob', number=52) self.assertEqual( str(context_manager.exception), 'job[a Folder/TestJob] number[52] does not exist')
def test_in_folder_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(401, reason="Not Authorised"), # crumb build_response_mock(401, reason="Not Authorised"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_build_stages(u'a Folder/TestJob', number=52) self.assertEqual( str(context_manager.exception), 'Error in request. Possibly authentication failed [401]: Not Authorised')
def test_in_folder_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(401, reason="Not Authorised"), # crumb build_response_mock(401, reason="Not Authorised"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_build_test_report(u'a Folder/TestJob', number=52) self.assertEqual( str(context_manager.exception), 'Error in request. Possibly authentication failed [401]: Not Authorised')
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(499, reason="Unhandled Error"), # request ]) with self.assertRaises(jenkins.BadHTTPException) as context_manager: self.j.get_version() self.assertEqual( str(context_manager.exception), 'Error communicating with server[{0}/]'.format(self.base_url))
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(401, reason="Basic Auth Failed"), # request ]) with self.assertRaises(jenkins.JenkinsException): self.j.get_whoami() self.assertEqual( session_send_mock.call_args_list[1][0][1].url, self.make_url('me/api/json'))
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_job_info(u'TestJob') self.assertEqual(session_send_mock.call_args_list[1][0][1].url, self.make_url('job/TestJob/api/json?depth=0')) self.assertEqual(str(context_manager.exception), 'job[TestJob] does not exist')
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_build_console_output(u'TestJob', number=52) self.assertEqual(session_send_mock.call_args_list[1][0][0].url, self.make_url('job/TestJob/52/consoleText')) self.assertEqual(str(context_manager.exception), 'job[TestJob] number[52] does not exist')
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(499, reason="Unhandled Error"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_node_info('test_node') self.assertEqual(session_send_mock.call_args_list[1][0][1].url, self.make_url('computer/test_node/api/json?depth=0')) self.assertEqual(str(context_manager.exception), 'node[test_node] does not exist')
def test_create_new_folder_that_already_exists(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(200, self.crumb_data), # crumb build_response_mock(400), # request ]) folder_name = u'Folder-that-already-exists' with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.create_folder(folder_name) self.assertEqual( str(context_manager.exception), u'Error creating folder [' + folder_name + ']. Probably it already exists.')
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(499, reason="Unhandled Error"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_nodes() self.assertEqual(session_send_mock.call_args_list[1][0][1].url, self.make_url('computer/api/json')) self.assertEqual( str(context_manager.exception), 'Error communicating with server[{0}]'.format(self.make_url('')))
def test_in_folder_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_build_console_output(u'a Folder/TestJob', number=52) self.assertEqual( session_send_mock.call_args_list[1][0][0].url, self.make_url('job/a%20Folder/job/TestJob/52/consoleText')) self.assertEqual( str(context_manager.exception), 'job[a Folder/TestJob] number[52] does not exist')
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(499, reason="Unhandled Error"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_node_info('test_node') self.assertEqual( session_send_mock.call_args_list[1][0][1].url, self.make_url('computer/test_node/api/json?depth=0')) self.assertEqual( str(context_manager.exception), 'node[test_node] does not exist')
def test_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(499, reason="Unhandled Error"), # request ]) with self.assertRaises(jenkins.BadHTTPException) as context_manager: self.j.get_info() self.assertEqual( session_send_mock.call_args_list[1][0][1].url, self.make_url('api/json')) self.assertEqual( str(context_manager.exception), 'Error communicating with server[{0}]'.format(self.make_url('')))
def test_simple(self, session_send_mock): data = {'foo': 'bar'} session_send_mock.side_effect = iter([ build_response_mock(200, self.crumb_data), build_response_mock(200, data), ]) request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) response = self.j.jenkins_open(request) self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/TestJob')) self.assertEqual(response, json.dumps(data)) self.assertEqual(self.j.crumb, self.crumb_data) self.assertEqual(request.headers['.crumb'], self.crumb_data['crumb'])
def test_get_view_jobs_raise_HTTPError(self, session_send_mock): session_send_mock.side_effect = iter([ build_response_mock(404, reason="Not Found"), # crumb build_response_mock(404, reason="Not Found"), # request ]) with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.get_jobs(view_name=u'Test View') self.assertEqual( session_send_mock.call_args_list[1][0][1].url, self.make_url( 'view/Test%20View/api/json?tree=jobs[url,color,name]')) self.assertEqual(str(context_manager.exception), 'view[Test View] does not exist')
def test_simple(self, session_send_mock): data = {'foo': 'bar'} session_send_mock.side_effect = iter([ build_response_mock(200, self.crumb_data), build_response_mock(200, data), ]) request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) response = self.j.jenkins_open(request) self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url('job/TestJob')) self.assertEqual(response, json.dumps(data)) self.assertEqual(self.j.crumb, self.crumb_data) self.assertEqual(request.headers['.crumb'], self.crumb_data['crumb'])
def test_return_without_version_header(self, session_send_mock): session_send_mock.return_value = build_response_mock(0) with self.assertRaises(KeyError) as context_manager: self.j.get_version() self.assertEqual( str(context_manager.exception), "'x-jenkins'")
def test_return_empty_response(self, session_send_mock): session_send_mock.return_value = build_response_mock(0) with self.assertRaises(jenkins.EmptyResponseException) as context_manager: self.j.get_version() self.assertEqual( str(context_manager.exception), 'Error communicating with server[{0}/]:' ' empty response'.format(self.base_url))
def test_with_token(self, session_send_mock): session_send_mock.return_value = build_response_mock( 302, {}, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job(u'TestJob', token='some_token') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/TestJob/build?token=some_token')) self.assertEqual(queue_id, 25)
def test_simple(self, session_send_mock): session_send_mock.return_value = build_response_mock( 302, {}, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job(u'Test Jøb') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url(quote(u'job/Test Jøb/build'.encode('utf8')))) self.assertEqual(queue_id, 25)
def test_raise_HTTPError(self, session_send_mock): session_send_mock.return_value = build_response_mock( 499, reason="Unhandled Error") with self.assertRaises(jenkins.BadHTTPException) as context_manager: self.j.get_plugins_info(depth=52) self.assertEqual( str(context_manager.exception), 'Error communicating with server[{0}/]'.format(self.base_url))
def test_in_folder_with_token(self, session_send_mock): session_send_mock.return_value = build_response_mock( 302, {}, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job(u'a Folder/TestJob', token='some_token') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/a%20Folder/job/TestJob/build?token=some_token')) self.assertEqual(queue_id, 25)
def test_in_folder(self, session_send_mock): session_send_mock.return_value = build_response_mock( 302, {}, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job(u'a Folder/Test Job') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/a%20Folder/job/Test%20Job/build')) self.assertEqual(queue_id, 25)
def test_assert_no_location(self, session_send_mock): session_send_mock.return_value = build_response_mock(302, {}) with self.assertRaises(jenkins.EmptyResponseException) as context_mgr: self.j.build_job(u'Test Job') self.assertEqual( str(context_mgr.exception), "Header 'Location' not found in response from server[{0}]".format( self.make_url('')))
def test_simple(self, session_send_mock): session_send_mock.return_value = build_response_mock( 302, {}, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job(u'Test Jøb') self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url(quote(u'job/Test Jøb/build'.encode('utf8')))) self.assertEqual(queue_id, 25)
def test_add_header(self, session_send_mock): session_send_mock.return_value = build_response_mock( 404, reason="Not Found") request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob') with patch.dict('os.environ', {"JENKINS_API_EXTRA_HEADERS": "X-Auth: 123\nX-Key: 234"}): j = jenkins.Jenkins(self.base_url, 'test', 'test') request = j._session.prepare_request(request) self.assertEqual(request.headers["X-Auth"], "123") self.assertEqual(request.headers["X-Key"], "234")
def test_response_404(self, session_send_mock): request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) session_send_mock.return_value = build_response_mock( 404, reason="basic auth failed") with self.assertRaises(jenkins.NotFoundException) as context_manager: self.j.jenkins_open(request, add_crumb=False) self.assertEqual(str(context_manager.exception), 'Requested item could not be found') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/TestJob'))
def test_simple(self, session_send_mock): session_send_mock.return_value = build_response_mock( 404, reason="Not Found") request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob') with patch.dict('os.environ', {}): j = jenkins.Jenkins(self.base_url, 'test', 'test') request = j._session.prepare_request(request) self.assertEqual(request.headers, self.j._session.headers) self.assertNotIn("X-Auth", request.headers)
def test_simple(self, session_send_mock): session_send_mock.return_value = build_response_mock( 404, reason="Not Found") request = jenkins.requests.Request('http://example.com/job/TestJob') self.j.maybe_add_crumb(request) self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('crumbIssuer/api/json')) self.assertFalse(self.j.crumb) self.assertFalse('.crumb' in request.headers)
def test_response_501(self, session_send_mock): request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) session_send_mock.return_value = build_response_mock( 501, reason="Not implemented") with self.assertRaises(jenkins.req_exc.HTTPError) as context_manager: self.j.jenkins_open(request, add_crumb=False) self.assertEqual(str(context_manager.exception), '501 Server Error: Not implemented for url: None') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/TestJob'))
def test_simple_no_content_lenght(self, session_send_mock, maybe_add_crumb_mock): maybe_add_crumb_mock.return_value = None session_send_mock.return_value = build_response_mock( 201, None, add_content_length=False, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job(u'Test Job') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/Test%20Job/build')) self.assertEqual(queue_id, 25)
def test_with_data(self, session_send_mock): session_send_mock.return_value = build_response_mock( 200, self.crumb_data) request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob') self.j.maybe_add_crumb(request) self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url('crumbIssuer/api/json')) self.assertEqual(self.j.crumb, self.crumb_data) self.assertEqual(request.headers['.crumb'], self.crumb_data['crumb'])
def test_simple(self, session_send_mock): session_send_mock.return_value = build_response_mock( 404, reason="Not Found") request = jenkins.requests.Request('http://example.com/job/TestJob') self.j.maybe_add_crumb(request) self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url('crumbIssuer/api/json')) self.assertFalse(self.j.crumb) self.assertFalse('.crumb' in request.headers)
def test_with_data(self, session_send_mock): session_send_mock.return_value = build_response_mock( 200, self.crumb_data) request = jenkins.requests.Request('GET', 'http://example.com/job/TestJob') self.j.maybe_add_crumb(request) self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('crumbIssuer/api/json')) self.assertEqual(self.j.crumb, self.crumb_data) self.assertEqual(request.headers['.crumb'], self.crumb_data['crumb'])
def test_response_404(self, session_send_mock): request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) session_send_mock.return_value = build_response_mock( 404, reason="basic auth failed") with self.assertRaises(jenkins.NotFoundException) as context_manager: self.j.jenkins_open(request, add_crumb=False) self.assertEqual( str(context_manager.exception), 'Requested item could not be found') self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url('job/TestJob'))
def test_response_501(self, session_send_mock): request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) session_send_mock.return_value = build_response_mock( 501, reason="Not implemented") with self.assertRaises(jenkins.req_exc.HTTPError) as context_manager: self.j.jenkins_open(request, add_crumb=False) self.assertEqual( str(context_manager.exception), '501 Server Error: Not implemented for url: None') self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url('job/TestJob'))
def test_with_parameters_and_token(self, session_send_mock): session_send_mock.return_value = build_response_mock( 302, {}, headers={'Location': self.make_url('/queue/item/25/')}) queue_id = self.j.build_job( u'TestJob', parameters={'when': 'now', 'why': 'because I felt like it'}, token='some_token') self.assertTrue('token=some_token' in session_send_mock.call_args[0][1].url) self.assertTrue('when=now' in session_send_mock.call_args[0][1].url) self.assertTrue('why=because+I+felt+like+it' in session_send_mock.call_args[0][1].url) self.assertEqual(queue_id, 25)
def test_assert_no_content_lenght_no_location(self, session_send_mock, maybe_add_crumb_mock): maybe_add_crumb_mock.return_value = None session_send_mock.return_value = build_response_mock( 201, None, add_content_length=False) with self.assertRaises(jenkins.EmptyResponseException) as context_mgr: self.j.build_job(u'Test Job') self.assertEqual( str(context_mgr.exception), 'Error communicating with server[{0}]: empty response'.format( self.make_url('')))
def test_empty_response(self, session_send_mock): request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) session_send_mock.return_value = build_response_mock( 401, reason="basic auth failed") with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.jenkins_open(request, False) self.assertEqual( str(context_manager.exception), 'Error in request. Possibly authentication failed [401]: ' 'basic auth failed') self.assertEqual(session_send_mock.call_args[0][1].url, self.make_url('job/TestJob'))
def test_assert_no_content_lenght_no_location(self, session_send_mock, maybe_add_crumb_mock): maybe_add_crumb_mock.return_value = None session_send_mock.return_value = build_response_mock( 201, None, add_content_length=False) with self.assertRaises(jenkins.EmptyResponseException) as context_mgr: self.j.build_job(u'Test Job') self.assertEqual( str(context_mgr.exception), "Header 'Location' not found in response from server[{0}]".format( self.make_url('')))
def test_empty_response(self, session_send_mock): request = jenkins.requests.Request('GET', self.make_url('job/TestJob')) session_send_mock.return_value = build_response_mock( 401, reason="basic auth failed") with self.assertRaises(jenkins.JenkinsException) as context_manager: self.j.jenkins_open(request, False) self.assertEqual( str(context_manager.exception), 'Error in request. Possibly authentication failed [401]: ' 'basic auth failed') self.assertEqual( session_send_mock.call_args[0][1].url, self.make_url('job/TestJob'))
def test_some_version(self, session_send_mock): session_send_mock.return_value = build_response_mock( 200, headers={'X-Jenkins': 'Version42', 'Content-Length': 0}) self.assertEqual(self.j.get_version(), 'Version42')