def test_containerization_settings(tmp_path, runtime, mocker): mocker.patch('os.path.isdir', return_value=True) mocker.patch('os.path.exists', return_value=True) mock_containerized = mocker.patch( 'ansible_runner.runner_config.RunnerConfig.containerized', new_callable=mocker.PropertyMock) mock_containerized.return_value = True rc = RunnerConfig(tmp_path) rc.ident = 'foo' rc.playbook = 'main.yaml' rc.command = 'ansible-playbook' rc.process_isolation = True rc.process_isolation_executable = runtime rc.container_image = 'my_container' rc.container_volume_mounts = ['/host1:/container1', '/host2:/container2'] rc.prepare() extra_container_args = [] if runtime == 'podman': extra_container_args = ['--quiet'] else: extra_container_args = [f'--user={os.getuid()}'] expected_command_start = [runtime, 'run', '--rm', '--tty', '--interactive', '--workdir', '/runner/project'] + \ ['-v', '{}/:/runner/:Z'.format(rc.private_data_dir)] + \ ['-v', '/host1/:/container1/', '-v', '/host2/:/container2/'] + \ ['--env-file', '{}/env.list'.format(rc.artifact_dir)] + \ extra_container_args + \ ['--name', 'ansible_runner_foo'] + \ ['my_container', 'ansible-playbook', '-i', '/runner/inventory/hosts', 'main.yaml'] assert expected_command_start == rc.command
def test_container_volume_mounting_with_Z(mocker, tmp_path): mocker.patch('os.path.isdir', return_value=True) mocker.patch('os.path.exists', return_value=True) rc = RunnerConfig(str(tmp_path)) rc.container_volume_mounts = ['/tmp/project_path:/tmp/project_path:Z'] rc.container_name = 'foo' rc.env = {} new_args = rc.wrap_args_for_containerization( ['ansible-playbook', 'foo.yml'], 0, None) assert new_args[0] == 'podman' for i, entry in enumerate(new_args): if entry == '-v': mount = new_args[i + 1] if mount.endswith(':/tmp/project_path/:Z'): break else: raise Exception( 'Could not find expected mount, args: {}'.format(new_args))