Example #1
0
class JenkinsSessionTest(unittest.TestCase):

    def setUp(self) -> None:
        self.jenkins = JenkinsFace()

    def test_delete_snapshot_with_delete_key(self):
        self.jenkins.create_session_jenkins('a', 'a', 'a', 'a')
        self.assertIsNotNone(self.jenkins._session)

    if __name__ == '__main__':
        unittest.main()
class JenkinsSessionTest(unittest.TestCase):

    def setUp(self) -> None:
        self.jenkins = JenkinsFace()

    def test_create_session_success(self):
        self.jenkins.create_session_jenkins('http', 'localhost:8080', 'username', 'password', False)
        session = self.jenkins._session
        settings = self.jenkins._settings
        endpoint = self.jenkins._endpoint
        self.assertIsNotNone(session)
        self.assertFalse(settings['verify'])
        self.assertFalse(settings['allow_redirects'])
        self.assertEqual('http://*****:*****@localhost:8080/', endpoint)

    def test_create_session_fail_when_url_is_none(self):
        raised = False
        try:
            self.jenkins.create_session_jenkins('http', None, 'username', 'password', False)
        except:
            raised = True
        self.assertTrue(raised)

    def test_create_session_fail_when_username_is_none(self):
        raised = False
        try:
            self.jenkins.create_session_jenkins('http', 'localhost:8080', None, 'password', False)
        except:
            raised = True
        self.assertTrue(raised)

    def test_create_session_fail_when_password_is_none(self):
        raised = False
        try:
            self.jenkins.create_session_jenkins('http', 'localhost:8080', 'username', None, False)
        except:
            raised = True
        self.assertTrue(raised)
 def setUp(self) -> None:
     self.jenkins = JenkinsFace()
class GetJenkinsJobTest(unittest.TestCase):
    def setUp(self) -> None:
        self.jenkins = JenkinsFace()

    @requests_mock.Mocker()
    def test_get_jenkins_job_success(self, mock):
        mock.get('http://*****:*****@localhost:8080/job/test/api/json',
                 json={
                     "_class": "",
                     "actions": [],
                     "description": None,
                     "displayName": "",
                     "displayNameOrNull": "",
                     "name": "",
                     "url": "http://localhost:8080/job/test/",
                     "buildable": True,
                     "builds": [],
                     "color": "blue",
                     "firstBuild": {},
                     "healthReport": [],
                     "inQueue": False,
                     "keepDependencies": False,
                     "lastBuild": {},
                     "lastCompletedBuild": {},
                     "lastFailedBuild": {},
                     "lastStableBuild": {},
                     "lastSuccessfulBuild": {},
                     "lastUnstableBuild": None,
                     "lastUnsuccessfulBuild": {},
                     "nextBuildNumber": 1,
                     "property": [],
                     "queueItem": None,
                     "concurrentBuild": False
                 })

        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        result = self.jenkins.get_jenkins_job('job/test')

        self.assertEqual(result['url'], 'http://localhost:8080/job/test/')

    def test_get_jenkins_job_fail_when_name_is_none(self):
        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        raised = False
        try:
            self.jenkins.get_jenkins_job()
        except:
            raised = True
        self.assertTrue(raised)

    def test_get_jenkins_job_fail_when_name_is_empty(self):
        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        raised = False
        try:
            self.jenkins.get_jenkins_job("")
        except:
            raised = True
        self.assertTrue(raised)

    @requests_mock.Mocker()
    def test_get_jenkins_job_fail_when_exception(self, mock):
        mock.get('http://*****:*****@localhost:8080/job/test/api/json',
                 exc=requests.exceptions.HTTPError)

        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        raised = False

        try:
            self.jenkins.get_jenkins_job('job/test')
        except requests.exceptions.HTTPError:
            raised = True

        self.assertTrue(raised)
Example #5
0
class BuildJenkinsWithParametersAndWaitUntilJobDoneTest(unittest.TestCase):
    def setUp(self) -> None:
        self.jenkins = JenkinsFace()

    @requests_mock.Mocker()
    def test_build_jenkins_with_parameters_and_wait_until_job_done_success(
            self, mock):
        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/api/json',
            json={
                "_class": "",
                "actions": [],
                "description": None,
                "displayName": "",
                "displayNameOrNull": "",
                "name": "",
                "url": "http://*****:*****@localhost:8080/job/folder_name/job/test_job/buildWithParameters',
            status_code=200)

        mock.register_uri(
            'GET',
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/1/api/json',
            [{
                'status_code': 404
            }, {
                'json': {
                    "_class": "",
                    "actions": [],
                    "description": None,
                    "displayName": "",
                    "displayNameOrNull": "",
                    "name": "",
                    "url":
                    "http://localhost:8080/job/folder_name/job/test_job/",
                    "buildable": True,
                    "builds": [],
                    "color": "blue",
                    "firstBuild": {},
                    "healthReport": [],
                    "inQueue": False,
                    "keepDependencies": False,
                    "lastBuild": {},
                    "lastCompletedBuild": {},
                    "lastFailedBuild": {},
                    "lastStableBuild": {},
                    "lastSuccessfulBuild": {},
                    "lastUnstableBuild": None,
                    "lastUnsuccessfulBuild": {},
                    "nextBuildNumber": 2,
                    "property": [],
                    "queueItem": None,
                    "concurrentBuild": False,
                    "building": False
                },
                'status_code': 200
            }])

        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)

        job_build_details = self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
            'folder_name/test_job', 'data_test', retry=5, retry_interval=1)
        self.assertEqual(job_build_details['nextBuildNumber'], 2)

    @requests_mock.Mocker()
    def test_build_jenkins_with_parameters_and_wait_until_job_done_when_http_exception_but_ignore_exception(
            self, mock):
        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/api/json',
            json={
                "_class": "",
                "actions": [],
                "description": None,
                "displayName": "",
                "displayNameOrNull": "",
                "name": "",
                "url": "http://*****:*****@localhost:8080/job/folder_name/job/test_job/buildWithParameters',
            status_code=200)

        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/6/api/json',
            status_code=500)

        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)

        job_build_details = self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
            'folder_name/test_job',
            'data_test',
            retry=1,
            retry_interval=1,
            ignore_exception=True)
        self.assertEqual(job_build_details, None)

    def test_build_jenkins_with_parameters_and_wait_until_job_done_fail_when_name_is_none(
            self):
        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        try:
            self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
            )
        except Exception as error:
            self.assertEqual(str(error), 'Job name should not be None')

    def test_build_jenkins_with_parameters_and_wait_until_job_done_when_name_is_empty(
            self):
        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        try:
            self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
                "")
        except Exception as error:
            self.assertEqual(str(error), 'Job name should not be None')

    @requests_mock.Mocker()
    def test_build_jenkins_with_parameters_and_wait_until_job_done_when_http_exception(
            self, mock):
        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/api/json',
            json={
                "_class": "",
                "actions": [],
                "description": None,
                "displayName": "",
                "displayNameOrNull": "",
                "name": "",
                "url": "http://*****:*****@localhost:8080/job/folder_name/job/test_job/buildWithParameters',
            status_code=200)

        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/6/api/json',
            status_code=500)

        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)

        try:
            self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
                'folder_name/test_job',
                'data_test',
                retry=1,
                ignore_exception=False)
        except requests.exceptions.HTTPError as err:
            self.assertTrue(err.response.status_code == 500)

    @requests_mock.Mocker()
    def test_build_jenkins_with_parameters_and_wait_until_job_done_when_exception(
            self, mock):
        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/api/json',
            json={
                "_class": "",
                "actions": [],
                "description": None,
                "displayName": "",
                "displayNameOrNull": "",
                "name": "",
                "url": "http://*****:*****@localhost:8080/job/folder_name/job/test_job/buildWithParameters',
            status_code=200)

        mock.get(
            'http://*****:*****@localhost:8080/job/folder_name/job/test_job/1/api/json',
            exc=requests.exceptions.ConnectionError)

        self.jenkins.create_session_jenkins('http', 'localhost:8080',
                                            'username', 'password', False)
        raised = False

        try:
            self.jenkins.build_jenkins_with_parameters_and_wait_until_job_done(
                name='folder_name/test_job',
                data='data_test',
                ignore_exception=False)
        except requests.exceptions.ConnectionError:
            raised = True

        self.assertTrue(raised)