def testNoVerification(self):
     try:
         # Need at least one verification.
         self._get_pc([], [])
         self.fail()
     except AssertionError:
         pass
     try:
         # Cannot have the same verifier two times.
         self._get_pc([fake.FakeVerifier(base.SUCCEEDED)],
                      [fake.FakeVerifier(base.SUCCEEDED)])
         self.fail()
     except AssertionError:
         pass
 def _check_4(self, f1, f2, f3, f4):
     issue = 31337
     fake1 = fake.FakeVerifier(f1)
     fake1.name = 'fake1'
     fake2 = fake.FakeVerifier(f2)
     fake2.name = 'fake2'
     fake3 = fake.FakeVerifier(f3)
     fake3.name = 'fake3'
     fake4 = fake.FakeVerifier(f4)
     fake4.name = 'fake4'
     nb = 1
     if f1 is base.SUCCEEDED:
         nb = 2
         if f2 is base.SUCCEEDED:
             nb = 3
             if f3 is base.SUCCEEDED:
                 nb = 4
     pc = self._get_pc([fake1, fake2], [fake3, fake4])
     self.assertEqual(len(pc.queue.iterate()), 0)
     pc.look_for_new_pending_commit()
     self.assertEqual(len(pc.queue.iterate()), 1)
     commit = pc.queue.get(issue)
     self.assertEqual(len(commit.verifications), 0)
     pc.process_new_pending_commit()
     if not all(f == base.SUCCEEDED for f in (f1, f2, f3, f4)):
         self.assertEqual([], pc.queue.iterate())
     else:
         commit = pc.queue.get(issue)
         self.assertEqual(commit.verifications['fake1'].get_state(), f1)
         self.assertEqual(commit.verifications['fake2'].get_state(), f2)
         self.assertEqual(commit.verifications['fake3'].get_state(), f3)
         self.assertEqual(commit.verifications['fake4'].get_state(), f4)
         self.assertEqual(len(commit.verifications), nb)
     pc.update_status()
     if not all(f == base.SUCCEEDED for f in (f1, f2, f3, f4)):
         self.assertEqual([], pc.queue.iterate())
     else:
         commit = pc.queue.get(issue)
         self.assertEqual(commit.verifications['fake1'].get_state(), f1)
         self.assertEqual(commit.verifications['fake2'].get_state(), f2)
         self.assertEqual(commit.verifications['fake3'].get_state(), f3)
         self.assertEqual(commit.verifications['fake4'].get_state(), f4)
         self.assertEqual(len(commit.verifications), nb)
     self._check_standard_verification(
         pc, all(x == base.SUCCEEDED for x in (f1, f2, f3, f4)), False)
     if all(x == base.SUCCEEDED for x in (f1, f2, f3, f4)):
         self.context.status.check_names(['initial', 'why not', 'commit'])
     else:
         self.context.status.check_names(['initial', 'abort'])
    def testPatchDiscardThrows(self):
        # Handle HTTPError correctly.
        result = []
        issue = 31337
        pc = self._get_pc([], [fake.FakeVerifier(base.FAILED)])

        def set_flag_throw(_issue, _patchset, _flag, _value):
            raise urllib2.HTTPError(None, None, None, None, None)

        def send_stack(*_args, **_kwargs):
            result.append(True)

        self.mock(breakpad, 'SendStack', send_stack)
        self.mock(traceback, 'print_stack', lambda: None)
        self.mock(logging, 'error', lambda _: None)
        pc.context.rietveld.set_flag = set_flag_throw

        self.assertEqual(len(pc.queue.iterate()), 0)
        pc.look_for_new_pending_commit()
        self.assertEqual(len(pc.queue.iterate()), 1)
        commit = pc.queue.get(issue)
        self.assertEqual(len(commit.verifications), 0)
        pc.process_new_pending_commit()
        self.assertEqual([], pc.queue.iterate())
        pc.update_status()
        self.assertEqual([], pc.queue.iterate())
        self.context.checkout.check_calls([
            'prepare(None)',
            'apply_patch(%r)' % (self.context.rietveld.patchsets[0], ),
        ])
        self.context.rietveld.check_calls([
            _try_comment(),
            "add_comment(%d, %r)" % (issue, pc.FAILED_NO_MESSAGE),
        ])
        self.context.status.check_names(['initial', 'abort'])
    def testLoadSave(self):
        pc = pending_manager.PendingManager(
            context.Context(None, None, mocks.AsyncPushMock(self)),
            [fake.FakeVerifier(base.SUCCEEDED)], [])
        filename = os.path.join(self.root_dir, 'foo.json')
        empty = """{
  "__persistent_type__": "PendingQueue",
  "pending_commits": {}
}
"""
        write(filename, empty)
        try:
            pc.load(filename)
            self.assertEqual(pc.queue.pending_commits, {})
            pc.save(filename)
            self.assertEqual(trim(empty), trim(read(filename)))
        finally:
            os.remove(filename)
            if os.path.exists(filename + '.old'):
                os.remove(filename + '.old')
    def testCommitBurst(self):
        issue = 31337
        pc = self._get_pc([fake.FakeVerifier(base.SUCCEEDED)], [])
        self.assertEqual(4, pc.MAX_COMMIT_BURST)
        timestamp = [1]
        self.mock(time, 'time', lambda: timestamp[-1])
        for i in range(pc.MAX_COMMIT_BURST + 2):
            self.context.rietveld.issues[i] = (
                self.context.rietveld.issues[issue].copy())
            self.context.rietveld.issues[i]['issue'] = i
        pc.look_for_new_pending_commit()
        self.assertEqual(len(pc.queue.iterate()), pc.MAX_COMMIT_BURST + 3)
        pc.process_new_pending_commit()
        pc.update_status()
        pc.scan_results()
        self.context.checkout.check_calls(
            self._prepare_apply_commit(0, 0) +
            self._prepare_apply_commit(1, 1) +
            self._prepare_apply_commit(2, 2) +
            self._prepare_apply_commit(3, 3))
        self.context.rietveld.check_calls([
            _try_comment(0),
            _try_comment(1),
            _try_comment(2),
            _try_comment(3),
            _try_comment(4),
            _try_comment(5),
            _try_comment(),
            'close_issue(0)',
            "update_description(0, u'foo')",
            "add_comment(0, 'Change committed as 125')",
            'close_issue(1)',
            "update_description(1, u'foo')",
            "add_comment(1, 'Change committed as 125')",
            'close_issue(2)',
            "update_description(2, u'foo')",
            "add_comment(2, 'Change committed as 125')",
            'close_issue(3)',
            "update_description(3, u'foo')",
            "add_comment(3, 'Change committed as 125')",
        ])
        self.assertEqual(3, len(pc.queue.iterate()))
        total = pc.MAX_COMMIT_BURST + 3
        self.context.status.check_names(['initial'] * total +
                                        (['why not', 'commit'] *
                                         pc.MAX_COMMIT_BURST) +
                                        ['why not'] * 3)

        # Dry run.
        pc.scan_results()
        self.context.checkout.check_calls([])
        self.context.rietveld.check_calls([])
        self.context.status.check_names(['why not'] * 3)

        # Remove one item from the burst.
        pc.recent_commit_timestamps.pop()
        pc.scan_results()
        next_item = pc.MAX_COMMIT_BURST
        self.context.checkout.check_calls(
            self._prepare_apply_commit(next_item, next_item))
        self.context.rietveld.check_calls([
            'close_issue(%d)' % next_item,
            "update_description(%d, u'foo')" % next_item,
            "add_comment(%d, 'Change committed as 125')" % next_item,
        ])
        self.context.status.check_names(['why not', 'commit'] +
                                        ['why not'] * 2)
        # After a delay, must flush the queue.
        timestamp.append(timestamp[-1] + pc.COMMIT_BURST_DELAY + 1)
        pc.scan_results()
        self.context.checkout.check_calls(
            self._prepare_apply_commit(next_item + 1, next_item + 1) +
            self._prepare_apply_commit(next_item + 2, issue))
        self.context.rietveld.check_calls([
            'close_issue(%d)' % (next_item + 1),
            "update_description(%d, u'foo')" % (next_item + 1),
            "add_comment(%d, 'Change committed as 125')" % (next_item + 1),
            'close_issue(%d)' % issue,
            "update_description(%d, u'foo')" % issue,
            "add_comment(%d, 'Change committed as 125')" % issue
        ])
        self.context.status.check_names(['why not', 'commit'] * 2)
 def testPatchVerificationFail(self):
     pc = self._get_pc([], [fake.FakeVerifier(base.FAILED)])
     self._check_1(pc, base.FAILED)
 def testPatchVerification(self):
     pc = self._get_pc([], [fake.FakeVerifier(base.SUCCEEDED)])
     self._check_1(pc, base.SUCCEEDED)
Beispiel #8
0
 def testCommitBurst(self):
     pc = self._get_pc([fake.FakeVerifier(base.SUCCEEDED)], [])
     self.assertEquals(3, pc.MAX_COMMIT_BURST)
     timestamp = [1]
     self.mock(time, 'time', lambda: timestamp[-1])
     for i in range(pc.MAX_COMMIT_BURST + 2):
         self.context.rietveld.issues[i] = (
             self.context.rietveld.issues[31337].copy())
         self.context.rietveld.issues[i]['issue'] = i
     pc.look_for_new_pending_commit()
     self.assertEquals(len(pc.queue.pending_commits),
                       pc.MAX_COMMIT_BURST + 3)
     pc.process_new_pending_commit()
     pc.update_status()
     pc.scan_results()
     self.context.checkout.check_calls([
         'prepare(None)',
         'apply_patch(%r)' % self.context.rietveld.patchsets[0],
         "commit(u'foo\\n\\nReview URL: http://nowhere/0', '*****@*****.**')",
         'prepare(None)',
         'apply_patch(%r)' % self.context.rietveld.patchsets[1],
         "commit(u'foo\\n\\nReview URL: http://nowhere/1', '*****@*****.**')",
         'prepare(None)',
         'apply_patch(%r)' % self.context.rietveld.patchsets[2],
         "commit(u'foo\\n\\nReview URL: http://nowhere/2', '*****@*****.**')"
     ])
     self.context.rietveld.check_calls([
         _try_comment(0),
         _try_comment(1),
         _try_comment(2),
         _try_comment(3),
         _try_comment(4),
         _try_comment(), 'close_issue(0)', "update_description(0, u'foo')",
         "add_comment(0, 'Change committed as 125')", 'close_issue(1)',
         "update_description(1, u'foo')",
         "add_comment(1, 'Change committed as 125')", 'close_issue(2)',
         "update_description(2, u'foo')",
         "add_comment(2, 'Change committed as 125')"
     ])
     self.assertEquals(3, len(pc.queue.pending_commits))
     # Dry run.
     pc.scan_results()
     self.context.checkout.check_calls([])
     self.context.rietveld.check_calls([])
     # Remove one item from the burst.
     pc.recent_commit_timestamps.pop()
     pc.scan_results()
     self.context.checkout.check_calls([
         'prepare(None)',
         'apply_patch(%r)' % self.context.rietveld.patchsets[3],
         "commit(u'foo\\n\\nReview URL: http://nowhere/3', '*****@*****.**')"
     ])
     self.context.rietveld.check_calls([
         'close_issue(3)', "update_description(3, u'foo')",
         "add_comment(3, 'Change committed as 125')"
     ])
     # After a delay, must flush the queue.
     timestamp.append(timestamp[-1] + pc.COMMIT_BURST_DELAY + 1)
     pc.scan_results()
     self.context.checkout.check_calls([
         'prepare(None)',
         'apply_patch(%r)' % self.context.rietveld.patchsets[4],
         "commit(u'foo\\n\\nReview URL: http://nowhere/4', '*****@*****.**')",
         'prepare(None)',
         'apply_patch(%r)' % self.context.rietveld.patchsets[5],
         "commit(u'foo\\n\\nReview URL: http://nowhere/31337', "
         "'*****@*****.**')"
     ])
     self.context.rietveld.check_calls([
         'close_issue(4)', "update_description(4, u'foo')",
         "add_comment(4, 'Change committed as 125')", 'close_issue(31337)',
         "update_description(31337, u'foo')",
         "add_comment(31337, 'Change committed as 125')"
     ])
     self.context.status.check_names(['initial'] * 6 + ['commit'] * 6)