From 720137e7f503b31ce47489a0a106ee799a038a39 Mon Sep 17 00:00:00 2001 From: mlbonhomme <bonhomme@teklia.com> Date: Thu, 30 Jun 2022 13:34:55 +0000 Subject: [PATCH] update self.config with default user_configuration values --- arkindex_worker/worker/base.py | 7 +++++++ tests/conftest.py | 37 +++++++++++++++++++++++++++++++++- tests/test_base_worker.py | 17 ++++++++++++---- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/arkindex_worker/worker/base.py b/arkindex_worker/worker/base.py index eacaad55..8e6113d5 100644 --- a/arkindex_worker/worker/base.py +++ b/arkindex_worker/worker/base.py @@ -170,6 +170,13 @@ class BaseWorker(object): f"Loaded worker {worker_version['worker']['name']} revision {worker_version['revision']['hash'][0:7]} from API" ) self.config = worker_version["configuration"]["configuration"] + if "user_configuration" in worker_version["configuration"]: + # Add default values (if set) to user_configuration + for key, value in worker_version["configuration"][ + "user_configuration" + ].items(): + if "default" in value: + self.user_configuration[key] = value["default"] self.worker_details = worker_version["worker"] required_secrets = worker_version["configuration"].get("secrets", []) elif self.args.config: diff --git a/tests/conftest.py b/tests/conftest.py index 04c8c794..ab72a9ee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -101,7 +101,7 @@ def setup_api(responses, monkeypatch, cache_yaml): @pytest.fixture(autouse=True) -def give_env_variable(monkeypatch): +def give_env_variable(request, monkeypatch): """Defines required environment variables""" monkeypatch.setenv("WORKER_VERSION_ID", "12341234-1234-1234-1234-123412341234") monkeypatch.setenv("ARKINDEX_PROCESS_ID", "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeffff") @@ -147,6 +147,41 @@ def mock_worker_version_api(responses): ) +@pytest.fixture +def mock_worker_version_user_configuration_api(responses): + """ + Provide a mock API response to get a worker configuration + that includes a `user_configuration` + """ + payload = { + "worker": {"id": "1234", "name": "Workerino", "slug": "workerino"}, + "revision": {"hash": "1234lala-lalalalala-lala"}, + "configuration": { + "configuration": {"param_1": "/some/path/file.pth", "param_2": 12}, + "user_configuration": { + "param_3": { + "title": "A Third Parameter", + "type": "string", + "default": "Animula vagula blandula", + }, + "param_4": {"title": "Parameter The Fourth", "type": "int"}, + "param_5": { + "title": "Parameter 5 (Five)", + "type": "bool", + "default": True, + }, + }, + }, + } + responses.add( + responses.GET, + "http://testserver/api/v1/workers/versions/12341234-1234-1234-1234-123412341234/", + status=200, + body=json.dumps(payload), + content_type="application/json", + ) + + @pytest.fixture def mock_process_api(responses): """Provide a mock of the API response to get information on a process. Workers activity is enabled""" diff --git a/tests/test_base_worker.py b/tests/test_base_worker.py index 5d17d091..fbcbfb45 100644 --- a/tests/test_base_worker.py +++ b/tests/test_base_worker.py @@ -160,11 +160,16 @@ def test_configure_worker_run(mocker, monkeypatch, responses, mock_config_api): assert worker.user_configuration == {"a": "b"} -def test_configure_worker_run_missing_worker_conf( - mocker, monkeypatch, responses, mock_config_api +def test_configure_user_configuration_defaults( + mocker, + monkeypatch, + mock_worker_version_user_configuration_api, + mock_user_api, + mock_process_api, + responses, ): worker = BaseWorker() - mocker.patch.object(sys, "argv", ["worker"]) + mocker.patch.object(sys, "argv") run_id = "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" monkeypatch.setenv("ARKINDEX_WORKER_RUN_ID", run_id) responses.add( @@ -174,7 +179,11 @@ def test_configure_worker_run_missing_worker_conf( ) worker.configure() - assert worker.user_configuration == {} + assert worker.config == {"param_1": "/some/path/file.pth", "param_2": 12} + assert worker.user_configuration == { + "param_3": "Animula vagula blandula", + "param_5": True, + } def test_configure_worker_run_missing_conf( -- GitLab