mirror of
https://github.com/ansible/awx.git
synced 2026-05-14 21:07:39 -02:30
Clean up and test patch changing methods
This commit is contained in:
45
awx/main/tests/unit/utils/test_execution_environments.py
Normal file
45
awx/main/tests/unit/utils/test_execution_environments.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import pytest
|
||||||
|
|
||||||
|
from awx.main.utils.execution_environments import to_container_path, to_host_path
|
||||||
|
|
||||||
|
|
||||||
|
private_data_dir = '/tmp/pdd_iso/awx_xxx'
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'container_path,host_path',
|
||||||
|
[
|
||||||
|
('/runner', private_data_dir),
|
||||||
|
('/runner/foo', '{0}/foo'.format(private_data_dir)),
|
||||||
|
('/runner/foo/bar', '{0}/foo/bar'.format(private_data_dir)),
|
||||||
|
('/runner{0}'.format(private_data_dir), '{0}{0}'.format(private_data_dir)),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_switch_paths(container_path, host_path):
|
||||||
|
assert to_container_path(host_path, private_data_dir) == container_path
|
||||||
|
assert to_host_path(container_path, private_data_dir) == host_path
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'container_path',
|
||||||
|
[
|
||||||
|
('/foobar'),
|
||||||
|
('/runner/..'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_invalid_container_path(container_path):
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
to_host_path(container_path, private_data_dir)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
'host_path',
|
||||||
|
[
|
||||||
|
('/foobar'),
|
||||||
|
('/tmp/pdd_iso'),
|
||||||
|
('/tmp/pdd_iso/awx_xxx/..'),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_invalid_host_path(host_path):
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
to_container_path(host_path, private_data_dir)
|
||||||
@@ -37,7 +37,15 @@ CONTAINER_ROOT = '/runner'
|
|||||||
|
|
||||||
def to_container_path(path, private_data_dir):
|
def to_container_path(path, private_data_dir):
|
||||||
if not os.path.isabs(private_data_dir):
|
if not os.path.isabs(private_data_dir):
|
||||||
raise Exception
|
raise RuntimeError('The private_data_dir path must be absolute')
|
||||||
if Path(private_data_dir) not in Path(path).parents:
|
if private_data_dir != path and Path(private_data_dir) not in Path(path).resolve().parents:
|
||||||
raise Exception
|
raise RuntimeError(f'Cannot convert path {path} unless it is a subdir of {private_data_dir}')
|
||||||
return path.replace(private_data_dir, CONTAINER_ROOT)
|
return path.replace(private_data_dir, CONTAINER_ROOT, 1)
|
||||||
|
|
||||||
|
|
||||||
|
def to_host_path(path, private_data_dir):
|
||||||
|
if not os.path.isabs(private_data_dir):
|
||||||
|
raise RuntimeError('The private_data_dir path must be absolute')
|
||||||
|
if CONTAINER_ROOT != path and Path(CONTAINER_ROOT) not in Path(path).resolve().parents:
|
||||||
|
raise RuntimeError(f'Cannot convert path {path} unless it is a subdir of {CONTAINER_ROOT}')
|
||||||
|
return path.replace(CONTAINER_ROOT, private_data_dir, 1)
|
||||||
|
|||||||
Reference in New Issue
Block a user