Example #1
0
 def test_service_import(self):
     """
     Test importing a custom service.
     """
     ServiceManager.add_services(_SERVICES_PATH)
     assert ServiceManager.get(SERVICE_ONE)
     assert ServiceManager.get(SERVICE_TWO)
Example #2
0
    def test_import_service(self):
        """
        Test importing a custom service.

        :param conftest.Core core: core fixture to test with
        """
        ServiceManager.add_services(_SERVICES_PATH)
        assert ServiceManager.get("MyService")
        assert ServiceManager.get("MyService2")
Example #3
0
    def test_import_service(self, core):
        """
        Test importing a custom service.

        :param conftest.Core core: core fixture to test with
        """
        core.session.services.importcustom(_SERVICES_PATH)
        assert ServiceManager.get("MyService")
        assert ServiceManager.get("MyService2")
Example #4
0
    def test_service_set_file(self, session):
        # given
        ServiceManager.add_services(_SERVICES_PATH)
        my_service = ServiceManager.get(SERVICE_ONE)
        node_one = session.add_node()
        node_two = session.add_node()
        file_name = my_service.configs[0]
        file_data_one = "# custom file one"
        file_data_two = "# custom file two"
        session.services.set_service_file(node_one.objid, my_service.name, file_name, file_data_one)
        session.services.set_service_file(node_two.objid, my_service.name, file_name, file_data_two)

        # when
        custom_service_one = session.services.get_service(node_one.objid, my_service.name)
        session.services.create_service_files(node_one, custom_service_one)
        custom_service_two = session.services.get_service(node_two.objid, my_service.name)
        session.services.create_service_files(node_two, custom_service_two)

        # then
        file_path_one = node_one.hostfilename(file_name)
        assert os.path.exists(file_path_one)
        with open(file_path_one, "r") as custom_file:
            assert custom_file.read() == file_data_one

        file_path_two = node_two.hostfilename(file_name)
        assert os.path.exists(file_path_two)
        with open(file_path_two, "r") as custom_file:
            assert custom_file.read() == file_data_two
Example #5
0
    def test_service_stop_error(self, session):
        # given
        ServiceManager.add_services(_SERVICES_PATH)
        my_service = ServiceManager.get(SERVICE_TWO)
        node = session.add_node()
        session.services.create_service_files(node, my_service)

        # when
        status = session.services.stop_service(node, my_service)

        # then
        assert status
Example #6
0
    def test_service_startup(self, session):
        # given
        ServiceManager.add_services(_SERVICES_PATH)
        my_service = ServiceManager.get(SERVICE_ONE)
        node = session.add_node()
        session.services.create_service_files(node, my_service)

        # when
        status = session.services.startup_service(node, my_service, wait=True)

        # then
        assert not status
Example #7
0
    def test_service_file(self, session):
        # given
        ServiceManager.add_services(_SERVICES_PATH)
        my_service = ServiceManager.get(SERVICE_ONE)
        node = session.add_node()
        file_name = my_service.configs[0]
        file_path = node.hostfilename(file_name)

        # when
        session.services.create_service_files(node, my_service)

        # then
        assert os.path.exists(file_path)
Example #8
0
    def test_service_custom_startup(self, session):
        # given
        ServiceManager.add_services(_SERVICES_PATH)
        my_service = ServiceManager.get(SERVICE_ONE)
        node = session.add_node()

        # when
        session.services.set_service(node.objid, my_service.name)
        custom_my_service = session.services.get_service(node.objid, my_service.name)
        custom_my_service.startup = ("sh custom.sh",)

        # then
        assert my_service.startup != custom_my_service.startup
Example #9
0
    def test_service_setget(self, session):
        # given
        ServiceManager.add_services(_SERVICES_PATH)
        my_service = ServiceManager.get(SERVICE_ONE)
        node = session.add_node()

        # when
        no_service = session.services.get_service(node.objid, SERVICE_ONE)
        default_service = session.services.get_service(node.objid, SERVICE_ONE, default_service=True)
        session.services.set_service(node.objid, SERVICE_ONE)
        custom_service = session.services.get_service(node.objid, SERVICE_ONE, default_service=True)

        # then
        assert no_service is None
        assert default_service == my_service
        assert custom_service and custom_service != my_service
Example #10
0
    def parseservice(self, service, n):
        """
        Use session.services manager to store service customizations before
        they are added to a node.
        """
        name = service.getAttribute("name")
        svc = ServiceManager.get(name)
        if svc is None:
            return False
        values = []
        startup_idx = service.getAttribute("startup_idx")
        if startup_idx is not None:
            values.append("startidx=%s" % startup_idx)
        startup_time = service.getAttribute("start_time")
        if startup_time is not None:
            values.append("starttime=%s" % startup_time)
        dirs = []
        for dir in service.getElementsByTagName("Directory"):
            dirname = dir.getAttribute("name")
            dirs.append(dirname)
        if len(dirs):
            values.append("dirs=%s" % dirs)

        startup = []
        shutdown = []
        validate = []
        for cmd in service.getElementsByTagName("Command"):
            type = cmd.getAttribute("type")
            cmdstr = xmlutils.get_text_child(cmd)
            if cmdstr is None:
                continue
            if type == "start":
                startup.append(cmdstr)
            elif type == "stop":
                shutdown.append(cmdstr)
            elif type == "validate":
                validate.append(cmdstr)
        if len(startup):
            values.append("cmdup=%s" % startup)
        if len(shutdown):
            values.append("cmddown=%s" % shutdown)
        if len(validate):
            values.append("cmdval=%s" % validate)

        files = []
        for file in service.getElementsByTagName("File"):
            filename = file.getAttribute("name")
            files.append(filename)
            data = xmlutils.get_text_child(file)
            self.session.services.set_service_file(node_id=n.objid, service_name=name, file_name=filename, data=data)

        if len(files):
            values.append("files=%s" % files)
        if not bool(service.getAttribute("custom")):
            return True
        self.session.services.set_service(n.objid, svc)
        # set custom values for custom service
        svc = self.session.services.get_service(n.objid, None)
        if not svc:
            raise ValueError("custom service(%s) for node(%s) does not exist", svc.name, n.objid)
        values = ConfigShim.str_to_dict("|".join(values))
        for name, value in values.iteritems():
            ServiceShim.setvalue(svc, name, value)
        return True
Example #11
0
    def parseservice(self, service, n):
        """
        Use session.services manager to store service customizations before
        they are added to a node.
        """
        name = service.getAttribute("name")
        svc = ServiceManager.get(name)
        if svc is None:
            return False
        values = []
        startup_idx = service.getAttribute("startup_idx")
        if startup_idx is not None:
            values.append("startidx=%s" % startup_idx)
        startup_time = service.getAttribute("start_time")
        if startup_time is not None:
            values.append("starttime=%s" % startup_time)
        dirs = []
        for dir in service.getElementsByTagName("Directory"):
            dirname = dir.getAttribute("name")
            dirs.append(dirname)
        if len(dirs):
            values.append("dirs=%s" % dirs)

        startup = []
        shutdown = []
        validate = []
        for cmd in service.getElementsByTagName("Command"):
            type = cmd.getAttribute("type")
            cmdstr = xmlutils.get_text_child(cmd)
            if cmdstr is None:
                continue
            if type == "start":
                startup.append(cmdstr)
            elif type == "stop":
                shutdown.append(cmdstr)
            elif type == "validate":
                validate.append(cmdstr)
        if len(startup):
            values.append("cmdup=%s" % startup)
        if len(shutdown):
            values.append("cmddown=%s" % shutdown)
        if len(validate):
            values.append("cmdval=%s" % validate)

        files = []
        for file in service.getElementsByTagName("File"):
            filename = file.getAttribute("name")
            files.append(filename)
            data = xmlutils.get_text_child(file)
            typestr = "service:%s:%s" % (name, filename)
            self.session.services.setservicefile(nodenum=n.objid,
                                                 type=typestr,
                                                 filename=filename,
                                                 srcname=None,
                                                 data=data)
        if len(files):
            values.append("files=%s" % files)
        if not bool(service.getAttribute("custom")):
            return True
        self.session.services.setcustomservice(n.objid, svc, values)
        return True
Example #12
0
 def parse_device_service(self, service, node):
     name = service.getAttribute('name')
     session_service = ServiceManager.get(name)
     if not session_service:
         assert False  # XXX for testing
     values = []
     startup_idx = service.getAttribute('startup_idx')
     if startup_idx:
         values.append('startidx=%s' % startup_idx)
     startup_time = service.getAttribute('start_time')
     if startup_time:
         values.append('starttime=%s' % startup_time)
     dirs = []
     for directory in xmlutils.iter_children_with_name(
             service, 'directory'):
         dirname = directory.getAttribute('name')
         dirs.append(str(dirname))
     if dirs:
         values.append("dirs=%s" % dirs)
     startup = []
     shutdown = []
     validate = []
     for command in xmlutils.iter_children_with_name(service, 'command'):
         command_type = command.getAttribute('type')
         command_text = xmlutils.get_child_text_trim(command)
         if not command_text:
             continue
         if command_type == 'start':
             startup.append(str(command_text))
         elif command_type == 'stop':
             shutdown.append(str(command_text))
         elif command_type == 'validate':
             validate.append(str(command_text))
     if startup:
         values.append('cmdup=%s' % startup)
     if shutdown:
         values.append('cmddown=%s' % shutdown)
     if validate:
         values.append('cmdval=%s' % validate)
     filenames = []
     files = []
     for f in xmlutils.iter_children_with_name(service, 'file'):
         filename = f.getAttribute('name')
         if not filename:
             continue
         filenames.append(filename)
         data = xmlutils.get_child_text_trim(f)
         if data:
             data = str(data)
         else:
             data = None
         typestr = 'service:%s:%s' % (name, filename)
         files.append((typestr, filename, data))
     if filenames:
         values.append('files=%s' % filenames)
     custom = service.getAttribute('custom')
     if custom and custom.lower() == 'true':
         self.session.services.setcustomservice(node.objid, session_service,
                                                values)
     # NOTE: if a custom service is used, setservicefile() must be
     # called after the custom service exists
     for typestr, filename, data in files:
         self.session.services.setservicefile(nodenum=node.objid,
                                              type=typestr,
                                              filename=filename,
                                              srcname=None,
                                              data=data)
     return str(name)
Example #13
0
    def parseservice(self, service, n):
        """
        Use session.services manager to store service customizations before
        they are added to a node.
        """
        name = service.getAttribute("name")
        svc = ServiceManager.get(name)
        if svc is None:
            return False
        values = []
        startup_idx = service.getAttribute("startup_idx")
        if startup_idx is not None:
            values.append("startidx=%s" % startup_idx)
        startup_time = service.getAttribute("start_time")
        if startup_time is not None:
            values.append("starttime=%s" % startup_time)
        dirs = []
        for dir in service.getElementsByTagName("Directory"):
            dirname = dir.getAttribute("name")
            dirs.append(dirname)
        if len(dirs):
            values.append("dirs=%s" % dirs)

        startup = []
        shutdown = []
        validate = []
        for cmd in service.getElementsByTagName("Command"):
            type = cmd.getAttribute("type")
            cmdstr = xmlutils.get_text_child(cmd)
            if cmdstr is None:
                continue
            if type == "start":
                startup.append(cmdstr)
            elif type == "stop":
                shutdown.append(cmdstr)
            elif type == "validate":
                validate.append(cmdstr)
        if len(startup):
            values.append("cmdup=%s" % startup)
        if len(shutdown):
            values.append("cmddown=%s" % shutdown)
        if len(validate):
            values.append("cmdval=%s" % validate)

        files = []
        for file in service.getElementsByTagName("File"):
            filename = file.getAttribute("name")
            files.append(filename)
            data = xmlutils.get_text_child(file)
            self.session.services.set_service_file(node_id=n.objid,
                                                   service_name=name,
                                                   file_name=filename,
                                                   data=data)

        if len(files):
            values.append("files=%s" % files)
        if not bool(service.getAttribute("custom")):
            return True
        self.session.services.set_service(n.objid, svc)
        # set custom values for custom service
        svc = self.session.services.get_service(n.objid, None)
        if not svc:
            raise ValueError("custom service(%s) for node(%s) does not exist",
                             svc.name, n.objid)
        values = ConfigShim.str_to_dict("|".join(values))
        for name, value in values.iteritems():
            ServiceShim.setvalue(svc, name, value)
        return True
Example #14
0
    def parse_device_service(self, service, node):
        name = service.getAttribute('name')
        session_service = ServiceManager.get(name)
        if not session_service:
            assert False  # XXX for testing
        values = []
        startup_idx = service.getAttribute('startup_idx')
        if startup_idx:
            values.append('startidx=%s' % startup_idx)
        startup_time = service.getAttribute('start_time')
        if startup_time:
            values.append('starttime=%s' % startup_time)
        dirs = []
        for directory in xmlutils.iter_children_with_name(service, 'directory'):
            dirname = directory.getAttribute('name')
            dirs.append(str(dirname))
        if dirs:
            values.append("dirs=%s" % dirs)
        startup = []
        shutdown = []
        validate = []
        for command in xmlutils.iter_children_with_name(service, 'command'):
            command_type = command.getAttribute('type')
            command_text = xmlutils.get_child_text_trim(command)
            if not command_text:
                continue
            if command_type == 'start':
                startup.append(str(command_text))
            elif command_type == 'stop':
                shutdown.append(str(command_text))
            elif command_type == 'validate':
                validate.append(str(command_text))
        if startup:
            values.append('cmdup=%s' % startup)
        if shutdown:
            values.append('cmddown=%s' % shutdown)
        if validate:
            values.append('cmdval=%s' % validate)

        filenames = []
        files = []
        for f in xmlutils.iter_children_with_name(service, 'file'):
            filename = f.getAttribute('name')
            if not filename:
                continue
            filenames.append(filename)
            data = xmlutils.get_child_text_trim(f)
            if data:
                data = str(data)
            else:
                data = None
            typestr = 'service:%s:%s' % (name, filename)
            files.append((typestr, filename, data))

        if filenames:
            values.append('files=%s' % filenames)

        custom = service.getAttribute('custom')
        if custom and custom.lower() == 'true':
            self.session.services.set_service(node.objid, session_service.name)
            values = ConfigShim.str_to_dict("|".join(values))
            for key, value in values.iteritems():
                ServiceShim.setvalue(session_service, key, value)

        # NOTE: if a custom service is used, setservicefile() must be
        # called after the custom service exists
        for typestr, filename, data in files:
            svcname = typestr.split(":")[1]
            self.session.services.set_service_file(
                node_id=node.objid,
                service_name=svcname,
                file_name=filename,
                data=data
            )
        return str(name)