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

View file

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