|
| 1 | +# |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# Copyright (c) 2020 Airbyte |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +# |
| 24 | + |
| 25 | +import io |
| 26 | + |
| 27 | +import docker |
| 28 | +import pytest |
| 29 | +from source_acceptance_test.utils import ConnectorRunner |
| 30 | + |
| 31 | + |
| 32 | +def build_docker_image(text: str, tag: str) -> docker.models.images.Image: |
| 33 | + """ |
| 34 | + Really for this test we dont need to remove the image since we access it by a string name |
| 35 | + and remove it also by a string name. But maybe we wanna use it somewhere |
| 36 | + """ |
| 37 | + client = docker.from_env() |
| 38 | + fileobj = io.BytesIO(bytes(text, "utf-8")) |
| 39 | + image, iterools_tee = client.images.build(fileobj=fileobj, tag=tag, forcerm=True, rm=True) |
| 40 | + return image |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def correct_connector_image() -> str: |
| 45 | + dockerfile_text = """ |
| 46 | + FROM scratch |
| 47 | + ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py" |
| 48 | + ENTRYPOINT ["python", "/airbyte/integration_code/main.py"] |
| 49 | + """ |
| 50 | + tag = "my-valid-one" |
| 51 | + build_docker_image(dockerfile_text, tag) |
| 52 | + yield tag |
| 53 | + client = docker.from_env() |
| 54 | + client.images.remove(image=tag, force=True) |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture |
| 58 | +def connector_image_without_env(): |
| 59 | + dockerfile_text = """ |
| 60 | + FROM scratch |
| 61 | + ENTRYPOINT ["python", "/airbyte/integration_code/main.py"] |
| 62 | + """ |
| 63 | + tag = "my-no-env" |
| 64 | + build_docker_image(dockerfile_text, tag) |
| 65 | + yield tag |
| 66 | + client = docker.from_env() |
| 67 | + client.images.remove(image=tag, force=True) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.fixture |
| 71 | +def connector_image_with_ne_properties(): |
| 72 | + dockerfile_text = """ |
| 73 | + FROM scratch |
| 74 | + ENV AIRBYTE_ENTRYPOINT "python /airbyte/integration_code/main.py" |
| 75 | + ENTRYPOINT ["python3", "/airbyte/integration_code/main.py"] |
| 76 | + """ |
| 77 | + tag = "my-ne-properties" |
| 78 | + build_docker_image(dockerfile_text, tag) |
| 79 | + yield tag |
| 80 | + client = docker.from_env() |
| 81 | + client.images.remove(image=tag, force=True) |
| 82 | + |
| 83 | + |
| 84 | +class TestEnvAttributes: |
| 85 | + def test_correct_connector_image(self, correct_connector_image, tmp_path): |
| 86 | + docker_runner = ConnectorRunner(image_name=correct_connector_image, volume=tmp_path) |
| 87 | + assert docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT"), "AIRBYTE_ENTRYPOINT must be set in dockerfile" |
| 88 | + assert docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT") == " ".join( |
| 89 | + docker_runner.entry_point |
| 90 | + ), "env should be equal to space-joined entrypoint" |
| 91 | + |
| 92 | + def test_connector_image_without_env(self, connector_image_without_env, tmp_path): |
| 93 | + docker_runner = ConnectorRunner(image_name=connector_image_without_env, volume=tmp_path) |
| 94 | + assert not docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT"), "this test should fail if AIRBYTE_ENTRYPOINT defined" |
| 95 | + |
| 96 | + def test_docker_image_env_ne_entrypoint(self, connector_image_with_ne_properties, tmp_path): |
| 97 | + docker_runner = ConnectorRunner(image_name=connector_image_with_ne_properties, volume=tmp_path) |
| 98 | + assert docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT"), "AIRBYTE_ENTRYPOINT must be set in dockerfile" |
| 99 | + assert docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT") != " ".join(docker_runner.entry_point), ( |
| 100 | + "This test should fail if we have " ".join(ENTRYPOINT)==ENV" |
| 101 | + ) |
0 commit comments