def test_su(): _su = sudo.Su('www-data').command(Cmd('echo', 'test test')) s, params = compiler(_su, None) assert s == 'su -c " echo \'test test\' " www-data' _su.shell('/bin/bash') s, params = compiler(_su, None) assert s == 'su -s /bin/bash -c " echo \'test test\' " www-data'
def test_mktemp(): mktemp = file.Mktemp() s, params = compiler(mktemp, None) assert s == 'mktemp XXXXX' mktemp = file.Mktemp().directory().tmpdir() s, params = compiler(mktemp, None) assert s == 'mktemp --directory --tmpdir XXXXX'
def test_rabbitmqadmin_declare_binding(): cmd = rabbitmq.RabbitmqAdmin().declare_binding('src', 'dst', routing_key='key') s, params = compiler(cmd, None) assert s == 'rabbitmqadmin declare binding source=src destination=dst routing_key=key' cmd = rabbitmq.RabbitmqAdmin().declare_binding('src', 'dst') s, params = compiler(cmd, None) assert s == 'rabbitmqadmin declare binding source=src destination=dst'
def test_rabbitmqadmin_list(): cmd = rabbitmq.RabbitmqAdmin().list_exchanges() s, params = compiler(cmd, None) assert s == 'rabbitmqadmin list exchanges' cmd = rabbitmq.RabbitmqAdmin().list_queues() s, params = compiler(cmd, None) assert s == 'rabbitmqadmin list queues' cmd = rabbitmq.RabbitmqAdmin().list_bindings() s, params = compiler(cmd, None) assert s == 'rabbitmqadmin list bindings'
def test_mkdir(): mkdir = file.Mkdir('/tmp/test1') s, params = compiler(mkdir, None) assert s == 'mkdir /tmp/test1' mkdir = file.Mkdir('/tmp/test test') s, params = compiler(mkdir, None) assert s == 'mkdir \'/tmp/test test\'' mkdir = file.Mkdir('/tmp/test\'s test') s, params = compiler(mkdir, None) assert s == 'mkdir \'/tmp/test\'"\'"\'s test\''
def test_pipe(): pipe = cmd.Pipe('test1', 'test2') s, params = compiler(pipe, None) assert s == 'test1 | test2' pipe = cmd.OutputRedirect('test1', 'test2') s, params = compiler(pipe, None) assert s == 'test1 > test2' pipe = cmd.OutputRedirectAppend('test1', 'test2') s, params = compiler(pipe, None) assert s == 'test1 >> test2'
def test_sudo(): _sudo = sudo.Sudo('www-data').command(Cmd( 'echo', 'test test', )) s, params = compiler(_sudo, None) assert s == 'sudo -S -u www-data echo \'test test\''
def run(self, cmd: Cmd, **kwargs): rcmd = self._prepare_cmd(cmd, **kwargs) # TODO: make splitted c, params = compiler(rcmd, self) logger.debug('{host}: {cmd}'.format(host=self.host, cmd=c)) # return ctx.run(c, **kwargs) process = Popen( shlex.split(c), # shell=True, # executable='/bin/sh', # env=env, stdout=PIPE, stderr=PIPE, stdin=PIPE, cwd=self._cwd ) exception = None stdout = '' stderr = '' while True: try: stdout, stderr = process.communicate() break except KeyboardInterrupt as e: process.send_signal(signal.SIGINT) exception = e break except BaseException as e: exception = e break warn = kwargs.get('warn', False) if warn and stderr: logger.warning(stderr.decode()) return Result(stdout.decode(), stderr.decode(), process.returncode)
def run(self, cmd: Cmd, **kwargs): rcmd = self._prepare_cmd(cmd, **kwargs) c, params = compiler(rcmd, self) logger.debug('{host}: {cmd}'.format(host=self.host, cmd=c)) stdin, stdout, stderr, returncode = self.connection.exec_command(c) warn = kwargs.get('warn', False) if warn and stderr: logger.warning(stderr) return Result(stdout, stderr, returncode)
def test_grep(): grep = file.Grep('test') s, params = compiler(grep, None) assert s == 'grep -i test'
def test_chown(): chown = file.Chown('/tmp/test1', 'user', 'user').recursive() s, params = compiler(chown, None) assert s == 'chown user:user -R /tmp/test1'
def test_stat(): stat = file.Stat('/tmp/test1') s, params = compiler(stat, None) assert s == 'stat -t /tmp/test1'
def test_cp(): cp = file.Cp('/tmp/file1', '/tmp/file 2') s, params = compiler(cp, None) assert s == 'cp /tmp/file1 \'/tmp/file 2\''
def test_mv(): mv = file.Mv('/tmp/file1', '/tmp/file 2') s, params = compiler(mv, None) assert s == 'mv /tmp/file1 \'/tmp/file 2\''
def test_cd(): cd = file.Cd('/tmp') s, params = compiler(cd, None) assert s == 'cd /tmp'
def test_iptables_append(): rule = iptables.Rule().not_proto('tcp').source('192.168.1.1/24').jump('ACCEPT') cmd = iptables.Iptables().append('test111', rule) s, params = compiler(cmd, None) assert s == 'iptables --append test111 ! --proto tcp --source 192.168.1.1/24 --jump ACCEPT'
def test_iptables_list(): cmd = iptables.Iptables().list('test111').numeric() s, params = compiler(cmd, None) assert s == 'iptables --numeric --list test111'
def test_cmd(): c = cmd.Cmd('echo', 'just string').redir('/tmp/test1') s, params = compiler(c, None) assert s == 'echo \'just string\' > /tmp/test1'
def test_diff(): diff = file.Diff().unified(5).files('/tmp/file1', '/tmp/file2') s, params = compiler(diff, None) assert s == 'diff --unified=5 /tmp/file1 /tmp/file2'
def test_rabbitmqctl_change_password(): cmd = rabbitmq.RabbitmqCtl().change_password('test', 'test') s, params = compiler(cmd, None) assert s == 'rabbitmqctl change_password test test'
def test_rabbitmqctl_add_user(): cmd = rabbitmq.RabbitmqCtl().add_user('test', 'test') s, params = compiler(cmd, None) assert s == 'rabbitmqctl add_user test test'
def test_rabbitmqadmin_declare_exchange(): cmd = rabbitmq.RabbitmqAdmin().declare_exchange('test', 'fanout') s, params = compiler(cmd, None) assert s == 'rabbitmqadmin declare exchange name=test type=fanout'
def test_rabbitmqctl_list_users(): cmd = rabbitmq.RabbitmqCtl().list_users() s, params = compiler(cmd, None) assert s == 'rabbitmqctl list_users'
def test_sed(): grep = file.Sed('test1', 'test2', '/tmp/test1') s, params = compiler(grep, None) assert s == 'sed \'s/test1/test2/g\' -i /tmp/test1'
def test_getent_shadow_root(): cmd = getent.Getent(getent.Database.SHADOW, 'root') s, params = compiler(cmd, None) assert s == 'getent shadow root'
def test_git_clone(): cmd = git.Git().clone('/tmp/test') s, params = compiler(cmd, None) assert s == 'git clone /tmp/test ./'
def test_getent_passwd(): db = getent.Database.PASSWD cmd = getent.Getent(db) s, params = compiler(cmd, None) assert s == 'getent passwd'