|
| 1 | +# Copyright 2026 The Feast Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# Guard: skip entire module if openlineage-python is not installed |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | +ol = pytest.importorskip( |
| 23 | + "openlineage.client", reason="openlineage-python not installed" |
| 24 | +) |
| 25 | + |
| 26 | +from openlineage.client.transport.console import ConsoleTransport # noqa: E402 |
| 27 | +from openlineage.client.transport.transform import TransformTransport # noqa: E402 |
| 28 | + |
| 29 | +from feast.openlineage.client import FeastOpenLineageClient # noqa: E402 |
| 30 | +from feast.openlineage.config import OpenLineageConfig # noqa: E402 |
| 31 | + |
| 32 | +_TRANSFORM_YML = """\ |
| 33 | +transport: |
| 34 | + type: transform |
| 35 | + transformer_class: openlineage.client.transport.transform.JobNamespaceReplaceTransformer |
| 36 | + transformer_properties: |
| 37 | + new_job_namespace: new_value |
| 38 | + transport: |
| 39 | + type: console |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +def _write_openlineage_yml(tmp_path: Path, content: str = _TRANSFORM_YML) -> str: |
| 44 | + """Write an openlineage.yml file and return its path.""" |
| 45 | + yml = tmp_path / "openlineage.yml" |
| 46 | + yml.write_text(content) |
| 47 | + return str(yml) |
| 48 | + |
| 49 | + |
| 50 | +class TestDefaultConsoleTransport: |
| 51 | + """When transport_type is None and there is no openlineage.yml, |
| 52 | + the OpenLineage SDK should fall back to ConsoleTransport.""" |
| 53 | + |
| 54 | + def test_default_config_uses_console_transport( |
| 55 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 56 | + ) -> None: |
| 57 | + # Ensure no openlineage.yml is found by changing cwd to an empty dir |
| 58 | + monkeypatch.chdir(tmp_path) |
| 59 | + monkeypatch.delenv("OPENLINEAGE_CONFIG", raising=False) |
| 60 | + monkeypatch.delenv("OPENLINEAGE_URL", raising=False) |
| 61 | + monkeypatch.delenv("OPENLINEAGE_DISABLED", raising=False) |
| 62 | + |
| 63 | + config = OpenLineageConfig(enabled=True) # transport_type defaults to None |
| 64 | + client = FeastOpenLineageClient(config=config) |
| 65 | + |
| 66 | + assert client.is_enabled |
| 67 | + assert isinstance(client._client.transport, ConsoleTransport) |
| 68 | + |
| 69 | + def test_default_from_dict_uses_console_transport( |
| 70 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 71 | + ) -> None: |
| 72 | + monkeypatch.chdir(tmp_path) |
| 73 | + monkeypatch.delenv("OPENLINEAGE_CONFIG", raising=False) |
| 74 | + monkeypatch.delenv("OPENLINEAGE_URL", raising=False) |
| 75 | + monkeypatch.delenv("OPENLINEAGE_DISABLED", raising=False) |
| 76 | + |
| 77 | + config = OpenLineageConfig.from_dict({"enabled": True}) |
| 78 | + client = FeastOpenLineageClient(config=config) |
| 79 | + |
| 80 | + assert client.is_enabled |
| 81 | + assert isinstance(client._client.transport, ConsoleTransport) |
| 82 | + |
| 83 | + |
| 84 | +class TestTransformTransportFromYml: |
| 85 | + def test_transform_yml_is_respected( |
| 86 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 87 | + ) -> None: |
| 88 | + yml_path = _write_openlineage_yml(tmp_path) |
| 89 | + monkeypatch.setenv("OPENLINEAGE_CONFIG", yml_path) |
| 90 | + monkeypatch.delenv("OPENLINEAGE_URL", raising=False) |
| 91 | + monkeypatch.delenv("OPENLINEAGE_DISABLED", raising=False) |
| 92 | + |
| 93 | + config = OpenLineageConfig(enabled=True) # transport_type=None |
| 94 | + client = FeastOpenLineageClient(config=config) |
| 95 | + |
| 96 | + assert client.is_enabled |
| 97 | + assert isinstance(client._client.transport, TransformTransport) |
| 98 | + # The inner transport should be console |
| 99 | + assert isinstance(client._client.transport.transport, ConsoleTransport) |
| 100 | + |
| 101 | + def test_transform_yml_in_cwd_is_respected( |
| 102 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 103 | + ) -> None: |
| 104 | + monkeypatch.delenv("OPENLINEAGE_CONFIG", raising=False) |
| 105 | + monkeypatch.delenv("OPENLINEAGE_URL", raising=False) |
| 106 | + monkeypatch.delenv("OPENLINEAGE_DISABLED", raising=False) |
| 107 | + |
| 108 | + # Write openlineage.yml in the dir we'll chdir to |
| 109 | + _write_openlineage_yml(tmp_path) |
| 110 | + monkeypatch.chdir(tmp_path) |
| 111 | + |
| 112 | + config = OpenLineageConfig(enabled=True) # transport_type=None |
| 113 | + client = FeastOpenLineageClient(config=config) |
| 114 | + |
| 115 | + assert client.is_enabled |
| 116 | + assert isinstance(client._client.transport, TransformTransport) |
| 117 | + |
| 118 | + |
| 119 | +class TestExplicitConfigOverridesYml: |
| 120 | + def test_explicit_console_ignores_yml_env( |
| 121 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 122 | + ) -> None: |
| 123 | + yml_path = _write_openlineage_yml(tmp_path) |
| 124 | + monkeypatch.setenv("OPENLINEAGE_CONFIG", yml_path) |
| 125 | + monkeypatch.delenv("OPENLINEAGE_URL", raising=False) |
| 126 | + monkeypatch.delenv("OPENLINEAGE_DISABLED", raising=False) |
| 127 | + |
| 128 | + config = OpenLineageConfig(enabled=True, transport_type="console") |
| 129 | + client = FeastOpenLineageClient(config=config) |
| 130 | + |
| 131 | + assert client.is_enabled |
| 132 | + # Must be plain ConsoleTransport, NOT TransformTransport |
| 133 | + assert isinstance(client._client.transport, ConsoleTransport) |
| 134 | + assert not isinstance(client._client.transport, TransformTransport) |
| 135 | + |
| 136 | + def test_explicit_console_ignores_yml_in_cwd( |
| 137 | + self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 138 | + ) -> None: |
| 139 | + monkeypatch.delenv("OPENLINEAGE_CONFIG", raising=False) |
| 140 | + monkeypatch.delenv("OPENLINEAGE_URL", raising=False) |
| 141 | + monkeypatch.delenv("OPENLINEAGE_DISABLED", raising=False) |
| 142 | + |
| 143 | + _write_openlineage_yml(tmp_path) |
| 144 | + monkeypatch.chdir(tmp_path) |
| 145 | + |
| 146 | + config = OpenLineageConfig(enabled=True, transport_type="console") |
| 147 | + client = FeastOpenLineageClient(config=config) |
| 148 | + |
| 149 | + assert client.is_enabled |
| 150 | + assert isinstance(client._client.transport, ConsoleTransport) |
| 151 | + assert not isinstance(client._client.transport, TransformTransport) |
0 commit comments