From 8e268afc936321753789a28817eb25f0ad619e85 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Wed, 21 Feb 2018 15:56:02 -0800 Subject: [PATCH] Add version guard for multi-service buildarg Add buildarg tests Signed-off-by: Joffrey F --- compose/cli/main.py | 9 +++++-- tests/acceptance/cli_test.py | 27 ++++++++++++++++++++ tests/fixtures/build-args/Dockerfile | 4 +++ tests/fixtures/build-args/docker-compose.yml | 7 +++++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/build-args/Dockerfile create mode 100644 tests/fixtures/build-args/docker-compose.yml diff --git a/compose/cli/main.py b/compose/cli/main.py index 164769bbf..fd7f92a81 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -236,8 +236,14 @@ class TopLevelCommand(object): -m, --memory MEM Sets memory limit for the build container. --build-arg key=val Set build-time variables for services. """ + service_names = options['SERVICE'] build_args = options.get('--build-arg', None) if build_args: + if not service_names and docker.utils.version_lt(self.project.client.api_version, '1.25'): + raise UserError( + '--build-arg is only supported when services are specified for API version < 1.25.' + ' Please use a Compose file version > 2.2 or specify which services to build.' + ) environment = Environment.from_env_file(self.project_dir) build_args = resolve_build_args(build_args, environment) @@ -1026,8 +1032,7 @@ class TopLevelCommand(object): if cascade_stop: print("Aborting on container exit...") - all_containers = self.project.containers( - service_names=options['SERVICE'], stopped=True) + all_containers = self.project.containers(service_names=options['SERVICE'], stopped=True) exit_code = compute_exit_code( exit_value_from, attached_containers, cascade_starter, all_containers ) diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 134485c40..125501b0d 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -658,6 +658,33 @@ class CLITestCase(DockerClientTestCase): result = self.dispatch(['build', '--no-cache', '--memory', '96m', 'service'], None) assert 'memory: 100663296' in result.stdout # 96 * 1024 * 1024 + def test_build_with_buildarg_from_compose_file(self): + pull_busybox(self.client) + self.base_dir = 'tests/fixtures/build-args' + result = self.dispatch(['build'], None) + assert 'Favorite Touhou Character: mariya.kirisame' in result.stdout + + def test_build_with_buildarg_cli_override(self): + pull_busybox(self.client) + self.base_dir = 'tests/fixtures/build-args' + result = self.dispatch(['build', '--build-arg', 'favorite_th_character=sakuya.izayoi'], None) + assert 'Favorite Touhou Character: sakuya.izayoi' in result.stdout + + @mock.patch.dict(os.environ) + def test_build_with_buildarg_old_api_version(self): + pull_busybox(self.client) + self.base_dir = 'tests/fixtures/build-args' + os.environ['COMPOSE_API_VERSION'] = '1.24' + result = self.dispatch( + ['build', '--build-arg', 'favorite_th_character=reimu.hakurei'], None, returncode=1 + ) + assert '--build-arg is only supported when services are specified' in result.stderr + + result = self.dispatch( + ['build', '--build-arg', 'favorite_th_character=hong.meiling', 'web'], None + ) + assert 'Favorite Touhou Character: hong.meiling' in result.stdout + def test_bundle_with_digests(self): self.base_dir = 'tests/fixtures/bundle-with-digests/' tmpdir = pytest.ensuretemp('cli_test_bundle') diff --git a/tests/fixtures/build-args/Dockerfile b/tests/fixtures/build-args/Dockerfile new file mode 100644 index 000000000..93ebcb9cd --- /dev/null +++ b/tests/fixtures/build-args/Dockerfile @@ -0,0 +1,4 @@ +FROM busybox:latest +LABEL com.docker.compose.test_image=true +ARG favorite_th_character +RUN echo "Favorite Touhou Character: ${favorite_th_character}" diff --git a/tests/fixtures/build-args/docker-compose.yml b/tests/fixtures/build-args/docker-compose.yml new file mode 100644 index 000000000..ed60a337b --- /dev/null +++ b/tests/fixtures/build-args/docker-compose.yml @@ -0,0 +1,7 @@ +version: '2.2' +services: + web: + build: + context: . + args: + - favorite_th_character=mariya.kirisame