def test_execute(self): status = util.execute(['sleep', '1']) self.assertEqual(status, 0) process = util.execute(['sleep', '3'], noWait=True) self.assertTrue(isinstance(process, subprocess.Popen)) process.kill()
def test_execute_NoneType(self): try: util.execute([0, 'foo', None]) except Exception as ex: assert str(ex).startswith('Wrong input.') else: self.fail('Should have thrown Exception.')
def xtest_execute_apple_specific(self): status, err = util.execute(['sleep'], withStderr=True) self.assertEqual(status, 1) self.assertEqual(err, 'usage: sleep seconds\n') status, out = util.execute(['sleep'], withOutput=True) self.assertEqual(status, 1) self.assertEqual(out, 'usage: sleep seconds\n')
def generate_ssh_keypair(key_filename): try: os.remove(key_filename) os.remove(key_filename + '.pub') except(OSError): pass ssh_cmd = 'ssh-keygen -f %s -N "" -q' % key_filename execute(ssh_cmd, shell=True)
def generateSshKeyPair(keyFilename): try: os.remove(keyFilename) os.remove(keyFilename + '.pub') except(OSError): pass sshCmd = 'ssh-keygen -f %s -N "" -q' % keyFilename execute(sshCmd, shell=True)
def _launch_process(self, target_script, exports=None, name=None): '''Returns launched process as subprocess.Popen instance. ''' try: fn = self._write_target_script_to_file(target_script, name) except Exception as e: util.printError('Writing script "%s" to file failed with: "%s". Retrying with random filename.' % (name, e)) fn = self._write_target_script_to_file(target_script) current_dir = os.getcwd() new_dir = util.get_temporary_storage_dir() os.chdir(new_dir) if 'HOME' not in os.environ: if exports is None: exports = {} exports['HOME'] = os.path.expanduser('~') try: process = util.execute(fn, noWait=True, extra_env=exports, withStderr=True) finally: os.chdir(current_dir) return process
def _ssh_execute_cli(cmd, host, user, sshKey, tcp_timeout, **kwargs): def _appendToSshCommandFromKwargs(keyword, append): if kwargs.get(keyword, False): sshCmd.append(append) try: del kwargs[keyword] except: pass def _removeInvalidExecuteKwargs(): for keyword in ['verboseLevel', 'verboseThreshold', 'password']: try: del kwargs[keyword] except: pass sshCmd = ['ssh', '-p', SSH_PORT, '-o', 'ConnectTimeout=%s' % tcp_timeout, '-o', 'StrictHostKeyChecking=no'] if sshKey and os.path.isfile(sshKey): sshCmd.append('-i') sshCmd.append(sshKey) for keyAppend in [('sshVerb', '-v'), ('sshQuiet', '-q'), ('pseudoTTY', '-t -t')]: _appendToSshCommandFromKwargs(*keyAppend) sshCmd.append('%s@%s' % (user, host)) sshCmd.append(cmd) _removeInvalidExecuteKwargs() return execute(sshCmd, **kwargs)
def __run_test(self, name, config={}, fail=False): cmd = ['make', name, "TESTOPTS=%s" % self._build_test_opts(config)] print('executing: %s ' % cmd) rc = execute(cmd) if rc != 0: self.failed_tests.append((name, config.get('connectors', ''))) if fail: raise Exception('Failed running test: %s' % name)
def test_sanitize_env_on_execute(self): try: util.execute(['true'], extra_env={'a': None}) except TypeError: self.fail('Should not raise TypeError.') def _sanitize_env_mocked(env): return env _sanitize_env_save = util._sanitize_env util._sanitize_env = Mock(side_effect=_sanitize_env_mocked) try: self.assertRaises(TypeError, util.execute, *(['true'],), **{'extra_env': {'a': None}}) try: util.execute(['true'], extra_env={'a': None}) except TypeError as ex: assert 'execve() arg 3 contains a non-string value' == str(ex) finally: util._sanitize_env = _sanitize_env_save
def _ssh_execute_cli(cmd, host, user, sshKey, tcp_timeout, **kwargs): def _appendToSshCommandFromKwargs(keyword, append): if kwargs.get(keyword, False): sshCmd.append(append) try: del kwargs[keyword] except: pass def _removeInvalidExecuteKwargs(): for keyword in ['verboseLevel', 'verboseThreshold', 'password']: try: del kwargs[keyword] except: pass sshCmd = ['ssh', '-p', SSH_PORT, '-o', 'ConnectTimeout=%s' % tcp_timeout, '-o', 'StrictHostKeyChecking=no'] if sshKey and os.path.isfile(sshKey): sshCmd.append('-i') sshCmd.append(sshKey) tty = kwargs.get('pseudoTTY', False) for keyAppend in [('sshVerb', '-v'), ('sshQuiet', '-q'), ('pseudoTTY', '-t -t')]: _appendToSshCommandFromKwargs(*keyAppend) sshCmd.append('--') sshCmd.append('%s@%s' % (user, host)) sshCmd.append(cmd) _removeInvalidExecuteKwargs() rc, stderr = execute(sshCmd, **kwargs) if not tty and 'sudo' in stderr and 'tty' in stderr: printError('sudo require tty: "%s". Retrying with tty' % stderr) sshCmd.insert(1, '-tt') return execute(sshCmd, **kwargs) return rc, stderr
def _scp_cli(src, dest, sshKey=None, timeout=CONNECT_TIMEOUT, **kwargs): scpCmd = ['scp', '-P', SSH_PORT, '-r', '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=%i' % timeout] if sshKey and os.path.isfile(sshKey): scpCmd.append('-i') scpCmd.append(sshKey) scpCmd.append(src) scpCmd.append(dest) return execute(scpCmd, **kwargs)
def test_sanitize_env_on_execute(self): try: util.execute(['true'], extra_env={'a': None}) except TypeError: self.fail('Should not raise TypeError.') def _sanitize_env_mocked(env): return env _sanitize_env_save = util._sanitize_env util._sanitize_env = Mock(side_effect=_sanitize_env_mocked) try: self.assertRaises(TypeError, util.execute, *(['true'], ), **{'extra_env': { 'a': None }}) try: util.execute(['true'], extra_env={'a': None}) except TypeError as ex: assert 'execve() arg 3 contains a non-string value' == str(ex) finally: util._sanitize_env = _sanitize_env_save
def _executeRaiseOnError(self, cmd): res = util.execute(cmd.split(' ')) if res: raise ExecutionException('Failed executing: %s' % cmd)