Skip to content

Commit

Permalink
Merge pull request #402 from freakboy3742/docker-version
Browse files Browse the repository at this point in the history
Add an explicit Docker version check.
  • Loading branch information
freakboy3742 authored May 22, 2020
2 parents 4c1c81b + eabb653 commit 048a7ef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 18 deletions.
1 change: 1 addition & 0 deletions changes/402.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added an explicit version check for Docker.
64 changes: 46 additions & 18 deletions src/briefcase/integrations/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@
from briefcase.exceptions import BriefcaseCommandError


def docker_install_details(host_os):
"""
Obtain a platform-specific template context dictionary for Docker
installation details.
:param host_os: The host OS for which installation details are required.
:returns: a context dictionary containing Docker installation details.
"""
if host_os == 'Windows':
install_url = "https://docs.docker.com/docker-for-windows/install/"
extra_content = ""
elif host_os == 'Darwin':
install_url = "https://docs.docker.com/docker-for-mac/install/"
extra_content = ""
else:
install_url = "https://docs.docker.com/engine/install/#server"
extra_content = """Alternatively, to run briefcase natively (i.e. without Docker), use the
`--no-docker` command-line argument.
"""
return {
'install_url': install_url,
'extra_content': extra_content,
}


def verify_docker(command):
"""
Verify that docker is available.
Expand All @@ -20,8 +45,26 @@ def verify_docker(command):
).strip('\n')

# Do a simple check that the docker that was invoked
# actually looks like the real deal. If it is,
if not output.startswith('Docker version '):
# actually looks like the real deal, and is a version that
# meets our requirements.
if output.startswith('Docker version '):
docker_version = output[15:]
version = docker_version.split('.')
if int(version[0]) < 19:
# Docker version isn't compatible.
raise BriefcaseCommandError("""
Briefcase requires Docker 19 or higher, but you are currently running
version {docker_version}. Visit:
{install_url}
to download and install an updated version of Docker.
{extra_content}""".format(
docker_version=docker_version,
**docker_install_details(command.host_os)
))

else:
print("""
*************************************************************************
** WARNING: Unable to determine the version of Docker **
Expand Down Expand Up @@ -65,18 +108,6 @@ def verify_docker(command):
""")
except FileNotFoundError:
# Docker executable doesn't exist.
if command.host_os == 'Windows':
install_url = "https://docs.docker.com/docker-for-windows/install/"
extra_content = ""
elif command.host_os == 'Darwin':
install_url = "https://docs.docker.com/docker-for-mac/install/"
extra_content = ""
else:
install_url = "https://docs.docker.com/engine/install/#server"
extra_content = """
Alternatively, to run briefcase natively (i.e. without Docker), use the
`--no-docker` command-line argument.
"""
raise BriefcaseCommandError("""
Briefcase requires Docker, but it is not installed (or is not on your PATH).
Visit:
Expand All @@ -87,10 +118,7 @@ def verify_docker(command):
{extra_content}
If you have installed Docker recently and are still getting this error, you may
need to restart your terminal session.
""".format(
install_url=install_url,
extra_content=extra_content,
))
""".format(**docker_install_details(command.host_os)))

# Return the Docker wrapper
return Docker
Expand Down
13 changes: 13 additions & 0 deletions tests/integrations/docker/test_verify_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ def test_docker_failure(test_command, capsys):


def test_docker_bad_version(test_command, capsys):
"If docker exists but the version string doesn't make sense, the Docker wrapper is returned with a warning."
# Mock a bad return value of `docker --version`
test_command.subprocess.check_output.return_value = "Docker version 17.2\n"

# Invoke verify_docker
with pytest.raises(
BriefcaseCommandError,
match=r'Briefcase requires Docker 19 or higher'
):
verify_docker(command=test_command)


def test_docker_unknown_version(test_command, capsys):
"If docker exists but the version string doesn't make sense, the Docker wrapper is returned with a warning."
# Mock a bad return value of `docker --version`
test_command.subprocess.check_output.return_value = "ceci nest pas un Docker\n"
Expand Down

0 comments on commit 048a7ef

Please # to comment.