コード例 #1
0
    def test_nobuilds_get_build_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://halob:8080/job/foo/', 'foo', self.J)
        with self.assertRaises(NoBuildData):
            j.get_build_dict()
コード例 #2
0
    def test_nobuilds_get_build_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://halob:8080/job/foo/', 'foo', self.J)
        with self.assertRaises(NoBuildData):
            j.get_build_dict()
コード例 #3
0
    def test_nobuilds_get_build_dict(self):
        j = Job('http://halob:8080/job/look_ma_no_builds/',
                'look_ma_no_builds', self.J)

        ret = j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 0)
コード例 #4
0
    def test_nobuilds_get_build_dict(self):
        j = Job(
            'http://halob:8080/job/look_ma_no_builds/',
            'look_ma_no_builds',
            self.J)

        ret = j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(len(ret), 0)
コード例 #5
0
class TestJob(unittest.TestCase):
    JOB_DATA = {
        "actions": [],
        "description": "test job",
        "displayName": "foo",
        "displayNameOrNull": None,
        "name": "foo",
        "url": "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def setUp(self):

        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_wrong_field__build_id_for_type(self):
        with self.assertRaises(AssertionError):
            self.j._buildid_for_type('wrong')

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_good_buildnumber(self):
        ret = self.j.get_last_good_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_failed_buildnumber(self):
        with self.assertRaises(NoBuildData):
            self.j.get_last_failed_buildnumber()

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_buildnumber(self):
        ret = self.j.get_last_buildnumber()
        self.assertEquals(ret, 4)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_completed_buildnumber(self):
        ret = self.j.get_last_completed_buildnumber()
        self.assertEquals(ret, 3)

    def test_get_build_dict(self):
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(Job, '_poll')
    def test_nobuilds_get_build_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(Job, '_poll')
    def test_nobuilds_get_revision_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(Job, '_poll')
    def test_nobuilds_get_last_build(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://halob:8080/job/foo/', 'foo', self.J)
        with self.assertRaises(NoBuildData):
            j.get_last_build()
コード例 #6
0
class TestJobGetAllBuilds(unittest.TestCase):
    # this job has builds
    JOB1_DATA = {
        "actions": [],
        "description": "test job",
        "displayName": "foo",
        "displayNameOrNull": None,
        "name": "foo",
        "url": "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def setUp(self):
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_dict(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEqual(len(ret), 4)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_incomplete_builds_list_will_call_jenkins_twice(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically, and to have two calls
        # to the Jenkins API
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.J.lazy = False
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_lazy_builds_list_will_not_call_jenkins_twice(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically, and to have two calls
        # to the Jenkins API
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.J.lazy = True
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_complete_builds_list_will_call_jenkins_once(self):
        # The job data contains all builds, so we will not gather remaining
        # builds
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_nobuilds_get_build_dict(self):
        j = Job(
            'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_ids(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically
        ret = list(self.j.get_build_ids())
        self.assertTrue(isinstance(ret, list))
        self.assertEqual(len(ret), 4)
コード例 #7
0
class TestJobGetAllBuilds(unittest.TestCase):
    # this job has builds
    JOB1_DATA = {
        "actions": [],
        "description": "test job",
        "displayName": "foo",
        "displayNameOrNull": None,
        "name": "foo",
        "url": "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def setUp(self):
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_dict(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_incomplete_builds_list_will_call_jenkins_twice(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically, and to have two calls
        # to the Jenkins API
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_complete_builds_list_will_call_jenkins_once(self):
        # The job data contains all builds, so we will not gather remaining
        # builds
        TestJobGetAllBuilds.__get_data_call_count = 0
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_nobuilds_get_build_dict(self):
        j = Job(
            'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_ids(self):
        # The job data contains only one build, so we expect that the
        # remaining jobs will be fetched automatically
        ret = list(self.j.get_build_ids())
        self.assertTrue(isinstance(ret, list))
        self.assertEquals(len(ret), 4)
コード例 #8
0
 def test_nobuilds_get_build_dict(self):
     j = Job('http://halob:8080/job/foo/', 'foo', self.J)
     with self.assertRaises(NoBuildData):
         j.get_build_dict()
コード例 #9
0
class TestJob(unittest.TestCase):
    JOB_DATA = {
        "actions": [{
            "parameterDefinitions": [{
                "defaultParameterValue": {
                    "name": "param1",
                    "value": "test1"
                },
                "description": "",
                "name": "param1",
                "type": "StringParameterDefinition"
            }, {
                "defaultParameterValue": {
                    "name": "param2",
                    "value": ""
                },
                "description": "",
                "name": "param2",
                "type": "StringParameterDefinition"
            }]
        }],
        "description":
        "test job",
        "displayName":
        "foo",
        "displayNameOrNull":
        None,
        "name":
        "foo",
        "url":
        "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def setUp(self):

        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_wrong_field__build_id_for_type(self):
        with self.assertRaises(AssertionError):
            self.j._buildid_for_type('wrong')

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_good_buildnumber(self):
        ret = self.j.get_last_good_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_stable_buildnumber(self):
        ret = self.j.get_last_stable_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_failed_buildnumber(self):
        with self.assertRaises(NoBuildData):
            self.j.get_last_failed_buildnumber()

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_buildnumber(self):
        ret = self.j.get_last_buildnumber()
        self.assertEquals(ret, 4)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_completed_buildnumber(self):
        ret = self.j.get_last_completed_buildnumber()
        self.assertEquals(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_dict(self):
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(JenkinsBase, 'get_data', fake_get_data_tree_empty)
    def test_nobuilds_get_build_dict(self):
        j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_ids(self):
        # We don't want to deal with listreverseiterator here
        # So we convert result to a list
        ret = list(self.j.get_build_ids())
        self.assertTrue(isinstance(ret, list))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(Job, '_poll')
    def test_nobuilds_get_revision_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(Job, '_poll')
    def test_nobuilds_get_last_build(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test__add_missing_builds_not_all_loaded(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test__add_missing_builds_no_first_build(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test__add_missing_builds_no_builds(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_params(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_params_list(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    # @mock.patch.object(JenkinsBase, 'get_data', fakeGetLastBuild)
    def test_get_build(self):
        buildnumber = 1
        with mock.patch('jenkinsapi.job.Build') as build_mock:
            instance = build_mock.return_value
            build = self.j.get_build(buildnumber)
            self.assertEquals(build, instance)
            build_mock.assert_called_with('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_metadata(self):
        buildnumber = 1
        with mock.patch('jenkinsapi.job.Build') as build_mock:
            instance = build_mock.return_value
            build = self.j.get_build_metadata(buildnumber)
            self.assertEquals(build, instance)
            build_mock.assert_called_with('http://halob:8080/job/foo/1/',
                                          buildnumber,
                                          job=self.j,
                                          depth=0)

    def assertJsonEqual(self, jsonA, jsonB, msg=None):
        A = json.loads(jsonA)
        B = json.loads(jsonB)
        self.assertEqual(A, B, msg)

    def test_get_json_for_single_param(self):
        params = {"B": "one two three"}
        expected = '{"parameter": {"name": "B", "value": "one two three"}, "statusCode": "303", "redirectTo": "."}'
        self.assertJsonEqual(Job.mk_json_from_build_parameters(params),
                             expected)

    def test_get_json_for_many_params(self):
        params = {"B": "Honey", "A": "Boo", "C": 2}
        expected = '{"parameter": [{"name": "A", "value": "Boo"}, {"name": "B", "value": "Honey"}, {"name": "C", "value": "2"}], "statusCode": "303", "redirectTo": "."}'

        self.assertJsonEqual(Job.mk_json_from_build_parameters(params),
                             expected)

    def test__mk_json_from_build_parameters(self):
        params = {'param1': 'value1', 'param2': 'value2'}
        result = self.j._mk_json_from_build_parameters(build_params=params)
        self.assertTrue(isinstance(result, dict))

        self.assertEquals(
            result, {
                "parameter": [{
                    "name": "param1",
                    "value": "value1"
                }, {
                    "name": "param2",
                    "value": "value2"
                }]
            })

    def test_wrong_mk_json_from_build_parameters(self):
        with self.assertRaises(AssertionError) as ar:
            self.j.mk_json_from_build_parameters(build_params='bad parameter')

        self.assertEquals(str(ar.exception), 'Build parameters must be a dict')

    def test_get_build_by_params(self):
        build_params = {'param1': 'value1'}
        get_params_mock = mock.Mock(side_effect=({}, {}, build_params))
        build_mock = mock.Mock(get_params=get_params_mock)
        with mock.patch.object(self.j, 'get_first_buildnumber', return_value=1), \
                mock.patch.object(self.j, 'get_last_buildnumber', return_value=3), \
                mock.patch.object(self.j, 'get_build', return_value=build_mock) as get_build_mock:
            result = self.j.get_build_by_params(build_params)
            assert get_build_mock.call_count == 3
            assert get_params_mock.call_count == 3
            assert result == build_mock
コード例 #10
0
class TestJob(unittest.TestCase):
    JOB_DATA = {
        "actions": [{
            "parameterDefinitions": [
            {
                "defaultParameterValue": {
                    "name": "param1",
                    "value": "test1"
                },
                "description": "",
                "name": "param1",
                "type": "StringParameterDefinition"
            },
            {
                "defaultParameterValue": {
                    "name": "param2",
                    "value": ""
                },
                "description": "",
                "name": "param2",
                "type": "StringParameterDefinition"
            }
            ]
        }],
        "description": "test job",
        "displayName": "foo",
        "displayNameOrNull": None,
        "name": "foo",
        "url": "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def setUp(self):

        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_wrong_field__build_id_for_type(self):
        with self.assertRaises(AssertionError):
            self.j._buildid_for_type('wrong')

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_good_buildnumber(self):
        ret = self.j.get_last_good_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_stable_buildnumber(self):
        ret = self.j.get_last_stable_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_failed_buildnumber(self):
        with self.assertRaises(NoBuildData):
            self.j.get_last_failed_buildnumber()

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_buildnumber(self):
        ret = self.j.get_last_buildnumber()
        self.assertEquals(ret, 4)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_completed_buildnumber(self):
        ret = self.j.get_last_completed_buildnumber()
        self.assertEquals(ret, 3)

    def test_get_build_dict(self):
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(Job, '_poll')
    def test_nobuilds_get_build_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(Job, '_poll')
    def test_nobuilds_get_revision_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(Job, '_poll')
    def test_nobuilds_get_last_build(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_empty_field__add_missing_builds(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_params(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_params_list(self, get_data):
        url = 'http://halob:8080/job/foo/%s' % config.JENKINS_API
        get_data.return_value = TestJob.URL_DATA[url].copy()
        j = Job('http://halob:8080/job/foo/', 'foo', self.J)

        params = j.get_params_list()

        self.assertIsInstance(params, list)
        self.assertEquals(len(params), 2)
        self.assertEquals(params, ['param1', 'param2'])
コード例 #11
0
ファイル: test_job.py プロジェクト: Khan/jenkinsapi
 def test_nobuilds_get_build_dict(self):
     j = Job('http://halob:8080/job/foo/', 'foo', self.J)
     with self.assertRaises(NoBuildData):
         j.get_build_dict()
コード例 #12
0
ファイル: test_job.py プロジェクト: Khan/jenkinsapi
class TestJob(unittest.TestCase):
    JOB_DATA = {
        "actions": [{
            "parameterDefinitions": [{
                "defaultParameterValue": {
                    "name": "param1",
                    "value": "test1"
                },
                "description": "",
                "name": "param1",
                "type": "StringParameterDefinition"
            }, {
                "defaultParameterValue": {
                    "name": "param2",
                    "value": ""
                },
                "description": "",
                "name": "param2",
                "type": "StringParameterDefinition"
            }
            ]
        }],
        "description": "test job",
        "displayName": "foo",
        "displayNameOrNull": None,
        "name": "foo",
        "url": "http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def setUp(self):

        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_wrong_field__build_id_for_type(self):
        with self.assertRaises(AssertionError):
            self.j._buildid_for_type('wrong')

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_good_buildnumber(self):
        ret = self.j.get_last_good_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_stable_buildnumber(self):
        ret = self.j.get_last_stable_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_failed_buildnumber(self):
        with self.assertRaises(NoBuildData):
            self.j.get_last_failed_buildnumber()

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_buildnumber(self):
        ret = self.j.get_last_buildnumber()
        self.assertEquals(ret, 4)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetData)
    def test_get_last_completed_buildnumber(self):
        ret = self.j.get_last_completed_buildnumber()
        self.assertEquals(ret, 3)

    @mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_dict(self):
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(JenkinsBase, 'get_data', fake_get_data_tree_empty)
    def test_nobuilds_get_build_dict(self):
        j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_ids(self):
        # We don't want to deal with listreverseiterator here
        # So we convert result to a list
        ret = list(self.j.get_build_ids())
        self.assertTrue(isinstance(ret, list))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(Job, '_poll')
    def test_nobuilds_get_revision_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(Job, '_poll')
    def test_nobuilds_get_last_build(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test__add_missing_builds_not_all_loaded(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test__add_missing_builds_no_first_build(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test__add_missing_builds_no_builds(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_params(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data')
    def test_get_params_list(self, get_data):
        url = 'http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    # @mock.patch.object(JenkinsBase, 'get_data', fakeGetLastBuild)
    def test_get_build(self):
        buildnumber = 1
        with mock.patch('jenkinsapi.job.Build') as build_mock:
            instance = build_mock.return_value
            build = self.j.get_build(buildnumber)
            self.assertEquals(build, instance)
            build_mock.assert_called_with('http://*****:*****@mock.patch.object(JenkinsBase, 'get_data', fakeGetDataTree)
    def test_get_build_metadata(self):
        buildnumber = 1
        with mock.patch('jenkinsapi.job.Build') as build_mock:
            instance = build_mock.return_value
            build = self.j.get_build_metadata(buildnumber)
            self.assertEquals(build, instance)
            build_mock.assert_called_with('http://halob:8080/job/foo/1/',
                                          buildnumber, job=self.j, depth=0)

    def assertJsonEqual(self, jsonA, jsonB, msg=None):
        A = json.loads(jsonA)
        B = json.loads(jsonB)
        self.assertEqual(
            A,
            B,
            msg
        )

    def test_get_json_for_single_param(self):
        params = {"B": "one two three"}
        expected = '{"parameter": {"name": "B", "value": "one two three"}, "statusCode": "303", "redirectTo": "."}'
        self.assertJsonEqual(
            Job.mk_json_from_build_parameters(params),
            expected
        )

    def test_get_json_for_many_params(self):
        params = {"B": "Honey", "A": "Boo", "C": 2}
        expected = '{"parameter": [{"name": "A", "value": "Boo"}, {"name": "B", "value": "Honey"}, {"name": "C", "value": "2"}], "statusCode": "303", "redirectTo": "."}'

        self.assertJsonEqual(
            Job.mk_json_from_build_parameters(params),
            expected
        )

    def test__mk_json_from_build_parameters(self):
        params = {'param1': 'value1', 'param2': 'value2'}
        result = self.j._mk_json_from_build_parameters(build_params=params)
        self.assertTrue(isinstance(result, dict))

        self.assertEquals(
            result,
            {"parameter": [{"name": "param1", "value": "value1"}, {
                "name": "param2", "value": "value2"}]}
        )

    def test_wrong_mk_json_from_build_parameters(self):
        with self.assertRaises(AssertionError) as ar:
            self.j.mk_json_from_build_parameters(build_params='bad parameter')

        self.assertEquals(
            str(ar.exception), 'Build parameters must be a dict')
コード例 #13
0
ファイル: test_job.py プロジェクト: baijum/jenkinsapi
class TestJob(unittest.TestCase):
    JOB_DATA = {
        "actions": [
            {
                "parameterDefinitions": [
                    {
                        "defaultParameterValue": {"name": "param1", "value": "test1"},
                        "description": "",
                        "name": "param1",
                        "type": "StringParameterDefinition",
                    },
                    {
                        "defaultParameterValue": {"name": "param2", "value": ""},
                        "description": "",
                        "name": "param2",
                        "type": "StringParameterDefinition",
                    },
                ]
            }
        ],
        "description": "test job",
        "displayName": "foo",
        "displayNameOrNull": None,
        "name": "foo",
        "url": "http://*****:*****@mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def setUp(self):

        self.J = mock.MagicMock()  # Jenkins object
        self.j = Job("http://*****:*****@mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def test_wrong_field__build_id_for_type(self):
        with self.assertRaises(AssertionError):
            self.j._buildid_for_type("wrong")

    @mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def test_get_last_good_buildnumber(self):
        ret = self.j.get_last_good_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def test_get_last_stable_buildnumber(self):
        ret = self.j.get_last_stable_buildnumber()
        self.assertTrue(ret, 3)

    @mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def test_get_last_failed_buildnumber(self):
        with self.assertRaises(NoBuildData):
            self.j.get_last_failed_buildnumber()

    @mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def test_get_last_buildnumber(self):
        ret = self.j.get_last_buildnumber()
        self.assertEquals(ret, 4)

    @mock.patch.object(JenkinsBase, "get_data", fakeGetData)
    def test_get_last_completed_buildnumber(self):
        ret = self.j.get_last_completed_buildnumber()
        self.assertEquals(ret, 3)

    def test_get_build_dict(self):
        ret = self.j.get_build_dict()
        self.assertTrue(isinstance(ret, dict))
        self.assertEquals(len(ret), 4)

    @mock.patch.object(Job, "_poll")
    def test_nobuilds_get_build_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job("http://*****:*****@mock.patch.object(Job, "_poll")
    def test_nobuilds_get_revision_dict(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job("http://*****:*****@mock.patch.object(Job, "_poll")
    def test_nobuilds_get_last_build(self, _poll):
        # Bare minimum build dict, we only testing dissapearance of 'builds'
        _poll.return_value = {"name": "foo"}

        j = Job("http://*****:*****@mock.patch.object(JenkinsBase, "get_data")
    def test__add_missing_builds_not_all_loaded(self, get_data):
        url = "http://*****:*****@mock.patch.object(JenkinsBase, "get_data")
    def test__add_missing_builds_no_first_build(self, get_data):
        url = "http://*****:*****@mock.patch.object(JenkinsBase, "get_data")
    def test__add_missing_builds_no_builds(self, get_data):
        url = "http://*****:*****@mock.patch.object(JenkinsBase, "get_data")
    def test_get_params(self, get_data):
        url = "http://*****:*****@mock.patch.object(JenkinsBase, "get_data")
    def test_get_params_list(self, get_data):
        url = "http://halob:8080/job/foo/%s" % config.JENKINS_API
        get_data.return_value = TestJob.URL_DATA[url].copy()
        j = Job("http://halob:8080/job/foo/", "foo", self.J)

        params = j.get_params_list()

        self.assertIsInstance(params, list)
        self.assertEquals(len(params), 2)
        self.assertEquals(params, ["param1", "param2"])