Skip to content

Commit 1cdf2aa

Browse files
vovavovavovavovakeu
authored andcommitted
SAT: verify AIRBYTE_ENTRYPOINT is defined (#4478)
* save changes required for work; TODO locate all places that need to be updated to make test working * move new test inside test_spec * apply suggestions * change return type + add check env = space_joined_entrypoint * requested * add check entrypoint with env * bump SAT --version && changelog update * merge && fix changelog * changes * add dynamic docker runner creator + test having properties * update the names * change names * make fixtures * upd text * Update airbyte-integrations/bases/source-acceptance-test/unit_tests/test_spec_unit.py Co-authored-by: Eugene Kulak <[email protected]> * requested changes * Update airbyte-integrations/bases/source-acceptance-test/unit_tests/test_spec_unit.py Co-authored-by: Eugene Kulak <[email protected]> * Update airbyte-integrations/bases/source-acceptance-test/unit_tests/test_spec_unit.py Co-authored-by: Eugene Kulak <[email protected]> * apply requested changes * change names (requested) * move binary strings to standard with convertation in builder * fixing merge-conflict side effect Co-authored-by: Eugene Kulak <[email protected]>
1 parent a9ae0eb commit 1cdf2aa

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

airbyte-integrations/bases/source-acceptance-test/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ Add: `test_spec` additionally checks if Dockerfile has `ENV AIRBYTE_ENTRYPOINT`
2121
Add test whether PKs present and not None if `source_defined_primary_key` defined: https://github.com/airbytehq/airbyte/pull/4140
2222

2323
## 0.1.5
24-
Add configurable timeout for the acceptance tests: https://github.com/airbytehq/airbyte/pull/4296
24+
Add configurable timeout for the acceptance tests: https://github.com/airbytehq/airbyte/pull/4296

airbyte-integrations/bases/source-acceptance-test/source_acceptance_test/tests/test_core.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def test_match_expected(self, connector_spec: ConnectorSpecification, connector_
4646
if connector_spec:
4747
assert spec_messages[0].spec == connector_spec, "Spec should be equal to the one in spec.json file"
4848

49+
assert docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT"), "AIRBYTE_ENTRYPOINT must be set in dockerfile"
50+
assert docker_runner.env_variables.get("AIRBYTE_ENTRYPOINT") == " ".join(
51+
docker_runner.entry_point
52+
), "env should be equal to space-joined entrypoint"
53+
4954
# Getting rid of technical variables that start with an underscore
5055
config = {key: value for key, value in connector_config.data.items() if not key.startswith("_")}
5156

airbyte-integrations/bases/source-acceptance-test/source_acceptance_test/utils/connector_runner.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,12 @@ def run(self, cmd, config=None, state=None, catalog=None, **kwargs) -> Iterable[
130130
yield AirbyteMessage.parse_raw(line)
131131
except ValidationError as exc:
132132
logging.warning("Unable to parse connector's output %s", exc)
133+
134+
@property
135+
def env_variables(self):
136+
env_vars = self._image.attrs["Config"]["Env"]
137+
return {env.split("=", 1)[0]: env.split("=", 1)[1] for env in env_vars}
138+
139+
@property
140+
def entry_point(self):
141+
return self._image.attrs["Config"]["Entrypoint"]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)