def copy_dockerfile(): try: # copy dockerfile dockerfile = context.config.userdata['DOCKERFILE'] dockerfile_dir = os.path.dirname(dockerfile) # create remote directory Runner(inventory_file=ansible_cfg, module_name='file', module_args='dest={0} state=directory'.format( context.remote_dir)).run() # copy dockerfile Runner(inventory_file=ansible_cfg, module_name='copy', module_args='src={0} dest={1}'.format( dockerfile, context.remote_dir)).run() # copy files from dockerfile f_in = open(dockerfile) for path in re.findall('(?:ADD|COPY) ([^ ]+) ', f_in.read()): for glob_path in glob.glob(os.path.join(dockerfile_dir, path)): # TODO Is there a nicer way to keep permissions? Runner( inventory_file=ansible_cfg, module_name='copy', module_args='src={0} dest={1} directory_mode mode={2}'. format(glob_path, context.remote_dir, oct(stat.S_IMODE( os.stat(glob_path).st_mode)))).run() except Exception as e: logging.warning("copy_dockerfile:%s", e)
def test_run_command_idle_timeout(rc): rc.command = ['pwd'] rc.idle_timeout = 0.0000001 runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'timeout' assert exitcode == 254
def test_run_command_explosive_finished_callback(rc): def boom(*args): raise Exception('boom') rc.command = ['pwd'] runner = Runner(config=rc, finished_callback=boom) with pytest.raises(Exception): runner.run()
def test_run_command_explosive_cancel_callback(rc): def boom(*args): raise Exception('boom') rc.command = ['sleep', '1'] runner = Runner(config=rc, cancel_callback=boom) with pytest.raises(Exception): runner.run()
def test_run_command_events_missing(rc): rc.command = ['pwd'] runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 with pytest.raises(AnsibleRunnerException): list(runner.events)
def test_run_command_no_stats(rc): rc.command = ['pwd'] runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 with pytest.raises(AnsibleRunnerException): runner.stats
def test_status_callback_interface(rc): runner = Runner(config=rc) assert runner.status == 'unstarted' runner.status_handler = mock.Mock() runner.status_callback("running") assert runner.status_handler.call_count == 1 runner.status_handler.assert_called_with('running') assert runner.status == 'running'
def test_event_callback_interface_calls_event_handler_for_verbose_event(rc): rc.ident = "testident" event_handler = mock.Mock() runner = Runner(config=rc, event_handler=event_handler) with mock.patch('os.mkdir', mock.Mock()): runner.event_callback(dict(uuid="testuuid", event='verbose', counter=0)) assert event_handler.call_count == 1 event_handler.assert_called_with(dict(runner_ident='testident', counter=0, uuid='testuuid', event='verbose'))
def test_run_command_cancel_callback(rc): def cancel(*args): return True rc.command = ['sleep','1'] runner = Runner(config=rc, cancel_callback=cancel) status, exitcode = runner.run() assert status == 'canceled' assert exitcode == 254
def test_status_callback_interface(rc): runner = Runner(config=rc) assert runner.status == 'unstarted' runner.status_handler = mock.Mock() runner.status_callback("running") assert runner.status_handler.call_count == 1 runner.status_handler.assert_called_with(dict(status='running', runner_ident=str(rc.ident)), runner_config=runner.config) assert runner.status == 'running'
def test_run_command_finished_callback(rc): finished_callback = MagicMock() rc.command = ['sleep', '1'] runner = Runner(config=rc, finished_callback=finished_callback) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 finished_callback.assert_called_with(runner)
def test_verbose_event_created_time(rc): rc.command = ['echo', 'helloworld'] runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 for event in runner.events: assert 'created' in event, event
def test_executable_not_found(rc): rc.command = ['supercalifragilistic'] runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'failed' assert exitcode == 127 events = list(runner.events) assert len(events) == 1 assert 'The command was not found or was not executable: supercalifragilistic' in events[0]['stdout'] # noqa
def test_run_command_stdout_missing(rc): rc.command = ['sleep', '1'] runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 os.unlink(os.path.join(runner.config.artifact_dir, 'stdout')) with pytest.raises(AnsibleRunnerException): list(runner.stdout)
def test_run_command_ansible_event_handler_failure(rc): def event_handler(*args): raise IOError() rc.module = "debug" rc.host_pattern = "localhost" rc.prepare() runner = Runner(config=rc, event_handler=event_handler) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0
def test_set_extra_vars(rc): rc.module = "debug" rc.module_args = "var=test_extra_vars" rc.host_pattern = "localhost" rc.extra_vars = dict(test_extra_vars='hello there') rc.prepare() runner = Runner(config=rc) status, exitcode = runner.run() with open(os.path.join(rc.artifact_dir, 'stdout')) as f: assert 'hello there' in f.read()
def test_event_callback_data_check(rc, mocker): rc.ident = "testident" rc.check_job_event_data = True runner = Runner(config=rc, remove_partials=False) runner.event_handler = mocker.Mock() with pytest.raises(AnsibleRunnerException) as exc: runner.event_callback(dict(uuid="testuuid", counter=0)) assert "Failed to open ansible stdout callback plugin partial data" in str( exc)
def test_stdout_file_no_write(rc, runner_mode): rc.command = ['echo', 'hello_world_marker ' ] # workaround bug in stdout handl wrapper rc.runner_mode = runner_mode rc.suppress_output_file = True runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' for filename in ('stdout', 'stderr'): stdout_path = Path(rc.artifact_dir) / filename assert not stdout_path.exists()
def test_run_command_ansible_event_handler(rc): event_handler = MagicMock() status_handler = MagicMock() rc.module = "debug" rc.host_pattern = "localhost" rc.prepare() runner = Runner(config=rc, event_handler=event_handler, status_handler=status_handler) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 event_handler.assert_called() status_handler.assert_called()
def test_run_command_ansible(rc): rc.module = "debug" rc.host_pattern = "localhost" rc.prepare() runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 assert list(runner.events) != [] assert runner.stats != {} assert list(runner.host_events('localhost')) != [] stdout = runner.stdout assert stdout.read() != ""
def test_get_fact_cache(rc): assert os.path.basename(rc.fact_cache) == 'fact_cache' rc.module = "setup" rc.host_pattern = "localhost" rc.prepare() runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' assert exitcode == 0 print(rc.cwd) assert os.path.exists(os.path.join(rc.artifact_dir, 'fact_cache')) assert os.path.exists(os.path.join(rc.artifact_dir, 'fact_cache', 'localhost')) data = runner.get_fact_cache('localhost') assert data
def run(command): if '{{' in command: command = command.replace("{{", "{{ '{{").replace("}}", "}}' }}") if '=' in command: command = command.replace('=', '\=') logging.info("Running '%s'", command) context.result = Runner(module_name="shell", inventory_file=ansible_cfg, module_args="{0} chdir={1}".format( command, context.remote_dir)).run() # dark means not responding if 'dark' in context.result: print(context.result) for host, values in context.result['contacted'].iteritems(): retvalue = values.get('rc') logging.info("On {0} returned {1}".format(host, retvalue)) if 'cmd' in values: logging.info("cmd: {0}".format(values['cmd'])) if 'stderr' in values: logging.info('stderr:%s', values['stderr']) result = '' if 'msg' in values: logging.info('msg:%s', values['msg']) result = values['msg'] if 'stdout' in values: logging.info('stdout:%s', values['stdout']) result = values['stdout'] if 'failed' in values: assert False return result
def test_cancel_callback_error(rc): def kaboom(): raise Exception('kaboom') rc.command = ['python', '-c', 'print(input("Password: "))'] with pytest.raises(CallbackError): Runner(config=rc, cancel_callback=kaboom).run()
def test_set_extra_vars(rc): rc.module = "debug" rc.module_args = "var=test_extra_vars" rc.host_pattern = "localhost" rc.extra_vars = dict(test_extra_vars='hello there') rc.prepare() runner = Runner(config=rc) status, exitcode = runner.run() # stdout file can be subject to a race condition for _ in iterate_timeout( 30.0, 'stdout file to be written with "hello there" in it', interval=0.2): with open(os.path.join(rc.artifact_dir, 'stdout')) as f: if 'hello there' in f.read(): break
def test_password_prompt(rc): rc.command = ['python', '-c' 'import time; print(input("Password: ")); time.sleep(.05)'] rc.expect_passwords[re.compile(r'Password:\s*?$', re.M)] = 'secret123' status, exitcode = Runner(config=rc).run() assert status == 'successful' assert exitcode == 0 with open(os.path.join(rc.artifact_dir, 'stdout')) as f: assert 'secret123' in f.read()
def test_password_prompt(rc): rc.command = ['python', '-c' 'from __future__ import print_function; import time; print(input("Password: "))'] rc.expect_passwords[re.compile(r'Password:\s*?$', re.M)] = '1234' status, exitcode = Runner(config=rc).run() assert status == 'successful' assert exitcode == 0 with open(os.path.join(rc.artifact_dir, 'stdout')) as f: assert '1234' in f.read()
def test_env_vars(rc, value): rc.command = ['python', '-c', 'import os; print(os.getenv("X_MY_ENV"))'] rc.env = {'X_MY_ENV': value} status, exitcode = Runner(config=rc).run() assert status == 'successful' assert exitcode == 0 with codecs.open(os.path.join(rc.artifact_dir, 'stdout'), 'r', encoding='utf-8') as f: assert value in f.read()
def test_stdout_file_write(rc, runner_mode): if runner_mode == 'pexpect': pytest.skip( 'Writing to stdout can be flaky, probably due to some pexpect bug') rc.command = ['echo', 'hello_world_marker ' ] # workaround bug in stdout handl wrapper rc.runner_mode = runner_mode runner = Runner(config=rc) status, exitcode = runner.run() assert status == 'successful' stdout_path = Path(rc.artifact_dir) / 'stdout' # poll until we are sure the file has been written to for _ in iterate_timeout(30.0, 'stdout file to be written', interval=0.2): if stdout_path.read_text().strip(): break assert 'hello_world_marker' in stdout_path.read_text() assert list(runner.events) assert 'hello_world_marker' in list(runner.events)[0]['stdout']
def test_run_command(rc): rc.command = ['sleep', '1'] status, exitcode = Runner(config=rc).run() assert status == 'successful' assert exitcode == 0 with open(os.path.join(rc.artifact_dir, 'command')) as f: data = json.load(f) assert data.get('command') == ['sleep', '1'] assert 'cwd' in data assert isinstance(data.get('env'), dict)
def test_event_callback_interface_has_ident(rc): rc.ident = "testident" runner = Runner(config=rc, remove_partials=False) runner.event_handler = mock.Mock() with mock.patch('codecs.open', mock.mock_open(read_data=json.dumps(dict(event="test")))): runner.event_callback(dict(uuid="testuuid", counter=0)) assert runner.event_handler.call_count == 1 runner.event_handler.assert_called_with(dict(runner_ident='testident', counter=0, uuid='testuuid', event='test')) runner.status_callback("running")