Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discovery_client: When a python measurement starts discovery service, hide discovery service logs #343

Merged
merged 10 commits into from
Jul 31, 2023
10 changes: 9 additions & 1 deletion ni_measurementlink_service/_internal/discovery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def register_measurement_service(
_logger.error(
"Unable to register with discovery service. Possible reason: discovery service not running."
)
return False
except Exception:
_logger.exception("Error in registering with discovery service.")
return False
Expand All @@ -161,6 +162,7 @@ def unregister_service(self) -> bool:
_logger.info("Successfully unregistered with discovery service.")
else:
_logger.info("Not registered with discovery service.")
return False
except grpc.RpcError as e:
if e.code() == grpc.StatusCode.UNAVAILABLE:
_logger.error(
Expand All @@ -173,6 +175,7 @@ def unregister_service(self) -> bool:
_logger.error(
"Unable to unregister with discovery service. Possible reason: discovery service not running."
)
return False
except Exception:
_logger.exception("Error in unregistering with discovery service.")
return False
Expand Down Expand Up @@ -254,7 +257,12 @@ def _key_file_exists(key_file_path: pathlib.Path) -> bool:

def _start_service(exe_file_path: pathlib.PurePath, key_file_path: pathlib.Path) -> None:
"""Starts the service at the specified path and wait for the service to get up and running."""
subprocess.Popen([exe_file_path], cwd=exe_file_path.parent)
subprocess.Popen(
[exe_file_path],
cwd=exe_file_path.parent,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
# After the execution of process, check for key file existence in the path
# stop checking after 30 seconds have elapsed and throw error
timeout_time = time.time() + _START_SERVICE_TIMEOUT
Expand Down
52 changes: 37 additions & 15 deletions tests/unit/test_discovery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
import json
import pathlib
import subprocess
from typing import cast

import pytest
Expand Down Expand Up @@ -76,19 +77,7 @@ def test___discovery_service_available___unregister_non_registered_service___unr
):
unregistration_success_flag = discovery_client.unregister_service()

assert ~unregistration_success_flag # False


def test___discovery_service_unavailable___register_service_registration_failure(
discovery_client: DiscoveryClient,
):
discovery_client.register_measurement_service(
_TEST_SERVICE_PORT, _TEST_SERVICE_INFO, _TEST_MEASUREMENT_INFO
)

unregistration_success_flag = discovery_client.unregister_service()

assert ~unregistration_success_flag # False
assert not unregistration_success_flag


def test___get_discovery_service_address___start_service_jit___returns_expected_value(
Expand All @@ -115,7 +104,12 @@ def test___get_discovery_service_address___start_service_jit___returns_expected_

exe_file_path = temp_directory / _MOCK_REGISTRATION_FILE_CONTENT["discovery"]["path"]

mock_popen.assert_called_once_with([exe_file_path], cwd=exe_file_path.parent)
mock_popen.assert_called_once_with(
[exe_file_path],
cwd=exe_file_path.parent,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
assert _TEST_SERVICE_PORT in discovery_service_address


Expand Down Expand Up @@ -158,7 +152,35 @@ def test___start_discovery_service___key_file_exist_after_poll___service_start_s

_start_service(exe_file_path, temp_discovery_key_file_path)

mock_popen.assert_called_once_with([exe_file_path], cwd=exe_file_path.parent)
mock_popen.assert_called_once_with(
[exe_file_path],
cwd=exe_file_path.parent,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)


def test___discovery_service_exe_unavailable___register_service___registration_failure(
mocker: MockerFixture,
temp_discovery_key_file_path: pathlib.Path,
temp_registration_json_file_path: pathlib.Path,
):
mocker.patch(
"ni_measurementlink_service._internal.discovery_client._get_key_file_path",
return_value=temp_discovery_key_file_path,
)
mocker.patch(
"ni_measurementlink_service._internal.discovery_client._get_registration_json_file_path",
return_value=temp_registration_json_file_path,
)
mocker.patch("subprocess.Popen", side_effect=FileNotFoundError)

discovery_client = DiscoveryClient()
registration_success_flag = discovery_client.register_measurement_service(
_TEST_SERVICE_PORT, _TEST_SERVICE_INFO, _TEST_MEASUREMENT_INFO
)

assert not registration_success_flag


@pytest.fixture
Expand Down