def test_env_accuracy_inside_container(request, printenv_example, container_runtime_installed): os.environ['SET_BEFORE_TEST'] = 'MADE_UP_VALUE' def remove_test_env_var(): if 'SET_BEFORE_TEST' in os.environ: del os.environ['SET_BEFORE_TEST'] request.addfinalizer(remove_test_env_var) res = run( private_data_dir=printenv_example, project_dir='/tmp', playbook='get_environment.yml', inventory=None, envvars={'FROM_TEST': 'FOOBAR'}, settings={ 'process_isolation_executable': container_runtime_installed, 'process_isolation': True } ) assert res.rc == 0, res.stdout.read() actual_env = get_env_data(res)['environment'] expected_env = res.config.env.copy() # NOTE: the reported environment for containerized jobs will not account for # all environment variables, particularly those set by the entrypoint script for key, value in expected_env.items(): assert key in actual_env assert actual_env[key] == value, 'Reported value wrong for {0} env var'.format(key) assert '/tmp' == res.config.cwd
def test_cancel_will_remove_container(test_data_dir, container_runtime_installed, cli): private_data_dir = os.path.join(test_data_dir, 'sleep') env_dir = os.path.join(private_data_dir, 'env') if os.path.exists(env_dir): shutil.rmtree(env_dir) cancel_standin = CancelStandIn(container_runtime_installed, cli) res = run( private_data_dir=private_data_dir, playbook='sleep.yml', settings={ 'process_isolation_executable': container_runtime_installed, 'process_isolation': True }, cancel_callback=cancel_standin.cancel, ident='foo?bar' # question mark invalid char, but should still work ) assert res.rc == 254, res.stdout.read() assert res.status == 'canceled' assert not is_running( cli, container_runtime_installed, 'ansible_runner_foo_bar' ), 'Found a running container, they should have all been stopped'
def test_run_role(project_fixtures): ''' Test that we can run a role via the API. ''' private_data_dir = project_fixtures / 'debug' res = run( private_data_dir=private_data_dir, role='hello_world', ) stdout = res.stdout.read() assert res.rc == 0, stdout assert 'Hello World!' in stdout
def test_invalid_registry_host(tmp_path, runtime): pdd_path = tmp_path / 'private_data_dir' pdd_path.mkdir() private_data_dir = str(pdd_path) image_name = 'quay.io/kdelee/does-not-exist' res = run(private_data_dir=private_data_dir, playbook='ping.yml', settings={ 'process_isolation_executable': runtime, 'process_isolation': True, 'container_image': image_name, 'container_options': ['--user=root', '--pull=always'], }, container_auth_data={ 'host': 'somedomain.invalid', 'username': '******', 'password': '******', 'verify_ssl': False }, ident='awx_123') assert res.status == 'failed' assert res.rc > 0 assert os.path.exists(res.config.registry_auth_path) result_stdout = res.stdout.read() auth_file_path = os.path.join(res.config.registry_auth_path, 'config.json') registry_conf = os.path.join(res.config.registry_auth_path, 'registries.conf') error_msg = 'access to the requested resource is not authorized' if runtime == 'podman': assert image_name in result_stdout error_msg = 'unauthorized' auth_file_path = res.config.registry_auth_path registry_conf = os.path.join( os.path.dirname(res.config.registry_auth_path), 'registries.conf') assert error_msg in result_stdout with open(auth_file_path, 'r') as f: content = f.read() assert res.config.container_auth_data['host'] in content assert 'Zm9vdXNlcjozNDlzazM0' in content # the b64 encoded of username and password assert os.path.exists(registry_conf) with open(registry_conf, 'r') as f: assert f.read() == '\n'.join([ '[[registry]]', 'location = "somedomain.invalid"', 'insecure = true' ])
def test_multiple_inventories(test_data_dir): private_data_dir = os.path.join(test_data_dir, 'debug') res = run( private_data_dir=private_data_dir, playbook='debug.yml', ) stdout = res.stdout.read() assert res.rc == 0, stdout # providing no inventory should cause <private_data_dir>/inventory # to be used, reading both inventories in the directory assert 'host_1' in stdout assert 'host_2' in stdout
def test_inventory_absolute_path(test_data_dir): private_data_dir = os.path.join(test_data_dir, 'debug') res = run( private_data_dir=private_data_dir, playbook='debug.yml', inventory=[ os.path.join(private_data_dir, 'inventory', 'inv_1'), ], ) stdout = res.stdout.read() assert res.rc == 0, stdout # hosts can be down-selected to one inventory out of those available assert 'host_1' in stdout assert 'host_2' not in stdout
def test_inventory_absolute_path(project_fixtures): private_data_dir = project_fixtures / 'debug' res = run( private_data_dir=private_data_dir, playbook='debug.yml', inventory=[ str(private_data_dir / 'inventory' / 'inv_1'), ], ) stdout = res.stdout.read() assert res.rc == 0, stdout # hosts can be down-selected to one inventory out of those available assert 'host_1' in stdout assert 'host_2' not in stdout
def test_env_accuracy(request, printenv_example): os.environ['SET_BEFORE_TEST'] = 'MADE_UP_VALUE' def remove_test_env_var(): if 'SET_BEFORE_TEST' in os.environ: del os.environ['SET_BEFORE_TEST'] request.addfinalizer(remove_test_env_var) res = run( private_data_dir=printenv_example, playbook='get_environment.yml', inventory=None, envvars={'FROM_TEST': 'FOOBAR'}, ) assert res.rc == 0, res.stdout.read() actual_env = get_env_data(res)['environment'] assert actual_env == res.config.env
def test_cancel_will_remove_container(project_fixtures, runtime, cli): private_data_dir = project_fixtures / 'sleep' ident = uuid4().hex[:12] container_name = f'ansible_runner_{ident}' cancel_standin = CancelStandIn(runtime, cli, container_name) res = run(private_data_dir=private_data_dir, playbook='sleep.yml', settings={ 'process_isolation_executable': runtime, 'process_isolation': True }, cancel_callback=cancel_standin.cancel, ident=ident) assert res.rc == 254, res.stdout.read() assert res.status == 'canceled' assert not is_running( cli, runtime, container_name ), 'Found a running container, they should have all been stopped'
def test_run(): r = run(module='debug', host_pattern='localhost') assert r.status == 'successful'
def test_run_playbook_data(playbook, tmp_path): r = run(private_data_dir=str(tmp_path), playbook=playbook) assert r.status == 'successful'