Пример #1
0
 def test_process_log_with_os_error_at_move(self):
     """Tests of try rotation with OS error while file move"""
     with tempfile.TemporaryDirectory() as sandbox:
         with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
             with self.assertLogs() as logger:
                 srcfile = Path(sandbox, 'pokus.log')
                 srcfile.touch()
                 destpath = Path(sandbox, 'backup')
                 destpath.touch()
                 compressors = process_log(
                     datetime.datetime(year=2019,
                                       month=1,
                                       day=10,
                                       hour=21,
                                       minute=30),
                     {
                         'target': '{{path}}/backup/{{name}}.{{ext}}',
                         'interval': 'hourly',
                         'compress': 'gzip -9',
                     }, 'hourly', str(srcfile), 10)
             self.assertEqual(compressors, [])
             self.assertTrue(srcfile.exists())
             self.assertEqual(
                 fake_stdout.getvalue(),
                 'Checking "{src}"... rotating... '.format(src=srcfile))
             self.assertIn(
                 "FileExistsError: [Errno 17] File exists: '{}'".format(
                     destpath), logger.output[0])
Пример #2
0
 def test_process_log_with_min_size_in_configuration(self):
     """Tests of try rotation with min_size in configuration"""
     with tempfile.TemporaryDirectory() as sandbox:
         with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
             srcfile = Path(sandbox, 'pokus.log')
             srcfile.touch()
             destfile = Path(sandbox, 'backup', 'pokus.log')
             compressors = process_log(
                 datetime.datetime(year=2019,
                                   month=1,
                                   day=10,
                                   hour=21,
                                   minute=30),
                 {
                     'target': '{{path}}/backup/{{name}}.{{ext}}',
                     'interval': 'hourly',
                     'min_size': 15
                 }, 'hourly', str(srcfile), 10)
             self.assertEqual(compressors, [])
             self.assertTrue(srcfile.exists())
             self.assertFalse(destfile.exists())
             self.assertEqual(
                 fake_stdout.getvalue(),
                 'Checking "{src}"... rotation not needed.\n'.format(
                     src=srcfile))
Пример #3
0
 def test_process_log_with_target_exists(self):
     """Tests of try rotation with target exists"""
     with tempfile.TemporaryDirectory() as sandbox:
         with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
             srcfile = Path(sandbox, 'pokus.log')
             srcfile.touch()
             destfile = Path(sandbox, 'backup', 'pokus.log')
             destfile.mkdir(parents=True)
             compressors = process_log(
                 datetime.datetime(year=2019,
                                   month=1,
                                   day=10,
                                   hour=21,
                                   minute=30),
                 {
                     'target': '{{path}}/backup/{{name}}.{{ext}}',
                     'interval': 'hourly',
                     'compress': 'gzip -9',
                 }, 'hourly', str(srcfile), 10)
             self.assertEqual(compressors, [])
             self.assertTrue(srcfile.exists())
             self.assertTrue(destfile.exists())
             self.assertEqual(
                 fake_stdout.getvalue(),
                 'Checking "{src}"... rotating... "{src}" -> "{dest}" target already exists!\n'
                 .format(src=srcfile, dest=destfile))
Пример #4
0
 def test_process_log_with_pre_and_post_in_configuration(self):
     """Tests of try rotation with positive pre and post exec in configuration"""
     with tempfile.TemporaryDirectory() as sandbox:
         with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
             srcfile = Path(sandbox, 'pokus.log')
             srcfile.touch()
             destfile = Path(sandbox, 'backup', 'pokus.log')
             compressors = process_log(
                 datetime.datetime(year=2019,
                                   month=1,
                                   day=10,
                                   hour=21,
                                   minute=30),
                 {
                     'target': '{{path}}/backup/{{name}}.{{ext}}',
                     'interval': 'hourly',
                     'compress': 'gzip -9',
                     'exec_pre': '/bin/true',
                     'exec_post': '/bin/true'
                 }, 'hourly', str(srcfile), 10)
             self.assertEqual(compressors,
                              [[sandbox, 'gzip', '-9',
                                str(destfile)]])
             self.assertFalse(srcfile.exists())
             self.assertTrue(destfile.exists())
             self.assertEqual(
                 fake_stdout.getvalue(),
                 'Checking "{src}"... rotating... "{src}" -> "{dest}" done.\n'
                 .format(src=srcfile, dest=destfile))
Пример #5
0
 def test_process_log_with_ignore_in_configuration(self):
     """Tests of try rotation with ignore in configuration"""
     with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
         compressors = process_log(
             datetime.datetime(year=2019,
                               month=1,
                               day=10,
                               hour=21,
                               minute=30), {'ignore': True}, 'hourly',
             '/tmp/pokus.log', 10)
         self.assertEqual(compressors, [])
         self.assertEqual(fake_stdout.getvalue(), '')
Пример #6
0
 def test_process_log_without_configuration(self):
     """Tests of try rotation without configuration"""
     with mock.patch('sys.stdout', new=io.StringIO()) as fake_stdout:
         compressors = process_log(
             datetime.datetime(year=2019,
                               month=1,
                               day=10,
                               hour=21,
                               minute=30), {}, 'hourly', '/tmp/pokus.log',
             10)
         self.assertEqual(compressors, [])
         self.assertEqual(
             fake_stdout.getvalue(),
             'Checking "/tmp/pokus.log"... rotation not needed.\n')
Пример #7
0
 def test_process_log_with_exec_post_in_configuration(self):
     """Tests of try rotation with exec_post in configuration"""
     with tempfile.TemporaryDirectory() as sandbox:
         with mock.patch('sys.stderr', new=io.StringIO()) as fake_stderr:
             with mock.patch('sys.stdout',
                             new=io.StringIO()) as fake_stdout:
                 stream_handler = logging.StreamHandler(fake_stderr)
                 logging.getLogger().addHandler(stream_handler)
                 try:
                     srcfile = Path(sandbox, 'pokus.log')
                     srcfile.touch()
                     destfile = Path(sandbox, 'backup', 'pokus.log')
                     compressors = process_log(
                         datetime.datetime(year=2019,
                                           month=1,
                                           day=10,
                                           hour=21,
                                           minute=30),
                         {
                             'target': '{{path}}/backup/{{name}}.{{ext}}',
                             'interval': 'hourly',
                             'compress': 'bzip2',
                             'exec_post': '/bin/false'
                         }, 'hourly', str(srcfile), 10)
                 finally:
                     logging.getLogger().removeHandler(stream_handler)
                 self.assertEqual(compressors, [])
                 self.assertFalse(srcfile.exists())
                 self.assertTrue(destfile.exists())
                 self.assertEqual(
                     fake_stdout.getvalue(),
                     'Checking "{src}"... rotating... "{src}" -> "{dest}" exec_post failed.\n'
                     .format(src=srcfile, dest=destfile))
                 self.assertEqual(
                     fake_stderr.getvalue(),
                     'exec_post "/bin/false {dest}" failed with code 1\n'.
                     format(dest=destfile))