def test_simple_spawn(): stdout = StringIO() status, rc = run.run_pexpect( ['ls', '-la'], HERE, {}, stdout, cancelled_callback=lambda: False, ) assert status == 'successful' assert rc == 0
def test_env_vars(): stdout = cStringIO.StringIO() status, rc = run.run_pexpect( ['python', '-c', 'import os; print os.getenv("X_MY_ENV")'], HERE, {'X_MY_ENV': 'abc123'}, stdout, cancelled_callback=lambda: False, ) assert status == 'successful' assert rc == 0 assert 'abc123' in stdout.getvalue()
def test_error_rc(): stdout = cStringIO.StringIO() status, rc = run.run_pexpect( ['ls', '-nonsense'], HERE, {}, stdout, cancelled_callback=lambda: False, ) assert status == 'failed' # I'd expect 2, but we shouldn't risk making this test platform-dependent assert rc > 0
def test_manual_cancellation(): stdout = StringIO() status, rc = run.run_pexpect( ['python', '-c', 'print raw_input("Password: ")'], HERE, {}, stdout, cancelled_callback=lambda: True, # this callable will cause cancellation # the lack of password inputs will cause stdin to hang pexpect_timeout=0, ) assert status == 'canceled'
def test_password_prompt(): stdout = cStringIO.StringIO() expect_passwords = OrderedDict() expect_passwords[re.compile(r'Password:\s*?$', re.M)] = 'secret123' status, rc = run.run_pexpect([ 'python', '-c', 'import time; print raw_input("Password: "); time.sleep(.05)' ], HERE, {}, stdout, cancelled_callback=lambda: False, expect_passwords=expect_passwords) assert status == 'successful' assert rc == 0 assert 'secret123' in stdout.getvalue()
def test_job_timeout(): stdout = StringIO() extra_update_fields={} status, rc = run.run_pexpect( ['python', '-c', 'import time; time.sleep(5)'], HERE, {}, stdout, cancelled_callback=lambda: False, extra_update_fields=extra_update_fields, job_timeout=.01, pexpect_timeout=0, ) assert status == 'failed' assert extra_update_fields == {'job_explanation': 'Job terminated due to timeout'}
def test_cancel_callback_error(): stdout = cStringIO.StringIO() def bad_callback(): raise Exception('unique exception') extra_fields = {} status, rc = run.run_pexpect(['sleep', '2'], HERE, {}, stdout, cancelled_callback=bad_callback, extra_update_fields=extra_fields) assert status == 'error' assert rc == 0 assert extra_fields[ 'job_explanation'] == "System error during job execution, check system logs"
def run_pexpect(cls, pexpect_args, *args, **kw): isolated_ssh_path = None try: if all([ getattr(settings, 'AWX_ISOLATED_KEY_GENERATION', False) is True, getattr(settings, 'AWX_ISOLATED_PRIVATE_KEY', None) ]): isolated_ssh_path = tempfile.mkdtemp(prefix='awx_isolated', dir=settings.AWX_PROOT_BASE_PATH) os.chmod(isolated_ssh_path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) isolated_key = os.path.join(isolated_ssh_path, '.isolated') ssh_sock = os.path.join(isolated_ssh_path, '.isolated_ssh_auth.sock') run.open_fifo_write(isolated_key, settings.AWX_ISOLATED_PRIVATE_KEY) pexpect_args = run.wrap_args_with_ssh_agent(pexpect_args, isolated_key, ssh_sock, silence_ssh_add=True) return run.run_pexpect(pexpect_args, *args, **kw) finally: if isolated_ssh_path: shutil.rmtree(isolated_ssh_path)