def test_set_crontab_many_jobs(self): num_of_jobs = MANY # Create a crontab with many jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) with open(TAB_FILE, 'r') as tabfile: crontab = tabfile.read() test_crontab = api.get_crontab(user1) self.assertEquals(test_crontab, False) api.set_crontab(crontab, user1) test_crontab = api.get_crontab(user1) self.assertEqual(crontab, test_crontab)
def test_get_crontab_empty_jobs(self): num_of_jobs = ZERO # Create a crontab with zero jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) crontab = api.get_crontab(user1) # Verify that the crontab is empty self.assertEqual(crontab, False)
def test_get_crontab_different_users_many_jobs(self): num_of_jobs = MANY # Create a crontab with many jobs for another user from crontab owner test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) with open(TAB_FILE, 'r') as tabfile: crontab = tabfile.read() api.set_crontab(crontab, user2) test_crontab = api.get_crontab(user2) self.assertEqual(crontab, test_crontab) # Append crontab for another user test_jobs = create_test_tab(num_of_jobs, user3) api.set_jobs(test_jobs, user3) with open(TAB_FILE, 'r') as tabfile: crontab = tabfile.read() api.set_crontab(crontab, user3) test_crontab = api.get_crontab(user3) self.assertEqual(crontab, test_crontab)
def get_crontab(opts, valid_crontab, tb_file): old_cron = api.get_crontab(opts.usr[0]) if (opts.file is False) or (valid_crontab is False): old_cron = api.get_crontab(opts.usr[0]) # Perform list operation if opts.lst: lst = old_cron if old_cron else "No crontab for %s." % opts.usr[1] print(lst) sys.exit(0) # Perform an edit in text editor if (opts.file is False) and (valid_crontab is None): # Write remote crontab to tempfile if it hasn't been written with tempfile.NamedTemporaryFile('w', delete=False) as temp: if old_cron: temp.write(old_cron + '\n') tb_file = temp.name # Open the crontab in editor editor_exist = False editors = [os.getenv('VISUAL'), os.getenv('EDITOR'), "/usr/bin/editor"] for editor in filter(bool, editors): try: subprocess.check_call([editor, str(tb_file)]) except OSError: print(editor + " not found!") except subprocess.CalledProcessError as e: sys.exit(str(e)) else: editor_exist = True break if not editor_exist: if opts.file is False: os.unlink(tb_file) sys.exit("No text editor available. Please set your VISUAL or " "EDITOR environment variable properly.") # Overwriting current crontab with local file elif opts.file: tb_file = opts.file old_tab = set(old_cron.split('\n')) if old_cron else set() return (tb_file, old_tab)
def test_get_crontab_one_job(self): num_of_jobs = ONE # Create a crontab with one job test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) with open(TAB_FILE, 'r') as tabfile: crontab = tabfile.read() api.set_crontab(crontab, user1) test_crontab = api.get_crontab(user1) self.assertEqual(crontab, test_crontab)
def test_set_crontab_empty_jobs(self): num_of_jobs = ZERO # Create a crontab with zero jobs test_jobs = create_test_tab(num_of_jobs, user1) api.set_jobs(test_jobs, user1) with open(TAB_FILE, 'r') as tabfile: crontab = tabfile.read() api.set_crontab(crontab, user1) test_crontab = api.get_crontab(user1) self.assertEqual(crontab, test_crontab)
def get_crontab(opts, valid_crontab, tb_file): old_cron = api.get_crontab(opts.usr[0]) if (opts.file is False) or (valid_crontab is False): old_cron = api.get_crontab(opts.usr[0]) # Perform list operation if opts.lst: lst = old_cron if old_cron else "No crontab for %s." % opts.usr[1] print(lst) sys.exit(0) # Perform an edit in text editor if (opts.file is False) and (valid_crontab is None): # Write remote crontab to tempfile if it hasn't been written with tempfile.NamedTemporaryFile("w", delete=False) as temp: if old_cron: temp.write(old_cron) tb_file = temp.name # Open the crontab in editor visual = os.getenv("VISUAL") editor = os.getenv("EDITOR") if visual: os.system("%s %s" % (visual, tb_file)) elif editor: os.system("%s %s" % (editor, tb_file)) else: try: subprocess.check_call(["vi", str(tb_file)]) except OSError: if opts.file is False: os.unlink(tb_file) sys.exit("No text editor available. Please set your VISUAL " "or EDITOR environment variable.") # Overwriting current crontab with local file elif opts.file: tb_file = opts.file old_tab = set(old_cron.split("\n")) if old_cron else set() return (tb_file, old_tab)
def test_get_crontab_missing_crontab(self): crontab = api.get_crontab(user1) # Verify that the crontab is empty self.assertEqual(crontab, False)