def test_read_write(self): """Test crontab read and write functions.""" crontab_orig = [ 'CRON_TZ=Europe/Berlin', '* * * * * CRABID=job_one command_one', '0 15 * * * command_two', '0 0 1 4 * date +\%Y\%m\%d', '59 23 12 31 * echo%a\%b%c\%d', '1 2 3 4 * CRABID=cal CRABCLIENTHOSTNAME=b CRABUSERNAME=a cal', ] (jobs, warnings) = parse_crontab(crontab_orig) self.assertEqual(jobs, [ { 'crabid': 'job_one', 'command': 'command_one', 'time': '* * * * *', 'timezone': 'Europe/Berlin', 'input': None, 'vars': {} }, { 'crabid': None, 'command': 'command_two', 'time': '0 15 * * *', 'timezone': 'Europe/Berlin', 'input': None, 'vars': {} }, { 'crabid': None, 'command': 'date +%Y%m%d', 'time': '0 0 1 4 *', 'timezone': 'Europe/Berlin', 'input': None, 'vars': {} }, { 'crabid': None, 'command': 'echo', 'time': '59 23 12 31 *', 'timezone': 'Europe/Berlin', 'input': 'a%b\nc%d', 'vars': {} }, { 'crabid': 'cal', 'command': 'cal', 'time': '1 2 3 4 *', 'timezone': 'Europe/Berlin', 'input': None, 'vars': { 'CRABCLIENTHOSTNAME': 'b', 'CRABUSERNAME': '******' } }, ]) self.assertEqual(warnings, []) crontab_written = write_crontab(jobs) self.assertEqual(crontab_written, crontab_orig)
def save_crontab(self, host, user, crontab, timezone=None, allow_filter=True): """Takes a list of crontab lines and uses them to update the job records. It looks for the CRABID and CRON_TZ variables, but otherwise ignores everything except command lines. It also checks for commands starting with a CRABID= definition, but otherwise inserts them into the database as is. If "allow_filter" is True (as is the default) then cron jobs are skipped if they have a specified user name or client host name which does not match the given host or user name. Returns a list of warning strings.""" # Save the raw crontab. self.write_raw_crontab(host, user, crontab) # Prepare set of existing job ID numbers. idset = set() for job in self.get_jobs(host, user): idset.add(job['id']) # Parse the crontab. (jobs, warning) = parse_crontab(crontab, timezone=timezone) # Iterate over the supplied cron jobs, removing each # job from the idset set as we encounter it. idsaved = set() with self.lock as c: for job in jobs: if allow_filter: vars_ = job['vars'] vars_hostname = vars_.get('CRABCLIENTHOSTNAME') if (vars_hostname is not None) and (vars_hostname != host): warning.append( 'Skipped job for other hostname: ' + job['rule']) continue vars_username = vars_.get('CRABUSERNAME') if (vars_username is not None) and (vars_username != user): warning.append( 'Skipped job for other user: '******'rule']) continue id_ = self._check_job( c, host, user, job['crabid'], job['command'], job['time'], job['timezone']) if id_ in idsaved: warning.append( 'Indistinguishable duplicated job: ' + job['rule']) else: idsaved.add(id_) idset.discard(id_) # Set any jobs remaining in the id set to deleted # because we did not see them in the current crontab for id_ in idset: self._delete_job(c, id_) return warning
def test_read_write(self): """Test crontab read and write functions.""" crontab_orig = [ "CRON_TZ=Europe/Berlin", "* * * * * CRABID=job_one command_one", "0 15 * * * command_two", "0 0 1 4 * date +\%Y\%m\%d", "59 23 12 31 * echo%a\%b%c\%d", "1 2 3 4 * CRABID=cal CRABCLIENTHOSTNAME=b CRABUSERNAME=a cal", ] (jobs, warnings) = parse_crontab(crontab_orig) self.assertEqual( jobs, [ { "crabid": "job_one", "command": "command_one", "time": "* * * * *", "timezone": "Europe/Berlin", "input": None, "vars": {}, }, { "crabid": None, "command": "command_two", "time": "0 15 * * *", "timezone": "Europe/Berlin", "input": None, "vars": {}, }, { "crabid": None, "command": "date +%Y%m%d", "time": "0 0 1 4 *", "timezone": "Europe/Berlin", "input": None, "vars": {}, }, { "crabid": None, "command": "echo", "time": "59 23 12 31 *", "timezone": "Europe/Berlin", "input": "a%b\nc%d", "vars": {}, }, { "crabid": "cal", "command": "cal", "time": "1 2 3 4 *", "timezone": "Europe/Berlin", "input": None, "vars": {"CRABCLIENTHOSTNAME": "b", "CRABUSERNAME": "******"}, }, ], ) self.assertEqual(warnings, []) crontab_written = write_crontab(jobs) self.assertEqual(crontab_written, crontab_orig)