Skip to content

Commit

Permalink
Tests for Docker images in Python (#19737)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-laj authored Nov 24, 2021
1 parent 1983bf9 commit 621d17b
Show file tree
Hide file tree
Showing 17 changed files with 519 additions and 447 deletions.
5 changes: 3 additions & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
/.github/workflows/ @potiuk @ashb @kaxil
breeze @potiuk
breeze-complete @potiuk
Dockerfile @potiuk @ashb
Dockerfile @potiuk @ashb @mik-laj
Dockerfile.ci @potiuk @ashb
/dev/ @potiuk @ashb @kaxil
/provider_packages/ @potiuk @ashb
/scripts/ @potiuk @ashb
/scripts/ @potiuk @ashb @mik-laj
/docker_tests/ @potiuk @ashb @mik-laj
2 changes: 1 addition & 1 deletion .github/boring-cyborg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ labelPRBasedOnFilePath:
- Dockerfile
- docs/docker-stack/**/*
- scripts/in_container/prod/*
- scripts/ci/tools/verify_docker_image.sh
- scripts/ci/libraries/_verify_image.sh
- docker_tests/**/*

# Various Flags to control behaviour of the "Labeler"
labelerFlags:
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ repos:
^scripts/.*\.py$|
^dev|
^provider_packages|
^docker_tests|
^kubernetes_tests|
.*example_dags/.*|
^chart/.*\.py$|
Expand Down
16 changes: 16 additions & 0 deletions docker_tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
41 changes: 41 additions & 0 deletions docker_tests/ci_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import subprocess

from docker_tests.docker_tests_utils import (
display_dependency_conflict_message,
docker_image,
run_bash_in_docker,
run_command,
)


class TestFiles:
def test_dist_folder_should_exists(self):
run_bash_in_docker('[ -f /opt/airflow/airflow/www/static/dist/manifest.json ] || exit 1')


class TestPythonPackages:
def test_pip_dependencies_conflict(self):
try:
run_command(
["docker", "run", "--rm", "--entrypoint", "/bin/bash", docker_image, "-c", 'pip check']
)
except subprocess.CalledProcessError as ex:
display_dependency_conflict_message()
raise ex
116 changes: 116 additions & 0 deletions docker_tests/docker_tests_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
import shlex
import subprocess
from pathlib import Path
from typing import List

docker_image = os.environ.get('DOCKER_IMAGE')
SOURCE_ROOT = Path(__file__).resolve().parents[1]

if not docker_image:
raise Exception("The DOCKER_IMAGE environment variable is required")


def run_command(cmd: List[str], print_output_on_error: bool = True, **kwargs):
print(f"$ {' '.join(shlex.quote(c) for c in cmd)}")
try:
return subprocess.check_output(cmd, **kwargs).decode()
except subprocess.CalledProcessError as ex:
if print_output_on_error:
print("========================= OUTPUT start ============================")
print(ex.stderr)
print(ex.stdout)
print("========================= OUTPUT end ============================")
raise


def run_bash_in_docker(bash_script, **kwargs):
docker_command = [
"docker",
"run",
"--rm",
"-e",
"COLUMNS=180",
"--entrypoint",
"/bin/bash",
docker_image,
"-c",
bash_script,
]
return run_command(docker_command, **kwargs)


def run_python_in_docker(python_script, **kwargs):
docker_command = [
"docker",
"run",
"--rm",
"-e",
"COLUMNS=180",
"-e",
"PYTHONDONTWRITEBYTECODE=true",
docker_image,
"python",
"-c",
python_script,
]
return run_command(docker_command, **kwargs)


def display_dependency_conflict_message():
print(
"""
***** Beginning of the instructions ****
The image did not pass 'pip check' verification. This means that there are some conflicting dependencies
in the image.
It can mean one of those:
1) The main is currently broken (other PRs will fail with the same error)
2) You changed some dependencies in setup.py or setup.cfg and they are conflicting.
In case 1) - apologies for the trouble.Please let committers know and they will fix it. You might
be asked to rebase to the latest main after the problem is fixed.
In case 2) - Follow the steps below:
* try to build CI and then PROD image locally with breeze, adding --upgrade-to-newer-dependencies flag
(repeat it for all python versions)
CI image:
./breeze build-image --upgrade-to-newer-dependencies --python 3.6
Production image:
./breeze build-image --production-image --upgrade-to-newer-dependencies --python 3.6
* You will see error messages there telling which requirements are conflicting and which packages caused the
conflict. Add the limitation that caused the conflict to EAGER_UPGRADE_ADDITIONAL_REQUIREMENTS
variable in Dockerfile.ci. Note that the limitations might be different for Dockerfile.ci and Dockerfile
because not all packages are installed by default in the PROD Dockerfile. So you might find that you
only need to add the limitation to the Dockerfile.ci
***** End of the instructions ****
"""
)
Loading

0 comments on commit 621d17b

Please sign in to comment.