2018-06-15 22:03:27 +01:00
|
|
|
import os
|
|
|
|
|
2018-06-20 02:36:53 +01:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from core.service import CoreService
|
2018-06-15 22:03:27 +01:00
|
|
|
from core.service import ServiceManager
|
|
|
|
|
|
|
|
_PATH = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
_SERVICES_PATH = os.path.join(_PATH, "myservices")
|
|
|
|
|
|
|
|
|
2018-06-20 02:36:53 +01:00
|
|
|
class ServiceA(CoreService):
|
|
|
|
name = "A"
|
|
|
|
dependencies = ("B",)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceB(CoreService):
|
|
|
|
name = "B"
|
|
|
|
dependencies = ("C",)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceC(CoreService):
|
|
|
|
name = "C"
|
|
|
|
dependencies = ()
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceD(CoreService):
|
|
|
|
name = "D"
|
|
|
|
dependencies = ("A",)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceE(CoreService):
|
|
|
|
name = "E"
|
|
|
|
dependencies = ("Z",)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceF(CoreService):
|
|
|
|
name = "F"
|
|
|
|
dependencies = ()
|
|
|
|
|
|
|
|
|
2018-06-15 22:03:27 +01:00
|
|
|
class TestServices:
|
2018-06-20 02:36:53 +01:00
|
|
|
def test_service_import(self):
|
2018-06-15 22:03:27 +01:00
|
|
|
"""
|
|
|
|
Test importing a custom service.
|
|
|
|
"""
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
|
|
|
assert ServiceManager.get("MyService")
|
|
|
|
assert ServiceManager.get("MyService2")
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
def test_services_dependencies(self, session):
|
|
|
|
# given
|
|
|
|
services = [
|
|
|
|
ServiceA(),
|
|
|
|
ServiceB(),
|
|
|
|
ServiceC(),
|
|
|
|
ServiceD(),
|
|
|
|
ServiceF(),
|
|
|
|
]
|
|
|
|
|
|
|
|
# when
|
2018-06-21 00:18:30 +01:00
|
|
|
startups = session.services.node_service_dependencies(services)
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
# then
|
|
|
|
assert len(startups) == 2
|
|
|
|
|
|
|
|
def test_services_dependencies_not_present(self, session):
|
|
|
|
# given
|
|
|
|
services = [
|
|
|
|
ServiceA(),
|
|
|
|
ServiceB(),
|
|
|
|
ServiceC(),
|
|
|
|
ServiceE()
|
|
|
|
]
|
|
|
|
|
|
|
|
# when
|
|
|
|
with pytest.raises(ValueError):
|
2018-06-21 00:18:30 +01:00
|
|
|
session.services.node_service_dependencies(services)
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
def test_services_dependencies_cycle(self, session):
|
|
|
|
# given
|
|
|
|
service_c = ServiceC()
|
|
|
|
service_c.dependencies = ("D",)
|
|
|
|
services = [
|
|
|
|
ServiceA(),
|
|
|
|
ServiceB(),
|
|
|
|
service_c,
|
|
|
|
ServiceD(),
|
|
|
|
ServiceF()
|
|
|
|
]
|
|
|
|
|
|
|
|
# when
|
|
|
|
with pytest.raises(ValueError):
|
2018-06-21 00:18:30 +01:00
|
|
|
session.services.node_service_dependencies(services)
|