From 8ef40d7d2b8b605ee48699cc9762acd8a587dc43 Mon Sep 17 00:00:00 2001 From: Eva Bardou <ebardou@teklia.com> Date: Thu, 13 Aug 2020 20:04:55 +0000 Subject: [PATCH] Use WORKER_VERSION_ID env var in helper methods --- arkindex_worker/worker.py | 7 +++++++ tests/conftest.py | 5 +++++ tests/test_base_worker.py | 15 +++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/arkindex_worker/worker.py b/arkindex_worker/worker.py index c0556b02..46aab692 100644 --- a/arkindex_worker/worker.py +++ b/arkindex_worker/worker.py @@ -30,6 +30,12 @@ class BaseWorker(object): self.work_dir = os.path.join(xdg_data_home, "arkindex") os.makedirs(self.work_dir, exist_ok=True) + self.worker_version_id = os.environ.get("WORKER_VERSION_ID") + if not self.worker_version_id: + raise Exception( + "Missing WORKER_VERSION_ID environment variable to start the Worker" + ) + logger.info(f"Worker will use {self.work_dir} as working directory") def configure(self): @@ -192,6 +198,7 @@ class ElementsWorker(BaseWorker): "corpus": element.corpus.id, "polygon": polygon, "parent": element.id, + "worker_version": self.worker_version_id, }, ) self.report.add_element(element.id, type) diff --git a/tests/conftest.py b/tests/conftest.py index a81af1fa..d98065c8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,3 +8,8 @@ import pytest def pass_schema(responses): schema_url = os.environ.get("ARKINDEX_API_SCHEMA_URL") responses.add_passthru(schema_url) + + +@pytest.fixture(autouse=True) +def give_worker_version_id_env_variable(monkeypatch): + monkeypatch.setenv("WORKER_VERSION_ID", "12341234-1234-1234-1234-123412341234") diff --git a/tests/test_base_worker.py b/tests/test_base_worker.py index 74098597..fb9284ad 100644 --- a/tests/test_base_worker.py +++ b/tests/test_base_worker.py @@ -4,6 +4,8 @@ import os import sys from pathlib import Path +import pytest + from arkindex_worker import logger from arkindex_worker.worker import BaseWorker @@ -12,6 +14,7 @@ def test_init_default_local_share(): worker = BaseWorker() assert worker.work_dir == os.path.expanduser("~/.local/share/arkindex") + assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" def test_init_default_xdg_data_home(monkeypatch): @@ -20,6 +23,7 @@ def test_init_default_xdg_data_home(monkeypatch): worker = BaseWorker() assert worker.work_dir == f"{path}/arkindex" + assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" def test_init_var_ponos_data_given(monkeypatch): @@ -28,6 +32,17 @@ def test_init_var_ponos_data_given(monkeypatch): worker = BaseWorker() assert worker.work_dir == f"{path}/current" + assert worker.worker_version_id == "12341234-1234-1234-1234-123412341234" + + +def test_init_var_worker_version_id_missing(monkeypatch): + monkeypatch.delenv("WORKER_VERSION_ID") + with pytest.raises(Exception) as e: + BaseWorker() + assert ( + str(e.value) + == "Missing WORKER_VERSION_ID environment variable to start the Worker" + ) def test_cli_default(mocker): -- GitLab