Beispiel #1
0
    def test_log_to_file_time_file_seconds(self):
        """
        Test
        """

        log_file = "/tmp/pythonsol_unittest.log"

        # Clean
        if FileUtility.is_file_exist(log_file):
            os.remove(log_file)
        for f in glob.glob("/tmp/pythonsol_unittest.log.*"):
            os.remove(f)

        # Init
        SolBase.logging_init(log_level="INFO",
                             log_to_file=log_file,
                             log_to_console=True,
                             log_to_syslog=False,
                             log_callback=self._on_log,
                             force_reset=True,
                             log_to_file_mode="time_file_seconds")
        SolBase.set_compo_name("COMPO_XXX")

        # Emit a log
        logger.info("TEST LOG 888")

        # Emit a log (str)
        logger.info(u"BUF \u001B\u0BD9\U0001A10D\u1501\xc3 FUB")

        # Check the file
        buf = FileUtility.file_to_textbuffer(log_file, "utf-8")

        self.assertIsNotNone(buf)
        self.assertGreaterEqual(buf.find("TEST LOG 888"), 0)

        self.assertGreaterEqual(buf.find("BUF "), 0)
        self.assertGreaterEqual(buf.find(" FUB"), 0)
        self.assertGreaterEqual(
            buf.find(u"BUF \u001B\u0BD9\U0001A10D\u1501\xc3 FUB"), 0)

        # Wait 5 sec
        for i in range(0, 10):
            SolBase.sleep(1100)
            logger.info("Log i=%s", i)

        # Emit a log
        logger.info("TEST LOG 999")

        # We should have "pythonsol_unittest.log.*" but no more than 7
        f_count = 0
        for f in glob.glob("/tmp/pythonsol_unittest.log.*"):
            logger.info("Found %s", f)
            f_count += 1
        self.assertGreater(f_count, 0)
        self.assertLessEqual(f_count, 7)

        # Reset
        SolBase.logging_init("INFO", True)
Beispiel #2
0
    def test_log_to_file(self):
        """
        Test
        """

        log_file = "/tmp/pythonsol_unittest.log"

        # Clean
        if FileUtility.is_file_exist(log_file):
            os.remove(log_file)

        # Init
        SolBase.logging_init(log_level="INFO",
                             log_to_file=log_file,
                             log_to_console=True,
                             log_to_syslog=False,
                             log_callback=self._on_log,
                             force_reset=True)
        SolBase.set_compo_name("COMPO_XXX")

        # Emit a log
        logger.info("TEST LOG 888")

        # Emit a log (str)
        logger.info(u"BUF \u001B\u0BD9\U0001A10D\u1501\xc3 FUB")

        # Check the file
        buf = FileUtility.file_to_textbuffer(log_file, "utf-8")

        self.assertIsNotNone(buf)
        self.assertGreaterEqual(buf.find("TEST LOG 888"), 0)

        self.assertGreaterEqual(buf.find("BUF "), 0)
        self.assertGreaterEqual(buf.find(" FUB"), 0)
        self.assertGreaterEqual(
            buf.find(u"BUF \u001B\u0BD9\U0001A10D\u1501\xc3 FUB"), 0)

        # Simulate a log rotate : kick the file and touch it
        os.remove(log_file)
        FileUtility.append_text_to_file(log_file,
                                        "TOTO\n",
                                        "utf-8",
                                        overwrite=False)

        # Re-emit
        logger.info("TEST LOG 999")

        buf = FileUtility.file_to_textbuffer(log_file, "utf-8")

        self.assertIsNotNone(buf)
        self.assertGreaterEqual(buf.find("TOTO"), 0)
        self.assertGreaterEqual(buf.find("TEST LOG 999"), 0)
Beispiel #3
0
def is_dante_detected():
    """
    Return true if dante is detected.
    :return: bool
    :rtype bool
    """

    return FileUtility.is_file_exist("/etc/danted.conf")
Beispiel #4
0
    def test_log_to_file_with_filter_greenlet(self):
        """
        Test
        """

        log_file = "/tmp/pythonsol_unittest.log"

        # Clean
        if FileUtility.is_file_exist(log_file):
            os.remove(log_file)

        # Init
        SolBase.logging_init(log_level="INFO",
                             log_to_file=log_file,
                             log_to_console=True,
                             log_to_syslog=False,
                             log_callback=self._on_log,
                             force_reset=True)
        SolBase.set_compo_name("COMPO_XXX")

        # Go
        g1 = gevent.spawn(self._run_filter, "ip001")
        g2 = gevent.spawn(self._run_filter, "ip002")
        gevent.joinall([g1, g2])

        # Re-read and check
        buf = FileUtility.file_to_textbuffer(log_file, "utf-8")

        self.assertGreaterEqual(buf.find("TEST LOG ip_addr=ip001"), 0)
        self.assertGreaterEqual(buf.find("TEST LOG ip_addr=ip002"), 0)

        # Via regex
        for r, b in [
            ["TEST LOG ip_addr=ip001 | k_ip:ip001 z_value:ip001", True],
            ["TEST LOG ip_addr=ip002 | k_ip:ip002 z_value:ip002", True],
            ["TEST LOG ip_addr=ip001 | k_ip:ip002", False],
            ["TEST LOG ip_addr=ip002 | k_ip:ip001", False],
        ]:
            idx = buf.find(r)
            if b:
                self.assertLess(0, idx, r)
            else:
                self.assertGreaterEqual(0, idx, r)
Beispiel #5
0
    def _clean_files(self):
        """
        Clean files
        """

        if FileUtility.is_file_exist(self.daemon_pid_file):
            logger.debug("Deleting %s", self.daemon_pid_file)
            os.remove(self.daemon_pid_file)

        if FileUtility.is_file_exist(self.daemon_std_out):
            logger.debug("Deleting %s", self.daemon_std_out)
            os.remove(self.daemon_std_out)

        if FileUtility.is_file_exist(self.daemon_std_err):
            logger.debug("Deleting %s", self.daemon_std_err)
            os.remove(self.daemon_std_err)

        if FileUtility.is_file_exist(CustomDaemon.DAEMON_LAST_ACTION_FILE):
            logger.debug("Deleting %s", CustomDaemon.DAEMON_LAST_ACTION_FILE)
            os.remove(CustomDaemon.DAEMON_LAST_ACTION_FILE)
Beispiel #6
0
    def test_stdout(self):
        """
        Test
        """

        if FileUtility.is_file_exist("/tmp/tamer.txt"):
            os.remove("/tmp/tamer.txt")

        sys.stdout = open("/tmp/tamer.txt", "a+")
        print("TAMER")
        ar = self._get_std_out_file("/tmp/tamer.txt")
        self.assertIn("TAMER", ar)
Beispiel #7
0
    def _file_to_list(cls, file_name, sep="\n"):
        """
        Load a file to a list, \n delimited
        :param file_name: File name
        :type file_name: str
        :param sep: separator
        :type sep: str
        :return list
        :rtype list
        """

        ret = None
        # noinspection PyBroadException,PyPep8
        try:
            if FileUtility.is_file_exist(file_name):
                ret = FileUtility.file_to_textbuffer(file_name, "ascii")
        except:
            ret = None
        finally:
            if ret and len(ret) > 0:
                return ret.split(sep)
            else:
                return list()
Beispiel #8
0
def is_squid_present():
    """
    Check is squid is present
    :return bool
    :rtype bool
    """

    for f in ["/etc/squid/squid.conf", "/etc/squid3/squid.conf"]:
        if FileUtility.is_file_exist(f):
            logger.info("Squid detected, f=%s", f)
            return True

    logger.info("Squid not detected")
    return False
Beispiel #9
0
    def _set_ssl_certificate_file(self, cert_file):
        """
        Set the ssl certificate file. Raise exception upon error.
        :param cert_file: A valid certificate file. Must exist and be accessible.
        :type cert_file: str
        """

        # Check
        if cert_file is None or len(cert_file) == 0:
            self._ssl_certificate_file = ""
            return

        # Check
        if not FileUtility.is_file_exist(cert_file):
            logger.warning(
                "cert_file do not exist or is not accessible, cert_file=%s",
                cert_file)

        # Set
        self._ssl_certificate_file = cert_file
Beispiel #10
0
    def _set_ssl_keyfile(self, key_file):
        """
        Set the ssl key file. Raise exception upon error.
        :param key_file: A valid key file. Must exist and be accessible.
        :type key_file: str
        :return: True upon success, false otherwise.
        :rtype bool
        """
        # Check
        if key_file is None or len(key_file) == 0:
            self._ssl_key_file = ""
            return

        if not FileUtility.is_file_exist(key_file):
            logger.warning(
                "key_file do not exist or is not accessible, key_file=%s",
                key_file)

        # Set
        self._ssl_key_file = key_file
Beispiel #11
0
    def test_start_status_reload_stop_logfile(self):
        """
        Test
        """

        try:
            # Start
            self._reset_std_capture()

            main_helper_file = self.current_dir + "CustomDaemon.py"
            main_helper_file = abspath(main_helper_file)
            self.assertTrue(FileUtility.is_file_exist(main_helper_file))

            # Params
            ar = list()
            ar.append(sys.executable)
            ar.append(main_helper_file)
            ar.append("-pidfile={0}".format(self.daemon_pid_file))
            ar.append("-stderr={0}".format(self.daemon_std_err))
            ar.append("-stdout=/dev/null")
            ar.append("-logfile={0}".format(self.daemon_std_out))
            ar.append("start")

            # =========================
            # START
            # =========================

            # Launch
            logger.info("Start : %s", " ".join(ar))
            p = subprocess.Popen(args=ar)
            logger.info("Started")
            SolBase.sleep(0)
            self._wait_process(p)

            # Try wait for stdout
            ms_start = SolBase.mscurrent()
            while SolBase.msdiff(ms_start) < self.stdout_timeout_ms:
                if "n".join(self._get_std_out()).find(
                        " INFO | CustomDaemon@_on_start") >= 0:
                    break
                else:
                    SolBase.sleep(10)

            # Get std (caution, we are async since forked)
            logger.info("stdOut ### START")
            for s in self._get_std_out():
                logger.info("stdOut => %s", s)
            logger.info("stdOut ### END")

            logger.info("stdErr ### START")
            for s in self._get_std_err():
                logger.info("stdErr => %s", s)
            logger.info("stdErr ### END")

            # Check
            self.assertTrue(len(self._get_std_err()) == 0)
            self.assertTrue(len(self._get_std_out()) > 0)
            self.assertTrue("n".join(self._get_std_out()).find(" ERROR ") < 0)
            self.assertTrue("n".join(self._get_std_out()).find(
                " INFO | CustomDaemon@_on_start") >= 0)
            self.assertTrue("n".join(self._get_std_out()).find(" WARN ") < 0)

            # =========================
            # STATUS
            # =========================

            for _ in range(0, 10):
                # Args
                ar = list()
                ar.append(sys.executable)
                ar.append(main_helper_file)
                ar.append("-pidfile={0}".format(self.daemon_pid_file))
                ar.append("status")

                # Launch
                p = subprocess.Popen(args=ar)
                self._wait_process(p)

            # =========================
            # RELOAD
            # =========================

            for _ in range(0, 10):
                # Args
                ar = list()
                ar.append(sys.executable)
                ar.append(main_helper_file)
                ar.append("-pidfile={0}".format(self.daemon_pid_file))
                ar.append("reload")

                # Launch
                p = subprocess.Popen(args=ar)
                self._wait_process(p)

            # =========================
            # STOP
            # =========================

            # Args
            ar = list()
            ar.append(sys.executable)
            ar.append(main_helper_file)
            ar.append("-pidfile={0}".format(self.daemon_pid_file))
            ar.append("stop")

            # Launch
            p = subprocess.Popen(args=ar)
            self._wait_process(p)

            # =========================
            # OVER, CHECK LOGS
            # =========================

            # Try wait for stdout
            ms_start = SolBase.mscurrent()
            while SolBase.msdiff(ms_start) < self.stdout_timeout_ms:
                if "n".join(self._get_std_out()).find(" INFO | CustomDaemon@_on_stop") >= 0 \
                        and "n".join(self._get_std_out()).find(" INFO | CustomDaemon@_on_status") >= 0:
                    break
                else:
                    SolBase.sleep(10)

            # Get std (caution, we are async since forked)
            logger.info("stdOut ### START")
            for s in self._get_std_out():
                logger.info("stdOut => %s", s)
            logger.info("stdOut ### END")

            logger.info("stdErr ### START")
            for s in self._get_std_err():
                logger.info("stdErr => %s", s)
            logger.info("stdErr ### END")

            # Check
            self.assertTrue(len(self._get_std_err()) == 0)
            self.assertTrue(len(self._get_std_out()) > 0)
            self.assertTrue("n".join(self._get_std_out()).find(" ERROR ") < 0)
            self.assertTrue("n".join(self._get_std_out()).find(
                " INFO | CustomDaemon@_on_start") >= 0)
            self.assertTrue("n".join(self._get_std_out()).find(
                " INFO | CustomDaemon@_on_stop") >= 0)
            self.assertTrue("n".join(self._get_std_out()).find(
                " INFO | CustomDaemon@_on_status") >= 0)
            self.assertTrue("n".join(self._get_std_out()).find(" WARN ") < 0)

            # =========================
            # OVER, CHECK ACTION FILE
            # =========================
            buf = FileUtility.file_to_textbuffer(
                CustomDaemon.DAEMON_LAST_ACTION_FILE, "ascii")
            self.assertTrue(buf.find("is_running=False") >= 0)
            self.assertTrue(buf.find("start_count=1") >= 0)
            self.assertTrue(buf.find("stop_count=1") >= 0)
            self.assertTrue(buf.find("status_count=10") >= 0)
            self.assertTrue(buf.find("reload_count=10") >= 0)
            self.assertTrue(buf.find("last_action=stop") >= 0)

        finally:
            logger.debug("Exiting test, idx=%s", self.run_idx)