def test_get_present_types(self): """ Test of getting unique set of types present in given list""" robots = { RobotDef('file', '10 10,12,15,17 * * *', 'P'), RobotDef('file', '59 23 * * *', 'P'), RobotDef('file', '*/10 * * * *', 'T'), RobotDef('file', '30 2 * * *', 'PD') } self.assertSetEqual(get_present_types(robots), {'P', 'T', 'D'})
def test_get_all_robots_in_file(self): """Test loading informations about robots in file""" test_path = Path(__file__).parent.joinpath( 'robot_defs', 'cron.air_reservation_refresh.sh') robots = get_all_robots([test_path, test_path]) self.assertSetEqual( robots, { RobotDef(str(test_path), '10 10,12,15,17 * * *', 'P'), RobotDef(str(test_path), '59 23 * * *', 'P'), RobotDef(str(test_path), '*/10 * * * *', 'T') })
def test_parse_crontab_list(self): """Test of parsing crontab content""" crontab = [ '# Some comment', '[email protected]', r'* * * * * very\ often\ job', "0 15 * * * 'commented job' #Comment", '@reboot job started at powerup', 'a * * * * bad timedef', '*/20 1-5,10 * * * very complex timedef', '' ] expected = [ CronLine(RobotDef('', '', ''), '# Some comment'), CronLine(RobotDef('', '', ''), '[email protected]'), CronLine(RobotDef('very often job', '* * * * *', ''), '* * * * * very\\ often\\ job'), CronLine(RobotDef('commented job', '0 15 * * *', ''), "0 15 * * * 'commented job' #Comment"), CronLine(RobotDef('job', '@reboot', ''), '@reboot job started at powerup'), CronLine(RobotDef('', '', ''), 'a * * * * bad timedef'), CronLine(RobotDef('very', '*/20 1-5,10 * * *', ''), '*/20 1-5,10 * * * very complex timedef'), CronLine(RobotDef('', '', ''), '') ] self.maxDiff = None # pylint: disable=invalid-name with self.assertLogs() as logs_catcher: self.assertListEqual(parse_crontab_list(crontab), expected) self.assertListEqual( logs_catcher.output, ['ERROR:root:Unknown cron line format: "a * * * * bad timedef"'])
def test_get_all_robots_in_file_and_folder(self): """Test loading informations about robots in folder - especialy duplicities""" test_path = Path(__file__).parent.joinpath( 'robot_defs', 'cron.air_reservation_refresh.sh') tests_path = Path(__file__).parent.joinpath('robot_defs') robots = get_all_robots([test_path, tests_path]) self.assertSetEqual( robots, { RobotDef(str(test_path), '10 10,12,15,17 * * *', 'P'), RobotDef(str(test_path), '59 23 * * *', 'P'), RobotDef(str(test_path), '*/10 * * * *', 'T'), RobotDef( str(tests_path) + '/cron.create-reservations-list-csv.sh', '30 2 * * *', 'PD') })
def test_get_robot_defs_simple(self): """Test of getting cron defs from one robot text with simple type""" result = get_robot_defs( 'file', """ #!/usr/bin/env bash #P:cron:10 10,12,15,17 * * * #P:cron:59 23 * * * #T:cron:*/10 * * * * cd $(dirname $0) . functions.shl """) self.assertListEqual(result, [ RobotDef('file', '10 10,12,15,17 * * *', 'P'), RobotDef('file', '59 23 * * *', 'P'), RobotDef('file', '*/10 * * * *', 'T') ])
def test_get_robot_defs_complex(self): """Test of getting cron defs from one robot text with complex type""" result = get_robot_defs( 'file', """ #!/usr/bin/env bash #PD:cron:30 2 * * * cd $(dirname $0) cd ../ao3-statistiky-sh """) self.assertListEqual(result, [RobotDef('file', '30 2 * * *', 'PD')])
def test_update_cron_by_robots(self): """Test of updating list of CronLine by list of RobotDef""" crontab = parse_crontab_list([ '# Some comment', '[email protected]', '* * * * * /removed/path/something', '* * * * * /modified/path/something', '1 2 3 4 5 /unchanged/path/something' ]) robots = [ RobotDef('/unchanged/path/something', '1 2 3 4 5', 'I'), RobotDef('/modified/path/something', '6 7 8 9 7', 'I'), RobotDef('/added/path/something', '* * * * *', 'I'), ] paths = [Path('/removed/'), Path('/modified'), Path('/unchanged')] expected = [ '# Some comment', '[email protected]', '1 2 3 4 5 /unchanged/path/something', '* * * * * /added/path/something', '6 7 8 9 7 /modified/path/something', ] self.maxDiff = None # pylint: disable=invalid-name self.assertListEqual(update_cron_by_robots(crontab, robots, paths), expected)
def test_filter_robots_by_type(self): """Test of filter robot defs by type char""" robots = { RobotDef('file', '10 10,12,15,17 * * *', 'P'), RobotDef('file', '59 23 * * *', 'P'), RobotDef('file', '*/10 * * * *', 'T'), RobotDef('file', '30 2 * * *', 'PD') } self.assertSetEqual( set(filter_robots_by_type(robots, 'P')), { RobotDef('file', '10 10,12,15,17 * * *', 'P'), RobotDef('file', '59 23 * * *', 'P'), RobotDef('file', '30 2 * * *', 'PD') })