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

ServiceInfo now gets the provided interfaces from the .serviceconfig #274

Merged
8 changes: 1 addition & 7 deletions ni_measurementlink_service/_internal/discovery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
import win32file
import winerror


_PROVIDED_MEASUREMENT_SERVICES = [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService",
]

_logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -111,7 +105,7 @@ def register_measurement_service(
service_descriptor.display_name = measurement_info.display_name
service_descriptor.service_class = service_info.service_class
service_descriptor.description_url = service_info.description_url
service_descriptor.provided_interfaces.extend(_PROVIDED_MEASUREMENT_SERVICES)
service_descriptor.provided_interfaces.extend(service_info.provided_interfaces)

# Registration Request Creation
request = discovery_service_pb2.RegisterServiceRequest(
Expand Down
6 changes: 6 additions & 0 deletions ni_measurementlink_service/measurement/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ class ServiceInfo(NamedTuple):
Measurements under same service class expected to perform same logic.
For e.g., different version of measurement can come under same service class.


description_url (str): Description URL of the measurement.

provided_interfaces (List[str]): List of interfaces the service provides.
For e.g., ni.measurementlink.measurement.v2.MeasurementService.
Defaults to ["ni.measurementlink.measurement.v1.MeasurementService"].

"""

service_class: str
description_url: str
provided_interfaces: typing.List[str] = ["ni.measurementlink.measurement.v1.MeasurementService"]


class TypeSpecialization(enum.Enum):
Expand Down
1 change: 1 addition & 0 deletions ni_measurementlink_service/measurement/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def __init__(
self.service_info = ServiceInfo(
service_class=service["serviceClass"],
description_url=service["descriptionUrl"],
provided_interfaces=service["providedInterfaces"],
)

self.configuration_parameter_list: list = []
Expand Down
13 changes: 13 additions & 0 deletions tests/assets/example.v1.serviceconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"services": [
{
"displayName": "SampleMeasurement",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v1.MeasurementService"
],
"path": "start.bat"
}
]
}
13 changes: 13 additions & 0 deletions tests/assets/example.v2.serviceconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"services": [
{
"displayName": "SampleMeasurement",
"serviceClass": "SampleMeasurement_Python",
"descriptionUrl": "https://www.example.com/SampleMeasurement.html",
"providedInterfaces": [
"ni.measurementlink.measurement.v2.MeasurementService"
],
"path": "start.bat"
}
]
}
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
"""Pytest configuration file."""
import pathlib

import pytest


@pytest.fixture(scope="module")
def test_assets_directory() -> pathlib.Path:
"""Gets path to test_assets directory."""
return pathlib.Path(__file__).parent / "assets"
8 changes: 6 additions & 2 deletions tests/unit/test_discovery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

from ni_measurementlink_service._internal.discovery_client import (
_PROVIDED_MEASUREMENT_SERVICES,
DiscoveryClient,
)
from ni_measurementlink_service._internal.stubs.ni.measurementlink.discovery.v1.discovery_service_pb2_grpc import (
Expand All @@ -14,8 +13,13 @@
from ni_measurementlink_service.measurement.info import MeasurementInfo, ServiceInfo
from tests.utilities.fake_discovery_service import FakeDiscoveryServiceStub

_PROVIDED_MEASUREMENT_SERVICES = [
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService",
]

_TEST_SERVICE_PORT = "9999"
_TEST_SERVICE_INFO = ServiceInfo("TestServiceClass", "TestUrl")
_TEST_SERVICE_INFO = ServiceInfo("TestServiceClass", "TestUrl", _PROVIDED_MEASUREMENT_SERVICES)
_TEST_MEASUREMENT_INFO = MeasurementInfo(
display_name="TestMeasurement",
version="1.0.0.0",
Expand Down
37 changes: 34 additions & 3 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests to validated user facing decorators in service.py."""
import pathlib
from typing import List

import pytest

Expand Down Expand Up @@ -228,12 +229,42 @@ def _fake_measurement_function():
pass


@pytest.mark.parametrize(
"service_config,provided_interfaces",
[
(
"example.serviceconfig",
[
"ni.measurementlink.measurement.v1.MeasurementService",
"ni.measurementlink.measurement.v2.MeasurementService",
],
),
("example.v1.serviceconfig", ["ni.measurementlink.measurement.v1.MeasurementService"]),
("example.v2.serviceconfig", ["ni.measurementlink.measurement.v2.MeasurementService"]),
],
)
def test___service_config___create_measurement_service___service_info_matches_service_config(
test_assets_directory: pathlib.Path, service_config: str, provided_interfaces: List[str]
):
measurement_service = MeasurementService(
service_config_path=test_assets_directory / service_config,
version="1.0.0.0",
ui_file_paths=[],
)

assert measurement_service.service_info.service_class == "SampleMeasurement_Python"
assert set(measurement_service.service_info.provided_interfaces) >= set(provided_interfaces)
assert (
measurement_service.service_info.description_url
== "https://www.example.com/SampleMeasurement.html"
)


@pytest.fixture
def measurement_service() -> MeasurementService:
def measurement_service(test_assets_directory: pathlib.Path) -> MeasurementService:
"""Create a MeasurementService."""
assets_directory = pathlib.Path(__file__).resolve().parent.parent / "assets"
return MeasurementService(
service_config_path=assets_directory / "example.serviceconfig",
service_config_path=test_assets_directory / "example.serviceconfig",
version="1.0.0.0",
ui_file_paths=[],
)