Skip to content

Commit

Permalink
Add unit tests (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
edingroot authored Feb 22, 2024
1 parent 1e7ce6f commit 742ceb8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
13 changes: 10 additions & 3 deletions service_configuration_lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import yaml

DEFAULT_SPARK_RUN_CONFIG = '/nail/srv/configs/spark.yaml'

POD_TEMPLATE_PATH = '/nail/tmp/spark-pt-{file_uuid}.yaml'

SPARK_EXECUTOR_POD_TEMPLATE = '/nail/srv/configs/spark_executor_pod_template.yaml'

LOCALHOST = '127.0.0.1'
EPHEMERAL_PORT_START = 49152
EPHEMERAL_PORT_END = 65535


log = logging.Logger(__name__)
log.setLevel(logging.INFO)

Expand All @@ -46,7 +49,11 @@ def load_spark_srv_conf(preset_values=None) -> Tuple[Mapping, Mapping, Mapping,
raise e


def ephemeral_port_reserve_range(preferred_port_start: int, preferred_port_end: int, ip='127.0.0.1') -> int:
def ephemeral_port_reserve_range(
preferred_port_start: int = EPHEMERAL_PORT_START,
preferred_port_end: int = EPHEMERAL_PORT_END,
ip: str = LOCALHOST,
) -> int:
"""
Pick an available from the preferred port range. If all ports from the port range are unavailable,
pick a random available ephemeral port.
Expand Down
55 changes: 55 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
import uuid
from socket import SO_REUSEADDR
from socket import socket as Socket
from socket import SOL_SOCKET
from unittest import mock
from unittest.mock import mock_open
from unittest.mock import patch

import pytest

from service_configuration_lib import utils
from service_configuration_lib.utils import ephemeral_port_reserve_range
from service_configuration_lib.utils import LOCALHOST


MOCK_ENV_NAME = 'mock_env_name'


def _bind_reuse(ip, port):
sock = Socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
sock.bind((ip, port))
return sock


def test_preferred_port():
port = ephemeral_port_reserve_range()
port2 = ephemeral_port_reserve_range(port, port + 1)
assert port == port2
assert _bind_reuse(LOCALHOST, port2)


def test_preferred_port_in_use():
"""if preferred port is in use, it will find an unused port"""
port = ephemeral_port_reserve_range()
sock = _bind_reuse(LOCALHOST, port)
sock.listen(1) # make the port in-use

port_end = port + 10
port2 = ephemeral_port_reserve_range(port, port_end)
assert port != port2
assert port2 > port and port2 <= port_end
assert _bind_reuse(LOCALHOST, port2)


def test_get_random_string():
length = 50
result = utils.get_random_string(50)
assert len(result) == length


@pytest.mark.parametrize(
Expand All @@ -29,3 +72,15 @@ def test_get_k8s_resource_name_limit_size_with_hash(instance_name, expected_inst
def test_generate_pod_template_path(hex_value):
with mock.patch.object(uuid, 'uuid4', return_value=uuid.UUID(hex=hex_value)):
assert utils.generate_pod_template_path() == f'/nail/tmp/spark-pt-{hex_value}.yaml'


@pytest.fixture
def mock_runtimeenv():
with patch('builtins.open', mock_open(read_data=MOCK_ENV_NAME)) as m:
yield m


def test_get_runtime_env(mock_runtimeenv):
result = utils.get_runtime_env()
assert result == MOCK_ENV_NAME
mock_runtimeenv.assert_called_once_with('/nail/etc/runtimeenv', mode='r')

0 comments on commit 742ceb8

Please sign in to comment.