def test_cmdline_playbook_hosts(): """Test error is raised with trying to pass '--hosts' with '-p' """ cmdline('run', 'private_data_dir', '-p', 'fake', '--hosts', 'all') with raises(SystemExit) as exc: main() assert exc == 1
def test_cmdline_role_with_playbook_option(): """Test error is raised with invalid command line option '-p' """ cmdline('run', 'private_data_dir', '-r', 'fake', '-p', 'fake') with raises(SystemExit) as exc: main() assert exc == 1
def test_cmdline_includes_one_option(): """Test error is raised if not '-p', '-m' or '-r' """ cmdline('run', 'private_data_dir') with raises(SystemExit) as exc: main() assert exc == 1
def test_playbook_start(project_fixtures): private_data_dir = project_fixtures / 'sleep' mpcontext = multiprocessing.get_context('fork') p = mpcontext.Process(target=main, args=[[ 'start', '-p', 'sleep.yml', str(private_data_dir), ]]) p.start() pid_path = private_data_dir / 'pid' for _ in iterate_timeout(30, "pid file creation"): if pid_path.exists(): break rc = main(['is-alive', str(private_data_dir)]) assert rc == 0 rc = main(['stop', str(private_data_dir)]) assert rc == 0 for _ in iterate_timeout(30, "background process to stop"): rc = main(['is-alive', str(private_data_dir)]) if rc == 1: break rc = main(['stop', str(private_data_dir)]) assert rc == 1
def test_role_run(): with temp_directory() as temp_dir: main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', 'run', temp_dir ])
def test_role_run_inventory_missing(project_fixtures): with pytest.raises(AnsibleRunnerException): main([ 'run', '-r', 'benthomasson.hello_role', '--hosts', 'testhost', '--roles-path', str(project_fixtures / 'use_role' / 'roles'), '--inventory', 'does_not_exist', str(project_fixtures / 'use_role') ])
def test_main_bad_private_data_dir(): tmpfile = os.path.join('/tmp', str(uuid.uuid4().hex)) open(tmpfile, 'w').write(random_string()) cmdline('run', tmpfile, '-p', 'fake') try: with raises(OSError): main() finally: os.remove(tmpfile)
def test_role_run_env_vars(): with temp_directory() as temp_dir: ensure_directory(os.path.join(temp_dir, 'env')) with open(os.path.join(temp_dir, 'env/envvars'), 'w') as f: f.write(yaml.dump(dict(msg='hi'))) main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', 'run', temp_dir ])
def test_role_bad_project_dir(tmp_path, project_fixtures): bad_project_path = tmp_path / "bad_project_dir" bad_project_path.write_text('not a directory') with pytest.raises(OSError): main([ 'run', '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', str(project_fixtures / 'use_role' / 'roles'), '--logfile', str(project_fixtures / 'use_role' / 'new_logfile'), str(bad_project_path) ])
def test_help(command, expected, capsys, monkeypatch): # Ensure that sys.argv of the test command does not affect the test environment. monkeypatch.setattr('sys.argv', command or []) with pytest.raises(SystemExit) as exc: main(command) stdout, stderr = capsys.readouterr() assert exc.value.code == 2, 'Should raise SystemExit with return code 2' assert expected['out'] in stdout assert expected['err'] in stderr
def test_role_run_inventory(): with temp_directory() as temp_dir: ensure_directory(os.path.join(temp_dir, 'inventory')) shutil.copy(os.path.join(HERE, 'inventories/localhost'), os.path.join(temp_dir, 'inventory/localhost')) main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', '--inventory', 'localhost', 'run', temp_dir ])
def test_role_run_inventory_missing(): with temp_directory() as temp_dir: ensure_directory(os.path.join(temp_dir, 'inventory')) shutil.copy(os.path.join(HERE, 'inventory/localhost'), os.path.join(temp_dir, 'inventory/localhost')) with pytest.raises(AnsibleRunnerException): main(['-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', os.path.join(HERE, 'project/roles'), '--inventory', 'does_not_exist', 'run', temp_dir])
def test_role_bad_project_dir(): with open("bad_project_dir", 'w') as f: f.write('not a directory') try: with raises(OSError): main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', '--logfile', 'new_logfile', 'run', 'bad_project_dir' ]) finally: os.unlink('bad_project_dir')
def run_role(options, private_data_dir=None, expected_rc=0): try: private_data_dir = private_data_dir or tempfile.mkdtemp() args = ['run', private_data_dir] args.extend(options) with patch('ansible_runner.interface.run') as mock_run: with raises(SystemExit) as exc: main() assert exc.type == SystemExit assert exc.value.code == expected_rc finally: shutil.rmtree(private_data_dir) return mock_run
def test_cmdline_cmdline_override(is_pre_ansible28): try: private_data_dir = tempfile.mkdtemp() play = [{'hosts': 'all', 'tasks': [{'debug': {'msg': random_string()}}]}] path = os.path.join(private_data_dir, 'project') os.makedirs(path) playbook = os.path.join(path, 'main.yaml') with open(playbook, 'w') as f: f.write(json.dumps(play)) path = os.path.join(private_data_dir, 'inventory') os.makedirs(path) inventory = os.path.join(path, 'hosts') with open(inventory, 'w') as f: if is_pre_ansible28: f.write('[all]\nlocalhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"') else: f.write('[all]\nlocalhost ansible_connection=local') # privateip: removed --hosts command line option from test beause it is # not a supported combination of cli options #cmdline('run', private_data_dir, '-p', playbook, '--hosts', 'all', '--cmdline', '-e foo=bar') cmdline('run', private_data_dir, '-p', playbook, '--cmdline', '-e foo=bar') assert main() == 0 finally: shutil.rmtree(private_data_dir)
def test_module_run(): rc = main(['-m', 'ping', '--hosts', 'localhost', 'run', 'ping']) assert rc == 0
def test_cmdline_cmdline_override(): try: private_data_dir = tempfile.mkdtemp() play = [{ 'hosts': 'all', 'tasks': [{ 'debug': { 'msg': random_string() } }] }] path = os.path.join(private_data_dir, 'project') os.makedirs(path) playbook = os.path.join(path, 'main.yaml') with open(playbook, 'w') as f: f.write(json.dumps(play)) path = os.path.join(private_data_dir, 'inventory') os.makedirs(path) inventory = os.path.join(path, 'hosts') with open(inventory, 'w') as f: f.write('[all]\nlocalhost ansible_connection=local') cmdline('run', private_data_dir, '-p', playbook, '--hosts', 'all', '--cmdline', '-e foo=bar') assert main() == 0 finally: shutil.rmtree(private_data_dir)
def test_role_bad_project_dir(): with open("bad_project_dir", 'w') as f: f.write('not a directory') try: with pytest.raises(OSError): main([ 'run', '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', os.path.join(HERE, 'project/roles'), '--logfile', 'new_logfile', 'bad_project_dir' ]) finally: os.unlink('bad_project_dir') ensure_removed("new_logfile")
def test_role_run_clean(skipif_pre_ansible28, clear_integration_artifacts): rc = main([ 'run', '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', "test/integration" ]) assert rc == 0
def test_module_run_clean(): with temp_directory() as temp_dir: rc = main(['-m', 'ping', '--hosts', 'localhost', 'run', temp_dir]) assert rc == 0
def test_role_run(): rc = main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', 'run', "test/integration" ]) assert rc == 0
def test_cmdline_playbook(is_pre_ansible28): try: private_data_dir = tempfile.mkdtemp() play = [{'hosts': 'all', 'tasks': [{'debug': {'msg': random_string()}}]}] path = os.path.join(private_data_dir, 'project') os.makedirs(path) playbook = os.path.join(path, 'main.yaml') with open(playbook, 'w') as f: f.write(json.dumps(play)) path = os.path.join(private_data_dir, 'inventory') os.makedirs(path) inventory = os.path.join(path, 'hosts') with open(inventory, 'w') as f: if is_pre_ansible28: f.write('[all]\nlocalhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python"') else: f.write('[all]\nlocalhost ansible_connection=local') cmdline('run', private_data_dir, '-p', playbook, '--inventory', inventory) assert main() == 0 with open(playbook) as f: assert json.loads(f.read()) == play finally: shutil.rmtree(private_data_dir)
def test_role_logfile(): rc = main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/project/roles', '--logfile', 'new_logfile', 'run', 'test/integration' ]) assert rc == 0
def test_role_run_artifacts_dir(): rc = main([ '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', '--artifact-dir', 'otherartifacts', 'run', "test/integration" ]) assert rc == 0
def test_role_run_cmd_line_abs(): with temp_directory() as temp_dir: rc = main([ 'run', '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', os.path.join(HERE, 'project/roles'), temp_dir ]) assert rc == 0
def test_role_logfile(skipif_pre_ansible28, clear_integration_artifacts): rc = main([ 'run', '-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/project/roles', '--logfile', 'new_logfile', 'test/integration' ]) assert os.path.exists('new_logfile') assert rc == 0
def test_role_run_abs(data_directory): with temp_directory(): rc = main(['-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', os.path.join(data_directory, 'misc', 'project/roles'), 'run', os.path.join(data_directory, 'misc')]) assert rc == 0
def test_role_run(data_directory): rc = main(['-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', 'test/integration/roles', 'run', os.path.join(data_directory, 'misc')]) assert rc == 0 ensure_removed("test/integration/artifacts")
def test_module_run(): try: rc = main(['run', '-m', 'ping', '--hosts', 'localhost', 'ping']) assert os.path.exists('./ping') assert os.path.exists('./ping/artifacts') assert rc == 0 finally: if os.path.exists('./ping'): shutil.rmtree('./ping')
def test_role_run_artifacts_dir(data_directory): rc = main(['-r', 'benthomasson.hello_role', '--hosts', 'localhost', '--roles-path', os.path.join(data_directory, 'misc', 'project', 'roles'), '--artifact-dir', 'otherartifacts', 'run', os.path.join(data_directory, 'misc')]) assert rc == 0 ensure_removed(os.path.join(data_directory, 'misc', 'artifacts'))