def test_get_last_log(tmpdir): tmpdir.chdir() # no logs in folder last_log = get_last_log(str(tmpdir)) assert last_log.path == None assert last_log.date == None # add .bz2 archive open('nginx-access-ui.log-20040101.bz2', 'a').close() # check plain files test_file_plain = 'nginx-access-ui.log-20010101' open(test_file_plain, 'a').close() assertion_date = date(2001, 1, 1) assertion_path = '{}/{}'.format(tmpdir, test_file_plain) last_log = get_last_log(str(tmpdir)) assert assertion_date == last_log.date assert assertion_path == last_log.path # check gzip files test_file_gz = 'nginx-access-ui.log-20020101.gz' open(test_file_gz, 'a').close() assertion_date = date(2002, 1, 1) assertion_path = '{}/{}'.format(tmpdir, test_file_gz) last_log = get_last_log(str(tmpdir)) assert assertion_date == last_log.date assert assertion_path == last_log.path
def check_files(self, config): self.assertIs(os.path.isdir(config['REPORT_DIR']), True) # Folder for reports exists/created if config['APP_LOG'] is not None: self.assertIs(os.path.isfile(config['APP_LOG']), True) # Logger initialized report_date = la.get_last_log(config['LOG_DIR']).date report_name = f'report-{report_date}.html' self.assertIs( os.path.isfile(os.path.join(config['REPORT_DIR'], report_name)), True) # report created
def test_get_last_log(self): with patch('log_analyzer.get_last_report_date') as mocked_report_date: with patch('os.listdir') as mocked_listdir: mocked_report_date.return_value = datetime(2000, 1, 1, 0, 0, 0) mocked_listdir.return_value = [ 'other_log.log-20170630.gz', 'nginx-access-ui.log-20170701.gz', 'nginx-access-ui.log-20170702', 'file.txt' ] log_file_name, log_date = get_last_log(self.config) self.assertEqual(log_file_name, 'nginx-access-ui.log-20170702') self.assertEqual(log_date, datetime(2017, 7, 2, 0, 0, 0))
def test_log_files(self): shutil.rmtree("test_log", ignore_errors=True, onerror=None) os.mkdir("test_log") os.mknod('test_log/nginx-access-ui.log-20181201.log') os.mknod('test_log/nginx-access-ui.log-20191212.gzip') os.mknod('test_log/nginx-access-ui.log-20181001.log') log_file, date = la.get_last_log('test_log') self.assertEqual(log_file, "nginx-access-ui.log-20181201.log") shutil.rmtree("test_log", ignore_errors=True, onerror=None)
def test_return_last_log_entry_in_directory(self): """ Should return a last log that matches pattern and date format in the logs directory """ early_date = datetime.datetime.strptime('01.01.2018', '%d.%m.%Y') late_date = datetime.datetime.strptime('02.01.2018', '%d.%m.%Y') early_log = self.log_name_template.format( date=early_date.strftime(self.date_format)) late_log = self.log_name_template.format( date=late_date.strftime(self.date_format)) with mock.patch('os.listdir', mock.MagicMock(return_value=[early_log, late_log])): self.assertEqual( get_last_log(self.logs_dir, self.log_name_template, self.date_format), LogEntry(log_name=late_log, log_date=late_date))
def test_get_last_log(self): self.assertEqual(la.get_last_log(self.log_folder_1)[1], 'nginx-access-ui.log-20170610.gz') self.assertEqual(la.get_last_log(self.log_folder_2)[1], 'nginx-access-ui.log-20170611') self.assertRaises(Exception, la.get_last_log, self.log_folder_3)
def test_get_last_log(self): path = log_analyzer.get_last_log('./test')[0] name = path.split(os.path.sep)[-1] self.assertEqual(name, 'nginx-access-ui.log-20170630.log')
def test_not_last_log(self): self.assertEqual(log_analyzer.get_last_log(["testlog-201012", "testlog-2010-01-13", "test"]), None)
def test_last_log(self): self.assertEqual(log_analyzer.get_last_log(["testlog-20001012", "testlog-20100113", "testlog-20181225"]), "testlog-20181225")
def test_get_last_log_other_logs(_): file = get_last_log('.') assert file is None
def test_get_last_log_nolog(_): file = get_last_log('.') assert file is None
def test_get_last_log_correct_gzip(_): file = get_last_log('.') assert file.file_path == './nginx-access-ui.log-20170830.gz', file.file_path assert file.date.strftime('%d.%m.%Y') == '30.08.2017', file.date
def test_get_last_log(self): result = ('nginx-access-ui.log-20180202.gz', '20180202') self.assertEqual(get_last_log('../test_data'), result)
def test_for_empty_directory(self): """Should return None if the logs directory is empty""" with mock.patch('os.listdir', mock.MagicMock(return_value=[])): self.assertIsNone( get_last_log(self.logs_dir, self.log_name_template, self.date_format))