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))
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))
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())
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))
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))
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))
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+")
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))
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))
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)
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)
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
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
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"))
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" ]))