mirror of
https://github.com/ansible/awx.git
synced 2026-01-18 05:01:19 -03:30
Clean up and test patch changing methods
This commit is contained in:
parent
1f1cdf8859
commit
d33a748eea
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):
|
||||
if not os.path.isabs(private_data_dir):
|
||||
raise Exception
|
||||
if Path(private_data_dir) not in Path(path).parents:
|
||||
raise Exception
|
||||
return path.replace(private_data_dir, CONTAINER_ROOT)
|
||||
raise RuntimeError('The private_data_dir path must be absolute')
|
||||
if private_data_dir != path and Path(private_data_dir) not in Path(path).resolve().parents:
|
||||
raise RuntimeError(f'Cannot convert path {path} unless it is a subdir of {private_data_dir}')
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user