From 3ce416b610431253a975c550b864e8181384adeb Mon Sep 17 00:00:00 2001 From: "Blake J. Harnden" Date: Tue, 9 Jan 2018 15:52:29 -0800 Subject: [PATCH] updated test cases to fix old broken code --- .gitignore | 3 +++ daemon/core/service.py | 29 +++++------------------------ daemon/sbin/core-daemon | 9 +++++++++ daemon/tests/conftest.py | 23 ++++++----------------- daemon/tests/test_core.py | 2 +- 5 files changed, 24 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 3e9cd0b7..8f0324f9 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ coverage.xml # ignore package files *.rpm *.deb + +# pytest cache files +.cache diff --git a/daemon/core/service.py b/daemon/core/service.py index f60059f0..7309baca 100644 --- a/daemon/core/service.py +++ b/daemon/core/service.py @@ -118,7 +118,11 @@ class ServiceManager(object): :return: list of core services :rtype: list """ - # validate path exists for importing + # validate path exists + logger.info("attempting to add services from path: %s", path) + if not os.path.isdir(path): + logger.warn("invalid custom service directory specified" ": %s" % path) + # check if path is in sys.path logger.info("getting custom services from: %s", path) parent_path = os.path.dirname(path) if parent_path not in sys.path: @@ -175,33 +179,10 @@ class CoreServices(ConfigurableManager): # dict of tuple of service objects, key is node number self.customservices = {} - paths = self.session.get_config_item('custom_services_dir') - if paths: - for path in paths.split(','): - path = path.strip() - self.importcustom(path) - # TODO: remove need for cyclic import from core.services import startup self.is_startup_service = startup.Startup.is_startup_service - def importcustom(self, path): - """ - Import services from a myservices directory. - - :param str path: path to import custom services from - :return: nothing - """ - - if not path or len(path) == 0: - return - - if not os.path.isdir(path): - logger.warn("invalid custom service directory specified" ": %s" % path) - return - - ServiceManager.add_services(path) - def reset(self): """ Called when config message with reset flag is received diff --git a/daemon/sbin/core-daemon b/daemon/sbin/core-daemon index b47ec8f5..85b9a8ac 100755 --- a/daemon/sbin/core-daemon +++ b/daemon/sbin/core-daemon @@ -39,6 +39,7 @@ from core.misc import nodemaps from core.misc import nodeutils from core.misc.utils import closeonexec from core.misc.utils import daemonize +from core.service import ServiceManager DEFAULT_MAXFD = 1024 @@ -313,6 +314,14 @@ def main(): for a in args: logger.error("ignoring command line argument: %s", a) + # attempt load custom services + service_paths = cfg.get("custom_services_dir") + logger.debug("custom service paths: %s", service_paths) + if service_paths: + for service_path in service_paths.split(','): + service_path = service_path.strip() + ServiceManager.add_services(service_path) + if cfg["daemonize"] == "True": daemonize(rootdir=None, umask=0, close_fds=False, stdin=os.devnull, diff --git a/daemon/tests/conftest.py b/daemon/tests/conftest.py index 878fb47a..96f740e8 100644 --- a/daemon/tests/conftest.py +++ b/daemon/tests/conftest.py @@ -8,12 +8,11 @@ import pytest from mock.mock import MagicMock +from core import services from core.coreserver import CoreServer from core.misc import nodemaps from core.misc import nodeutils from core.netns import nodes -from core.services import quagga -from core.services import utility from core.session import Session from core.api.coreapi import CoreConfMessage from core.api.coreapi import CoreEventMessage @@ -244,14 +243,6 @@ class Core(object): class CoreServerTest(object): def __init__(self): - # setup nodes - node_map = nodemaps.NODES - nodeutils.set_node_map(node_map) - - # load emane services - quagga.load_services() - utility.load_services() - address = ("localhost", CORE_API_PORT) self.server = CoreServer(address, CoreRequestHandler, { "numthreads": 1, @@ -320,19 +311,14 @@ class CoreServerTest(object): @pytest.fixture() def session(): - # configure default nodes - node_map = nodemaps.NODES - nodeutils.set_node_map(node_map) + # load default services + services.load() # create and return session session_fixture = Session(1, persistent=True) session_fixture.master = True assert os.path.exists(session_fixture.session_dir) - # load emane services - quagga.load_services() - utility.load_services() - # set location # session_fixture.master = True session_fixture.location.setrefgeo(47.57917, -122.13232, 2.00000) @@ -362,6 +348,9 @@ def core(session, ip_prefix): @pytest.fixture() def cored(): + # load default services + services.load() + # create and return server server = CoreServerTest() yield server diff --git a/daemon/tests/test_core.py b/daemon/tests/test_core.py index 2e9094cc..f47827a0 100644 --- a/daemon/tests/test_core.py +++ b/daemon/tests/test_core.py @@ -36,7 +36,7 @@ class TestCore: :param conftest.Core core: core fixture to test with """ - core.session.services.importcustom(_SERVICES_PATH) + ServiceManager.add_services(_SERVICES_PATH) assert ServiceManager.get("MyService") assert ServiceManager.get("MyService2")