From c0901d05825a2e59e2e78d6fe76408e7cb9088ca Mon Sep 17 00:00:00 2001 From: Tianhao-Gu Date: Fri, 26 Apr 2024 15:13:25 -0500 Subject: [PATCH 1/4] call get_ip only when necessary --- JobRunner/Callback.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/JobRunner/Callback.py b/JobRunner/Callback.py index cda66cd..cead130 100644 --- a/JobRunner/Callback.py +++ b/JobRunner/Callback.py @@ -12,7 +12,9 @@ class Callback(): def __init__(self): workdir = os.environ.get("JOB_DIR", '/tmp/') self.conf = Config(job_id="callback", workdir=workdir, use_ee2=False) - self.ip = os.environ.get('CALLBACK_IP', get_ip()) + self.ip = os.environ.get('CALLBACK_IP') + if not self.ip: + self.ip = get_ip() self.port = os.environ.get('CALLBACK_PORT') self.cbs = None self.callback_url = None From dfa129a6d42be1fa8ea93bb9139a1db682a04587 Mon Sep 17 00:00:00 2001 From: Tianhao-Gu Date: Fri, 26 Apr 2024 18:32:48 -0500 Subject: [PATCH 2/4] get base URL from env --- JobRunner/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JobRunner/config.py b/JobRunner/config.py index b425d66..5e2ebe7 100644 --- a/JobRunner/config.py +++ b/JobRunner/config.py @@ -6,6 +6,7 @@ _TOKEN_ENV = "KB_AUTH_TOKEN" _ADMIN_TOKEN_ENV = "KB_ADMIN_AUTH_TOKEN" _DEBUG_ENVNAME = "JOBRUNNER_DEBUG_MODE" +_KB_BASE_URL = "KB_BASE_URL" def _get_token(): @@ -34,7 +35,7 @@ def _get_admin_token(): class Config: def __init__(self, workdir=None, base_url=None, job_id=None, use_ee2=True): self.job_id = job_id - self.base_url = "https://ci.kbase.us/services/" + self.base_url = os.environ.get(_KB_BASE_URL, "https://ci.kbase.us/services/") self.ee2_url = None self.debug = False self.cgroup = None From 77074ab81ee7d1072403adf84955138648352765 Mon Sep 17 00:00:00 2001 From: Tianhao-Gu Date: Mon, 29 Apr 2024 10:37:47 -0500 Subject: [PATCH 3/4] add tests --- JobRunner/Callback.py | 4 +-- test/test_Callback.py | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 test/test_Callback.py diff --git a/JobRunner/Callback.py b/JobRunner/Callback.py index cead130..5128c1f 100644 --- a/JobRunner/Callback.py +++ b/JobRunner/Callback.py @@ -12,9 +12,7 @@ class Callback(): def __init__(self): workdir = os.environ.get("JOB_DIR", '/tmp/') self.conf = Config(job_id="callback", workdir=workdir, use_ee2=False) - self.ip = os.environ.get('CALLBACK_IP') - if not self.ip: - self.ip = get_ip() + self.ip = os.environ.get('CALLBACK_IP') or get_ip() self.port = os.environ.get('CALLBACK_PORT') self.cbs = None self.callback_url = None diff --git a/test/test_Callback.py b/test/test_Callback.py new file mode 100644 index 0000000..872d4e5 --- /dev/null +++ b/test/test_Callback.py @@ -0,0 +1,66 @@ +import unittest +from unittest.mock import patch + +from JobRunner.Callback import Callback + + +def setup_mock_environ(mock_environ_get, + callback_ip="127.0.0.1", + callback_port="8080"): + # Helper function to mock environment variables + mock_environ_get.side_effect = lambda x, default=None: { + "JOB_DIR": "/tmp/", + "CALLBACK_IP": callback_ip, + "CALLBACK_PORT": callback_port, + }.get(x, default) + + +def assert_callback_attributes(callback, + job_id="callback", + workdir="/tmp/", + ip="127.0.0.1", + port="8080", + cbs=None, + callback_url=None): + # Helper function to assert Callback attributes + assert callback.conf.job_id == job_id + assert callback.conf.workdir == workdir + assert callback.ip == ip + assert callback.port == port + assert callback.cbs == cbs + assert callback.callback_url == callback_url + + +class TestCallback(unittest.TestCase): + + @patch('JobRunner.Callback.os.environ.get') + def test_init_with_environment_variables(self, mock_environ_get): + # test the __init__ method of the Callback class when the environment variables including CALLBACK_IP are all set + + setup_mock_environ(mock_environ_get) + callback = Callback() + + assert_callback_attributes(callback) + + @patch('JobRunner.Callback.os.environ.get') + @patch('JobRunner.Callback.get_ip') + def test_init_without_callback_ip_environment_variables(self, mock_get_ip, mock_environ_get): + # test the __init__ method of the Callback class when the CALLBACK_IP environment variable is missing + + setup_mock_environ(mock_environ_get, callback_ip=None) + mock_get_ip.return_value = "192.168.1.1" + + callback = Callback() + assert_callback_attributes(callback, ip="192.168.1.1") + + @patch('JobRunner.Callback.os.environ.get') + @patch('JobRunner.Callback.get_ip') + def test_init_get_ip_error(self, mock_get_ip, mock_environ_get): + # test the __init__ method of the Callback class when the get_ip function raises an error but the CALLBACK_IP is set + + setup_mock_environ(mock_environ_get) + # Mock get_ip function to raise an error + mock_get_ip.side_effect = RuntimeError("Failed to get IP") + + callback = Callback() + assert_callback_attributes(callback) \ No newline at end of file From cdc292c981c1634320f20558733e41eb575efcf5 Mon Sep 17 00:00:00 2001 From: Tianhao-Gu Date: Mon, 29 Apr 2024 11:11:41 -0500 Subject: [PATCH 4/4] add tests --- test/{test_Callback.py => test_module_Callback.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename test/{test_Callback.py => test_module_Callback.py} (98%) diff --git a/test/test_Callback.py b/test/test_module_Callback.py similarity index 98% rename from test/test_Callback.py rename to test/test_module_Callback.py index 872d4e5..c23bf93 100644 --- a/test/test_Callback.py +++ b/test/test_module_Callback.py @@ -63,4 +63,4 @@ def test_init_get_ip_error(self, mock_get_ip, mock_environ_get): mock_get_ip.side_effect = RuntimeError("Failed to get IP") callback = Callback() - assert_callback_attributes(callback) \ No newline at end of file + assert_callback_attributes(callback)