2018-06-15 22:03:27 +01:00
|
|
|
import os
|
|
|
|
|
2018-06-20 02:36:53 +01:00
|
|
|
import pytest
|
2019-10-24 07:15:19 +01:00
|
|
|
from mock import MagicMock
|
2019-09-11 05:33:35 +01:00
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
from core.emulator.session import Session
|
2019-10-24 07:15:19 +01:00
|
|
|
from core.errors import CoreCommandError
|
2020-05-21 06:14:03 +01:00
|
|
|
from core.nodes.base import CoreNode
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.services.coreservices import CoreService, ServiceDependencies, ServiceManager
|
2018-06-15 22:03:27 +01:00
|
|
|
|
|
|
|
_PATH = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
_SERVICES_PATH = os.path.join(_PATH, "myservices")
|
|
|
|
|
2018-06-25 16:41:23 +01:00
|
|
|
SERVICE_ONE = "MyService"
|
|
|
|
SERVICE_TWO = "MyService2"
|
|
|
|
|
2018-06-15 22:03:27 +01:00
|
|
|
|
2018-06-20 02:36:53 +01:00
|
|
|
class ServiceA(CoreService):
|
|
|
|
name = "A"
|
|
|
|
dependencies = ("B",)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceB(CoreService):
|
|
|
|
name = "B"
|
2018-08-01 18:13:57 +01:00
|
|
|
dependencies = ()
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ServiceC(CoreService):
|
|
|
|
name = "C"
|
2018-08-01 18:13:57 +01:00
|
|
|
dependencies = ("B", "D")
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ServiceD(CoreService):
|
|
|
|
name = "D"
|
2018-08-01 18:13:57 +01:00
|
|
|
dependencies = ()
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
class ServiceBadDependency(CoreService):
|
2018-06-20 02:36:53 +01:00
|
|
|
name = "E"
|
|
|
|
dependencies = ("Z",)
|
|
|
|
|
|
|
|
|
|
|
|
class ServiceF(CoreService):
|
|
|
|
name = "F"
|
|
|
|
dependencies = ()
|
|
|
|
|
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
class ServiceCycleDependency(CoreService):
|
|
|
|
name = "G"
|
|
|
|
|
|
|
|
|
2018-06-15 22:03:27 +01:00
|
|
|
class TestServices:
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_all_files(self, session: Session):
|
2018-06-25 16:41:23 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
|
|
|
file_name = "myservice.sh"
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-25 16:41:23 +01:00
|
|
|
|
|
|
|
# when
|
2019-04-27 06:07:51 +01:00
|
|
|
session.services.set_service_file(node.id, SERVICE_ONE, file_name, "# test")
|
2018-06-25 16:41:23 +01:00
|
|
|
|
|
|
|
# then
|
2019-04-27 06:07:51 +01:00
|
|
|
service = session.services.get_service(node.id, SERVICE_ONE)
|
2018-06-25 16:41:23 +01:00
|
|
|
all_files = session.services.all_files(service)
|
|
|
|
assert service
|
|
|
|
assert all_files and len(all_files) == 1
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_all_configs(self, session: Session):
|
2018-06-25 16:41:23 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-25 16:41:23 +01:00
|
|
|
|
|
|
|
# when
|
2019-04-27 06:07:51 +01:00
|
|
|
session.services.set_service(node.id, SERVICE_ONE)
|
|
|
|
session.services.set_service(node.id, SERVICE_TWO)
|
2018-06-25 16:41:23 +01:00
|
|
|
|
|
|
|
# then
|
|
|
|
all_configs = session.services.all_configs()
|
|
|
|
assert all_configs
|
|
|
|
assert len(all_configs) == 2
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_add_services(self, session: Session):
|
2018-06-25 16:41:23 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-25 16:41:23 +01:00
|
|
|
total_service = len(node.services)
|
|
|
|
|
|
|
|
# when
|
|
|
|
session.services.add_services(node, node.type, [SERVICE_ONE, SERVICE_TWO])
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert node.services
|
|
|
|
assert len(node.services) == total_service + 2
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_file(self, request, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
file_name = my_service.configs[0]
|
|
|
|
file_path = node.hostfilename(file_name)
|
|
|
|
|
|
|
|
# when
|
|
|
|
session.services.create_service_files(node, my_service)
|
|
|
|
|
|
|
|
# then
|
2019-10-24 07:15:19 +01:00
|
|
|
if not request.config.getoption("mock"):
|
|
|
|
assert os.path.exists(file_path)
|
2018-06-22 23:47:02 +01:00
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_validate(self, session: Session):
|
2018-06-25 16:41:23 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-25 16:41:23 +01:00
|
|
|
session.services.create_service_files(node, my_service)
|
|
|
|
|
|
|
|
# when
|
|
|
|
status = session.services.validate_service(node, my_service)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert not status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_validate_error(self, session: Session):
|
2018-06-25 16:41:23 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
|
|
|
my_service = ServiceManager.get(SERVICE_TWO)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-25 16:41:23 +01:00
|
|
|
session.services.create_service_files(node, my_service)
|
2019-10-24 07:15:19 +01:00
|
|
|
node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid"))
|
2018-06-25 16:41:23 +01:00
|
|
|
|
|
|
|
# when
|
|
|
|
status = session.services.validate_service(node, my_service)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_startup(self, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
session.services.create_service_files(node, my_service)
|
|
|
|
|
|
|
|
# when
|
|
|
|
status = session.services.startup_service(node, my_service, wait=True)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert not status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_startup_error(self, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_TWO)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
session.services.create_service_files(node, my_service)
|
2019-10-24 07:15:19 +01:00
|
|
|
node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid"))
|
2018-06-22 23:47:02 +01:00
|
|
|
|
|
|
|
# when
|
|
|
|
status = session.services.startup_service(node, my_service, wait=True)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_stop(self, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
session.services.create_service_files(node, my_service)
|
|
|
|
|
|
|
|
# when
|
|
|
|
status = session.services.stop_service(node, my_service)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert not status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_stop_error(self, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_TWO)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
session.services.create_service_files(node, my_service)
|
2019-10-24 07:15:19 +01:00
|
|
|
node.cmd = MagicMock(side_effect=CoreCommandError(-1, "invalid"))
|
2018-06-22 23:47:02 +01:00
|
|
|
|
|
|
|
# when
|
|
|
|
status = session.services.stop_service(node, my_service)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert status
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_custom_startup(self, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-07-25 17:37:59 +01:00
|
|
|
|
|
|
|
# when
|
2019-04-27 06:07:51 +01:00
|
|
|
session.services.set_service(node.id, my_service.name)
|
|
|
|
custom_my_service = session.services.get_service(node.id, my_service.name)
|
2018-07-25 17:37:59 +01:00
|
|
|
custom_my_service.startup = ("sh custom.sh",)
|
|
|
|
|
|
|
|
# then
|
|
|
|
assert my_service.startup != custom_my_service.startup
|
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_set_file(self, session: Session):
|
2018-07-25 17:37:59 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node_one = session.add_node(CoreNode)
|
|
|
|
node_two = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
file_name = my_service.configs[0]
|
2018-07-25 17:37:59 +01:00
|
|
|
file_data_one = "# custom file one"
|
|
|
|
file_data_two = "# custom file two"
|
2019-09-10 23:10:24 +01:00
|
|
|
session.services.set_service_file(
|
|
|
|
node_one.id, my_service.name, file_name, file_data_one
|
|
|
|
)
|
|
|
|
session.services.set_service_file(
|
|
|
|
node_two.id, my_service.name, file_name, file_data_two
|
|
|
|
)
|
2018-06-22 23:47:02 +01:00
|
|
|
|
|
|
|
# when
|
2019-04-27 06:07:51 +01:00
|
|
|
custom_service_one = session.services.get_service(node_one.id, my_service.name)
|
2018-07-25 17:37:59 +01:00
|
|
|
session.services.create_service_files(node_one, custom_service_one)
|
2019-04-27 06:07:51 +01:00
|
|
|
custom_service_two = session.services.get_service(node_two.id, my_service.name)
|
2018-07-25 17:37:59 +01:00
|
|
|
session.services.create_service_files(node_two, custom_service_two)
|
2018-06-22 23:47:02 +01:00
|
|
|
|
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)
|
2018-06-25 16:41:23 +01:00
|
|
|
assert ServiceManager.get(SERVICE_ONE)
|
|
|
|
assert ServiceManager.get(SERVICE_TWO)
|
2018-06-20 02:36:53 +01:00
|
|
|
|
2020-05-25 07:37:38 +01:00
|
|
|
def test_service_setget(self, session: Session):
|
2018-06-22 23:47:02 +01:00
|
|
|
# given
|
|
|
|
ServiceManager.add_services(_SERVICES_PATH)
|
2018-06-25 16:41:23 +01:00
|
|
|
my_service = ServiceManager.get(SERVICE_ONE)
|
2020-05-21 06:14:03 +01:00
|
|
|
node = session.add_node(CoreNode)
|
2018-06-22 23:47:02 +01:00
|
|
|
|
|
|
|
# when
|
2019-04-27 06:07:51 +01:00
|
|
|
no_service = session.services.get_service(node.id, SERVICE_ONE)
|
2019-09-10 23:10:24 +01:00
|
|
|
default_service = session.services.get_service(
|
|
|
|
node.id, SERVICE_ONE, default_service=True
|
|
|
|
)
|
2019-04-27 06:07:51 +01:00
|
|
|
session.services.set_service(node.id, SERVICE_ONE)
|
2019-09-10 23:10:24 +01:00
|
|
|
custom_service = session.services.get_service(
|
|
|
|
node.id, SERVICE_ONE, default_service=True
|
|
|
|
)
|
2018-06-22 23:47:02 +01:00
|
|
|
|
|
|
|
# then
|
|
|
|
assert no_service is None
|
|
|
|
assert default_service == my_service
|
|
|
|
assert custom_service and custom_service != my_service
|
2018-06-22 22:41:06 +01:00
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
def test_services_dependencies(self):
|
2018-06-20 02:36:53 +01:00
|
|
|
# given
|
2019-09-10 23:10:24 +01:00
|
|
|
services = [ServiceA, ServiceB, ServiceC, ServiceD, ServiceF]
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
# when
|
2018-08-01 18:13:57 +01:00
|
|
|
boot_paths = ServiceDependencies(services).boot_paths()
|
2018-06-20 02:36:53 +01:00
|
|
|
|
|
|
|
# then
|
2018-08-01 18:13:57 +01:00
|
|
|
assert len(boot_paths) == 2
|
2018-06-20 02:36:53 +01:00
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
def test_services_dependencies_not_present(self):
|
2018-06-20 02:36:53 +01:00
|
|
|
# given
|
|
|
|
services = [
|
2018-06-22 22:41:06 +01:00
|
|
|
ServiceA,
|
|
|
|
ServiceB,
|
|
|
|
ServiceC,
|
2018-08-01 18:13:57 +01:00
|
|
|
ServiceD,
|
|
|
|
ServiceF,
|
2019-09-10 23:10:24 +01:00
|
|
|
ServiceBadDependency,
|
2018-06-20 02:36:53 +01:00
|
|
|
]
|
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
# when, then
|
2018-06-20 02:36:53 +01:00
|
|
|
with pytest.raises(ValueError):
|
2018-08-01 18:13:57 +01:00
|
|
|
ServiceDependencies(services).boot_paths()
|
2018-06-20 02:36:53 +01:00
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
def test_services_dependencies_cycle(self):
|
2018-06-20 02:36:53 +01:00
|
|
|
# given
|
2018-08-01 18:13:57 +01:00
|
|
|
service_d = ServiceD()
|
|
|
|
service_d.dependencies = ("C",)
|
2019-09-10 23:10:24 +01:00
|
|
|
services = [ServiceA, ServiceB, ServiceC, service_d, ServiceF]
|
2018-06-20 02:36:53 +01:00
|
|
|
|
2018-08-01 18:13:57 +01:00
|
|
|
# when, then
|
2018-06-20 02:36:53 +01:00
|
|
|
with pytest.raises(ValueError):
|
2018-08-01 18:13:57 +01:00
|
|
|
ServiceDependencies(services).boot_paths()
|