|
7 | 7 | import importlib |
8 | 8 | import inspect |
9 | 9 | import os |
| 10 | +import shutil |
10 | 11 | import sys |
11 | 12 | from collections.abc import Callable |
12 | 13 | from pathlib import Path |
13 | 14 | from typing import cast |
14 | 15 |
|
| 16 | +import pytest |
15 | 17 | import yaml |
16 | 18 | from boltons.typeutils import classproperty |
17 | 19 |
|
18 | 20 | from airbyte_cdk.models import ( |
19 | 21 | AirbyteMessage, |
20 | 22 | Type, |
21 | 23 | ) |
| 24 | +from airbyte_cdk.models.connector_metadata import MetadataFile |
22 | 25 | from airbyte_cdk.test import entrypoint_wrapper |
23 | 26 | from airbyte_cdk.test.standard_tests._job_runner import IConnector, run_test_job |
24 | 27 | from airbyte_cdk.test.standard_tests.models import ( |
|
28 | 31 | ACCEPTANCE_TEST_CONFIG, |
29 | 32 | find_connector_root, |
30 | 33 | ) |
| 34 | +from airbyte_cdk.utils.docker import build_connector_image, run_docker_command |
31 | 35 |
|
32 | 36 |
|
33 | 37 | class ConnectorTestSuiteBase(abc.ABC): |
@@ -179,3 +183,73 @@ def get_scenarios( |
179 | 183 | test.configured_catalog_path = connector_root / test.configured_catalog_path |
180 | 184 |
|
181 | 185 | return test_scenarios |
| 186 | + |
| 187 | + @pytest.mark.skipif( |
| 188 | + shutil.which("docker") is None, |
| 189 | + reason="docker CLI not found in PATH, skipping docker image tests", |
| 190 | + ) |
| 191 | + def test_docker_image_build_and_spec( |
| 192 | + self, |
| 193 | + ) -> None: |
| 194 | + """Run `docker_image` acceptance tests.""" |
| 195 | + tag = "dev-latest" |
| 196 | + connector_dir = self.get_connector_root_dir() |
| 197 | + metadata = MetadataFile.from_file(connector_dir / "metadata.yaml") |
| 198 | + build_connector_image( |
| 199 | + connector_name=connector_dir.name, |
| 200 | + connector_directory=connector_dir, |
| 201 | + metadata=metadata, |
| 202 | + tag=tag, |
| 203 | + no_verify=False, |
| 204 | + ) |
| 205 | + run_docker_command( |
| 206 | + [ |
| 207 | + "docker", |
| 208 | + "run", |
| 209 | + "--rm", |
| 210 | + f"{metadata.data.dockerRepository}:{tag}", |
| 211 | + "spec", |
| 212 | + ], |
| 213 | + ) |
| 214 | + |
| 215 | + @pytest.mark.skipif( |
| 216 | + shutil.which("docker") is None, |
| 217 | + reason="docker CLI not found in PATH, skipping docker image tests", |
| 218 | + ) |
| 219 | + def test_docker_image_build_and_check( |
| 220 | + self, |
| 221 | + scenario: ConnectorTestScenario, |
| 222 | + ) -> None: |
| 223 | + """Run `docker_image` acceptance tests. |
| 224 | +
|
| 225 | + This test builds the connector image and runs the `check` command inside the container. |
| 226 | +
|
| 227 | + Note: |
| 228 | + - It is expected for docker image caches to be reused between test runs. |
| 229 | + - In the rare case that image caches need to be cleared, please clear |
| 230 | + the local docker image cache using `docker image prune -a` command. |
| 231 | + """ |
| 232 | + tag = "dev-latest" |
| 233 | + connector_dir = self.get_connector_root_dir() |
| 234 | + metadata = MetadataFile.from_file(connector_dir / "metadata.yaml") |
| 235 | + build_connector_image( |
| 236 | + connector_name=connector_dir.name, |
| 237 | + connector_directory=connector_dir, |
| 238 | + metadata=metadata, |
| 239 | + tag=tag, |
| 240 | + no_verify=False, |
| 241 | + ) |
| 242 | + container_config_path = "/secrets/config.json" |
| 243 | + with scenario.with_temp_config_file() as temp_config_file: |
| 244 | + run_docker_command( |
| 245 | + [ |
| 246 | + "docker", |
| 247 | + "run", |
| 248 | + "--rm", |
| 249 | + "-v", |
| 250 | + f"{temp_config_file}:{container_config_path}", |
| 251 | + f"{metadata.data.dockerRepository}:{tag}", |
| 252 | + "check", |
| 253 | + f"--config={container_config_path}", |
| 254 | + ], |
| 255 | + ) |
0 commit comments