def test_remote_ssh_wol_action_no_keys(self, write_local_file_mock, temp_dir_mock): self.expect_commands( ExpectMaster([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'wakeonlan', '00:11:22:33:44:55' ]).exit(0), ExpectMaster([ '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_poll_force_push(self): # There's a previous revision, but not linked with new rev self.expect_commands( ExpectMaster( ['hg', 'pull', '-b', 'default', 'ssh://example.com/foo/baz']).workdir('/some/dir'), ExpectMaster([ 'hg', 'heads', 'default', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b'5' + LINESEP_BYTES), ExpectMaster([ 'hg', 'log', '-r', '4::5', '--template={rev}:{node}\\n' ]).workdir('/some/dir').stdout(b""), ExpectMaster([ 'hg', 'log', '-r', '5', '--template={rev}:{node}\\n' ]).workdir('/some/dir').stdout(LINESEP_BYTES.join([b'5:784bd'])), ExpectMaster([ '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_local_wake_action(self): self.expect_commands( ExpectMaster(['cmd', 'arg1', 'arg2']).exit(1), ExpectMaster(['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( ExpectMaster(['wol', '00:11:22:33:44:55']).exit(1), ExpectMaster(['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""" self.attachChangeSource( P4Source(p4port=None, p4user=None, p4base='//depot/myproject/', split_file=get_simple_split, server_tz="Europe/Berlin")) self.expect_commands( ExpectMaster(['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_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( ExpectMaster([ 'ssh', '-o', 'BatchMode=yes', '-i', ssh_key_path, '-o', 'UserKnownHostsFile={0}'.format(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 test_acquire_ticket_auth_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.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'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(ExpectMaster(["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 method(testcase): testcase.expect_commands( ExpectMaster(["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 method(testcase): testcase.expect_commands( ExpectMaster(["command"]).stdout(b'stdout').stderr(b'stderr')) res = yield runprocess.run_process(None, ["command"], collect_stdout=True, collect_stderr=False) self.assertEqual(res, (0, b'stdout')) 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 ExpectMaster(args)
def test_prepare_base_image_full(self): self.expect_commands(ExpectMaster(["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(ExpectMaster(["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_getFiles(self): s = 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( ExpectMaster(exp_argv).stdout(self.query_files_success), ExpectMaster(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 do_test_get_prefix(self, base, output, expected): s = self.attachSVNPoller(base) self.expect_commands( ExpectMaster(['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_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( ExpectMaster( ['hg', 'pull', '-b', 'default', 'ssh://example.com/foo/baz']).workdir('/some/dir'), ExpectMaster([ '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 test_prepare_base_image_cheap(self): self.expect_commands( ExpectMaster(["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 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 ExpectMaster(args)
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( ExpectMaster([ 'hg', 'pull', '-B', 'one', '-B', 'two', 'ssh://example.com/foo/baz' ]).workdir('/some/dir'), ExpectMaster([ 'hg', 'heads', 'one', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b'6' + LINESEP_BYTES), ExpectMaster( ['hg', 'log', '-r', '4::6', '--template={rev}:{node}\\n']).workdir('/some/dir').stdout( LINESEP_BYTES.join([ b'4:1aaa5', b'6:784bd', ])), ExpectMaster([ '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'' ])), ExpectMaster([ '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( ExpectMaster([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'remotebin', 'arg1' ]).exit(1), ExpectMaster([ '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( ExpectMaster([ 'hg', 'pull', '-b', 'one', '-b', 'two', 'ssh://example.com/foo/baz' ]).workdir('/some/dir'), ExpectMaster([ 'hg', 'heads', 'one', '--template={rev}' + os.linesep ]).workdir('/some/dir').stdout(b"73591"), ExpectMaster([ '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): 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( ExpectMaster(['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( ExpectMaster(['hg', 'init', '/some/dir']), ExpectMaster( ['hg', 'pull', '-b', 'default', 'ssh://example.com/foo/baz']).workdir('/some/dir'), ExpectMaster([ '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( ExpectMaster([ 'ssh', '-o', 'BatchMode=yes', 'remote_host', 'systemctl', 'suspend' ]).exit(0), ExpectMaster([ '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): 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_getFilesFromEvent(self): self.expect_commands( ExpectMaster([ 'ssh', '-o', 'BatchMode=yes', 'user@host', '-p', '29418', 'gerrit', 'query', '4321', '--format', 'JSON', '--files', '--patch-sets' ]).stdout(self.query_files_success)) s = 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): 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), ) # 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): 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 method(testcase): testcase.expect_commands(ExpectMaster(["command"])) testcase.add_run_process_expect_env({'key': 'value'}) d = runprocess.run_process(None, ["command"]) return d
def test_method_chaining(self): expect = ExpectMaster('command') self.assertEqual(expect, expect.exit(0)) self.assertEqual(expect, expect.stdout(b"output")) self.assertEqual(expect, expect.stderr(b"error"))