def test_poll_force_push(self): # There's a previous revision, but not linked with new rev self.expect_commands( ExpectMasterShell( ['hg', 'pull', '-b', 'default', 'ssh://example.com/foo/baz']).workdir('/some/dir'), ExpectMasterShell([ 'hg', 'heads', 'default', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b'5' + LINESEP_BYTES), ExpectMasterShell([ 'hg', 'log', '-r', '4::5', '--template={rev}:{node}\\n' ]).workdir('/some/dir').stdout(b""), ExpectMasterShell([ 'hg', 'log', '-r', '5', '--template={rev}:{node}\\n' ]).workdir('/some/dir').stdout(LINESEP_BYTES.join([b'5:784bd'])), ExpectMasterShell([ 'hg', 'log', '-r', '784bd', '--template={date|hgdate}' + os.linesep + '{author}' + os.linesep + "{files % '{file}" + os.pathsep + "'}" + os.linesep + '{desc|strip}' ]).workdir('/some/dir').stdout( LINESEP_BYTES.join([ b'1273258009.0 -7200', b'Joe Test <*****@*****.**>', b'file1 file2', b'Comment for rev 5', b'' ])), ) yield self.poller._setCurrentRev(4) yield self.poller.poll() yield self.check_current_rev(5) self.assertEqual(len(self.master.data.updates.changesAdded), 1) change = self.master.data.updates.changesAdded[0] self.assertEqual(change['revision'], '784bd') self.assertEqual(change['comments'], 'Comment for rev 5')
def test_remote_ssh_wol_action_no_keys(self, write_local_file_mock, temp_dir_mock): self.expect_commands( ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'wakeonlan', '00:11:22:33:44:55' ]).exit(0), ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'wolbin', '00:11:22:33:44:55' ]).exit(0), ) manager = FakeManager(self.reactor) action = RemoteSshWOLAction('remote_host', '00:11:22:33:44:55') self.assertTrue((yield action.perform(manager))) action = RemoteSshWOLAction('remote_host', '00:11:22:33:44:55', wolBin='wolbin') self.assertTrue((yield action.perform(manager))) self.assert_all_commands_ran() self.assertEqual(temp_dir_mock.dirs, []) write_local_file_mock.assert_not_called()
def test_local_wake_action(self): self.expect_commands( ExpectMasterShell(['cmd', 'arg1', 'arg2']).exit(1), ExpectMasterShell(['cmd', 'arg1', 'arg2']).exit(0), ) manager = FakeManager(self.reactor) action = LocalWakeAction(['cmd', 'arg1', 'arg2']) self.assertFalse((yield action.perform(manager))) self.assertTrue((yield action.perform(manager))) self.assert_all_commands_ran()
def test_local_wol_action(self): self.expect_commands( ExpectMasterShell(['wol', '00:11:22:33:44:55']).exit(1), ExpectMasterShell(['wakeonlan', '00:11:22:33:44:55']).exit(0), ) manager = FakeManager(self.reactor) action = LocalWOLAction('00:11:22:33:44:55', wolBin='wol') self.assertFalse((yield action.perform(manager))) action = LocalWOLAction('00:11:22:33:44:55') self.assertTrue((yield action.perform(manager))) self.assert_all_commands_ran()
def test_server_tz(self): """Verify that the server_tz parameter is handled correctly""" yield self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split, server_tz="Europe/Berlin")) self.expect_commands( ExpectMasterShell(['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 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.assert_all_commands_ran()
def test_acquire_ticket_auth_fail(self): yield self.attachChangeSource( P4Source(p4port=None, p4user=None, p4passwd='pass', p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1), use_tickets=True)) self.expect_commands( ExpectMasterShell(['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'login']]) pp.makeConnection(transport) self.assertEqual(b'pass\n', transport.msg) pp.outReceived(b'Enter password:\n') pp.errReceived(b"Password invalid.\n") so = error.ProcessDone(status=1) pp.processEnded(failure.Failure(so)) self.patch(reactor, 'spawnProcess', spawnProcess) yield self.changesource.poll()
def method(testcase): testcase.expect_commands( ExpectMasterShell(["command" ]).stdout(b'stdout').stderr(b'stderr')) res = yield runprocess.run_process(None, ["command"]) self.assertEqual(res, (0, b'stdout', b'stderr')) testcase.assert_all_commands_ran()
def test_remote_ssh_wake_action_with_keys(self, write_local_file_mock, temp_dir_mock): temp_dir_path = os.path.join('path-to-master', 'ssh-@@@') ssh_key_path = os.path.join(temp_dir_path, 'ssh-key') ssh_known_hosts_path = os.path.join(temp_dir_path, 'ssh-known-hosts') self.expect_commands( ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', '-i', ssh_key_path, '-o', f'UserKnownHostsFile={ssh_known_hosts_path}', 'remote_host', 'remotebin', 'arg1' ]).exit(0), ) manager = FakeManager(self.reactor, 'path-to-master') action = RemoteSshWakeAction('remote_host', ['remotebin', 'arg1'], sshKey='ssh_key', sshHostKey='ssh_host_key') self.assertTrue((yield action.perform(manager))) self.assert_all_commands_ran() self.assertEqual(temp_dir_mock.dirs, [(temp_dir_path, 0o700)]) self.assertSequenceEqual(write_local_file_mock.call_args_list, [ mock.call(ssh_key_path, 'ssh_key', mode=0o400), mock.call(ssh_known_hosts_path, '* ssh_host_key'), ])
def method(testcase): testcase.expect_commands(ExpectMasterShell(["command"])) testcase.add_run_process_expect_env({'key': 'value'}) res = yield runprocess.run_process(None, ["command"], env={'key': 'value'}) self.assertEqual(res, (0, b'', b'')) testcase.assert_all_commands_ran()
def makeInfoExpect(self, password='******'): args = [ 'svn', 'info', '--xml', '--non-interactive', sample_base, '--username=dustin' ] if password is not None: args.append('--password=' + password) return ExpectMasterShell(args)
def method(testcase): testcase.expect_commands( ExpectMasterShell(["command"]).stderr(b"some test")) res = yield runprocess.run_process(None, ["command"], collect_stderr=False, stderr_is_error=True) self.assertEqual(res, (-1, b'')) testcase.assert_all_commands_ran()
def test_get_secret_in_pass(self): self.expect_commands( ExpectMasterShell(['pass', 'secret']).stdout(b'value')) value = yield self.srvpass.get("secret") self.assertEqual(value, "value") self.assert_all_commands_ran()
def test_prepare_base_image_full(self): self.expect_commands(ExpectMasterShell(["cp", "o", "p"])) bs = self.create_worker('bot', 'pass', hd_image='p', base_image='o') bs.cheap_copy = False yield bs._prepare_base_image() self.assert_all_commands_ran()
def test_prepare_base_image_fail(self): self.expect_commands(ExpectMasterShell(["cp", "o", "p"]).exit(1)) bs = self.create_worker('bot', 'pass', hd_image='p', base_image='o') bs.cheap_copy = False with self.assertRaises(LatentWorkerFailedToSubstantiate): yield bs._prepare_base_image() self.assert_all_commands_ran()
def test_get_secret_in_pass_multiple_lines_unix(self): self.expect_commands( ExpectMasterShell(['pass', 'secret']).stdout(b"value1\nvalue2\nvalue3")) value = yield self.srvpass.get("secret") self.assertEqual(value, "value1") self.assert_all_commands_ran()
def test_getFiles(self): s = yield self.newChangeSource('host', 'user', gerritport=2222) exp_argv = [ 'ssh', '-o', 'BatchMode=yes', 'user@host', '-p', '2222', 'gerrit', 'query', '1000', '--format', 'JSON', '--files', '--patch-sets' ] self.expect_commands( ExpectMasterShell(exp_argv).stdout(self.query_files_success), ExpectMasterShell(exp_argv).stdout(self.query_files_failure)) res = yield s.getFiles(1000, 13) self.assertEqual(set(res), {'/COMMIT_MSG', 'file1', 'file2'}) res = yield s.getFiles(1000, 13) self.assertEqual(res, ['unknown']) self.assert_all_commands_ran()
def test_poll_several_heads(self): # If there are several heads on the named branch, the poller mustn't # climb (good enough for now, ideally it should even go to the common # ancestor) self.expect_commands( ExpectMasterShell( ['hg', 'pull', '-b', 'default', 'ssh://example.com/foo/baz']).workdir('/some/dir'), ExpectMasterShell([ 'hg', 'heads', 'default', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b'5' + LINESEP_BYTES + b'6' + LINESEP_BYTES)) yield self.poller._setCurrentRev(3) # do the poll: we must stay at rev 3 yield self.poller.poll() yield self.check_current_rev(3)
def makeLogExpect(self, password='******'): args = [ 'svn', 'log', '--xml', '--verbose', '--non-interactive', '--username=dustin' ] if password is not None: args.append('--password='******'--limit=100', sample_base]) return ExpectMasterShell(args)
def test_prepare_base_image_cheap(self): self.expect_commands( ExpectMasterShell( ["qemu-img", "create", "-b", "o", "-f", "qcow2", "p"])) bs = self.create_worker('bot', 'pass', hd_image='p', base_image='o') yield bs._prepare_base_image() self.assert_all_commands_ran()
def do_test_get_prefix(self, base, output, expected): s = yield self.attachSVNPoller(base) self.expect_commands( ExpectMasterShell( ['svn', 'info', '--xml', '--non-interactive', base]).stdout(output)) prefix = yield s.get_prefix() self.assertEqual(prefix, expected) self.assert_all_commands_ran()
def test_poll_regular(self): # normal operation. There's a previous revision, we get a new one. # Let's say there was an intervening commit on an untracked branch, to # make it more interesting. self.expect_commands( ExpectMasterShell([ 'hg', 'pull', '-B', 'one', '-B', 'two', 'ssh://example.com/foo/baz' ]).workdir('/some/dir'), ExpectMasterShell([ 'hg', 'heads', 'one', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b'6' + LINESEP_BYTES), ExpectMasterShell( ['hg', 'log', '-r', '4::6', '--template={rev}:{node}\\n']).workdir('/some/dir').stdout( LINESEP_BYTES.join([ b'4:1aaa5', b'6:784bd', ])), ExpectMasterShell([ 'hg', 'log', '-r', '784bd', '--template={date|hgdate}' + os.linesep + '{author}' + os.linesep + "{files % '{file}" + os.pathsep + "'}" + os.linesep + '{desc|strip}' ]).workdir('/some/dir').stdout( LINESEP_BYTES.join([ b'1273258009.0 -7200', b'Joe Test <*****@*****.**>', b'file1 file2', b'Comment', b'' ])), ExpectMasterShell([ 'hg', 'heads', 'two', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b'3' + LINESEP_BYTES), ) yield self.poller._setCurrentRev(3, 'two') yield self.poller._setCurrentRev(4, 'one') yield self.poller.poll() yield self.check_current_rev(6, 'one') self.assertEqual(len(self.master.data.updates.changesAdded), 1) change = self.master.data.updates.changesAdded[0] self.assertEqual(change['revision'], '784bd') self.assertEqual(change['comments'], 'Comment')
def test_remote_ssh_wake_action_no_keys(self, write_local_file_mock, temp_dir_mock): self.expect_commands( ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'remotebin', 'arg1' ]).exit(1), ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'remotebin', 'arg1' ]).exit(0), ) manager = FakeManager(self.reactor) action = RemoteSshWakeAction('remote_host', ['remotebin', 'arg1']) self.assertFalse((yield action.perform(manager))) self.assertTrue((yield action.perform(manager))) self.assert_all_commands_ran() self.assertEqual(temp_dir_mock.dirs, []) write_local_file_mock.assert_not_called()
def test_poll_initial(self): self.expect_commands( ExpectMasterShell([ 'hg', 'pull', '-b', 'one', '-b', 'two', 'ssh://example.com/foo/baz' ]).workdir('/some/dir'), ExpectMasterShell([ 'hg', 'heads', 'one', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b"73591"), ExpectMasterShell([ 'hg', 'heads', 'two', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b"22341"), ) # do the poll yield self.poller.poll() # check the results self.assertEqual(len(self.master.data.updates.changesAdded), 0) yield self.check_current_rev(73591, 'one') yield self.check_current_rev(22341, 'two')
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_initial(self): self.repo_ready = False # Test that environment variables get propagated to subprocesses # (See #2116) expected_env = {ENVIRON_2116_KEY: 'TRUE'} self.add_run_process_expect_env(expected_env) self.expect_commands( ExpectMasterShell(['hg', 'init', '/some/dir']), ExpectMasterShell( ['hg', 'pull', '-b', 'default', 'ssh://example.com/foo/baz']).workdir('/some/dir'), ExpectMasterShell([ 'hg', 'heads', 'default', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b"73591"), ) # do the poll yield self.poller.poll() # check the results self.assertEqual(len(self.master.data.updates.changesAdded), 0) yield self.check_current_rev(73591)
def test_remote_ssh_suspend_action_no_keys(self, write_local_file_mock, temp_dir_mock): self.expect_commands( ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'systemctl', 'suspend' ]).exit(0), ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'dosuspend', 'arg1' ]).exit(0), ) manager = FakeManager(self.reactor) action = RemoteSshSuspendAction('remote_host') self.assertTrue((yield action.perform(manager))) action = RemoteSshSuspendAction('remote_host', remoteCommand=['dosuspend', 'arg1']) self.assertTrue((yield action.perform(manager))) self.assert_all_commands_ran() self.assertEqual(temp_dir_mock.dirs, []) write_local_file_mock.assert_not_called()
def test_poll_failed_changes(self): yield self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expect_commands( ExpectMasterShell(['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_getFilesFromEvent(self): self.expect_commands( ExpectMasterShell([ 'ssh', '-o', 'BatchMode=yes', 'user@host', '-p', '29418', 'gerrit', 'query', '4321', '--format', 'JSON', '--files', '--patch-sets' ]).stdout(self.query_files_success)) s = yield self.newChangeSource('host', 'user', get_files=True, handled_events=["change-merged"]) yield s.lineReceived(json.dumps(self.change_merged_event)) c = self.master.data.updates.changesAdded[0] self.assertEqual(set(c['files']), {'/COMMIT_MSG', 'file1', 'file2'}) self.assert_all_commands_ran()
def test_poll_unicode_error(self): yield self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expect_commands( ExpectMasterShell(['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 with self.assertRaises(UnicodeError): yield self.changesource._poll() self.assert_all_commands_ran()
def test_poll_failed_describe(self): yield self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=lambda x: x.split('/', 1))) self.expect_commands( ExpectMasterShell(['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()