"Fossies" - the Fresh Open Source Software Archive

Member "polysh-polysh-0.13/tests/tests/control_commands.py" (11 May 2020, 11105 Bytes) of package /linux/privat/polysh-polysh-0.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "control_commands.py" see the Fossies "Dox" file reference documentation.

    1 """Polysh - Tests - Control Commands
    2 
    3 Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
    4 Copyright (c) 2018 InnoGames GmbH
    5 """
    6 # This program is free software: you can redistribute it and/or modify
    7 # it under the terms of the GNU General Public License as published by
    8 # the Free Software Foundation, either version 2 of the License, or
    9 # (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   18 
   19 import os
   20 import unittest
   21 import pexpect
   22 from polysh_tests import launch_polysh
   23 
   24 
   25 class TestControlCommands(unittest.TestCase):
   26     def testControl(self):
   27         child = launch_polysh(['localhost'])
   28         child.expect('ready \(1\)> ')
   29         child.sendline(':')
   30         child.expect('ready \(1\)> ')
   31         child.sendline('echo a; echo; echo; echo; echo; echo; echo; echo b')
   32         child.expect('a')
   33         child.expect('b')
   34         child.expect('ready \(1\)> ')
   35         child.sendline(':unknown')
   36         child.expect('Unknown control command: unknown')
   37         child.expect('ready \(1\)> ')
   38         child.sendline('cat')
   39         child.expect('waiting \(1/1\)> ')
   40         child.sendline(':send_ctrl \tz\t\t')
   41         child.expect('ready \(1\)> ')
   42         child.sendline(':send_ctrl')
   43         child.expect('Expected at least a letter')
   44         child.expect('ready \(1\)> ')
   45         child.sendline(':send_ctrl word')
   46         child.expect('Expected a single letter, got: word')
   47         child.expect('ready \(1\)> ')
   48         child.sendline('fg')
   49         child.expect('waiting \(1/1\)> ')
   50         child.sendline(':send_ctrl d')
   51         child.expect('ready \(1\)> ')
   52         child.sendline('sleep 1h')
   53         child.expect('waiting \(1/1\)> ')
   54         child.sendcontrol('c')
   55         child.expect('ready \(1\)> ')
   56         child.sendline('cat')
   57         child.expect('waiting \(1/1\)> ')
   58         child.sendcontrol('d')
   59         child.expect('ready \(1\)> ')
   60         child.sendline('cat')
   61         child.expect('waiting \(1/1\)> ')
   62         child.sendline(':disabl\tlocal* not_found\t')
   63         child.expect('not_found not found\r\n')
   64         child.expect('ready \(0\)> ')
   65         child.sendline(':enable local\t')
   66         child.expect('waiting \(1/1\)> ')
   67         child.sendline(':list')
   68         child.expect('localhost enabled running:')
   69         child.expect('waiting \(1/1\)> ')
   70         child.sendline(':list local\t')
   71         child.expect('localhost enabled running:')
   72         child.expect('waiting \(1/1\)> ')
   73         child.sendline(':list unknown')
   74         child.expect('unknown not found')
   75         child.expect('waiting \(1/1\)> ')
   76         child.sendline(':send_ctrl c')
   77         child.expect('ready \(1\)> ')
   78         child.sendline(':quit')
   79         child.expect(pexpect.EOF)
   80 
   81     def testReconnect(self):
   82         child = launch_polysh(['localhost'] * 2)
   83         child.expect('ready \(2\)> ')
   84         child.sendline(':disable localhost')
   85         child.sendline('exit')
   86         child.expect('exit\r\n')
   87         child.expect('ready \(0\)>')
   88         child.sendline(':reconnect l\t')
   89         child.sendline(':enable')
   90         child.expect('ready \(2\)> ')
   91         child.sendeof()
   92         child.expect(pexpect.EOF)
   93 
   94     def testListManipulation(self):
   95         child = launch_polysh(['localhost'])
   96         child.expect('ready \(1\)> ')
   97         child.sendline(':add localhost')
   98         child.expect('ready \(2\)> ')
   99         child.sendline(':rename $(echo newname)')
  100         child.expect('ready \(2\)> ')
  101         child.sendline('date')
  102         child.expect('newname')
  103         child.expect('newname')
  104         child.expect('ready \(2\)> ')
  105         child.sendline(':rename $EMPTY_VARIABLE')
  106         child.expect('ready \(2\)> ')
  107         child.sendline('date')
  108         child.expect('localhost')
  109         child.expect('localhost')
  110         child.expect('ready \(2\)> ')
  111         child.sendline(':rename $(echo newname)')
  112         child.expect('ready \(2\)> ')
  113         child.sendline('date')
  114         child.expect('newname')
  115         child.expect('newname')
  116         child.expect('ready \(2\)> ')
  117         child.sendline(':disable newname')
  118         child.sendline(':purge')
  119         child.sendline(':enable *')
  120         child.expect('ready \(1\)> ')
  121         child.sendline(':rename')
  122         child.expect('ready \(1\)> ')
  123         child.sendline('date')
  124         child.expect('localhost :')
  125         child.expect('ready \(1\)> ')
  126         child.sendeof()
  127         child.expect(pexpect.EOF)
  128 
  129     def testLocalCommand(self):
  130         child = launch_polysh(['localhost'])
  131         child.expect('ready \(1\)> ')
  132         child.sendline('cat')
  133         child.expect('waiting \(1/1\)> ')
  134         child.sendline('!ech\t te""st')
  135         child.expect('test')
  136         child.sendline(':send_ctrl d')
  137         child.expect('ready \(1\)> ')
  138         child.sendline('!exit 42')
  139         child.expect('Child returned 42')
  140         child.expect('ready \(1\)> ')
  141         child.sendline('!python -c "import os; os.kill(os.getpid(), 9)"')
  142         child.expect('Child was terminated by signal 9')
  143         child.expect('ready \(1\)> ')
  144         child.sendline(':chdir /does/not/exist')
  145         child.expect("\[Errno 2\] .*: '/does/not/exist'")
  146         child.sendline(':chdir /usr/sbi\t/does/not/exist')
  147         child.expect('/usr/sbin')
  148         child.expect('ready \(1\)> ')
  149         child.sendeof()
  150         child.expect(pexpect.EOF)
  151 
  152     def testLocalAbsPathCompletion(self):
  153         child = launch_polysh(['localhost'])
  154         child.expect('ready \(1\)> ')
  155         child.sendline('echo /dev/nul\t')
  156         child.expect('\033\[1;36mlocalhost : \033\[1;m/dev/null')
  157         child.expect('ready \(1\)> ')
  158         child.sendline('echo /sbi\t')
  159         child.expect('\033\[1;36mlocalhost : \033\[1;m/sbin/')
  160         child.expect('ready \(1\)> ')
  161         child.sendeof()
  162         child.expect(pexpect.EOF)
  163 
  164     def testLogOutput(self):
  165         child = launch_polysh(['--log-file=/', 'localhost'])
  166         child.expect("\[Errno 21\].*'/'")
  167         child.expect(pexpect.EOF)
  168         child = launch_polysh(['--log-file=/cannot_write', 'localhost'])
  169         child.expect("\[Errno 13\].*'/cannot_write'")
  170         child.expect(pexpect.EOF)
  171         child = launch_polysh(['--log-file=/dev/full', 'localhost'])
  172         child.sendline('echo something')
  173         child.expect('Exception while writing log: /dev/full')
  174         child.expect('\[Errno 28\]')
  175         child.expect(pexpect.EOF)
  176 
  177         child = launch_polysh(['localhost'])
  178 
  179         def testEcho(msg):
  180             child.expect('ready \(1\)> ')
  181             child.sendline('echo %s' % msg)
  182             child.expect('\033\[1;36mlocalhost : \033\[1;m%s' % msg)
  183         testEcho('not logging')
  184         child.sendline(':set_log')
  185         testEcho('still not logging')
  186         child.sendline('!rm -f /tmp/polysh_test.log')
  187         testEcho('still not logging')
  188         child.sendline(':set_log /tmp/polysh_test.log')
  189         testEcho('now logging')
  190         testEcho('still logging')
  191         child.sendline(':set_log')
  192         testEcho('back to no logging')
  193         child.sendline(':set_log /tmp/polysh_test.lo\t')
  194         testEcho('appended to the log')
  195         child.sendline(':set_log')
  196         child.expect('ready \(1\)> ')
  197         child.sendline(':set_log /no-permission')
  198         child.expect("[Errno 13] .*: '/no-permission'")
  199         child.expect('Logging disabled')
  200         child.expect('ready \(1\)> ')
  201         child.sendeof()
  202         child.expect(pexpect.EOF)
  203 
  204         EXPECTED_LOG = """
  205 > echo now logging
  206 localhost : now logging
  207 > echo still logging
  208 localhost : still logging
  209 > :set_log
  210 > echo appended to the log
  211 localhost : appended to the log
  212 > :set_log
  213 """.strip()
  214         log = open('/tmp/polysh_test.log')
  215         log_lines = [l for l in log.readlines() if not l.startswith('[dbg] ')]
  216         actual_log = ''.join(log_lines).strip()
  217         self.assertEqual(actual_log, EXPECTED_LOG)
  218         os.remove('/tmp/polysh_test.log')
  219 
  220     def testSetDebug(self):
  221         child = launch_polysh(['localhost'])
  222         child.expect('ready \(1\)> ')
  223         child.sendline(':set_debug')
  224         child.expect('Expected at least a letter')
  225         child.sendline(':set_debug word')
  226         child.expect("Expected 'y' or 'n', got: word")
  227         child.sendline(':set_debug \ty\t\t')
  228         child.expect('ready \(1\)> ')
  229         child.sendline('echo "te""st"')
  230         child.expect('\[dbg\] localhost\[idle\]: state => running')
  231         child.expect('\[dbg\] localhost\[running\]: <== echo "te""st"')
  232         child.expect('\[dbg\] localhost\[running\]: ==> test')
  233         child.expect('\033\[1;36mlocalhost : \033\[1;mtest')
  234         child.expect('\[dbg\] localhost\[running\]: state => idle')
  235         child.expect('ready \(1\)> ')
  236         child.sendeof()
  237         child.expect(pexpect.EOF)
  238 
  239     def testHidePassword(self):
  240         child = launch_polysh(['localhost'])
  241         child.expect('ready \(1\)> ')
  242         child.sendline('# passwordnotprotected')
  243         child.expect('ready \(1\)> ')
  244         child.sendline(':set_debug y')
  245         child.sendline(':set_log /dev/nul\t')
  246         child.sendline(':hide_password')
  247         child.expect('Debugging disabled')
  248         child.expect('Logging disabled')
  249         child.expect('ready \(1\)> ')
  250         child.sendline('# passwordprotected')
  251         child.expect('ready \(1\)> ')
  252         child.sendline('echo password\t')
  253         child.expect('passwordnotprotected')
  254         child.expect('ready \(1\)> ')
  255         child.sendeof()
  256         child.expect(pexpect.EOF)
  257 
  258     def testResetPrompt(self):
  259         child = launch_polysh(['localhost'])
  260         child.expect('ready \(1\)> ')
  261         child.sendline('bash')
  262         child.sendline(':reset_prompt l\t')
  263         child.expect('ready \(1\)> ')
  264         child.sendline(':quit')
  265         child.expect(pexpect.EOF)
  266 
  267     def testPurge(self):
  268         child = launch_polysh(['localhost'] * 3)
  269         child.expect('ready \(3\)> ')
  270         child.sendline(':disable localhost#*')
  271         child.expect('ready \(1\)> ')
  272         child.sendline('kill -9 $$')
  273         child.expect('ready \(0\)> ')
  274         child.sendline(':enable')
  275         child.expect('ready \(2\)> ')
  276         child.sendline(':pur\t\t')
  277         child.expect('ready \(2\)> ')
  278         child.sendline(':list')
  279         child.expect('localhost#1 enabled idle:')
  280         child.expect('localhost#2 enabled idle:')
  281         child.expect('ready \(2\)> ')
  282         child.sendeof()
  283         child.expect(pexpect.EOF)
  284 
  285     def testPrintReadBuffer(self):
  286         child = launch_polysh(['--ssh=echo message; sleep'] + ['2h'] * 3)
  287         child.expect('waiting \(3/3\)> ')
  288         child.sendline(':show_read_buffer \t*')
  289         for i in range(3):
  290             child.expect('\033\[1;[0-9]+m2h[ #][ 12] : \033\[1;mmessage')
  291         child.expect('waiting \(3/3\)> ')
  292         child.sendintr()
  293         child.expect(pexpect.EOF)