How to use _run_cleanup method in autotest

Best Python code snippet using autotest_python

test_cleanup.py

Source:test_cleanup.py Github

copy

Full Screen

...39 raise Exception("failing_cleanup goes boom!")40class TestRunCleanup(CleanupsTestCase):41 def test_no_errors(self):42 """The function passed to _run_cleanup is run."""43 self.assertTrue(_run_cleanup(self.no_op_cleanup))44 self.assertEqual(['no_op_cleanup'], self.call_log)45 def test_cleanup_with_args_kwargs(self):46 def func_taking_args_kwargs(*args, **kwargs):47 self.call_log.append(('func', args, kwargs))48 _run_cleanup(func_taking_args_kwargs, 'an arg', kwarg='foo')49 self.assertEqual(50 [('func', ('an arg',), {'kwarg': 'foo'})], self.call_log)51 def test_cleanup_error(self):52 """An error from the cleanup function is logged by _run_cleanup, but not53 propagated.54 This is there's no way for _run_cleanup to know if there's an existing55 exception in this situation::56 try:57 some_func()58 finally:59 _run_cleanup(cleanup_func)60 So, the best _run_cleanup can do is always log errors but never raise61 them.62 """63 self.assertFalse(_run_cleanup(self.failing_cleanup))64 self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom')65 def test_cleanup_error_debug_flag(self):66 """The -Dcleanup debug flag causes cleanup errors to be reported to the67 user.68 """69 log = StringIO()70 trace.push_log_file(log)71 debug.debug_flags.add('cleanup')72 self.assertFalse(_run_cleanup(self.failing_cleanup))73 self.assertContainsRe(74 log.getvalue(),75 "bzr: warning: Cleanup failed:.*failing_cleanup goes boom")76 def test_prior_error_cleanup_succeeds(self):77 """Calling _run_cleanup from a finally block will not interfere with an78 exception from the try block.79 """80 def failing_operation():81 try:82 1/083 finally:84 _run_cleanup(self.no_op_cleanup)85 self.assertRaises(ZeroDivisionError, failing_operation)86 self.assertEqual(['no_op_cleanup'], self.call_log)87 def test_prior_error_cleanup_fails(self):88 """Calling _run_cleanup from a finally block will not interfere with an89 exception from the try block even when the cleanup itself raises an90 exception.91 The cleanup exception will be logged.92 """93 def failing_operation():94 try:95 1/096 finally:97 _run_cleanup(self.failing_cleanup)98 self.assertRaises(ZeroDivisionError, failing_operation)99 self.assertLogContains('Cleanup failed:.*failing_cleanup goes boom')100class TestDoWithCleanups(CleanupsTestCase):101 def trivial_func(self):102 self.call_log.append('trivial_func')103 return 'trivial result'104 def test_runs_func(self):105 """_do_with_cleanups runs the function it is given, and returns the106 result.107 """108 result = _do_with_cleanups([], self.trivial_func)109 self.assertEqual('trivial result', result)110 def test_runs_cleanups(self):111 """Cleanup functions are run (in the given order)."""...

Full Screen

Full Screen

test_backups.py

Source:test_backups.py Github

copy

Full Screen

...25 self.assertEqual(sorted(b.name for b in expected_backups),26 sorted(backups_list))27 for b in expected_backups:28 self.assertTrue(self._check_backup_not_empty(b))29 def _run_cleanup(self, **kwargs):30 """ Run cleanup_old_backups with null parameters. """31 cleanup_kwargs = {32 "backup_dir": self.backup_dir.name,33 "dry_run": False,34 "keep_all": None,35 "keep_daily": None,36 "keep_weekly": None,37 "keep_monthly": None,38 "keep_yearly": None,39 }40 cleanup_kwargs.update(**kwargs)41 bk.cleanup_old_backups(**cleanup_kwargs)42 def test_no_backups(self):43 """ Test behaviour with no available backups """44 bk.cleanup_old_backups(self.backup_dir.name)45 self.assertFalse(os.listdir(self.backup_dir.name))46 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)47 def test_only_one_backup(self, mock_datetime):48 """ Test the only backup will not be removed in any case """49 mock_datetime.now.return_value = datetime(2021, 10, 20)50 only_backup = self._add_backup("20010101_0000")51 self._run_cleanup(keep_all=1)52 self._check_backups([only_backup])53 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)54 def test_at_least_one_should_be_left(self, mock_datetime):55 """ Test at least one backup should be left """56 mock_datetime.now.return_value = datetime(2021, 10, 20)57 backups = [58 self._add_backup("20211103_0300"),59 self._add_backup("20201216_0100"),60 self._add_backup("20200716_0100"),61 self._add_backup("20181116_0100"),62 ]63 expected_backups = [backups[0]]64 self._run_cleanup()65 self._check_backups(expected_backups)66 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)67 def test_keep_all_threshold_only(self, mock_datetime):68 """ Test threshold for keeping all backups """69 mock_datetime.now.return_value = datetime(2021, 10, 20)70 backups = [71 self._add_backup("20211019_0300"),72 self._add_backup("20211017_0100"),73 self._add_backup("20211016_2300"),74 ]75 expected_backups = backups[:2]76 self._run_cleanup(keep_all=3)77 self._check_backups(expected_backups)78 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)79 def test_keep_daily_threshold_only(self, mock_datetime):80 """ Test threshold for keeping daily backups """81 mock_datetime.now.return_value = datetime(2021, 10, 20)82 backups = [83 self._add_backup("20211019_0300"),84 self._add_backup("20211017_2100"),85 self._add_backup("20211017_0100"),86 self._add_backup("20211017_0030"),87 self._add_backup("20211016_2300"),88 self._add_backup("20211016_0100"),89 ]90 expected_backups = [backups[0], backups[3]]91 self._run_cleanup(keep_daily=3)92 self._check_backups(expected_backups)93 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)94 def test_keep_all_and_daily_thresholds(self, mock_datetime):95 """ Test threshold for keeping all and daily backups """96 mock_datetime.now.return_value = datetime(2021, 10, 20)97 backups = [98 self._add_backup("20211019_0300"),99 self._add_backup("20211017_0200"),100 self._add_backup("20211017_0100"),101 self._add_backup("20211016_2300"),102 self._add_backup("20211016_2200"),103 self._add_backup("20211015_2200"),104 self._add_backup("20211015_1500"),105 self._add_backup("20211015_0200"),106 self._add_backup("20211014_2200"),107 self._add_backup("20211014_2000"),108 self._add_backup("20211014_1232"),109 ]110 expected_backups = backups[0:3] + [backups[4]] + [backups[7]]111 self._run_cleanup(keep_all=3, keep_daily=5)112 self._check_backups(expected_backups)113 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)114 def test_keep_weekly_threshold_only(self, mock_datetime):115 """ Test threshold for keeping weekly backups """116 mock_datetime.now.return_value = datetime(2021, 11, 11)117 backups = [118 self._add_backup("20211111_0300"),119 self._add_backup("20211110_0300"),120 self._add_backup("20211108_0100"),121 self._add_backup("20211107_2300"),122 self._add_backup("20211107_0100"),123 self._add_backup("20211031_0100"),124 self._add_backup("20211025_0100"),125 self._add_backup("20211024_0100"),126 self._add_backup("20211023_0100"),127 self._add_backup("20211022_0100"),128 self._add_backup("20211008_0100"),129 self._add_backup("20211007_0100"),130 self._add_backup("20211004_0100"),131 self._add_backup("20211003_0100"),132 ]133 expected_backups = [backups[2], backups[4], backups[6],134 backups[9], backups[12]]135 self._run_cleanup(keep_weekly=5)136 self._check_backups(expected_backups)137 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)138 def test_keep_monthly_threshold_only(self, mock_datetime):139 """ Test threshold for keeping monthly backups """140 mock_datetime.now.return_value = datetime(2021, 11, 11)141 backups = [142 self._add_backup("20211103_0300"),143 self._add_backup("20211019_0300"),144 self._add_backup("20211017_2100"),145 self._add_backup("20211017_0100"),146 self._add_backup("20210916_2300"),147 self._add_backup("20210916_0100"),148 self._add_backup("20210816_0100"),149 self._add_backup("20210810_0000"),150 self._add_backup("20210716_0100"),151 ]152 expected_backups = [backups[0], backups[3], backups[5], backups[7]]153 self._run_cleanup(keep_monthly=3)154 self._check_backups(expected_backups)155 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)156 def test_keep_yearly_threshold_only(self, mock_datetime):157 """ Test threshold for keeping yearly backups """158 mock_datetime.now.return_value = datetime(2021, 11, 11)159 backups = [160 self._add_backup("20211103_0300"),161 self._add_backup("20210810_0000"),162 self._add_backup("20210716_0100"),163 self._add_backup("20201216_0100"),164 self._add_backup("20200716_0100"),165 self._add_backup("20190316_0100"),166 self._add_backup("20181116_0100"),167 ]168 expected_backups = [backups[2], backups[4], backups[5], backups[6]]169 self._run_cleanup(keep_yearly=3)170 self._check_backups(expected_backups)171 @mock.patch(f"{bk.__name__}.datetime", wraps=datetime)172 def test_dry_run(self, mock_datetime):173 """ Test dry run does not remove anything """174 mock_datetime.now.return_value = datetime(2021, 11, 11)175 backups = [176 self._add_backup("20211103_0300"),177 self._add_backup("20210810_0000"),178 self._add_backup("20210716_0100"),179 self._add_backup("20200716_0100"),180 self._add_backup("20181116_0100"),181 ]182 self._run_cleanup(keep_all=2, dry_run=True)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful