Simplify docker user fallback implementation and test

This commit is contained in:
Edgar Geier 2019-07-25 11:20:03 +02:00
parent a21a4f46c7
commit d4a9ff4d1f
2 changed files with 11 additions and 23 deletions

View file

@ -14,8 +14,6 @@ from pre_commit.util import cmd_output
ENVIRONMENT_DIR = 'docker' ENVIRONMENT_DIR = 'docker'
PRE_COMMIT_LABEL = 'PRE_COMMIT' PRE_COMMIT_LABEL = 'PRE_COMMIT'
FALLBACK_UID = 1000
FALLBACK_GID = 1000
get_default_version = helpers.basic_get_default_version get_default_version = helpers.basic_get_default_version
healthy = helpers.basic_healthy healthy = helpers.basic_healthy
@ -75,25 +73,18 @@ def install_environment(
os.mkdir(directory) os.mkdir(directory)
def getuid(): # pragma: windows no cover def get_docker_user(): # pragma: windows no cover
try: try:
return os.getuid() return '{}:{}'.format(os.getuid(), os.getgid())
except AttributeError: except AttributeError:
return FALLBACK_UID return '1000:1000'
def getgid(): # pragma: windows no cover
try:
return os.getgid()
except AttributeError:
return FALLBACK_GID
def docker_cmd(): # pragma: windows no cover def docker_cmd(): # pragma: windows no cover
return ( return (
'docker', 'run', 'docker', 'run',
'--rm', '--rm',
'-u', '{}:{}'.format(getuid(), getgid()), '-u', get_docker_user(),
# https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from # https://docs.docker.com/engine/reference/commandline/run/#mount-volumes-from-container-volumes-from
# The `Z` option tells Docker to label the content with a private # The `Z` option tells Docker to label the content with a private
# unshared label. Only the current container can use a private volume. # unshared label. Only the current container can use a private volume.

View file

@ -15,15 +15,12 @@ def test_docker_is_running_process_error():
assert docker.docker_is_running() is False assert docker.docker_is_running() is False
def test_docker_fallback_uid(): def test_docker_fallback_user():
def invalid_attribute(): def invalid_attribute():
raise AttributeError raise AttributeError
with mock.patch('os.getuid', invalid_attribute, create=True): with mock.patch.multiple(
assert docker.getuid() == docker.FALLBACK_UID 'os', create=True,
getuid=invalid_attribute,
getgid=invalid_attribute,
def test_docker_fallback_gid(): ):
def invalid_attribute(): assert docker.get_docker_user() == '1000:1000'
raise AttributeError
with mock.patch('os.getgid', invalid_attribute, create=True):
assert docker.getgid() == docker.FALLBACK_GID