Skip to content

Commit

Permalink
Add the ability to use --init flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Majeri Kasmaei Chervine committed Mar 1, 2018
1 parent 2d90f3f commit 332a70a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/reference/changelog.md
Expand Up @@ -7,6 +7,7 @@ Misc:
* A new end-to-end test suite setup will help us reduce the cycle time associated with testing Telepresence as we port over existing tests.
([429](https://github.com/datawire/telepresence/pull/429))
* Improved cleanup of our testing cluster used by CI.
* Added the ability to specify `--init=false` flag when using `--docker-run` ([481](https://github.com/datawire/telepresence/issues/481))

#### 0.75 (January 30, 2018)

Expand Down
14 changes: 9 additions & 5 deletions telepresence/container.py
Expand Up @@ -46,13 +46,17 @@ def kill():
return kill


def parse_docker_args(docker_run: List[str]) -> Tuple[List[str], List[str]]:
"""Separate --publish flags from the rest of the docker arguments"""
def parse_docker_args(docker_run: List[str]) -> Tuple[List[str], List[str], List[str]]:
"""Separate --publish and --init flags from the rest of the docker arguments"""
parser = argparse.ArgumentParser(allow_abbrev=False)
parser.add_argument("--publish", "-p", action="append", default=[])
publish_ns, docker_args = parser.parse_known_args(docker_run)
publish_args = ["-p={}".format(pub) for pub in publish_ns.publish]
return docker_args, publish_args
init_args = [ init for init in docker_args if "--init" in init ]
docker_args = [ arg for arg in docker_args if arg not in init_args ]
if not init_args:
init_args = ["--init"]
return docker_args, publish_args, init_args


def run_docker_command(
Expand Down Expand Up @@ -89,7 +93,7 @@ def run_docker_command(

# Extract --publish flags and add them to the sshuttle container, which is
# responsible for defining the network entirely.
docker_args, publish_args = parse_docker_args(args.docker_run)
docker_args, publish_args, init_args = parse_docker_args(args.docker_run)

# Start the sshuttle container:
name = random_name()
Expand Down Expand Up @@ -161,7 +165,7 @@ def run_docker_command(
])
# Older versions of Docker don't have --init:
if "--init" in runner.get_output(["docker", "run", "--help"]):
docker_command += ["--init"]
docker_command += init_args
docker_command += docker_args
p = Popen(docker_command)

Expand Down
65 changes: 63 additions & 2 deletions tests/test_unit.py
Expand Up @@ -244,9 +244,10 @@ def test_docker_publish_args():

expected_docker = ['--rm', '-it', 'fedora:latest', 'curl', 'qotm']
expected_publish = ['-p=8000:localhost:8000']
expected_init_true = ['--init']

no_publish = "--rm -it fedora:latest curl qotm".split()
assert parse_docker_args(no_publish) == (expected_docker, [])
assert parse_docker_args(no_publish) == (expected_docker, [], expected_init_true)
publish_variants = [
"--rm -it -p 8000:localhost:8000 fedora:latest curl qotm",
"--rm -it --publish 8000:localhost:8000 fedora:latest curl qotm",
Expand All @@ -255,7 +256,67 @@ def test_docker_publish_args():
]
for variant in publish_variants:
assert parse_docker_args(variant.split()) == \
(expected_docker, expected_publish)
(expected_docker, expected_publish, expected_init_true)


def test_docker_init_args():
"""Test extraction of docker init arguments"""
parse_docker_args = telepresence.container.parse_docker_args

expected_docker = ['--rm', '-it', 'fedora:latest', 'curl', 'qotm']
expected_init_true = ['--init']
expected_init_equal_true = ['--init=true']
expected_init_false = ['--init=false']

no_init = "--rm -it fedora:latest curl qotm".split()
assert parse_docker_args(no_init) == (expected_docker, [], expected_init_true)
init_no_value = "--rm -it --init fedora:latest curl qotm".split()
assert parse_docker_args(init_no_value) == (expected_docker, [], expected_init_true)
init_true = "--rm -it --init=true fedora:latest curl qotm".split()
assert parse_docker_args(init_true) == (expected_docker, [], expected_init_equal_true)
init_false = "--rm -it --init=false fedora:latest curl qotm".split()
assert parse_docker_args(init_false) == (expected_docker, [], expected_init_false)

def test_docker_publish_init_args():
"""Test extraction of docker publish arguments combined with init"""

parse_docker_args = telepresence.container.parse_docker_args

expected_docker = ['--rm', '-it', 'fedora:latest', 'curl', 'qotm']
expected_publish = ['-p=8000:localhost:8000']
expected_init_true = ['--init']
expected_init_equal_true = ['--init=true']
expected_init_false = ['--init=false']

init_true_publish_variants = [
"--rm -it -p 8000:localhost:8000 --init=true fedora:latest curl qotm",
"--rm -it --publish 8000:localhost:8000 --init=true fedora:latest curl qotm",
"--rm -it -p=8000:localhost:8000 --init=true fedora:latest curl qotm",
"--rm -it --publish=8000:localhost:8000 --init=true fedora:latest curl qotm",
]
for variant in init_true_publish_variants:
assert parse_docker_args(variant.split()) == \
(expected_docker, expected_publish, expected_init_equal_true)

init_equal_true_publish_variants = [
"--rm -it -p 8000:localhost:8000 --init fedora:latest curl qotm",
"--rm -it --publish 8000:localhost:8000 --init fedora:latest curl qotm",
"--rm -it -p=8000:localhost:8000 --init fedora:latest curl qotm",
"--rm -it --publish=8000:localhost:8000 --init fedora:latest curl qotm",
]
for variant in init_equal_true_publish_variants:
assert parse_docker_args(variant.split()) == \
(expected_docker, expected_publish, expected_init_true)

init_false_publish_variants = [
"--rm -it -p 8000:localhost:8000 --init=false fedora:latest curl qotm",
"--rm -it --publish 8000:localhost:8000 --init=false fedora:latest curl qotm",
"--rm -it -p=8000:localhost:8000 --init=false fedora:latest curl qotm",
"--rm -it --publish=8000:localhost:8000 --init=false fedora:latest curl qotm",
]
for variant in init_false_publish_variants:
assert parse_docker_args(variant.split()) == \
(expected_docker, expected_publish, expected_init_false)


def test_default_method():
Expand Down

0 comments on commit 332a70a

Please sign in to comment.