示例#1
0
 def test_stop_stuck_builds_failed_to_stop(self, update_desc, stop_build):
     sample_buid_data = sample_data(
         [Pr('1').dict, Pr('2').dict, Pr('3').dict], [])
     build_data = self.timer.get_stuck_builds(sample_buid_data)
     self.timer.stop_stuck_builds(build_data)
     stop_build.assert_called_once_with(2)
     self.assertFalse(update_desc.called)
示例#2
0
 def test_stop_duplicates_failed_to_stop(self, update_desc, stop_build):
     sample_build_data = sample_data(
         [Pr('1').dict, Pr('2').dict, Pr('2').dict, Pr('3').dict], [])
     build_data = self.deduper.get_running_builds(sample_build_data)
     self.deduper.stop_duplicates(build_data)
     stop_build.assert_called_once_with(2)
     self.assertFalse(update_desc.called)
示例#3
0
 def test_stop_stuck_builds_none_stuck(self, update_desc, stop_build):
     sample_build_data = sample_data(
         [Pr('1').dict, Pr('2').dict], [Pr('2').dict])
     build_data = self.timer.get_stuck_builds(sample_build_data)
     self.timer.stop_stuck_builds(build_data)
     self.assertFalse(stop_build.called)
     self.assertFalse(update_desc.called)
示例#4
0
 def test_stop_stuck_builds_none_stuck(self, update_desc, stop_build):
     sample_buid_data = sample_data(
         [Pr('1').dict, Pr('2').dict], [Pr('2').dict])
     build_data = self.timer.get_stuck_builds(sample_buid_data)
     self.timer.stop_stuck_builds(build_data)
     self.assertFalse(stop_build.called)
     self.assertFalse(update_desc.called)
示例#5
0
 def test_stop_per_author_no_duplicates_by_pr(self, update_desc,
                                              stop_build):
     sample_build_data = sample_data(
         [Pr('2', author='foo').dict, Pr('3', author='foo').dict], [])
     build_data = self.deduper.get_running_builds(sample_build_data)
     self.deduper.stop_duplicates(build_data)
     self.assertFalse(stop_build.called)
示例#6
0
 def test_get_stuck_builds_1_building(self):
     data = sample_data(
         [Pr('4').dict],
         [Pr('1').dict, Pr('2').dict,
          Pr('3').dict])
     builds = self.timer.get_stuck_builds(data)
     self.assertEqual(len(builds), 0)
示例#7
0
 def test_stop_duplicates_no_duplicates(self, update_desc, stop_build):
     sample_build_data = sample_data(
         [Pr('1').dict, Pr('2').dict, Pr('3').dict], [Pr('2').dict])
     build_data = self.deduper.get_running_builds(sample_build_data)
     self.deduper.stop_duplicates(build_data)
     self.assertFalse(stop_build.called)
     self.assertFalse(update_desc.called)
示例#8
0
 def test_stop_stuck_builds_failed_to_stop(self, update_desc, stop_build):
     sample_buid_data = sample_data(
         [Pr('1').dict, Pr('2').dict,
          Pr('3').dict], [])
     build_data = self.timer.get_stuck_builds(sample_buid_data)
     self.timer.stop_stuck_builds(build_data)
     stop_build.assert_called_once_with(2)
     self.assertFalse(update_desc.called)
示例#9
0
 def test_stop_duplicates_with_duplicates(self, mock_desc,
                                          update_desc,
                                          stop_build):
     sample_build_data = sample_data(
         [Pr('1').dict, Pr('2').dict, Pr('2').dict, Pr('3').dict], [])
     build_data = self.deduper.get_running_builds(sample_build_data)
     self.deduper.stop_duplicates(build_data)
     stop_build.assert_called_once_with(2)
     update_desc.assert_called_once_with(2, mock_desc())
示例#10
0
    def test_stop_stuck_builds_with_stuck(self, mock_desc, update_desc,
                                          stop_build):
        sample_build_data = sample_data(
            [Pr('0').dict,
             Pr('1').dict,
             Pr('2').dict,
             Pr('3').dict], [])
        build_data = self.timer.get_stuck_builds(sample_build_data)
        self.timer.stop_stuck_builds(build_data)

        stop_build.assert_has_calls([call(3), call(2)], any_order=True)

        update_desc.assert_has_calls(
            [call(2, mock_desc()), call(3, mock_desc())], any_order=True)
示例#11
0
    def test_stop_stuck_builds_with_stuck(self, mock_desc,
                                          update_desc,
                                          stop_build):
        sample_buid_data = sample_data(
            [Pr('0').dict, Pr('1').dict, Pr('2').dict, Pr('3').dict], [])
        build_data = self.timer.get_stuck_builds(sample_buid_data)
        self.timer.stop_stuck_builds(build_data)

        stop_build.assert_has_calls([call(3), call(2)], any_order=True)

        update_desc.assert_hass_calls(
            [call(2, mock_desc()), call(3, mock_desc())],
            any_order=True
        )
示例#12
0
class DeduperTestCase(TestCase):

    """
    TestCase class for testing deduper.py.
    """

    def setUp(self):
        self.job_url = 'http://*****:*****@patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    @patch('jenkins.deduper.GhprbOutdatedBuildAborter._aborted_description',
           return_value='new description')
    def test_stop_duplicates_with_duplicates(self, mock_desc,
                                             update_desc,
                                             stop_build):
        sample_build_data = sample_data(
            [Pr('1').dict, Pr('2').dict, Pr('2').dict, Pr('3').dict], [])
        build_data = self.deduper.get_running_builds(sample_build_data)
        self.deduper.stop_duplicates(build_data)
        stop_build.assert_called_once_with(2)
        update_desc.assert_called_once_with(2, mock_desc())

    @patch('jenkins.job.JenkinsJob.stop_build', side_effect=HTTPError())
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    def test_stop_duplicates_failed_to_stop(self, update_desc, stop_build):
        sample_build_data = sample_data(
            [Pr('1').dict, Pr('2').dict, Pr('2').dict, Pr('3').dict], [])
        build_data = self.deduper.get_running_builds(sample_build_data)
        self.deduper.stop_duplicates(build_data)
        stop_build.assert_called_once_with(2)
        self.assertFalse(update_desc.called)

    @patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    def test_stop_duplicates_no_duplicates(self, update_desc, stop_build):
        sample_build_data = sample_data(
            [Pr('1').dict, Pr('2').dict, Pr('3').dict], [Pr('2').dict])
        build_data = self.deduper.get_running_builds(sample_build_data)
        self.deduper.stop_duplicates(build_data)
        self.assertFalse(stop_build.called)
        self.assertFalse(update_desc.called)

    @patch('jenkins.job.JenkinsJob.get_json', return_value=sample_data(
        [Pr('1').dict, Pr('2', author='a').dict, Pr('2', author='b').dict],
        [Pr('4').dict, Pr('5').dict, Pr('5').dict]))
    @patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    @patch('jenkins.deduper.GhprbOutdatedBuildAborter._aborted_description',
           return_value='new description')
    def test_main_default_per_author(
            self, mock_desc, update_desc, stop_build, get_json):
        args = [
            '-t', self.api_key,
            '-u', self.user,
            '-j', self.job_url,
            '--log-level', 'INFO',
        ]
        deduper_main(args)
        get_json.assert_called_once_with()
        stop_build.assert_called_once_with(2)
        update_desc.assert_called_once_with(2, mock_desc())

    @patch('jenkins.job.JenkinsJob.get_json', return_value=sample_data(
        [Pr('1').dict, Pr('2', author='a').dict, Pr('2', author='b').dict],
        [Pr('4').dict, Pr('5').dict, Pr('5').dict]))
    @patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    def test_main_one_per_author(self, update_desc, stop_build, get_json):
        args = [
            '-t', self.api_key,
            '-u', self.user,
            '-j', self.job_url,
            '--log-level', 'INFO',
            '--one-per-author'
        ]
        deduper_main(args)
        get_json.assert_called_once_with()
        self.assertFalse(stop_build.called)
        self.assertFalse(update_desc.called)
示例#13
0
 def setUp(self):
     self.sample_build_data = sample_data(
         [Pr('2', author='bar').dict],
         []
     )['builds'][0]
示例#14
0
class TimeoutTestCase(TestCase):
    """
    TestCase class for testing timeout.py.
    """
    def setUp(self):
        self.job_url = 'http://localhost:8080/fakejenkins'
        self.user = '******'
        self.api_key = 'apikey'
        job = JenkinsJob(self.job_url, self.user, self.api_key)
        self.timer = BuildTimeout(job, 2)

    @mock_utcnow
    def test_get_stuck_builds_3_building(self):
        data = sample_data(
            [Pr('1').dict, Pr('2').dict,
             Pr('3').dict], [Pr('4').dict])
        builds = self.timer.get_stuck_builds(data)
        self.assertEqual(len(builds), 1)

    @mock_utcnow
    def test_get_stuck_builds_1_building(self):
        data = sample_data(
            [Pr('4').dict],
            [Pr('1').dict, Pr('2').dict,
             Pr('3').dict])
        builds = self.timer.get_stuck_builds(data)
        self.assertEqual(len(builds), 0)

    @mock_utcnow
    def test_get_stuck_builds_none_building(self):
        data = sample_data(
            [], [Pr('1').dict,
                 Pr('2').dict,
                 Pr('3').dict,
                 Pr('4').dict])
        builds = self.timer.get_stuck_builds(data)
        self.assertEqual(len(builds), 0)

    @mock_utcnow
    def test_description(self):
        expected = ("Build #1 automatically aborted because it has "
                    "exceeded the timeout of 3 minutes.")
        returned = self.timer._aborted_description(3, 1)
        self.assertEqual(expected, returned)

    @mock_utcnow
    @patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    @patch('jenkins.timeout.BuildTimeout._aborted_description',
           return_value='new description')
    def test_stop_stuck_builds_with_stuck(self, mock_desc, update_desc,
                                          stop_build):
        sample_build_data = sample_data(
            [Pr('0').dict,
             Pr('1').dict,
             Pr('2').dict,
             Pr('3').dict], [])
        build_data = self.timer.get_stuck_builds(sample_build_data)
        self.timer.stop_stuck_builds(build_data)

        stop_build.assert_has_calls([call(3), call(2)], any_order=True)

        update_desc.assert_has_calls(
            [call(2, mock_desc()), call(3, mock_desc())], any_order=True)

    @mock_utcnow
    @patch('jenkins.job.JenkinsJob.stop_build', side_effect=HTTPError())
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    def test_stop_stuck_builds_failed_to_stop(self, update_desc, stop_build):
        sample_build_data = sample_data(
            [Pr('1').dict, Pr('2').dict,
             Pr('3').dict], [])
        build_data = self.timer.get_stuck_builds(sample_build_data)
        self.timer.stop_stuck_builds(build_data)
        stop_build.assert_called_once_with(2)
        self.assertFalse(update_desc.called)

    @mock_utcnow
    @patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    def test_stop_stuck_builds_none_stuck(self, update_desc, stop_build):
        sample_build_data = sample_data(
            [Pr('1').dict, Pr('2').dict], [Pr('2').dict])
        build_data = self.timer.get_stuck_builds(sample_build_data)
        self.timer.stop_stuck_builds(build_data)
        self.assertFalse(stop_build.called)
        self.assertFalse(update_desc.called)

    @mock_utcnow
    @patch('jenkins.job.JenkinsJob.get_json',
           return_value=sample_data(
               [Pr('1').dict, Pr('2').dict,
                Pr('2').dict],
               [Pr('4').dict, Pr('5').dict,
                Pr('5').dict]))
    @patch('jenkins.job.JenkinsJob.stop_build', return_value=True)
    @patch('jenkins.job.JenkinsJob.update_build_desc', return_value=True)
    @patch('jenkins.timeout.BuildTimeout._aborted_description',
           return_value='new description')
    def test_main(self, mock_desc, update_desc, stop_build, get_json):
        args = [
            '-t',
            self.api_key,
            '-u',
            self.user,
            '-j',
            self.job_url,
            '--log-level',
            'INFO',
            '--timeout',
            '2',
        ]

        timeout_main(args)
        get_json.assert_called_once_with()
        stop_build.assert_called_once_with(2)
        update_desc.assert_called_once_with(2, mock_desc())
示例#15
0
 def test_stop_per_author_with_duplicates(self, update_desc, stop_build):
     sample_build_data = sample_data(
         [Pr('2', author='foo').dict, Pr('2', author='foo').dict], [])
     build_data = self.deduper.get_running_builds(sample_build_data)
     self.deduper.stop_duplicates(build_data)
     stop_build.assert_called_once_with(1)
示例#16
0
 def test_get_running_builds_1_building(self):
     data = sample_data(
         [Pr('4').dict], [Pr('1').dict, Pr('2').dict, Pr('3').dict])
     builds = self.deduper.get_running_builds(data)
     self.assertEqual(len(builds), 1)
示例#17
0
 def test_get_stuck_builds_none_building(self):
     data = sample_data(
         [], [Pr('1').dict, Pr('2').dict, Pr('3').dict, Pr('4').dict])
     builds = self.timer.get_stuck_builds(data)
     self.assertEqual(len(builds), 0)
示例#18
0
 def test_get_json_ok(self):
     data = sample_data([Pr('1').dict], [])
     with patch('requests.get', return_value=mock_response(200, data)):
         response = self.job.get_json()
         self.assertEqual(data, response)
示例#19
0
 def setUp(self):  # pylint: disable=super-method-not-called
     self.sample_build_data = sample_data([Pr('2', author='bar').dict],
                                          [])['builds'][0]
示例#20
0
 def test_get_running_builds_none_building(self):
     data = sample_data(
         [], [Pr('1').dict, Pr('2').dict, Pr('3').dict, Pr('4').dict])
     builds = self.deduper.get_running_builds(data)
     self.assertEqual(len(builds), 0)
示例#21
0
 def test_get_json_ok(self):
     data = sample_data([Pr('1').dict], [])
     with patch('requests.get', return_value=mock_response(200, data)):
         response = self.job.get_json()
         self.assertEqual(data, response)
示例#22
0
 def setUp(self):
     self.sample_build_data = sample_data([Pr('2', author='bar').dict],
                                          [])['builds'][0]