Skip to content

Commit 427fd7e

Browse files
authored
Merge pull request #213 from ecmwf-projects/COPDS-2076
Filter out empty lines from config file
2 parents cf8e05f + 825a1c5 commit 427fd7e

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

cads_processing_api_service/config.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ def profiles_api_url(self) -> str:
9696
return f"http://{self.profiles_service}:{self.profiles_api_service_port}"
9797

9898
@property
99-
def data_volume(self) -> str:
100-
data_volumes_config_path = self.download_nodes_config
101-
with open(data_volumes_config_path) as fp:
102-
data_volumes = [os.path.expandvars(line.rstrip("\n")) for line in fp]
103-
data_volume = random.choice(data_volumes)
104-
return data_volume
99+
def download_node(self) -> str:
100+
download_nodes = []
101+
with open(self.download_nodes_config) as fp:
102+
for line in fp:
103+
if download_node := os.path.expandvars(line.rstrip("\n")):
104+
download_nodes.append(download_node)
105+
return random.choice(download_nodes)
105106

106107

107108
def ensure_settings(

cads_processing_api_service/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,11 @@ def get_job_from_broker_db(
470470
return job
471471

472472

473-
def update_results_href(local_path: str, data_volume: str | None = None) -> str:
474-
if data_volume is None:
475-
data_volume = config.ensure_settings().data_volume
473+
def update_results_href(local_path: str, download_node: str | None = None) -> str:
474+
if download_node is None:
475+
download_node = config.ensure_settings().download_node
476476
file_path = local_path.split("://", 1)[-1]
477-
results_href = urllib.parse.urljoin(data_volume, file_path)
477+
results_href = urllib.parse.urljoin(download_node, file_path)
478478
return results_href
479479

480480

tests/test_30_utils.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414

1515
# mypy: ignore-errors
16-
1716
import datetime
17+
import pathlib
1818
import unittest.mock
1919
import uuid
2020
from typing import Any
@@ -244,19 +244,32 @@ def test_get_job_from_broker_db() -> None:
244244

245245

246246
def test_update_results_href() -> None:
247-
data_volume = "http://data_volume/"
247+
download_node = "http://download_node/"
248248

249249
local_path = "protocol://results/1234"
250-
updated_href = utils.update_results_href(local_path, data_volume)
251-
exp_updated_href = "http://data_volume/results/1234"
250+
updated_href = utils.update_results_href(local_path, download_node)
251+
exp_updated_href = "http://download_node/results/1234"
252252
assert updated_href == exp_updated_href
253253

254254
local_path = "results/1234"
255-
updated_href = utils.update_results_href(local_path, data_volume)
256-
exp_updated_href = "http://data_volume/results/1234"
255+
updated_href = utils.update_results_href(local_path, download_node)
256+
exp_updated_href = "http://download_node/results/1234"
257257
assert updated_href == exp_updated_href
258258

259259

260+
def test_update_results_href_from_config(
261+
monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path
262+
) -> None:
263+
config_path = tmp_path / "download-nodes.config"
264+
config_path.write_text("\n\nhttp://download_node/\n\n$DOWNLOAD_NODE\n\n")
265+
monkeypatch.setenv("DOWNLOAD_NODE", "http://download_node/")
266+
monkeypatch.setenv("DOWNLOAD_NODES_CONFIG", str(config_path))
267+
268+
local_path = "protocol://results/1234"
269+
updated_href = utils.update_results_href(local_path)
270+
assert updated_href == "http://download_node/results/1234"
271+
272+
260273
def test_get_results_from_job() -> None:
261274
mock_session = unittest.mock.Mock(spec=sqlalchemy.orm.Session)
262275
job = cads_broker.SystemRequest(

0 commit comments

Comments
 (0)