docker.py tests thanks @hroncok

This commit is contained in:
Fabrice Flore-Thébault 2020-06-15 14:44:05 +02:00
parent da85e738c7
commit 12a40fdab2
2 changed files with 31 additions and 16 deletions

View file

@ -78,23 +78,28 @@ def install_environment(
os.mkdir(directory)
@functools.lru_cache(maxsize=1)
def docker_is_rootless() -> bool:
def _docker_is_rootless() -> bool:
returncode, stdout, stderr = cmd_output(
('docker', 'system', 'info'),
'docker', 'system', 'info',
)
for line in stdout.splitlines():
# rootless docker has "rootless"
# rootless podman has "rootless: true"
if line.strip().startswith('rootless'):
if 'false' not in line:
print(line)
return True
break
return False
@functools.lru_cache(maxsize=1)
def docker_is_rootless() -> bool:
return _docker_is_rootless()
def get_docker_user() -> Tuple[str, ...]: # pragma: win32 no cover
if: docker_is_rootless():
if docker_is_rootless():
return ()
try:
return ('-u', f'{os.getuid()}:{os.getgid()}')

View file

@ -51,7 +51,7 @@ Server:
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: true
'''
''' # noqa
DOCKER_ROOTLESS_SYSTEM_INFO = '''
Client:
@ -99,7 +99,7 @@ Server:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
'''
''' # noqa
PODMAN_SYSTEM_INFO = '''
host:
@ -183,7 +183,9 @@ store:
number: 23
runRoot: /tmp/105971
volumePath: /home/remote/user/.local/share/containers/storage/volumes
'''
''' # noqa
def test_docker_is_running_process_error():
with mock.patch(
'pre_commit.languages.docker.cmd_output_b',
@ -204,16 +206,24 @@ def test_docker_fallback_user():
def test_docker_is_not_rootless():
with mock.patch.object(docker, 'cmd_output', return_value=(0, DOCKER_NOT_ROOTLESS_SYSTEM_INFO, '')):
assert docker.docker_is_rootless() == False
with mock.patch.object(
docker, 'cmd_output',
return_value=(0, DOCKER_NOT_ROOTLESS_SYSTEM_INFO, ''),
):
assert docker._docker_is_rootless() is False
def test_docker_is_rootless():
with mock.patch.object(docker, 'cmd_output', return_value=(0, DOCKER_ROOTLESS_SYSTEM_INFO, '')):
assert docker.docker_is_rootless() == True
with mock.patch.object(
docker, 'cmd_output',
return_value=(0, DOCKER_ROOTLESS_SYSTEM_INFO, ''),
):
assert docker._docker_is_rootless() is True
def test_podman_is_rootless():
with mock.patch.object(docker, 'cmd_output', return_value=(0, PODMAN_SYSTEM_INFO, '')):
assert docker.docker_is_rootless() == True
with mock.patch.object(
docker, 'cmd_output',
return_value=(0, PODMAN_SYSTEM_INFO, ''),
):
assert docker._docker_is_rootless() is True