From 975c70e53a7fff15981addf001ff77543caf1d11 Mon Sep 17 00:00:00 2001
From: manon blanco <blanco@teklia.com>
Date: Fri, 5 Nov 2021 10:06:46 +0000
Subject: [PATCH] Use WorkerConfiguration instead of WorkerRun.configuration

---
 arkindex_worker/worker/base.py | 11 ++++++++---
 tests/test_base_worker.py      | 33 +++++++++++++++++++++++++++++++--
 2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/arkindex_worker/worker/base.py b/arkindex_worker/worker/base.py
index 1c76157c..320aed8c 100644
--- a/arkindex_worker/worker/base.py
+++ b/arkindex_worker/worker/base.py
@@ -166,9 +166,14 @@ class BaseWorker(object):
             worker_run = self.request(
                 "RetrieveWorkerRun", id=os.environ["ARKINDEX_WORKER_RUN_ID"]
             )
-            self.user_configuration = worker_run.get("configuration")
-            if self.user_configuration:
-                logger.info("Loaded user configuration from WorkerRun")
+            configuration_id = worker_run.get("configuration_id")
+            if configuration_id:
+                worker_configuration = self.request(
+                    "RetrieveWorkerConfiguration", id=configuration_id
+                )
+                self.user_configuration = worker_configuration.get("configuration")
+                if self.user_configuration:
+                    logger.info("Loaded user configuration from WorkerRun")
 
         task_id = os.environ.get("PONOS_TASK")
         paths = None
diff --git a/tests/test_base_worker.py b/tests/test_base_worker.py
index 4e8c3c01..5d17d091 100644
--- a/tests/test_base_worker.py
+++ b/tests/test_base_worker.py
@@ -143,18 +143,24 @@ def test_configure_worker_run(mocker, monkeypatch, responses, mock_config_api):
     worker = BaseWorker()
     mocker.patch.object(sys, "argv", ["worker"])
     run_id = "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
+    configuration_id = "bbbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
     monkeypatch.setenv("ARKINDEX_WORKER_RUN_ID", run_id)
     responses.add(
         responses.GET,
         f"http://testserver/api/v1/imports/workers/{run_id}/",
-        json={"id": run_id, "configuration": {"a": "b"}},
+        json={"id": run_id, "configuration_id": configuration_id},
+    )
+    responses.add(
+        responses.GET,
+        f"http://testserver/api/v1/workers/configurations/{configuration_id}/",
+        json={"id": configuration_id, "name": "BBB", "configuration": {"a": "b"}},
     )
     worker.configure()
 
     assert worker.user_configuration == {"a": "b"}
 
 
-def test_configure_worker_run_missing_conf(
+def test_configure_worker_run_missing_worker_conf(
     mocker, monkeypatch, responses, mock_config_api
 ):
     worker = BaseWorker()
@@ -168,6 +174,29 @@ def test_configure_worker_run_missing_conf(
     )
     worker.configure()
 
+    assert worker.user_configuration == {}
+
+
+def test_configure_worker_run_missing_conf(
+    mocker, monkeypatch, responses, mock_config_api
+):
+    worker = BaseWorker()
+    mocker.patch.object(sys, "argv", ["worker"])
+    run_id = "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
+    configuration_id = "bbbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
+    monkeypatch.setenv("ARKINDEX_WORKER_RUN_ID", run_id)
+    responses.add(
+        responses.GET,
+        f"http://testserver/api/v1/imports/workers/{run_id}/",
+        json={"id": run_id, "configuration_id": configuration_id},
+    )
+    responses.add(
+        responses.GET,
+        f"http://testserver/api/v1/workers/configurations/{configuration_id}/",
+        json={"id": configuration_id, "name": "BBB"},
+    )
+    worker.configure()
+
     assert worker.user_configuration is None
 
 
-- 
GitLab