Skip to content

Commit

Permalink
Merge pull request #85 from kbase/dev_fix_get_ip
Browse files Browse the repository at this point in the history
call get_ip only when necessary
  • Loading branch information
Tianhao-Gu authored May 1, 2024
2 parents 7469d43 + cdc292c commit 207d4d3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion JobRunner/Callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +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', 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
Expand Down
3 changes: 2 additions & 1 deletion JobRunner/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions test/test_module_Callback.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 207d4d3

Please sign in to comment.