Пример #1
0
 def _configure_rsyslog():
     """Restart rsyslog service for reflecting supportbundle rsyslog config."""
     try:
         Log.info("Restarting rsyslog service")
         service_obj = Service("rsyslog.service")
         service_obj.restart()
     except Exception as e:
         Log.warn(f"Error in rsyslog service restart: {e}")
    def config(self):
        """
        Performs configurations.

        Raises exception on error
        """
        try:
            # Create backup of elasticsearch_config file.
            if not os.path.exists(
                    f'{self.elasticsearch_file_path}/elasticsearch.yml.bkp'):
                shutil.copyfile(
                    self.elasticsearch_config_path,
                    f'{self.elasticsearch_file_path}/elasticsearch.yml.bkp')
            else:
                shutil.copyfile(
                    f'{self.elasticsearch_file_path}/elasticsearch.yml.bkp',
                    self.elasticsearch_config_path)
            # Get config entries that needs to add in elasticsearch.yml
            config_to_add = self.get_config_entries()
            file_contents = Elasticsearch.read_file_contents(
                self.elasticsearch_config_path)
            with open(self.elasticsearch_config_path, "a+") as f:
                for line in config_to_add:
                    f.write(f'\n{line}')
                f.close()

            # load omelasticsearch module in rsyslog.
            file_contents = Elasticsearch.read_file_contents(self.rsyslog_conf)
            insert_line = 'module(load="omelasticsearch")'
            if not any(insert_line in i for i in file_contents):
                with open(self.rsyslog_conf, "w") as f:
                    for line in file_contents:
                        if line == '\n':
                            continue
                        f.write(f'\n{line}')
                        if line.strip('\n') == "#### MODULES ####":
                            f.write(f'\n{insert_line}\n')
                    f.close()

            try:
                service_obj = Service('rsyslog.service')
                service_obj.restart()
            except ServiceError as e:
                msg = f"Restarting rsyslog.service failed due to error, {e}."
                Log.error(msg)

        except (Exception, OSError) as e:
            msg = f"Failed in config stage due to error {e}."
            Log.error(msg)
            raise

        Log.info("Config done.")
        return 0
class TestSystemHandler(unittest.TestCase):
    _service_name = 'rsyslog.service'

    def setUp(self):
        self.service_obj = Service(TestSystemHandler._service_name)

    def test_start(self):
        self.service_obj.start()
        service_state = self.test_get_state()
        self.assertEqual(service_state.state, 'active')
        self.assertEqual(service_state.substate, 'running')

    def test_stop(self):
        self.service_obj.stop()
        service_state = self.test_get_state()
        self.assertEqual(service_state.state, 'inactive')
        self.assertEqual(service_state.substate, 'dead')

    def test_restart(self):
        self.service_obj.restart()
        service_state = self.test_get_state()
        self.assertEqual(service_state.state, 'active')
        self.assertEqual(service_state.substate, 'running')

    def test_enable(self):
        self.service_obj.enable()
        is_enable = self.test_is_enabled()
        self.assertTrue(is_enable)

    def test_disable(self):
        self.service_obj.disable()
        is_enable = self.test_is_enabled()
        self.assertFalse(is_enable)

    def test_get_state(self):
        return self.service_obj.get_state()

    def test_is_enabled(self):
        return self.service_obj.is_enabled()