def testUser(self): child = launch_polysh(['--ssh=echo', 'machine']) child.expect('[^@]machine') child.expect(pexpect.EOF) child = launch_polysh(['--ssh=echo', '--user=login', 'machine']) child.expect('login@machine') child.expect(pexpect.EOF)
def testNoHosts(self): child = launch_polysh([]) child.expect('error: no hosts given') child.expect(pexpect.EOF) child = launch_polysh(['--hosts-file=/dev/null']) child.expect('error: no hosts given') child.expect(pexpect.EOF)
def testAbortError(self): child = launch_polysh(['localhost', 'unknown_host']) child.expect('Error talking to unknown_host') child.sendline(':quit') child.expect(pexpect.EOF) child = launch_polysh(['--abort-errors', 'localhost', 'unknown_host']) child.expect('Error talking to unknown_host') child.expect(pexpect.EOF)
def testLogOutput(self): child = launch_polysh(['--log-file=/', 'localhost']) child.expect("\[Errno 21\].*'/'") child.expect(pexpect.EOF) child = launch_polysh(['--log-file=/cannot_write', 'localhost']) child.expect("\[Errno 13\].*'/cannot_write'") child.expect(pexpect.EOF) child = launch_polysh(['--log-file=/dev/full', 'localhost']) child.sendline('echo something') child.expect('Exception while writing log: /dev/full') child.expect('\[Errno 28\]') child.expect(pexpect.EOF) child = launch_polysh(['localhost']) def testEcho(msg): child.expect('ready \(1\)> ') child.sendline('echo %s' % msg) child.expect('\033\[1;36mlocalhost : \033\[1;m%s' % msg) testEcho('not logging') child.sendline(':set_log') testEcho('still not logging') child.sendline('!rm -f /tmp/polysh_test.log') testEcho('still not logging') child.sendline(':set_log /tmp/polysh_test.log') testEcho('now logging') testEcho('still logging') child.sendline(':set_log') testEcho('back to no logging') child.sendline(':set_log /tmp/polysh_test.lo\t') testEcho('appended to the log') child.sendline(':set_log') child.expect('ready \(1\)> ') child.sendline(':set_log /no-permission') child.expect("[Errno 13] .*: '/no-permission'") child.expect('Logging disabled') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF) EXPECTED_LOG = """ > echo now logging localhost : now logging > echo still logging localhost : still logging > :set_log > echo appended to the log localhost : appended to the log > :set_log """.strip() log = file('/tmp/polysh_test.log') log_lines = [l for l in log.readlines() if not l.startswith('[dbg] ')] actual_log = ''.join(log_lines).strip() self.assertEqual(actual_log, EXPECTED_LOG) os.remove('/tmp/polysh_test.log')
def testLogOutput(self): child = launch_polysh(['--log-file=/', 'localhost']) child.expect("\[Errno 21\].*'/'") child.expect(pexpect.EOF) child = launch_polysh(['--log-file=/cannot_write', 'localhost']) child.expect("\[Errno 13\].*'/cannot_write'") child.expect(pexpect.EOF) child = launch_polysh(['--log-file=/dev/full', 'localhost']) child.sendline('echo something') child.expect('Exception while writing log: /dev/full') child.expect('\[Errno 28\]') child.expect(pexpect.EOF) child = launch_polysh(['localhost']) def testEcho(msg): child.expect('ready \(1\)> ') child.sendline('echo %s' % msg) child.expect('\033\[1;36mlocalhost : \033\[1;m%s' % msg) testEcho('not logging') child.sendline(':set_log') testEcho('still not logging') child.sendline('!rm -f /tmp/polysh_test.log') testEcho('still not logging') child.sendline(':set_log /tmp/polysh_test.log') testEcho('now logging') testEcho('still logging') child.sendline(':set_log') testEcho('back to no logging') child.sendline(':set_log /tmp/polysh_test.lo\t') testEcho('appended to the log') child.sendline(':set_log') child.expect('ready \(1\)> ') child.sendline(':set_log /no-permission') child.expect("[Errno 13] .*: '/no-permission'") child.expect('Logging disabled') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF) EXPECTED_LOG=""" > echo now logging localhost : now logging > echo still logging localhost : still logging > :set_log > echo appended to the log localhost : appended to the log > :set_log """.strip() log = file('/tmp/polysh_test.log') log_lines = [l for l in log.readlines() if not l.startswith('[dbg] ')] actual_log = ''.join(log_lines).strip() self.assertEqual(actual_log, EXPECTED_LOG) os.remove('/tmp/polysh_test.log')
def testInitError(self): child = launch_polysh(['--ssh=echo message', 'localhost']) child.expect('message localhost') child.expect(pexpect.EOF) child = launch_polysh(['--ssh=echo The authenticity of host', 'l']) child.expect('Closing connection') child.expect('Consider manually connecting or using ssh-keyscan') child.expect(pexpect.EOF) child = launch_polysh(['--ssh=echo REMOTE HOST IDENTIFICATION ' 'HAS CHANGED', 'l']) child.expect('Remote host identification has changed') child.expect('Consider manually connecting or using ssh-keyscan') child.expect(pexpect.EOF)
def testInitError(self): child = launch_polysh(['--ssh=echo message', 'localhost']) child.expect('message localhost') child.expect(pexpect.EOF) child = launch_polysh(['--ssh=echo The authenticity of host', 'l']) child.expect('Closing connection') child.expect('Consider manually connecting or using ssh-keyscan') child.expect(pexpect.EOF) child = launch_polysh( ['--ssh=echo REMOTE HOST IDENTIFICATION ' 'HAS CHANGED', 'l']) child.expect('Remote host identification has changed') child.expect('Consider manually connecting or using ssh-keyscan') child.expect(pexpect.EOF)
def testError(self): child = launch_polysh(['localhost', 'localhost']) child.expect('ready \(2\)> ') child.sendline('kill -9 $$') child.expect('Error talking to localhost') child.expect('Error talking to localhost') child.expect(pexpect.EOF)
def testListManipulation(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline(':add localhost') child.expect('ready \(2\)> ') child.sendline(':rename $(echo newname)') child.expect('ready \(2\)> ') child.sendline('date') child.expect('newname') child.expect('newname') child.expect('ready \(2\)> ') child.sendline(':rename $EMPTY_VARIABLE') child.expect('ready \(2\)> ') child.sendline('date') child.expect('localhost') child.expect('localhost') child.expect('ready \(2\)> ') child.sendline(':rename $(echo newname)') child.expect('ready \(2\)> ') child.sendline('date') child.expect('newname') child.expect('newname') child.expect('ready \(2\)> ') child.sendline(':disable newname') child.sendline(':purge') child.sendline(':enable *') child.expect('ready \(1\)> ') child.sendline(':rename') child.expect('ready \(1\)> ') child.sendline('date') child.expect('localhost :') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF)
def CommandCode(command, code): child = launch_polysh(['--command=%s' % command] + ['localhost'] * 5) child.expect(pexpect.EOF) while child.isalive(): child.wait() self.assertEqual(child.exitstatus, code)
def testError(self): child = launch_polysh(["localhost", "localhost"]) child.expect("ready \(2\)> ") child.sendline("kill -9 $$") child.expect("Error talking to localhost") child.expect("Error talking to localhost") child.expect(pexpect.EOF)
def testCleanExit(self): child = launch_polysh(['localhost', 'localhost']) child.expect('ready \(2\)> ') child.sendeof() idx = child.expect(['Error talking to localhost', 'exit']) self.assertEqual(idx, 1) idx = child.expect(['Error talking to localhost', pexpect.EOF]) self.assertEqual(idx, 1)
def testProfile(self): child = launch_polysh(['--profile', 'localhost']) child.expect('Profiling using ') child.expect('ready \(1\)> ') child.sendline(':quit') child.expect(' function calls in ') child.expect('Ordered by') child.expect(pexpect.EOF)
def testResetPrompt(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline('bash') child.sendline(':reset_prompt l\t') child.expect('ready \(1\)> ') child.sendline(':quit') child.expect(pexpect.EOF)
def testCleanExit(self): child = launch_polysh(["localhost", "localhost"]) child.expect("ready \(2\)> ") child.sendeof() idx = child.expect(["Error talking to localhost", "exit"]) self.assertEqual(idx, 1) idx = child.expect(["Error talking to localhost", pexpect.EOF]) self.assertEqual(idx, 1)
def testPrintReadBuffer(self): child = launch_polysh(['--ssh=echo message; sleep'] + ['2h'] * 3) child.expect('waiting \(3/3\)> ') child.sendline(':show_read_buffer \t*') for i in xrange(3): child.expect('\033\[1;[0-9]+m2h[ #][ 12] : \033\[1;mmessage') child.expect('waiting \(3/3\)> ') child.sendintr() child.expect(pexpect.EOF)
def testReplicate(self): tmp_dir = tempfile.mkdtemp() local = tmp_dir + '/local' child = launch_polysh(['localhost'] * 6) child.expect('ready \(6\)> ') child.sendline(':replicate') child.expect('Usage: :replicate SHELL:REMOTE_PATH') child.expect('ready \(6\)> ') child.sendline(':replicate localhost:') child.expect('No remote path given') child.expect('ready \(6\)> ') child.sendline(':disable') child.expect('ready \(0\)> ') child.sendline(':enable localhost') child.expect('ready \(1\)> ') child.sendline(':replicate localhost#1:/dev/null') child.expect('localhost#1 is not enabled') child.expect('ready \(1\)> ') child.sendline(':replicate localhost:/dev/null') child.expect('No other remote shell to replicate files to') child.expect('ready \(1\)> ') child.sendline(':enable') child.expect('ready \(6\)> ') child.sendline(':replicate nohost:/dev/null') child.expect('nohost not found') child.expect('ready \(6\)> ') child.sendline(':disable localhost#5') child.expect('ready \(5\)> ') child.sendline("cd %s" % tmp_dir) child.expect('ready \(5\)> ') child.sendline('!mkdir %s' % local) child.expect('ready \(5\)> ') child.sendline('!yes "$(dmesg)" | head -c 20m > %s/file' % local) child.expect('ready \(5\)> ') child.sendline('!cd %s && sha1sum file > SHA1SUM' % local) child.expect('ready \(5\)> ') child.sendline(':export_vars') child.expect('ready \(5\)> ') child.sendline('mkdir $POLYSH_RANK') child.expect('ready \(5\)> ') child.sendline('cd $POLYSH_RANK') child.expect('ready \(5\)> ') child.sendline(':replicate l\t:%s/fil\t' % local) child.expect('Done transferring 20981760 bytes') child.expect('ready \(5\)> ') child.sendline('sha1sum file > SHA1SUM') child.expect('ready \(5\)> ') cat = 'cat %s/*/SHA1SUM' % tmp_dir wc_output = shell_output('%s | wc | tr -s " "' % cat) self.assertEqual(wc_output, '5 10 235') uniq_wc_output = shell_output('%s | uniq | wc | tr -s " "' % cat) self.assertEqual(uniq_wc_output, '1 2 47') child.sendline("!rm -fr '%s'" % tmp_dir) child.expect('ready \(5\)> ') child.sendline(':quit') child.expect(pexpect.EOF)
def testCleanExit(self): child = launch_polysh(['localhost', 'localhost']) child.expect('ready \(2\)> ') child.sendeof() # We test for logout as this is the expected response of sending EOF to # a non login shell idx = child.expect(['Error talking to localhost', 'logout']) self.assertEqual(idx, 1) idx = child.expect(['Error talking to localhost', pexpect.EOF]) self.assertEqual(idx, 1)
def startTestPassword(self, password_file): try: os.unlink('/tmp/polysh_test.log') except OSError: # File not found pass passwd = '--password-file=' + password_file child = launch_polysh([SSH_ARG, passwd, '--debug', '--log-file=/tmp/polysh_test.log', '1', '2']) return child
def testLocalAbsPathCompletion(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline('echo /dev/nul\t') child.expect('\033\[1;36mlocalhost : \033\[1;m/dev/null') child.expect('ready \(1\)> ') child.sendline('echo /sbi\t') child.expect('\033\[1;36mlocalhost : \033\[1;m/sbin/') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF)
def assertHostSyntax(self, to_expand, expanded): child = launch_polysh([to_expand, 'localhost']) child.expect('ready') child.sendline(':list') with_spaces = [e.replace('.', '\\.') + ' ' for e in expanded] for i in xrange(len(expanded)): found = child.expect(with_spaces) del with_spaces[found] child.expect('ready') child.sendeof() child.expect(pexpect.EOF)
def testMultipleCommandStdin(self): commands = """ echo first echo next echo last """ child = launch_polysh(['localhost'], input_data=commands) child.expect('localhost : first') child.expect('localhost : next') child.expect('localhost : last') child.expect(pexpect.EOF)
def testMultipleCommandStdin(self): commands = """ echo first echo next echo last """ child = launch_polysh(["localhost"], input_data=commands) child.expect("localhost : first") child.expect("localhost : next") child.expect("localhost : last") child.expect(pexpect.EOF)
def assertHostSyntax(self, to_expand, expanded): child = launch_polysh([to_expand, 'localhost']) child.expect('ready') child.sendline(':list') with_spaces = [e.replace('.', '\\.') + ' ' for e in expanded] for i in range(len(expanded)): found = child.expect(with_spaces) del with_spaces[found] child.expect('ready') child.sendeof() child.expect(pexpect.EOF)
def testPrependPrompt(self): child = launch_polysh(["localhost"]) child.expect("ready \(1\)> ") child.sendline("sleep 1") child.expect("waiting \(1/1\)> ") child.send("echo begin-") child.expect("ready \(1\)> ") child.sendline("end") child.expect("begin-end") child.expect("ready \(1\)> ") child.sendeof() child.expect(pexpect.EOF)
def testPrependPrompt(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline('sleep 1') child.expect('waiting \(1/1\)> ') child.send('echo begin-') child.expect('ready \(1\)> ') child.sendline('end') child.expect('begin-end') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF)
def testReconnect(self): child = launch_polysh(['localhost'] * 2) child.expect('ready \(2\)> ') child.sendline(':disable localhost') child.sendline('exit') child.expect('exit\r\n') child.expect('ready \(0\)>') child.sendline(':reconnect l\t') child.sendline(':enable') child.expect('ready \(2\)> ') child.sendeof() child.expect(pexpect.EOF)
def testGoodHostsFilename(self): tmp_name = '/tmp/polysh_tests.%d' % (os.getpid()) tmp = open(tmp_name, 'w', 0o600) print('localhost # Comment', file=tmp) print('# Ignore me', file=tmp) print('127.0.0.1', file=tmp) tmp.close() child = launch_polysh(['--hosts-file=%s' % (tmp_name)]) child.expect('ready \(2\)> ') child.sendeof() child.expect(pexpect.EOF) os.remove(tmp_name)
def startTestPassword(self, password_file): try: os.unlink('/tmp/polysh_test.log') except OSError: # File not found pass passwd = '--password-file=' + password_file child = launch_polysh([ SSH_ARG, passwd, '--debug', '--log-file=/tmp/polysh_test.log', '1', '2' ]) return child
def testGoodHostsFilename(self): tmp_name = '/tmp/polysh_tests.%d' % (os.getpid()) tmp = open(tmp_name, 'w', 0600) print >> tmp, 'localhost # Comment' print >> tmp, '127.0.0.1' print >> tmp, '# Ignore me' print >> tmp, 'localhost.' tmp.close() child = launch_polysh(['--hosts-file=%s' % (tmp_name)]) child.expect('ready \(3\)> ') child.sendeof() child.expect(pexpect.EOF) os.remove(tmp_name)
def testHole(self): child = launch_polysh(['--ssh=sh;:'] + ['a'] * 100) child.expect('ready \(100\)> ') child.sendline(':disable *1*') child.expect('ready \(81\)> ') child.sendline('exit') child.expect('ready \(0\)> ') child.sendline(':enable') child.expect('ready \(19\)> ') child.sendline(':purge') child.expect('ready \(19\)> ') for i in xrange(20, 101): child.sendline(':add a') child.expect('ready \(%d\)> ' % i) child.sendline(':quit') child.expect(pexpect.EOF)
def testPurge(self): child = launch_polysh(['localhost'] * 3) child.expect('ready \(3\)> ') child.sendline(':disable localhost#*') child.expect('ready \(1\)> ') child.sendline('kill -9 $$') child.expect('ready \(0\)> ') child.sendline(':enable') child.expect('ready \(2\)> ') child.sendline(':pur\t\t') child.expect('ready \(2\)> ') child.sendline(':list') child.expect('localhost#1 enabled idle:') child.expect('localhost#2 enabled idle:') child.expect('ready \(2\)> ') child.sendeof() child.expect(pexpect.EOF)
def testSetDebug(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline(':set_debug') child.expect('Expected at least a letter') child.sendline(':set_debug word') child.expect("Expected 'y' or 'n', got: word") child.sendline(':set_debug \ty\t\t') child.expect('ready \(1\)> ') child.sendline('echo "te""st"') child.expect('\[dbg\] localhost\[idle\]: state => running') child.expect('\[dbg\] localhost\[running\]: <== echo "te""st"') child.expect('\[dbg\] localhost\[running\]: ==> test') child.expect('\033\[1;36mlocalhost : \033\[1;mtest') child.expect('\[dbg\] localhost\[running\]: state => idle') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF)
def testHidePassword(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline('# passwordnotprotected') child.expect('ready \(1\)> ') child.sendline(':set_debug y') child.sendline(':set_log /dev/nul\t') child.sendline(':hide_password') child.expect('Debugging disabled') child.expect('Logging disabled') child.expect('ready \(1\)> ') child.sendline('# passwordprotected') child.expect('ready \(1\)> ') child.sendline('echo password\t') child.expect('passwordnotprotected') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF)
def testLocalCommand(self): child = launch_polysh(['localhost']) child.expect('ready \(1\)> ') child.sendline('cat') child.expect('waiting \(1/1\)> ') child.sendline('!ech\t te""st') child.expect('test') child.sendline(':send_ctrl d') child.expect('ready \(1\)> ') child.sendline('!exit 42') child.expect('Child returned 42') child.expect('ready \(1\)> ') child.sendline('!python -c "import os; os.kill(os.getpid(), 9)"') child.expect('Child was terminated by signal 9') child.expect('ready \(1\)> ') child.sendline(':chdir /does/not/exist') child.expect("\[Errno 2\] .*: '/does/not/exist'") child.sendline(':chdir /usr/sbi\t/does/not/exist') child.expect('/usr/sbin') child.expect('ready \(1\)> ') child.sendeof() child.expect(pexpect.EOF)
def testUpload(self): tmp_dir = tempfile.mkdtemp() local = tmp_dir + '/local' child = launch_polysh(['localhost'] * 5) child.expect('ready \(5\)> ') child.sendline(':upload') child.expect('No local path given') child.expect('ready \(5\)> ') child.sendline("cd %s" % tmp_dir) child.expect('ready \(5\)> ') child.sendline('!mkdir %s' % local) child.expect('ready \(5\)> ') child.sendline('!yes "$(dmesg)" | head -c 20m > %s/file' % local) child.expect('ready \(5\)> ') child.sendline('!cd %s && sha1sum file > SHA1SUM' % local) child.expect('ready \(5\)> ') child.sendline(':export_vars') child.expect('ready \(5\)> ') child.sendline('mkdir $POLYSH_RANK') child.expect('ready \(5\)> ') child.sendline('cd $POLYSH_RANK') child.expect('ready \(5\)> ') child.sendline(':upload %s/fil\t' % local) child.expect('Done transferring 20981760 bytes') child.expect('ready \(5\)> ') child.sendline('sha1sum file > SHA1SUM') child.expect('ready \(5\)> ') cat = 'cat %s/*/SHA1SUM' % tmp_dir wc_output = shell_output('%s | wc | tr -s " "' % cat) self.assertEqual(wc_output, '6 12 282') uniq_wc_output = shell_output('%s | uniq | wc | tr -s " "' % cat) self.assertEqual(uniq_wc_output, '1 2 47') child.sendline("!rm -fr '%s'" % tmp_dir) child.expect('ready \(5\)> ') child.sendline(':quit') child.expect(pexpect.EOF)
def testInvalidCommandStdin(self): child = launch_polysh(["localhost", "--command=date"], input_data="uptime") child.expect("--command and reading from stdin are incompatible") child.expect(pexpect.EOF)
def testInvalidCommandStdin(self): child = launch_polysh(['localhost', '--command=date'], input_data='uptime') child.expect('--command and reading from stdin are incompatible') child.expect(pexpect.EOF)
def testBadHostsFilename(self): child = launch_polysh(['--hosts-file=do not exist/at all']) child.expect('error') child.expect(pexpect.EOF)
def CommandCode(command, code): child = launch_polysh(["--command=%s" % command] + ["localhost"] * 5) child.expect(pexpect.EOF) while child.isalive(): child.wait() self.assertEqual(child.exitstatus, code)
def testCommandNormal(self): child = launch_polysh(["--command=echo text", "localhost"]) child.expect("\033\[1;36mlocalhost : \033\[1;mtext") child.expect(pexpect.EOF)
def start_child(): child = launch_polysh(args) child.expect('ready \(%d\)> ' % (nr_localhost)) return child
def testCommandIntr(self): child = launch_polysh(["--command=echo text; cat", "localhost"]) child.expect("\033\[1;36mlocalhost : \033\[1;mtext") child.sendintr() child.expect(pexpect.EOF)
def testSimpleCommandStdin(self): child = launch_polysh(["localhost"], input_data="echo line") child.expect("localhost : line") child.expect(pexpect.EOF)