From fbefcd4d630cbc97b04a2f96e93c7daf05553467 Mon Sep 17 00:00:00 2001 From: Sondre Sortland Date: Mon, 3 May 2021 10:11:27 +0200 Subject: [PATCH] Allow cert to be None --- python/res/job_queue/queue.py | 9 ++-- tests/res/job_queue/test_job_queue.py | 69 ++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/python/res/job_queue/queue.py b/python/res/job_queue/queue.py index f84b362aa4..93e4d0a43e 100644 --- a/python/res/job_queue/queue.py +++ b/python/res/job_queue/queue.py @@ -587,9 +587,10 @@ def add_ensemble_evaluator_information_to_jobs_file( self, ee_id, dispatch_url, cert, token ): for q_index, q_node in enumerate(self.job_list): - cert_path = f"{q_node.run_path}/{CERT_FILE}" - with open(cert_path, "w") as cert_file: - cert_file.write(cert) + if cert is not None: + cert_path = f"{q_node.run_path}/{CERT_FILE}" + with open(cert_path, "w") as cert_file: + cert_file.write(cert) with open(f"{q_node.run_path}/{JOBS_FILE}", "r+") as jobs_file: data = json.load(jobs_file) @@ -598,7 +599,7 @@ def add_ensemble_evaluator_information_to_jobs_file( data["step_id"] = 0 data["dispatch_url"] = dispatch_url data["ee_token"] = token - data["ee_cert_path"] = cert_path + data["ee_cert_path"] = cert_path if cert is not None else None jobs_file.seek(0) jobs_file.truncate() diff --git a/tests/res/job_queue/test_job_queue.py b/tests/res/job_queue/test_job_queue.py index 2ba4e6e128..d7c9633bfa 100644 --- a/tests/res/job_queue/test_job_queue.py +++ b/tests/res/job_queue/test_job_queue.py @@ -2,7 +2,7 @@ from tests import ResTest from tests.utils import wait_until from ecl.util.test import TestAreaContext -import os, stat, time +import os, stat, time, pathlib, json from threading import BoundedSemaphore @@ -179,3 +179,70 @@ def test_timeout_jobs(self): for job in job_queue.job_list: job.wait_for() + + def test_add_ensemble_evaluator_info(self): + with TestAreaContext("job_queue_add_ensemble_evaluator_info") as work_area: + job_queue = create_queue(simple_script) + ee_id = "some_id" + dispatch_url = "wss://some_url.com" + cert = "My very nice cert" + token = "my_super_secret_token" + cert_file = ".ee.pem" + runpaths = [ + pathlib.Path(dummy_config["run_path"].format(i)) for i in range(10) + ] + for runpath in runpaths: + with open(runpath / "jobs.json", "w") as f: + json.dump({}, f) + job_queue.add_ensemble_evaluator_information_to_jobs_file( + ee_id=ee_id, + dispatch_url=dispatch_url, + cert=cert, + token=token, + ) + + for runpath in runpaths: + job_file_path = runpath / "jobs.json" + with open(job_file_path) as f: + content = json.load(f) + assert content["step_id"] == 0 + assert content["dispatch_url"] == dispatch_url + assert content["ee_token"] == token + + assert content["ee_cert_path"] == str(runpath / cert_file) + with open(runpath / cert_file) as f: + assert f.read() == cert + + def test_add_ensemble_evaluator_info_cert_none(self): + with TestAreaContext( + "job_queue_add_ensemble_evaluator_info_cert_none" + ) as work_area: + job_queue = create_queue(simple_script) + ee_id = "some_id" + dispatch_url = "wss://some_url.com" + cert = None + token = None + cert_file = ".ee.pem" + runpaths = [ + pathlib.Path(dummy_config["run_path"].format(i)) for i in range(10) + ] + for runpath in runpaths: + with open(runpath / "jobs.json", "w") as f: + json.dump({}, f) + job_queue.add_ensemble_evaluator_information_to_jobs_file( + ee_id=ee_id, + dispatch_url=dispatch_url, + cert=cert, + token=token, + ) + + for runpath in runpaths: + job_file_path = runpath / "jobs.json" + with open(job_file_path) as f: + content = json.load(f) + assert content["step_id"] == 0 + assert content["dispatch_url"] == dispatch_url + assert content["ee_token"] == token + + assert content["ee_cert_path"] == None + assert not (runpath / cert_file).exists()