From 6388f0b9def3f9270f405cb64fcb3845527056d2 Mon Sep 17 00:00:00 2001 From: Aleksandr Komissarov Date: Tue, 28 May 2024 16:42:38 +0200 Subject: [PATCH] fix: allow multistage Dockerfiles by removing AS in validation - Modified the validate_docker_compose_yml function to support multistage Dockerfiles. - Updated the FROM line parsing to remove the AS part for case-insensitive comparison. - This ensures compatibility with Dockerfiles using multistage builds. --- .../eaas/core/validation/validators/base.py | 2 +- .../core/validation/validators/test_base.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/connect/eaas/core/validation/validators/base.py b/connect/eaas/core/validation/validators/base.py index 80b6064..ec18bb3 100644 --- a/connect/eaas/core/validation/validators/base.py +++ b/connect/eaas/core/validation/validators/base.py @@ -224,7 +224,7 @@ def validate_docker_compose_yml(context): # noqa: CCR001 ), ) continue - image = from_cmd[4:].strip() + image = re.sub(r'\s+AS\s+.*', '', from_cmd[4:], flags=re.IGNORECASE).strip() if image != runner_image: messages.append( ValidationItem( diff --git a/tests/connect/eaas/core/validation/validators/test_base.py b/tests/connect/eaas/core/validation/validators/test_base.py index 8db1fef..4ecb009 100644 --- a/tests/connect/eaas/core/validation/validators/test_base.py +++ b/tests/connect/eaas/core/validation/validators/test_base.py @@ -566,6 +566,37 @@ def test_validate_docker_compose_yml_invalid_image_dockerfile(mocker): assert item.file == 'fake_dir/Dockerfile' +def test_validate_docker_compose_yml_multistage_image_dockerfile(mocker): + mocker.patch( + 'connect.eaas.core.validation.validators.base.os.path.isfile', + return_value=True, + ) + mocked_open = mocker.MagicMock() + mocked_open.read.return_value = ( + 'FROM cloudblueconnect/connect-extension-runner:0.3 as a-stage-name\n' + ) + mocker.patch( + 'connect.eaas.core.validation.validators.base.open', + side_effect=[None, mocked_open], + ) + mocker.patch( + 'connect.eaas.core.validation.validators.base.yaml.safe_load', + return_value={ + 'services': { + 'dev': { + 'build': {'dockerfile': 'Dockerfile'}, + }, + }, + }, + ) + + result = validate_docker_compose_yml({'project_dir': 'fake_dir', 'runner_version': '0.3'}) + + assert isinstance(result, ValidationResult) + assert result.must_exit is False + assert len(result.items) == 0 + + def test_validate_docker_compose_yml_invalid_image_no_dockerfile(mocker): mocker.patch( 'connect.eaas.core.validation.validators.base.os.path.isfile',