def test_name(self): # no name: cs1 = P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1)) self.assertEqual("P4Source:None://depot/myproject/", cs1.name) # explicit name: cs2 = P4Source(p4port=None, p4user=None, name='MyName', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1)) self.assertEqual("MyName", cs2.name)
def test_acquire_ticket_auth(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4passwd='pass', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), use_tickets=True)) self.expectCommands( gpo.Expect('p4', '-P', 'TICKET_ID_GOES_HERE', 'changes', '-m', '1', '//depot/myproject/...').stdout(first_p4changes)) transport = FakeTransport() # p4poller uses only those arguments at the moment def spawnProcess(pp, cmd, argv, env): self.assertEqual([cmd, argv], ['p4', [b'p4', b'login', b'-p']]) pp.makeConnection(transport) self.assertEqual(b'pass\n', transport.msg) pp.outReceived( b'Enter password:\nSuccess: Password verified.\nTICKET_ID_GOES_HERE\n' ) so = error.ProcessDone(None) pp.processEnded(failure.Failure(so)) self.patch(reactor, 'spawnProcess', spawnProcess) yield self.changesource.poll() self.assertEqual(self.changesource._ticket_passwd, 'TICKET_ID_GOES_HERE')
def testAlreadyWorking(self): """don't launch a new poll while old is still going""" self.t = P4Source() self.t.working = True self.assert_(self.t.last_change is None) d = self.t.checkp4() d.addCallback(self._testAlreadyWorking2)
def test_acquire_ticket_auth_invalid_encoding(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4passwd='pass', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), use_tickets=True)) transport = FakeTransport() # p4poller uses only those arguments at the moment def spawnProcess(pp, cmd, argv, env): self.assertEqual([cmd, argv], ['p4', [b'p4', b'login', b'-p']]) pp.makeConnection(transport) self.assertEqual(b'pass\n', transport.msg) pp.outReceived(b'\xff\xff\xff\xff\xff') so = error.ProcessDone(None) pp.processEnded(failure.Failure(so)) self.patch(reactor, 'spawnProcess', spawnProcess) yield self.changesource.poll() errors = self.flushLoggedErrors(P4PollerError) self.assertEqual(len(errors), 1) self.assertIn('\'utf-8\' codec can\'t decode byte 0xff', errors[0].getErrorMessage()) self.assertIn('Failed to parse P4 ticket', errors[0].getErrorMessage())
def test_acquire_ticket_auth2_fail(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4passwd='pass', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), use_tickets=True)) self.expectCommands( gpo.Expect('p4', '-P', None, 'changes', '-m', '1', '//depot/myproject/...').stdout(first_p4changes)) transport = FakeTransport() # p4poller uses only those arguments at the moment def spawnProcess(pp, cmd, argv, env): self.assertEqual([cmd, argv], ['p4', [b'p4', b'login', b'-p']]) pp.makeConnection(transport) self.assertEqual('pass\n', transport.msg) pp.outReceived('Enter password:\n') pp.errReceived( "Password invalid.\n'auth-check' validation failed: Incorrect password!\n" ) so = error.ProcessDone(status=1) pp.processEnded(failure.Failure(so)) self.patch(reactor, 'spawnProcess', spawnProcess) yield self.changesource.poll() self.assertEqual(self.changesource._ticket_passwd, None)
def test_poll_failed_describe(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.add_p4_changes_result(second_p4changes) self.add_p4_describe_result(3, 'Perforce client error:\n...') self.update_p4_describe_results( p4change) # note change 3 is overridden by prev line self.changesource.last_change = 2 # tell poll() that it's already been called once # call _poll, so we can catch the failure d = self.changesource._poll() def cb(_): self.fail("_poll should have failed") def eb(f): f.trap(P4PollerError) self.assertEquals(self.changesource.last_change, 2) # 2 was processed OK d.addCallbacks(cb, eb) return d
def test_acquire_ticket_auth(self): self.attachChangeSource( P4Source(p4port=None, p4user='******', p4passwd='pass', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), use_tickets=True)) self.expect_commands( ExpectMaster(['p4', 'changes', '-m', '1', '//depot/myproject/...']).stdout(first_p4changes)) transport = FakeTransport() # p4poller uses only those arguments at the moment def spawnProcess(pp, cmd, argv, env): self.assertEqual( [cmd, argv], ['p4', [b'p4', b'-u', b'buildbot_user', b'login']]) pp.makeConnection(transport) self.assertEqual(b'pass\n', transport.msg) pp.outReceived(b'Enter password:\nUser buildbot_user logged in.\n') so = error.ProcessDone(None) pp.processEnded(failure.Failure(so)) self.patch(reactor, 'spawnProcess', spawnProcess) yield self.changesource.poll() self.assert_all_commands_ran()
def test_describe(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.assertSubstring("p4source", self.changesource.describe())
def test_poll_failed_describe(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expectCommands( gpo.Expect( 'p4', 'changes', '//depot/myproject/...@3,#head').stdout(second_p4changes), ) self.add_p4_describe_result(2, p4change[2]) self.add_p4_describe_result(3, 'Perforce client error:\n...') self.changesource.last_change = 2 # tell poll() that it's already been called once # call _poll, so we can catch the failure d = self.changesource._poll() self.assertFailure(d, P4PollerError) @d.addCallback def check(_): # check that 2 was processed OK self.assertEquals(self.changesource.last_change, 2) self.assertAllCommandsRan() return d
def test_server_tz(self): """Verify that the server_tz parameter is handled correctly""" self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split, server_tz="Europe/Berlin")) self.expectCommands( gpo.Expect( 'p4', 'changes', '//depot/myproject/...@51,#head').stdout(third_p4changes), ) self.add_p4_describe_result(5, p4change[5]) self.changesource.last_change = 50 d = self.changesource.poll() def check(res): # when_timestamp is converted from 21:55:39 Berlin time to UTC when_berlin = self.makeTime("2006/04/13 21:55:39") when_berlin = when_berlin.replace( tzinfo=dateutil.tz.gettz('Europe/Berlin')) when = datetime2epoch(when_berlin) self.assertEqual([ ch['when_timestamp'] for ch in self.master.data.updates.changesAdded ], [when, when]) self.assertAllCommandsRan() d.addCallback(check) return d
def test_poll_split_file(self): """Make sure split file works on branch only changes""" self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split)) self.expectCommands( gpo.Expect( 'p4', 'changes', '//depot/myproject/...@51,#head').stdout(third_p4changes), ) self.add_p4_describe_result(5, p4change[5]) self.changesource.last_change = 50 yield self.changesource.poll() # when_timestamp is converted from a local time spec, so just # replicate that here when = self.makeTime("2006/04/13 21:55:39") def changeKey(change): """ Let's sort the array of changes by branch, because in P4Source._poll(), changeAdded() is called by iterating over a dictionary of branches""" return change['branch'] self.assertEqual(sorted(self.master.data.updates.changesAdded, key=changeKey), sorted([{ 'author': 'mpatel', 'branch': 'branch_c', 'category': None, 'codebase': None, 'comments': 'This is a multiline comment with tabs and spaces\n\nA list:\n Item 1\n\tItem 2', 'files': ['branch_c_file'], 'project': '', 'properties': {}, 'repository': '', 'revision': '5', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when), }, { 'author': 'mpatel', 'branch': 'branch_b', 'category': None, 'codebase': None, 'comments': 'This is a multiline comment with tabs and spaces\n\nA list:\n Item 1\n\tItem 2', 'files': ['branch_b_file'], 'project': '', 'properties': {}, 'repository': '', 'revision': '5', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when), }], key=changeKey)) self.assertEqual(self.changesource.last_change, 5) self.assertAllCommandsRan()
def test_poll_failed_changes(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expectCommands( gpo.Expect('p4', 'changes', '-m', '1', '//depot/myproject/...').stdout(b'Perforce client error:\n...')) # call _poll, so we can catch the failure d = self.changesource._poll() return self.assertFailure(d, P4PollerError)
def test_poll_failed_changes(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.add_p4_changes_result('Perforce client error:\n...') # call _poll, so we can catch the failure d = self.changesource._poll() return self.assertFailure(d, P4PollerError)
def test_poll_split_file(self): """Make sure split file works on branch only changes""" self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split)) self.expectCommands( gpo.Expect( 'p4', 'changes', '//depot/myproject/...@51,#head').stdout(third_p4changes), ) self.add_p4_describe_result(5, p4change[5]) self.changesource.last_change = 50 d = self.changesource.poll() def check(res): # when_timestamp is converted from a local time spec, so just # replicate that here when = self.makeTime("2006/04/13 21:55:39") self.assertEqual(self.master.data.updates.changesAdded, [{ 'author': u'mpatel', 'branch': u'branch_c', 'category': None, 'codebase': None, 'comments': u'This is a multiline comment with tabs and spaces\n\nA list:\n Item 1\n\tItem 2', 'files': [u'branch_c_file'], 'project': '', 'properties': {}, 'repository': '', 'revision': '5', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when), }, { 'author': u'mpatel', 'branch': u'branch_b', 'category': None, 'codebase': None, 'comments': u'This is a multiline comment with tabs and spaces\n\nA list:\n Item 1\n\tItem 2', 'files': [u'branch_b_file'], 'project': '', 'properties': {}, 'repository': '', 'revision': '5', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when), }]) self.assertEqual(self.changesource.last_change, 5) self.assertAllCommandsRan() d.addCallback(check) return d
def test_poll_unicode_error2(self): yield self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), encoding='ascii')) # Trying to decode a certain character with ascii codec should fail. self.expect_commands( ExpectMasterShell(['p4', 'changes', '-m', '1', '//depot/myproject/...']) .stdout(fourth_p4changes), ) yield self.changesource._poll() self.assert_all_commands_ran()
def test_poll_unicode_error2(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), encoding='ascii')) # Trying to decode a certain character with ascii codec should fail. self.expectCommands( gpo.Expect('p4', 'changes', '-m', '1', '//depot/myproject/...').stdout(fourth_p4changes), ) d = self.changesource._poll() return d
def test_poll_failed_changes(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expect_commands( ExpectMaster(['p4', 'changes', '-m', '1', '//depot/myproject/...' ]).stdout(b'Perforce client error:\n...')) # call _poll, so we can catch the failure with self.assertRaises(P4PollerError): yield self.changesource._poll() self.assert_all_commands_ran()
def test_poll_split_file(self): """Make sure split file works on branch only changes""" self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split)) self.add_p4_changes_result(third_p4changes) self.update_p4_describe_results(p4change) self.changesource.last_change = 50 d = self.changesource.poll() def check(res): self.assertEquals(len(self.changes_added), 2) self.assertEquals(self.changesource.last_change, 5) d.addCallback(check) return d
def test_poll_failed_changes(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.add_p4_changes_result('Perforce client error:\n...') # call _poll, so we can catch the failure d = self.changesource._poll() def cb(_): self.fail("_poll should have failed") def eb(f): f.trap(P4PollerError) self.assertEquals(self.changesource.last_change, None) d.addCallbacks(cb, eb) return d
def test_acquire_ticket_auth(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4passwd='pass', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), use_tickets=True)) self.expectCommands( gpo.Expect('p4', '-P', 'TICKET_ID_GOES_HERE', 'changes', '-m', '1', '//depot/myproject/...').stdout(first_p4changes)) class FakeTransport: def __init__(self): self.msg = None def write(self, msg): self.msg = msg def closeStdin(self): pass transport = FakeTransport() def spawnProcess( pp, cmd, argv, env): # p4poller uses only those arguments at the moment self.assertEqual([cmd, argv], ['p4', ['p4', 'login', '-p']]) pp.makeConnection(transport) self.assertEqual('pass\n', transport.msg) pp.outReceived('Enter password:\nTICKET_ID_GOES_HERE\n') so = error.ProcessDone(None) pp.processEnded(failure.Failure(so)) self.patch(reactor, 'spawnProcess', spawnProcess) d = self.changesource.poll() def check_ticket_passwd(_): self.assertEquals(self.changesource._ticket_passwd, 'TICKET_ID_GOES_HERE') d.addCallback(check_ticket_passwd) return d
def test_poll_unicode_error(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expectCommands( gpo.Expect( 'p4', 'changes', '//depot/myproject/...@3,#head').stdout(second_p4changes), ) # Add a character which cannot be decoded with utf-8 undecodableText = p4change[2] + b"\x81" self.add_p4_describe_result(2, undecodableText) # tell poll() that it's already been called once self.changesource.last_change = 2 # call _poll, so we can catch the failure d = self.changesource._poll() return self.assertFailure(d, UnicodeError)
def test_poll_split_file(self): """Make sure split file works on branch only changes""" self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split)) self.expectCommands( gpo.Expect('p4', 'changes', '//depot/myproject/...@51,now').stdout(third_p4changes), ) self.add_p4_describe_result(5, p4change[5]) self.changesource.last_change = 50 d = self.changesource.poll() def check(res): self.assertEquals(len(self.changes_added), 2) self.assertEquals(self.changesource.last_change, 5) self.assertAllCommandsRan() d.addCallback(check) return d
def test_poll_failed_describe(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expect_commands( ExpectMaster(['p4', 'changes', '//depot/myproject/...@3,#head' ]).stdout(second_p4changes), ) self.add_p4_describe_result(2, p4change[2]) self.add_p4_describe_result(3, b'Perforce client error:\n...') # tell poll() that it's already been called once self.changesource.last_change = 2 # call _poll, so we can catch the failure with self.assertRaises(P4PollerError): yield self.changesource._poll() # check that 2 was processed OK self.assertEqual(self.changesource.last_change, 2) self.assert_all_commands_ran()
def test_resolveWho_callable(self): with self.assertRaisesConfigError( "You need to provide a valid callable for resolvewho"): P4Source(resolvewho=None)
def do_test_poll_successful(self, **kwargs): encoding = kwargs.get('encoding', 'utf8') self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), **kwargs)) self.expectCommands( gpo.Expect('p4', 'changes', '-m', '1', '//depot/myproject/...').stdout(first_p4changes), gpo.Expect('p4', 'changes', '//depot/myproject/...@2,now').stdout(second_p4changes), ) encoded_p4change = p4change.copy() encoded_p4change[3] = encoded_p4change[3].encode(encoding) self.add_p4_describe_result(2, encoded_p4change[2]) self.add_p4_describe_result(3, encoded_p4change[3]) # The first time, it just learns the change to start at. self.assert_(self.changesource.last_change is None) d = self.changesource.poll() def check_first_check(_): self.assertEquals(self.changes_added, []) self.assertEquals(self.changesource.last_change, 1) d.addCallback(check_first_check) # Subsequent times, it returns Change objects for new changes. d.addCallback(lambda _: self.changesource.poll()) def check_second_check(res): self.assertEquals(len(self.changes_added), 3) self.assertEquals(self.changesource.last_change, 3) # They're supposed to go oldest to newest, so this one must be first. self.assertEquals( self.changes_added[0], dict(author='slamb', files=['whatbranch'], project='', comments=change_2_log, revision='2', when_timestamp=self.makeTime("2006/04/13 21:46:23"), branch='trunk')) # These two can happen in either order, since they're from the same # Perforce change. if self.changes_added[1]['branch'] == 'branch_c': self.changes_added[1:] = reversed(self.changes_added[1:]) self.assertEquals( self.changes_added[1], dict( author='bob', files=['branch_b_file', 'whatbranch'], project='', comments=change_3_log, # converted to unicode correctly revision='3', when_timestamp=self.makeTime("2006/04/13 21:51:39"), branch='branch_b')) self.assertEquals( self.changes_added[2], dict( author='bob', files=['whatbranch'], project='', comments=change_3_log, # converted to unicode correctly revision='3', when_timestamp=self.makeTime("2006/04/13 21:51:39"), branch='branch_c')) self.assertAllCommandsRan() d.addCallback(check_second_check) return d
def do_test_poll_successful(self, **kwargs): encoding = kwargs.get('encoding', 'utf8') self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), **kwargs)) self.expectCommands( gpo.Expect('p4', 'changes', '-m', '1', '//depot/myproject/...').stdout(first_p4changes), gpo.Expect( 'p4', 'changes', '//depot/myproject/...@2,#head').stdout(second_p4changes), ) encoded_p4change = p4change.copy() encoded_p4change[3] = encoded_p4change[3].encode(encoding) self.add_p4_describe_result(2, encoded_p4change[2]) self.add_p4_describe_result(3, encoded_p4change[3]) # The first time, it just learns the change to start at. self.assert_(self.changesource.last_change is None) d = self.changesource.poll() def check_first_check(_): self.assertEquals(self.master.data.updates.changesAdded, []) self.assertEquals(self.changesource.last_change, 1) d.addCallback(check_first_check) # Subsequent times, it returns Change objects for new changes. d.addCallback(lambda _: self.changesource.poll()) def check_second_check(res): # when_timestamp is converted from a local time spec, so just # replicate that here when1 = self.makeTime("2006/04/13 21:46:23") when2 = self.makeTime("2006/04/13 21:51:39") # these two can happen in either order, since they're from the same # perforce change. changesAdded = self.master.data.updates.changesAdded if changesAdded[1]['branch'] == 'branch_c': changesAdded[1:] = reversed(changesAdded[1:]) self.assertEqual(self.master.data.updates.changesAdded, [{ 'author': u'slamb', 'branch': u'trunk', 'category': None, 'codebase': None, 'comments': u'creation', 'files': [u'whatbranch'], 'project': '', 'properties': {}, 'repository': '', 'revision': '2', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when1), }, { 'author': u'bob', 'branch': u'branch_b', 'category': None, 'codebase': None, 'comments': u'short desc truncated because this is a long description.\nASDF-GUI-P3-\u2018Upgrade Icon\u2019 disappears sometimes.', 'files': [u'branch_b_file', u'whatbranch'], 'project': '', 'properties': {}, 'repository': '', 'revision': '3', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when2), }, { 'author': u'bob', 'branch': u'branch_c', 'category': None, 'codebase': None, 'comments': u'short desc truncated because this is a long description.\nASDF-GUI-P3-\u2018Upgrade Icon\u2019 disappears sometimes.', 'files': [u'whatbranch'], 'project': '', 'properties': {}, 'repository': '', 'revision': '3', 'revlink': '', 'src': None, 'when_timestamp': datetime2epoch(when2), }]) self.assertAllCommandsRan() d.addCallback(check_second_check) return d
def __init__(self, p4changes, p4change, *args, **kwargs): P4Source.__init__(self, *args, **kwargs) self.p4changes = p4changes self.p4change = p4change
def test_poll_successful(self): self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.add_p4_changes_result(first_p4changes) self.add_p4_changes_result(second_p4changes) self.update_p4_describe_results(p4change) # The first time, it just learns the change to start at. self.assert_(self.changesource.last_change is None) d = self.changesource.poll() def check_first_check(_): self.assertEquals(self.changes_added, []) self.assertEquals(self.changesource.last_change, 1) d.addCallback(check_first_check) # Subsequent times, it returns Change objects for new changes. d.addCallback(lambda _: self.changesource.poll()) def check_second_check(res): self.assertEquals(len(self.changes_added), 3) self.assertEquals(self.changesource.last_change, 3) # They're supposed to go oldest to newest, so this one must be first. self.assertEquals( self.changes_added[0], dict(who='slamb', files=['whatbranch'], comments=change_2_log, revision='2', when=self.makeTime("2006/04/13 21:46:23"), branch='trunk')) # These two can happen in either order, since they're from the same # Perforce change. if self.changes_added[1]['branch'] == 'branch_c': self.changes_added[1:] = reversed(self.changes_added[1:]) self.assertEquals( self.changes_added[1], dict(who='bob', files=['branch_b_file', 'whatbranch'], comments=change_3_log, revision='3', when=self.makeTime("2006/04/13 21:51:39"), branch='branch_b')) self.assertEquals( self.changes_added[2], dict(who='bob', files=['whatbranch'], comments=change_3_log, revision='3', when=self.makeTime("2006/04/13 21:51:39"), branch='branch_c')) d.addCallback(check_second_check) return d