Esempio n. 1
0
    def tearDown(self):
        """
          resetting the IgnisLogging singleton state
        """
        IgnisLogging._reset_to_defaults(__name__)

        super().tearDown()
Esempio n. 2
0
    def test_no_config_file(self):
        """
        Test there are no config file
        :return:
        """
        logger = IgnisLogging().get_logger(__name__)
        logger.log_to_file(test="test")

        self.assertFalse(os.path.exists(self._default_log))
Esempio n. 3
0
    def test_file_true_typo(self):
        """
        Only tests the main param: file_logging
        :return:
        """
        with open(self._config_file, "w") as fd:
            fd.write("file_logging: tru")

        logger = IgnisLogging().get_logger(__name__)
        logger.log_to_file(test="test")

        self.assertFalse(os.path.exists(self._default_log))
Esempio n. 4
0
    def test_file_true_typo(self):
        """
        Only tests the main param: file_logging
        :return:
        """
        with open(os.path.join(self._qiskit_dir, "logging.yaml"), "w") as file:
            file.write("file_logging: tru")

        logger = IgnisLogging().get_logger(__name__)
        logger.log_to_file(test="test")

        self.assertFalse(os.path.exists(self._default_log))
Esempio n. 5
0
    def test_log_file_path(self):
        """
        test that a custom log file path is honored
        :return:
        """
        log_path = "test_log.log"
        with open(self._config_file, "w") as fd:
            fd.write("file_logging: true\nlog_file: %s\n" % log_path)

        logger = IgnisLogging().get_logger(__name__)
        logger.log_to_file(test="test")

        self.assertTrue(os.path.exists(log_path))
Esempio n. 6
0
    def test_read_multiple_files(self):
        """
        Test reading multiple lines
        """
        with open(self._config_file, "w") as fd:
            fd.write("file_logging: true\nmax_size: 40\nmax_rotations: 5\n")

        logger = IgnisLogging().get_logger(__name__)
        for i in range(10):
            logger.log_to_file(k="data%d" % i)

        reader = IgnisLogReader()
        files = reader.get_log_files()
        self.assertEqual(len(files), 6)
Esempio n. 7
0
    def test_log_file_path(self):
        """
        test that a custom log file path is honored
        :return:
        """
        with open(os.path.join(self._qiskit_dir, "logging.yaml"), "w") as file:
            file.write("file_logging: true\nlog_file: test_log.log")

        logger = IgnisLogging().get_logger(__name__)
        logger.log_to_file(test="test")

        self.assertTrue(os.path.exists("test_log.log"))
        try:
            os.remove("test_log.log")
        except OSError:
            pass
Esempio n. 8
0
    def test_read_multiple_files(self):
        """
        Test reading multiple lines
        """
        with open(os.path.join(self._qiskit_dir, "logging.yaml"), "w") as file:
            file.write("file_logging: true\nmax_size: 40\nmax_rotations: 5")

        logger = IgnisLogging().get_logger(__name__)
        for i in range(10):
            logger.log_to_file(k="data%d" % i)

        reader = IgnisLogReader()
        files = reader.get_log_files()
        self.assertEqual(len(files), 6)

        for file in reader.get_log_files():
            try:
                os.remove(file)
            except OSError:
                pass
Esempio n. 9
0
    def test_file_rotation(self):
        """
        Test that the file rotation is working
        """
        log_path = 'test_log_rotate.log'
        with open(self._config_file, "w") as fd:
            fd.write("file_logging: true\n"
                     "max_size: 10\n"
                     "max_rotations: 3\n"
                     "log_file: %s" % log_path)

        logger = IgnisLogging().get_logger(__name__)

        for i in range(100):
            logger.log_to_file(test="test%d" % i)

        self.assertTrue(os.path.exists(log_path))
        self.assertTrue(os.path.exists(log_path + ".1"))
        self.assertTrue(os.path.exists(log_path + ".2"))
        self.assertTrue(os.path.exists(log_path + ".3"))
Esempio n. 10
0
    def test_manual_enabling(self):
        """
        Test that enabling the logging manually works
        """
        logger = IgnisLogging().get_logger(__name__)
        logger.enable_file_logging()
        logger.log_to_file(test="test")

        self.assertTrue(os.path.exists(self._default_log))
Esempio n. 11
0
    def test_file_rotation(self):
        """
        Test that the file rotation is working
        :return:
        """
        with open(os.path.join(self._qiskit_dir, "logging.yaml"), "w") as file:
            file.write("file_logging: true\n"
                       "max_size: 10\n"
                       "max_rotations: 3")

        logger = IgnisLogging().get_logger(__name__)
        for i in range(100):
            logger.log_to_file(test="test%d" % i)

        self.assertTrue(os.path.exists(self._default_log))
        self.assertTrue(os.path.exists(self._default_log + ".1"))
        self.assertTrue(os.path.exists(self._default_log + ".2"))
        self.assertTrue(os.path.exists(self._default_log + ".3"))

        list(
            map(os.remove, [
                self._default_log + ".1", self._default_log + ".2",
                self._default_log + ".3"
            ]))
Esempio n. 12
0
    def test_save_line(self):
        """
        Test basic log operations
        """
        logger = IgnisLogging().get_logger(__name__)

        logger.enable_file_logging()
        logger.log_to_file(test="test")

        self.assertTrue(os.path.exists(self._default_log))
        with open(self._default_log, 'r') as file:
            self.assertIn("\'test\':\'test\'", file.read())
Esempio n. 13
0
    def test_manual_disabling(self):
        """
        Test that disabling the logging manually works
        """
        with open(self._config_file, "w") as fd:
            fd.write("file_logging: true\n")

        logger = IgnisLogging().get_logger(__name__)
        logger.disable_file_logging()
        logger.log_to_file(test="test")

        self.assertFalse(os.path.exists(self._default_log))
Esempio n. 14
0
    def test_format(self):
        """
        Test format of the saved line
        """
        logger = IgnisLogging().get_logger(__name__)

        logger.enable_file_logging()
        logger.log_to_file(test="test")

        with open(self._default_log, 'r') as file:
            self.assertRegex(
                file.read(),
                r"\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} ignis_logging \S+")
Esempio n. 15
0
    def test_manual_disabling(self):
        """
        Test that disabling the logging manually works
        :return:
        """
        with open(os.path.join(self._qiskit_dir, "logging.yaml"), "w") as file:
            file.write("file_logging: true\n")

        logger = IgnisLogging().get_logger(__name__)
        logger.disable_file_logging()
        logger.log_to_file(test="test")

        self.assertFalse(os.path.exists(self._default_log))
Esempio n. 16
0
    def test_multiple_lines(self):
        """
        Test logging multiple lines
        """
        logger = IgnisLogging().get_logger(__name__)

        logger.enable_file_logging()
        logger.log_to_file(test="test1")
        logger.log_to_file(test="test2")

        with open(self._default_log, 'r') as file:
            lines = file.read().split('\n')

        self.assertGreaterEqual(len(lines), 2)
Esempio n. 17
0
    def tearDown(self):
        """
        Remove auto-generated files, resurrecting original files, and
        resetting the IgnisLogging singleton state

        :return:
        """
        try:
            os.remove("logging.yaml")
        except OSError:
            pass

        # Resurrecting the original files
        _safe_rename_file(os.path.join(self._qiskit_dir, "logging.yaml.orig"),
                          os.path.join(self._qiskit_dir, "logging.yaml"))

        _safe_rename_file(self._default_log + ".orig", self._default_log)

        # Resetting the following attributes, to make the singleton reset
        IgnisLogging().get_logger(__name__).__init__(__name__)
        IgnisLogging._instance = None  # pylint: disable=W0212
        IgnisLogging._file_logging_enabled = False  # pylint: disable=W0212
        IgnisLogging._log_file = None  # pylint: disable=W0212
        IgnisLogging._config_file_exists = False  # pylint: disable=W0212