Skip to content

Commit

Permalink
Handle custom project_dir
Browse files Browse the repository at this point in the history
Signed-off-by: Abhijeet Kasurde <[email protected]>
  • Loading branch information
Akasurde committed Mar 23, 2023
1 parent 1d04ddb commit caee325
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ansible_runner/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,17 @@ def __init__(self,

self.artifact_dir = os.path.join(artifact_dir, "{}".format(self.ident))

self.loader = ArtifactLoader(self.private_data_dir)

if not project_dir:
self.project_dir = os.path.join(self.private_data_dir, 'project')
else:
self.project_dir = project_dir
self.project_dir = self.loader.abspath(project_dir)

self.rotate_artifacts = rotate_artifacts
self.fact_cache_type = fact_cache_type
self.fact_cache = os.path.join(self.artifact_dir, fact_cache or 'fact_cache') if self.fact_cache_type == 'jsonfile' else None

self.loader = ArtifactLoader(self.private_data_dir)

if self.host_cwd:
self.host_cwd = os.path.abspath(self.host_cwd)
Expand Down Expand Up @@ -473,6 +474,9 @@ def wrap_args_for_containerization(self, args, execution_mode, cmdline_args):
self._ensure_path_safe_to_mount(self.host_cwd)
self._update_volume_mount_paths(new_args, self.host_cwd)
workdir = self.host_cwd
elif self.project_dir:
workdir = "/runner/project"
self._update_volume_mount_paths(new_args, self.project_dir, workdir)
else:
workdir = "/runner/project"

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/projects/project_dir_test/env/envvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANSIBLE_DEVEL_WARNING: no
1 change: 1 addition & 0 deletions test/fixtures/projects/project_dir_test/inventory/inv_1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
host_1 ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"
6 changes: 6 additions & 0 deletions test/fixtures/projects/project_dir_test/my_project/debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: all
gather_facts: no

tasks:
- debug:
msg: "In my_project"
6 changes: 6 additions & 0 deletions test/fixtures/projects/project_dir_test/project/debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: all
gather_facts: no

tasks:
- debug:
msg: "In project"
62 changes: 62 additions & 0 deletions test/integration/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,68 @@ def test_multiple_inventories(project_fixtures):
assert 'host_2' in stdout


def test_default_project_dir(project_fixtures):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
playbook='debug.yml',
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In project' in stdout


def test_project_dir(project_fixtures):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
project_dir='my_project',
playbook='debug.yml',
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In my_project' in stdout


@pytest.mark.test_all_runtimes
def test_default_project_dir_inside_container(project_fixtures, runtime):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
playbook='debug.yml',
process_isolation_executable=runtime,
process_isolation=True,
container_image=defaults.default_container_image,
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In project' in stdout


@pytest.mark.test_all_runtimes
def test_project_dir_inside_container(project_fixtures, runtime):
private_data_dir = project_fixtures / 'project_dir_test'

res = run(
private_data_dir=private_data_dir,
project_dir='my_project',
playbook='debug.yml',
process_isolation_executable=runtime,
process_isolation=True,
container_image=defaults.default_container_image,
)
stdout = res.stdout.read()

assert res.rc == 0, stdout
assert 'In my_project' in stdout


def test_inventory_absolute_path(project_fixtures):
private_data_dir = project_fixtures / 'debug'

Expand Down

0 comments on commit caee325

Please sign in to comment.