The crontab.pyc has been gitignored.

---
 crontab/control    |   22 ++++++++
 crontab/crontab.py |  142 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+)
 create mode 100644 crontab/control
 create mode 100644 crontab/crontab.py

diff --git a/crontab/control b/crontab/control
new file mode 100644
index 0000000..6d605a3
--- /dev/null
+++ b/crontab/control
@@ -0,0 +1,22 @@
+AUTHOR = "Rajeev S <[email protected]>"
+NAME = "crontab"
+TEST_CATEGORY = "Functional"
+TEST_CLASS = "Kernel"
+TEST_TYPE = "client"
+TIME = 'SHORT'
+DOC = '''
+Test for testing the crontab.
+
+Uses run-parts to execute the scripts under the standard cron 
directories,/etc/cron.xxxx.
+Automation is done using the /etc/crontab file.
+
+Args:
+    wait_time:Duration of the sleep time.Should be greater than 60(sec)
+'''
+from datetime import datetime
+
+LOGFILE='/tmp/autotest_cron-%s'%(str(datetime.now())[:10])
+
+tests=['normalCron','denyCron','allowCron']
+for i in range(0,3):
+    job.run_test('crontab',test=tests[i],wait_time=65,tag=tests[i],log=LOGFILE)
diff --git a/crontab/crontab.py b/crontab/crontab.py
new file mode 100644
index 0000000..0ef509a
--- /dev/null
+++ b/crontab/crontab.py
@@ -0,0 +1,142 @@
+#!/bin/python
+import os, shutil, glob, logging
+from autotest.client import test, utils
+from autotest.client.shared import error
+from time import sleep
+
+
+class crontab(test.test):
+    '''
+    Autotest module for crontab.
+
+    Creates a new cron job that creates a log entry in log.The count of
+    log entry is compared before and after a period of 80 sec.Difference decide
+    s whether the cron executed
+    successfully.
+
+    @author :Rajeev S <[email protected]>
+    '''
+    version = 1
+    initialCount = 0
+    log=''
+
+    def count_log(self,string):
+       '''returns count of the 'string' in log'''
+        count=0
+        try:
+            f=open(self.log)
+        except IOError:
+            utils.open_write_close(self.log,'')
+            f=open(self.log)
+        for i in f.readlines():
+            if string in i:
+                count+=1
+        f.close()
+        return count
+
+    def initialize(self,test,log):
+        '''Does the init part of the test 
+        1.Finds initial count of entry in log
+        2.Creates a file 'cron' under cron.d
+        3.Backs up /etc/crontab
+        4.Modifies /etc/crontab    '''
+        self.log=log
+
+        self.initialCount=self.count_log('Cron automation')
+        f=open('/etc/cron.d/cron','w')
+        f.write('''#!/bin/bash
+touch %s
+echo 'Cron automation' >> %s
+        '''%(self.log,self.log))
+        f.close()
+        utils.system('chmod +x /etc/cron.d/cron')
+        utils.system('cat /etc/crontab > /tmp/backup')
+        f=open('/etc/crontab','w')
+        f.write('* * * * * root run-parts /etc/cron.d/\n')
+        f.close()
+        if test == 'denyCron':
+            if os.path.exists('/etc/cron.d/jobs.deny'):
+                utils.system('mv /etc/cron.d/jobs.deny /tmp/jobs.deny 
2>/dev/null')
+            f=open('/etc/cron.d/jobs.deny','w')
+            f.write('cron')
+            f.close()
+        elif test =='allowCron' :
+            utils.system('rm /etc/cron.d/jobs.deny')
+            if os.path.exists('/etc/cron.d/jobs.allow'):
+                utils.system('mv /etc/cron.d/jobs.allow /tmp/jobs.allow 
2>/dev/null')
+            f=open('/etc/cron.d/jobs.allow','w')
+            f.write('cron')
+            f.close()
+
+
+    def allowCron(self,wait_time):
+        if self.count_log('Cron automation')>self.initialCount:
+            utils.open_write_close(self.results_path,'''Test Successful.
+            Test time:%s
+            Test mode:2(run-parts with jobs.allow)'''%(wait_time))
+        else:
+            utils.open_write_close(self.results_path,'''Test Failed.
+            There were no new entries in log.
+            Job not executed inspite of jobs.allow entry.
+            Test time:%s
+            Test mode:2 (run-parts with jobs.allow)'''%(wait_time))
+
+
+    def normalCron(self,wait_time):
+        if self.count_log('Cron automation')>self.initialCount:
+            utils.open_write_close(self.results_path,'''Test successful.
+            Test time:%s
+            Test mode:0(normal test for run-parts)'''%(wait_time))
+        else:
+            utils.open_write_close(self.results_path,'''Test Failed.
+            There were no new entries in log.
+            Test time:%s
+            Test mode:0 (normal test for run-parts)'''%(wait_time))
+
+
+    def denyCron(self,wait_time):
+        if self.count_log('Cron automation')>self.initialCount:
+            utils.open_write_close(self.results_path,'''Test Failed.
+            run-parts overrides jobs.deny.
+            Test time:%s
+            Test mode:1(run-parts with jobs.deny)'''%(wait_time))
+        else:
+            utils.open_write_close(self.results_path,'''Test Successful.
+            There were no new entries in log.
+            Test time:%s
+            Test mode:1 (run-parts with jobs.deny)'''%(wait_time))
+
+
+    def make_fun(self,test,wait_time):
+        '''Calls the function with the name <test> '''
+        test=getattr(self,test)
+        test(wait_time)
+
+    def run_once(self,test,wait_time):
+        '''Runs the test,writes test success if cron successfully executes,else
+           writes test failed.
+           Resets /etc/crontab
+           Pass 0:Normal operation of run-parts
+           Pass 1:run-parts with jobs.deny
+           Pass 2:run-parts with jobs.allow
+        '''
+        os.chdir(self.srcdir)
+        self.test=test
+        self.results_path = os.path.join(self.resultsdir,
+                           'raw_output_mode_%s'%test)
+        try:
+            sleep(wait_time)
+            self.make_fun(test,wait_time)
+        except Exception as e:
+            logging.error('\nTest failed:%s'%e)
+
+    def cleanup(self):
+        utils.system('cat /tmp/backup > /etc/crontab')
+        if os.path.exists('/etc/cron.d/jobs.allow'):
+            utils.system('rm /etc/cron.d/jobs.allow /etc/cron.d/cron')
+        else:
+            utils.system('rm /etc/cron.d/cron')
+        if os.path.exists('/tmp/jobs.allow'):
+            utils.system('mv /tmp/jobs.allow /etc/cron.d/jobs.allow 
2>/dev/null')
+        if os.path.exists('/tmp/jobs.deny'):
+            utils.system('mv /tmp/jobs.allow /etc/cron.d/jobs.allow 
2>/dev/null')

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to