class test_libHelperFunctions(unittest.TestCase): """ """ @classmethod def setUpClass(self): """ """ self.logger = CyLogger(debug_mode=True) self.logger.initializeLogs() self.logger.log(lp.DEBUG, "Test " + self.__name__ + " initialized...") @classmethod def tearDownClass(self): """ """ pass def test_FoundException(self): """ """ pass def test_get_os_vers(self): """ """ pass def test_get_os_minor_vers(self): """ """ pass
def __init__(self): """ Initialization Method... """ self.logger = CyLogger() self.getLibc()
class RunThread(threading.Thread) : """ Use a thread & subprocess.Popen to run something To use - where command could be an array, or a string... : run_thread = RunThread(<command>, message_level) run_thread.start() run_thread.join() print run_thread.stdout @author: Roy Nielsen """ def __init__(self, command=[], logger=False) : """ Initialization method """ self.command = command self.logger = logger self.retout = None self.reterr = None threading.Thread.__init__(self) if isinstance(self.command, types.ListType) : self.shell = True self.printcmd = " ".join(self.command) if isinstance(self.command, types.StringTypes) : self.shell = False self.printcmd = self.command if not isinstance(logger, (bool, CyLogger)): self.logger = CyLogger() else: self.logger = logger self.logger.log(lp.INFO, "Initialized runThread...") ########################################################################## def run(self): if self.command : try : p = Popen(self.command, stdout=PIPE, stderr=PIPE, shell=self.shell) except Exception, err : self.logger.log(lp.WARNING, "Exception trying to open: " + \ str(self.printcmd)) self.logger.log(lp.WARNING, "Associated exception: " + str(err)) raise err else : try: self.retout, self.reterr = p.communicate() except Exception, err : self.logger.log(lp.WARNING, "Exception trying to open: " + \ str(self.printcmd)) self.logger.log(lp.WARNING, "Associated exception: " + str(err)) raise err else :
class SedFile4VersionStamp(object): def __init__(self, files=[], logger=False): if not logger: self.logger = CyLogger() self.logger.initializeLogs() else: self.logger = logger self.acquireStamp() self.module_version = '20160224.032043.009191' if files: for myfile in files: self.sedFileWithDateTimeStamp(myfile) def acquireStamp(self): """ Get the UTC time and format a time stamp string for the version. @author: Roy Nielsen """ format = "" datestamp = datetime.utcnow() self.stamp = datestamp.strftime("%Y%m%d.%H%M%S.%f") self.logger.log(lp.DEBUG, "Stamp: " + str(self.stamp)) def sedFileWithDateTimeStamp(self, file2change=""): """ Find "^(\s+module_version\s*=\s*)\S*" or "^(\s+self.module_version\s*=\s*)\S*" and replace with x.group(1) + "'" + acquireStamp() + "'" @author: Roy Nielsen """ self.logger.log(lp.INFO, "********** Entered sed method...**************") startString = "" found = False if file2change: fp = open(file2change, "r") lines = fp.readlines() fp.close() fp = open(file2change, "w") for line in lines: check1 = re.match("^(\s+module_version\s*=\s*)\S*", line) check2 = re.match("^(\s+self\.module_version\s*=\s*)\S*", line) if check1: self.logger.log(lp.DEBUG, "Found first check..") startString = check1.group(1) fp.write(re.sub("^\s+module_version\s*=\s*\S*", \ startString + "'" + \ self.stamp + "'", line)) elif check2: self.logger.log(lp.DEBUG, "Found second check...") startString = check2.group(1) fp.write(re.sub("^\s+self\.module_version\s*=\s*\S*", \ startString + "'" + \ self.stamp + "'", line)) else: fp.write(line) fp.close()
class test_ramdiskFactory(unittest.TestCase, GenericTestUtilities): ''' ''' @classmethod def setUpClass(self): '''Initializer''' # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization def setUp(self): '''This method runs before each test run. @author: Roy Nielsen ''' pass ############################################################################### ##### Method Tests ################################## def test_ramdiskFactoryFirstTest(self): ''' ''' pass ################################## def test_ramdiskFactorySecondTest(self): ''' ''' pass ############################################################################### ##### Functional Tests ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): '''disconnect ramdisk''' self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log( lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
def __init__(self, logger): """ """ if logger: self.logger = logger else: self.logger = CyLogger() self.module_version = '20160224.032043.009191' self.prefix = []
def setUpClass(self): """ Runs once before any tests start """ # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.manage_user = MacOSUser()
def setUpClass(self): '''Initializer''' # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization
class test_commonRamdiskTemplate(unittest.TestCase): ''' ''' @classmethod def setUpClass(self): '''Runs once before any tests start''' # Start timer in miliseconds self.test_start_time = datetime.now() ################################## def setUp(self): '''This method runs before each test run. @author: Roy Nielsen ''' pass ############################################################################### ##### Method Tests ################################## def test_init(self): ''' ''' pass ################################## def test_get_data(self): ''' ''' pass ############################################################################### ##### Functional Tests ################################## ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): '''Final cleanup actions...''' self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log( lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
def setUpClass(self): """ """ ##### # Set up logging self.logger = CyLogger(debug_mode=True) self.logger.initializeLogs() self.rw = RunWith(self.logger) ##### # Start timer in miliseconds self.test_start_time = datetime.now()
def setUpClass(self): """ Initializer """ # Start timer in miliseconds self.test_start_time = datetime.now() #self.message_level = "debug" self.message_level = "debug" self.libcPath = None # initial initialization ##### # If we don't have a supported platform, skip this test. if not sys.platform.startswith("darwin") and \ not sys.platform.startswith("linux"): unittest.SkipTest("This is not valid on this OS") self.subdirs = ["two", "three" "one/four"] """ Set up a ramdisk and use that random location as a root to test the filesystem functionality of what is being tested. """ #Calculate size of ramdisk to make for this unit test. size_in_mb = 1800 ramdisk_size = size = size_in_mb self.mnt_pnt_requested = "" self.logger = CyLogger() self.success = False self.mountPoint = False self.ramdiskDev = False self.mnt_pnt_requested = False # get a ramdisk of appropriate size, with a secure random mountpoint self.my_ramdisk = RamDisk(str(ramdisk_size), self.mnt_pnt_requested, self.message_level) (self.success, self.mountPoint, self.ramdiskDev) = self.my_ramdisk.getData() if self.success: self.logger.log( lp.INFO, "::::::::Ramdisk Mount Point: " + str(self.mountPoint)) self.logger.log( lp.INFO, "::::::::Ramdisk Device : " + str(self.ramdiskDev)) else: raise IOError("Cannot get a ramdisk for some reason. . .") ##### # Create a temp location on disk to run benchmark tests against self.fs_dir = tempfile.mkdtemp()
def __init__(self, files=[], logger=False): if not logger: self.logger = CyLogger() self.logger.initializeLogs() else: self.logger = logger self.acquireStamp() self.module_version = '20160224.032043.009191' if files: for myfile in files: self.sedFileWithDateTimeStamp(myfile)
def tearDownClass(self): '''disconnect ramdisk''' logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + \ " time to complete...")
def tearDownClass(self): """ Final cleanup actions... """ self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
def setUpClass(self): """ """ self.getLibc() self.subdirs = ["two", "three" "one/four"] self.logger = CyLogger() self.logger.log(lp.CRITICAL, "Logger initialized............................") """ Set up a ramdisk and use that random location as a root to test the filesystem functionality of what is being tested. """ #Calculate size of ramdisk to make for this unit test. size_in_mb = 1800 ramdisk_size = size = size_in_mb self.mnt_pnt_requested = "testmntpnt" self.success = False self.mountPoint = "" self.ramdiskDev = False self.mnt_pnt_requested = False # get a ramdisk of appropriate size, with a secure random mountpoint self.my_ramdisk = RamDisk(str(ramdisk_size), self.mnt_pnt_requested, logger=self.logger) (self.success, self.mountPoint, self.ramdiskDev) = self.my_ramdisk.getData() self.logger.log( lp.WARNING, str(self.success) + " : " + str(self.mountPoint) + " : " + str(self.ramdiskDev)) self.mount = self.mountPoint self.logger.log(lp.INFO, "::::::::Ramdisk Mount Point: " + str(self.mountPoint)) self.logger.log(lp.INFO, "::::::::Ramdisk Device : " + str(self.ramdiskDev)) if not self.success: raise IOError("Cannot get a ramdisk for some reason. . .") ##### # Create a temp location on disk to run benchmark tests against self.fs_dir = tempfile.mkdtemp() # Start timer in miliseconds self.test_start_time = datetime.now() self.setUpInstanceSpecifics()
def tearDownClass(self): """ disconnect ramdisk """ logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + \ " time to complete...")
def __init__(self, logger=False): if not logger: self.logger = CyLogger() else: self.logger = logger self.command = None self.output = None self.error = None self.module_version = '20160224.184019.673753' self.returncode = None self.printcmd = None self.myshell = None ##### # setting up to call ctypes to do a filesystem sync self.libc = getLibc()
def detach(device=" ", logger=False): """ Eject the ramdisk Detach (on the mac) is a better solution than unmount and eject separately.. Besides unmounting the disk, it also stops any processes related to the mntPoint @author: Roy Nielsen """ success = False if not logger: logger = CyLogger() else: logger = logger myRunWith = RunWith(logger) if not re.match("^\s*$", device): cmd = ["/usr/bin/hdiutil", "detach", device] myRunWith.setCommand(cmd) myRunWith.communicate() retval, reterr, retcode = myRunWith.getNlogReturns() if not reterr: success = True myRunWith.getNlogReturns() else: raise Exception("Cannot eject a device with an empty name..") return success
def __init__(self, logger): """ """ if logger: self.logger = logger else: self.logger = CyLogger() self.module_version = '20160224.032043.009191' self.prefix=[]
def setUpClass(self): """ Initializer """ unittest.SkipTest("Tests need to be written...") # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization ##### # If we don't have a supported platform, skip this test. if not sys.platform.startswith("darwin") and \ not sys.platform.startswith("linux"): raise unittest.SkipTest("This is not valid on this OS") raise unittest.SkipTest("Not a supported tests....")
def setUpInstanceSpecifics(self): '''Initializer''' # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() ##### # Initialize the helper class self.initializeHelper = False
def setUpClass(self): """ Initializer """ # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization
def setUpClass(self): """ Initializer """ unittest.SkipTest("Tests need to be written...") # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization
def getLibc(logger=False): '''Acquire a reference to the system libc, initially to access the filesystem "sync" function. :param logger: (Default value = False) :returns: s: python reference to the C libc object, or False, if it can't find libc on the system. @author: Roy Nielsen ''' if not logger: logger = CyLogger() osFamily = sys.platform.lower().strip() #print "---==## OS Family: " + str(osFamily) + " #==---" if osFamily and osFamily.startswith("darwin"): ##### # For Mac try: #temp_dir = sys._MEIPASS #libc_path = os.path.join(temp_dir, "libc.dylib") #libc = ctypes.CDLL("libSystem.dylib") libc = ctypes.CDLL("/usr/lib/libc.dylib") except: raise Exception("DAMN IT JIM!!!") else: print("Loading Mac dylib......................................") elif osFamily and osFamily.startswith("linux"): ##### # For Linux possible_paths = [ "/lib/x86_64-linux-gnu/libc.so.6", "/lib/i386-linux-gnu/libc.so.6", "/usr/lib64/libc.so.6" ] for path in possible_paths: if os.path.exists(path): libc = ctypes.CDLL(path) #print " Found libc!!!" break else: libc = False try: if libc: libc.sync() #print":::::Syncing..............." except: raise Exception("..............................Cannot Sync.") #print "OS Family: " + str(osFamily) return libc
def __init__(self, command=[], logger=False): """ Initialization method """ self.command = command self.logger = logger self.retout = None self.reterr = None threading.Thread.__init__(self) if isinstance(self.command, list): self.shell = True self.printcmd = " ".join(self.command) if isinstance(self.command, str): self.shell = False self.printcmd = self.command if not isinstance(logger, (bool, CyLogger)): self.logger = CyLogger() else: self.logger = logger self.logger.log(lp.INFO, "Initialized runThread...")
def __init__(self, size=0, mountpoint=False, logger=False, environ=False): """ """ ##### # Version/timestamp is # <YYYY><MM><DD>.<HH><MM><SS>.<microseconds> # in UTC time self.module_version = '20160224.032043.009191' if not logger: self.logger = CyLogger() else: self.logger = logger if not environ: self.environ = Environment() else: self.environ = environ self.chkApp = CheckApplicable(self.environ, self.logger)
def setUpInstanceSpecifics(self): """ Initializer """ # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() ##### # Initialize the helper class self.initializeHelper = False ##### # If we don't have a supported platform, skip this test. if not sys.platform.startswith("linux"): raise unittest.SkipTest("This is not valid on this OS")
def setUpClass(self): """ Initializer """ # Start timer in miliseconds self.test_start_time = datetime.now() #self.message_level = "debug" self.message_level = "debug" self.libcPath = None # initial initialization self.subdirs = ["two", "three" "one/four"] """ Set up a ramdisk and use that random location as a root to test the filesystem functionality of what is being tested. """ #Calculate size of ramdisk to make for this unit test. size_in_mb = 1800 ramdisk_size = size = size_in_mb self.mnt_pnt_requested = "" self.logger = CyLogger() self.success = False self.mountPoint = False self.ramdiskDev = False self.mnt_pnt_requested = False # get a ramdisk of appropriate size, with a secure random mountpoint self.my_ramdisk = RamDisk(str(ramdisk_size), self.mnt_pnt_requested, self.message_level) (self.success, self.mountPoint, self.ramdiskDev) = self.my_ramdisk.getData() if self.success: self.logger.log(lp.INFO, "::::::::Ramdisk Mount Point: " + str(self.mountPoint)) self.logger.log(lp.INFO, "::::::::Ramdisk Device : " + str(self.ramdiskDev)) else: raise IOError("Cannot get a ramdisk for some reason. . .") ##### # Create a temp location on disk to run benchmark tests against self.fs_dir = tempfile.mkdtemp()
def __init__(self, logger=None): """ Identify OS and instantiate an instance of a ramdisk """ self.module_version = '20160224.203258.288119' self.size = 0 self.mountpoint = None self.ramdiskType = None if not logger: self.logger = CyLogger() else: self.logger = logger self.activeRamdisk = None self.ramdisks = [] self.validRamdiskTypes = ["loop", "tmpfs"] self.validOSFamilies = ["macos", "linux"] self.myosfamily = getOsFamily() if not self.myosfamily in self.validOSFamilies: raise OSNotValidForRamdiskHelper("Needs to be MacOS or Linux...")
def __init__(self, command=[], logger=False) : """ Initialization method """ self.command = command self.logger = logger self.retout = None self.reterr = None threading.Thread.__init__(self) if isinstance(self.command, types.ListType) : self.shell = True self.printcmd = " ".join(self.command) if isinstance(self.command, types.StringTypes) : self.shell = False self.printcmd = self.command if not isinstance(logger, (bool, CyLogger)): self.logger = CyLogger() else: self.logger = logger self.logger.log(lp.INFO, "Initialized runThread...")
def __init__(self, size=0, mountpoint=False, logger=False): """ """ ##### # Version/timestamp is # <YYYY><MM><DD>.<HH><MM><SS>.<microseconds> # in UTC time self.module_version = '20160224.032043.009191' if not logger: self.logger = CyLogger() self.logger.log(lp.INFO, "Logger: " + str(self.logger)) else: self.logger = logger self.logger.log(lp.INFO, "Logger: " + str(self.logger)) self.diskSize = size self.success = False self.myRamdiskDev = None if not mountpoint: self.getRandomizedMountpoint() else: self.mntPoint = mountpoint self.logger.log(lp.DEBUG, "disk size: " + str(self.diskSize)) self.logger.log(lp.DEBUG, "volume name: " + str(self.mntPoint))
class RunThread(threading.Thread): '''Use a thread & subprocess.Popen to run something To use - where command could be an array, or a string... : run_thread = RunThread(<command>, message_level) run_thread.start() run_thread.join() print run_thread.stdout @author: Roy Nielsen ''' def __init__(self, command=[], logger=False): """ Initialization method """ self.command = command self.logger = logger self.retout = None self.reterr = None threading.Thread.__init__(self) if isinstance(self.command, list): self.shell = True self.printcmd = " ".join(self.command) if isinstance(self.command, str): self.shell = False self.printcmd = self.command if not isinstance(logger, (bool, CyLogger)): self.logger = CyLogger() else: self.logger = logger self.logger.log(lp.INFO, "Initialized runThread...") ########################################################################## def run(self): if self.command: try: p = Popen(self.command, stdout=PIPE, stderr=PIPE, shell=self.shell) except Exception as err: self.logger.log(lp.WARNING, "Exception trying to open: " + \ str(self.printcmd)) self.logger.log(lp.WARNING, "Associated exception: " + str(err)) raise err else: try: self.retout, self.reterr = p.communicate() except Exception as err: self.logger.log(lp.WARNING, "Exception trying to open: " + \ str(self.printcmd)) self.logger.log(lp.WARNING, "Associated exception: " + str(err)) raise err else: #logMessage("Return values: ", "debug", self.message_level) #logMessage("retout: " + str(self.retout), # "debug", self.message_level) #logMessage("reterr: " + str(self.reterr), # "debug", self.message_level) self.logger.log(lp.WARNING, "Finished \"run\" of: " + \ str(self.printcmd)) ########################################################################## def getStdout(self): '''Getter for standard output @author: Roy Nielsen ''' self.logger.log(lp.INFO, "Getting stdout...") return self.retout ########################################################################## def getStderr(self): '''Getter for standard err @author: Roy Nielsen ''' self.logger.log(lp.DEBUG, "Getting stderr...") return self.reterr
class test_ramdiskFactory(unittest.TestCase, GenericTestUtilities): """ """ @classmethod def setUpClass(self): """ Initializer """ # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization def setUp(self): """ This method runs before each test run. @author: Roy Nielsen """ pass ############################################################################### ##### Method Tests ################################## def test_ramdiskFactoryFirstTest(self): """ """ pass ################################## def test_ramdiskFactorySecondTest(self): """ """ pass ############################################################################### ##### Functional Tests ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): """ disconnect ramdisk """ self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
class BuildAndRunSuite(object): def __init__(self, logger): """ """ if logger: self.logger = logger else: self.logger = CyLogger() self.module_version = '20160224.032043.009191' self.prefix=[] ############################################## def setPrefix(self, prefix=[]): """ Setter for the prefix variable... """ if prefix and isinstance(prefix, list): self.prefix = prefix else: self.prefix=["test_"] ############################################## def get_all_tests(self, prefix=[]): """ Collect all available tests using the test prefix(s) @author: Roy Nielsen """ test_list = [] if not self.modules: allfiles = os.listdir(testdir) for check_file in allfiles: test_name = str(check_file).split(".")[0] pycfile = os.path.join("./tests/", test_name + ".pyc") if os.path.exists(pycfile): os.unlink(pycfile) elif re.match("^test_.+.py$", check_file): print "Loading test: " + str(check_file) test_list.append(os.path.join("./tests/", check_file)) print str(test_list) return test_list ############################################## def run_suite(self, modules=[]): """ Gather all the tests from this module in a test suite. @author: Roy Nielsen """ self.test_dir_name = testdir.split("/")[1] self.modules = modules ##### # Initialize the test suite self.test_suite = unittest.TestSuite() ##### # Generate the test list if self.modules and isinstance(self.modules, list): test_list = self.modules else: test_list = self.get_all_tests(prefix) ##### # Import each of the tests and add them to the suite for check_file in test_list: self.logger.log(lp.DEBUG, str(check_file)) test_name = str(check_file).split("/")[-1] test_name = str(test_name).split(".")[0] self.logger.log(lp.DEBUG, "test_name: " + str(test_name)) test_name_import_path = ".".join([self.test_dir_name, test_name]) self.logger.log(lp.DEBUG, "test_name_import_path: " + str(test_name_import_path)) ################################################ # Test class needs to be named the same as the # filename for this to work. # import the file named in "test_name" variable module_to_run = __import__(test_name_import_path, fromlist=[test_name], level=-1) # getattr(x, 'foobar') is equivalent to x.foobar test_to_run = getattr(module_to_run, test_name) # Add the test class to the test suite self.test_suite.addTest(unittest.makeSuite(test_to_run)) ##### # calll the run_action to execute the test suite self.run_action() ############################################## def run_action(self): """ Run the Suite. """ runner = unittest.TextTestRunner() runner.run(self.test_suite)
default=0, help="Print debug messages") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=0, help="Print status messages") (opts, args) = parser.parse_args() if opts.verbose != 0: level = Logger(level=lp.INFO) elif opts.debug != 0: level = Logger(level=lp.DEBUG) else: level=lp.WARNING if opts.size: size = int(opts.size) # in Megabytes mntpnt = opts.mntpnt logger = CyLogger() logger.initializeLogs() ramdisk = RamDisk(str(size), mntpnt, logger) ramdisk.logData() ramdisk.printData() if not ramdisk.success: raise Exception("Ramdisk setup failed..") print((ramdisk.getDevice()))
level=lp.WARNING if opts.size: size = int(opts.size) # in Megabytes else: size = str(512) if opts.mntpnt: mntpnt = opts.mntpnt else: mntpnt = "uniontest" if not os.path.exists(mntpnt): os.makedirs(mntpnt) logger = CyLogger(level=level) logger.initializeLogs() ramdisk = RamDisk(size=size, logger=logger) ramdisk.logData() ramdisk.printData() ramdisk.unionOver(mntpnt) ramdisk.printData() if not ramdisk.success: raise Exception("Ramdisk setup failed..") print ramdisk.getDevice()
class GenericRamdiskTest(unittest.TestCase, GenericTestUtilities): """ Holds helper methods. DO NOT create an init Inspiration for using classmethod: http://simeonfranklin.com/testing2.pdf @author: Roy Nielsen """ @classmethod def setUpClass(self): """ """ #self.getLibc() self.subdirs = ["two", "three" "one/four"] self.logger = CyLogger() self.logger.log(lp.CRITICAL, "Logger initialized............................") """ Set up a ramdisk and use that random location as a root to test the filesystem functionality of what is being tested. """ #Calculate size of ramdisk to make for this unit test. size_in_mb = 1800 ramdisk_size = size = size_in_mb self.mnt_pnt_requested = "" self.success = False self.mountPoint = False self.ramdiskDev = False self.mnt_pnt_requested = False # get a ramdisk of appropriate size, with a secure random mountpoint self.my_ramdisk = RamDisk(size=str(ramdisk_size), logger=self.logger) (self.success, self.mountPoint, self.ramdiskDev) = self.my_ramdisk.getData() self.mount = self.mountPoint self.logger.log(lp.INFO, "::::::::Ramdisk Mount Point: " + str(self.mountPoint)) self.logger.log(lp.INFO, "::::::::Ramdisk Device : " + str(self.ramdiskDev)) if not self.success: raise IOError("Cannot get a ramdisk for some reason. . .") ##### # Create a temp location on disk to run benchmark tests against self.fs_dir = tempfile.mkdtemp() # Start timer in miliseconds self.test_start_time = datetime.now() self.setUpInstanceSpecifics() @classmethod def setUpInstanceSpecifics(self): """ Call the child class setUpClass initializer, if possible.. Here to be over-ridden by a child class. @author: Roy Nielsen """ pass ################################################ ##### Helper Methods def _unloadRamdisk(self): """ """ if self.my_ramdisk.unmount(): self.logger.log(lp.INFO, r"Successfully detached disk: " + \ str(self.my_ramdisk.mntPoint).strip()) else: self.logger.log(lp.WARNING, r"Couldn't detach disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) raise Exception(r"Cannot eject disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) ############################################################################### ##### Functional Tests ################################## def test_files_n_dirs(self): """ Should work when files exist in ramdisk. """ # Do file setup for this test for subdir in self.subdirs: dirpath = self.mountPoint + "/" + subdir self.logger.log(lp.DEBUG, "DIRPATH: : " + str(dirpath)) self.mkdirs(dirpath) self.touch(dirpath + "/" + "test") # Do the tests for subdir in self.subdirs: # CANNOT use os.path.join this way. os.path.join cannot deal with # absolute directories. May work with mounting ramdisk in local # relative directories. self.assertTrue(os.path.exists(self.mountPoint + "/" + subdir + "/" + "test"), "Problem with ramdisk...") ################################## def test_four_file_sizes(self): """ Test file creation of various sizes, ramdisk vs. filesystem """ ##### # Clean up the ramdisk self.my_ramdisk._format() ##### # 100Mb file size oneHundred = 100 ##### # 100Mb file size twoHundred = 200 ##### # 500Mb file size fiveHundred = 500 ##### # 1Gb file size oneGig = 1000 my_fs_array = [oneHundred, twoHundred, fiveHundred, oneGig] for file_size in my_fs_array: self.logger.log(lp.INFO, "testfile size: " + str(file_size)) ##### # Create filesystem file and capture the time it takes... fs_time = self.mkfile(os.path.join(self.fs_dir, "testfile"), file_size) self.logger.log(lp.INFO, "fs_time: " + str(fs_time)) ##### # get the time it takes to create the file in ramdisk... ram_time = self.mkfile(os.path.join(self.mountPoint, "testfile"), file_size) self.logger.log(lp.INFO, "ram_time: " + str(ram_time)) speed = fs_time - ram_time self.logger.log(lp.INFO, "ramdisk: " + str(speed) + " faster...") self.assertTrue((fs_time - ram_time).days > -1, "Problem with ramdisk...") ################################## def test_many_small_files_creation(self): """ """ ##### # Clean up the ramdisk self.my_ramdisk._format() ##### # ramdisk_starttime = datetime.now() for i in range(1000): self.mkfile(os.path.join(self.mountPoint, "testfile" + str(i)), 1) ramdisk_endtime = datetime.now() rtime = ramdisk_endtime - ramdisk_starttime fs_starttime = datetime.now() for i in range(1000): self.mkfile(os.path.join(self.fs_dir, "testfile" + str(i)), 1) fsdisk_endtime = datetime.now() fstime = fsdisk_endtime - fs_starttime self.assertTrue((fstime - rtime).days > -1, "Problem with ramdisk...") ################################## @classmethod def tearDownInstanceSpecifics(self): """ Skeleton method in case a child class wants/needs to override it. @author: Roy Nielsen """ pass @classmethod def tearDownClass(self): """ """ self.tearDownInstanceSpecifics() if unmount(self.mount): self.logger.log(lp.INFO, r"Successfully detached disk: " + \ str(self.my_ramdisk.mntPoint).strip()) else: self.logger.log(lp.WARNING, r"Couldn't detach disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) raise Exception(r"Cannot eject disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + \ " time to complete...")
class test_commonRamdiskTemplate(unittest.TestCase): """ """ @classmethod def setUpClass(self): """ Runs once before any tests start """ # Start timer in miliseconds self.test_start_time = datetime.now() ################################## def setUp(self): """ This method runs before each test run. @author: Roy Nielsen """ pass ############################################################################### ##### Method Tests ################################## def test_init(self): """ """ pass ################################## def test_get_data(self): """ """ pass ############################################################################### ##### Functional Tests ################################## ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): """ Final cleanup actions... """ self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
class test_addUserToGroup(unittest.TestCase): """ """ @classmethod def setUpClass(self): """ Runs once before any tests start """ # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.manage_user = MacOSUser() ################################## def setUp(self): """ This method runs before each test run. @author: Roy Nielsen """ pass ############################################################################### ##### Method Tests ################################## def test_init(self): """ """ pass ################################## def test_get_data(self): """ """ pass ############################################################################### ##### Functional Tests ################################## ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): """ Final cleanup actions... """ self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log( lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
class SedFile4VersionStamp(object): def __init__(self, files=[], logger=False): if not logger: self.logger = CyLogger() self.logger.initializeLogs() else: self.logger = logger self.acquireStamp() self.module_version = '20160224.032043.009191' if files: for myfile in files: self.sedFileWithDateTimeStamp(myfile) def acquireStamp(self): '''Get the UTC time and format a time stamp string for the version. @author: Roy Nielsen ''' format = "" datestamp = datetime.utcnow() self.stamp = datestamp.strftime("%Y%m%d.%H%M%S.%f") self.logger.log(lp.DEBUG, "Stamp: " + str(self.stamp)) def sedFileWithDateTimeStamp(self, file2change=""): '''Find "^(\s+module_version\s*=\s*)\S*" or "^(\s+self.module_version\s*=\s*)\S*" and replace with x.group(1) + "'" + acquireStamp() + "'" @author: Roy Nielsen :param file2change: (Default value = "") ''' self.logger.log(lp.INFO, "********** Entered sed method...**************") startString = "" found = False if file2change: fp = open(file2change, "r") lines = fp.readlines() fp.close() fp = open(file2change, "w") for line in lines: check1 = re.match("^(\s+module_version\s*=\s*)\S*", line) check2 = re.match("^(\s+self\.module_version\s*=\s*)\S*", line) if check1: self.logger.log(lp.DEBUG, "Found first check..") startString = check1.group(1) fp.write(re.sub("^\s+module_version\s*=\s*\S*", \ startString + "'" + \ self.stamp + "'", line)) elif check2: self.logger.log(lp.DEBUG, "Found second check...") startString = check2.group(1) fp.write(re.sub("^\s+self\.module_version\s*=\s*\S*", \ startString + "'" + \ self.stamp + "'", line)) else: fp.write(line) fp.close()
# Options processing ##### # ... processing modules ... if options.all: modules = None elif options.modules: modules = options.modules else: modules = None ##### # ... processing logging options... verbose = options.verbose debug = options.debug logger = CyLogger(debug_mode=options.debug, verbose_mode=options.verbose) logger.initializeLogs(syslog=options.skip_syslog) logger.log(lp.DEBUG, "Modules: " + str(modules)) ##### # ... processing test prefixes if options.prefix: prefix = options.prefix else: prefix = ["test_"] bars = BuildAndRunSuite(logger) bars.run_suite(modules)
class RamDiskTemplate(object): """ """ def __init__(self, size=0, mountpoint=False, logger=False): """ """ ##### # Version/timestamp is # <YYYY><MM><DD>.<HH><MM><SS>.<microseconds> # in UTC time self.module_version = '20160224.032043.009191' if not logger: self.logger = CyLogger() self.logger.log(lp.INFO, "Logger: " + str(self.logger)) else: self.logger = logger self.logger.log(lp.INFO, "Logger: " + str(self.logger)) self.diskSize = size self.success = False self.myRamdiskDev = None if not mountpoint: self.getRandomizedMountpoint() else: self.mntPoint = mountpoint self.logger.log(lp.DEBUG, "disk size: " + str(self.diskSize)) self.logger.log(lp.DEBUG, "volume name: " + str(self.mntPoint)) ########################################################################### def getData(self): """ Getter for mount data, and if the mounting of a ramdisk was successful """ return (self.success, str(self.mntPoint), str(self.myRamdiskDev)) ########################################################################### def printData(self): """ Getter for mount data, and if the mounting of a ramdisk was successful. Also prints to the data to the console """ print "Success: " + str(self.success) print "Mount point: " + str(self.mntPoint) print "Device: " + str(self.myRamdiskDev) return (self.success, str(self.mntPoint), str(self.myRamdiskDev)) ########################################################################### def logData(self): """ Getter for mount data, and if the mounting of a ramdisk was successful Also logs the data. """ self.logger.log(lp.INFO, "Success: " + str(self.success)) self.logger.log(lp.INFO, "Mount point: " + str(self.mntPoint)) self.logger.log(lp.INFO, "Device: " + str(self.myRamdiskDev)) return (self.success, str(self.mntPoint), str(self.myRamdiskDev)) ########################################################################### def getRandomizedMountpoint(self) : """ Create a randomized (secure) mount point - per python's implementation of mkdtemp - a way to make an unguessable directory on the system @author: Roy Nielsen """ success = False try : self.mntPoint = mkdtemp() except Exception, err : self.logger.log(lp.WARNING, "Exception trying to create temporary directory") raise err else :
class test_ramdisk(GenericRamdiskTest): """ """ @classmethod def setUpClass(self): """ Initializer """ # Start timer in miliseconds self.test_start_time = datetime.now() #self.message_level = "debug" self.message_level = "debug" self.libcPath = None # initial initialization self.subdirs = ["two", "three" "one/four"] """ Set up a ramdisk and use that random location as a root to test the filesystem functionality of what is being tested. """ #Calculate size of ramdisk to make for this unit test. size_in_mb = 1800 ramdisk_size = size = size_in_mb self.mnt_pnt_requested = "" self.logger = CyLogger() self.success = False self.mountPoint = False self.ramdiskDev = False self.mnt_pnt_requested = False # get a ramdisk of appropriate size, with a secure random mountpoint self.my_ramdisk = RamDisk(str(ramdisk_size), self.mnt_pnt_requested, self.message_level) (self.success, self.mountPoint, self.ramdiskDev) = self.my_ramdisk.getData() if self.success: self.logger.log(lp.INFO, "::::::::Ramdisk Mount Point: " + str(self.mountPoint)) self.logger.log(lp.INFO, "::::::::Ramdisk Device : " + str(self.ramdiskDev)) else: raise IOError("Cannot get a ramdisk for some reason. . .") ##### # Create a temp location on disk to run benchmark tests against self.fs_dir = tempfile.mkdtemp() def setUp(self): """ This method runs before each test run. @author: Roy Nielsen """ self.libcPath = None # initial initialization ##### # setting up to call ctypes to do a filesystem sync if sys.platform.startswith("darwin"): ##### # For Mac self.libc = C.CDLL("/usr/lib/libc.dylib") elif sys.platform.startswith("linux"): ##### # For Linux self.findLinuxLibC() self.libc = C.CDLL(self.libcPath) else: self.libc = self._pass() ############################################################################### ##### Helper Methods ############################################################################### ##### Method Tests ################################## def test_init(self): """ """ pass ################################## def test_get_data(self): """ """ pass ################################## def test_getRandomizedMountpoint(self): """ """ pass ################################## def test_create(self): """ """ pass ################################## def test_mount(self): """ """ pass ################################## def test_attach(self): """ """ pass ################################## def test_remove_journal(self): """ """ pass ################################## def test_unmount(self): """ """ pass ################################## def test_eject(self): """ """ pass ################################## def test_format(self): """ """ pass ################################## def test_partition(self): """ """ pass ################################## def test_isMemoryAvailable(self): """ """ pass ################################## def test_runcmd(self): """ """ pass ################################## def test_getDevice(self): """ """ pass ################################## def test_setDevice(self): """ """ pass ################################## def test_getVersion(self): """ """ pass ################################## def test_detach(self): """ """ pass ############################################################################### ##### Functional Tests ################################## def test_files_n_dirs(self): """ Should work when files exist in ramdisk. """ # Do file setup for this test for subdir in self.subdirs: dirpath = self.mountPoint + "/" + subdir self.logger.log(lp.DEBUG, "DIRPATH: : " + str(dirpath)) self.mkdirs(dirpath) self.touch(dirpath + "/" + "test") # Do the tests for subdir in self.subdirs: # CANNOT use os.path.join this way. os.path.join cannot deal with # absolute directories. May work with mounting ramdisk in local # relative directories. self.assertTrue(os.path.exists(self.mountPoint + "/" + subdir + "/" + "test")) ################################## def test_four_file_sizes(self): """ Test file creation of various sizes, ramdisk vs. filesystem """ ##### # Clean up the ramdisk self.my_ramdisk._format() ##### # 100Mb file size oneHundred = 100 ##### # 100Mb file size twoHundred = 200 ##### # 500Mb file size fiveHundred = 500 ##### # 1Gb file size oneGig = 1000 my_fs_array = [oneHundred, twoHundred, fiveHundred, oneGig] time.sleep(1) for file_size in my_fs_array: self.logger.log(lp.DEBUG, "testfile size: " + str(file_size)) ##### # Create filesystem file and capture the time it takes... fs_time = self.mkfile(os.path.join(self.fs_dir, "testfile"), file_size) self.logger.log(lp.DEBUG, "fs_time: " + str(fs_time)) time.sleep(1) ##### # get the time it takes to create the file in ramdisk... ram_time = self.mkfile(os.path.join(self.mountPoint, "testfile"), file_size) self.logger.log(lp.DEBUG, "ram_time: " + str(ram_time)) time.sleep(1) speed = fs_time - ram_time self.logger.log(lp.DEBUG, "ramdisk: " + str(speed) + " faster...") self.assertTrue((fs_time - ram_time).days>-1) def test_many_small_files_creation(self): """ """ ##### # Clean up the ramdisk self.my_ramdisk._format() ##### # ramdisk_starttime = datetime.now() for i in range(1000): self.mkfile(os.path.join(self.mountPoint, "testfile" + str(i)), 1) ramdisk_endtime = datetime.now() rtime = ramdisk_endtime - ramdisk_starttime fs_starttime = datetime.now() for i in range(1000): self.mkfile(os.path.join(self.fs_dir, "testfile" + str(i)), 1) fsdisk_endtime = datetime.now() fstime = fsdisk_endtime - fs_starttime self.assertTrue((fstime - rtime).days > -11) ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): """ disconnect ramdisk """ if self.my_ramdisk.unmount(): self.logger.log(lp.INFO, r"Successfully detached disk: " + \ str(self.my_ramdisk.mntPoint).strip()) else: self.logger.log(lp.WARNING, r"Couldn't detach disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) raise Exception(r"Cannot eject disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + \ " time to complete...")
class test_ramdisk(GenericRamdiskTest): ''' ''' @classmethod def setUpClass(self): '''Initializer''' # Start timer in miliseconds self.test_start_time = datetime.now() #self.message_level = "debug" self.message_level = "debug" self.libcPath = None # initial initialization self.subdirs = ["two", "three" "one/four"] """ Set up a ramdisk and use that random location as a root to test the filesystem functionality of what is being tested. """ #Calculate size of ramdisk to make for this unit test. size_in_mb = 1800 ramdisk_size = size = size_in_mb self.mnt_pnt_requested = "" self.logger = CyLogger() self.success = False self.mountPoint = False self.ramdiskDev = False self.mnt_pnt_requested = False # get a ramdisk of appropriate size, with a secure random mountpoint self.my_ramdisk = RamDisk(str(ramdisk_size), self.mnt_pnt_requested, self.message_level) (self.success, self.mountPoint, self.ramdiskDev) = self.my_ramdisk.getData() if self.success: self.logger.log(lp.INFO, "::::::::Ramdisk Mount Point: " + str(self.mountPoint)) self.logger.log(lp.INFO, "::::::::Ramdisk Device : " + str(self.ramdiskDev)) else: raise IOError("Cannot get a ramdisk for some reason. . .") ##### # Create a temp location on disk to run benchmark tests against self.fs_dir = tempfile.mkdtemp() def setUp(self): '''This method runs before each test run. @author: Roy Nielsen ''' self.libcPath = None # initial initialization ##### # setting up to call ctypes to do a filesystem sync if sys.platform.startswith("darwin"): ##### # For Mac self.libc = C.CDLL("/usr/lib/libc.dylib") elif sys.platform.startswith("linux"): ##### # For Linux self.findLinuxLibC() self.libc = C.CDLL(self.libcPath) else: self.libc = self._pass() ############################################################################### ##### Helper Methods ############################################################################### ##### Method Tests ################################## def test_init(self): ''' ''' pass ################################## def test_get_data(self): ''' ''' pass ################################## def test_getRandomizedMountpoint(self): ''' ''' pass ################################## def test_create(self): ''' ''' pass ################################## def test_mount(self): ''' ''' pass ################################## def test_attach(self): ''' ''' pass ################################## def test_remove_journal(self): ''' ''' pass ################################## def test_unmount(self): ''' ''' pass ################################## def test_eject(self): ''' ''' pass ################################## def test_format(self): ''' ''' pass ################################## def test_partition(self): ''' ''' pass ################################## def test_isMemoryAvailable(self): ''' ''' pass ################################## def test_runcmd(self): ''' ''' pass ################################## def test_getDevice(self): ''' ''' pass ################################## def test_setDevice(self): ''' ''' pass ################################## def test_getVersion(self): ''' ''' pass ################################## def test_detach(self): ''' ''' pass ############################################################################### ##### Functional Tests ################################## def test_files_n_dirs(self): '''Should work when files exist in ramdisk.''' # Do file setup for this test for subdir in self.subdirs: dirpath = self.mountPoint + "/" + subdir self.logger.log(lp.DEBUG, "DIRPATH: : " + str(dirpath)) self.mkdirs(dirpath) self.touch(dirpath + "/" + "test") # Do the tests for subdir in self.subdirs: # CANNOT use os.path.join this way. os.path.join cannot deal with # absolute directories. May work with mounting ramdisk in local # relative directories. self.assertTrue(os.path.exists(self.mountPoint + "/" + subdir + "/" + "test")) ################################## def test_four_file_sizes(self): '''Test file creation of various sizes, ramdisk vs. filesystem''' ##### # Clean up the ramdisk self.my_ramdisk._format() ##### # 100Mb file size oneHundred = 100 ##### # 100Mb file size twoHundred = 200 ##### # 500Mb file size fiveHundred = 500 ##### # 1Gb file size oneGig = 1000 my_fs_array = [oneHundred, twoHundred, fiveHundred, oneGig] time.sleep(1) for file_size in my_fs_array: self.logger.log(lp.DEBUG, "testfile size: " + str(file_size)) ##### # Create filesystem file and capture the time it takes... fs_time = self.mkfile(os.path.join(self.fs_dir, "testfile"), file_size) self.logger.log(lp.DEBUG, "fs_time: " + str(fs_time)) time.sleep(1) ##### # get the time it takes to create the file in ramdisk... ram_time = self.mkfile(os.path.join(self.mountPoint, "testfile"), file_size) self.logger.log(lp.DEBUG, "ram_time: " + str(ram_time)) time.sleep(1) speed = fs_time - ram_time self.logger.log(lp.DEBUG, "ramdisk: " + str(speed) + " faster...") self.assertTrue((fs_time - ram_time).days>-1) def test_many_small_files_creation(self): ''' ''' ##### # Clean up the ramdisk self.my_ramdisk._format() ##### # ramdisk_starttime = datetime.now() for i in range(1000): self.mkfile(os.path.join(self.mountPoint, "testfile" + str(i)), 1) ramdisk_endtime = datetime.now() rtime = ramdisk_endtime - ramdisk_starttime fs_starttime = datetime.now() for i in range(1000): self.mkfile(os.path.join(self.fs_dir, "testfile" + str(i)), 1) fsdisk_endtime = datetime.now() fstime = fsdisk_endtime - fs_starttime self.assertTrue((fstime - rtime).days > -11) ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): '''disconnect ramdisk''' if self.my_ramdisk.unmount(): self.logger.log(lp.INFO, r"Successfully detached disk: " + \ str(self.my_ramdisk.mntPoint).strip()) else: self.logger.log(lp.WARNING, r"Couldn't detach disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) raise Exception(r"Cannot eject disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint)) ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log(lp.INFO, self.__module__ + " took " + str(test_time) + \ " time to complete...")
class test_ramdiskFactory(unittest.TestCase, GenericTestUtilities): """ """ @classmethod def setUpClass(self): """ Initializer """ unittest.SkipTest("Tests need to be written...") # Start timer in miliseconds self.test_start_time = datetime.now() self.logger = CyLogger() self.libcPath = None # initial initialization ##### # If we don't have a supported platform, skip this test. if not sys.platform.startswith("darwin") and \ not sys.platform.startswith("linux"): raise unittest.SkipTest("This is not valid on this OS") raise unittest.SkipTest("Not a supported tests....") def setUp(self): """ This method runs before each test run. @author: Roy Nielsen """ pass ############################################################################### ##### Method Tests ################################## def test_ramdiskFactoryFirstTest(self): """ """ pass ################################## def test_ramdiskFactorySecondTest(self): """ """ pass ############################################################################### ##### Functional Tests ############################################################################### ##### unittest Tear down @classmethod def tearDownClass(self): """ disconnect ramdisk """ self.logger = CyLogger() ##### # capture end time test_end_time = datetime.now() ##### # Calculate and log how long it took... test_time = (test_end_time - self.test_start_time) self.logger.log( lp.INFO, self.__module__ + " took " + str(test_time) + " time to complete...")
help="Name of the device to detach") parser.add_option("-d", "--debug", action="store_true", dest="debug", default=0, help="Print debug messages") parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=0, help="Print status messages") (opts, args) = parser.parse_args() if opts.verbose != 0: level = CyLogger(level=lp.INFO) elif opts.debug != 0: level = CyLogger(level=lp.DEBUG) else: level=lp.WARNING if opts.device == 0: raise Exception("Cannot detach a device with no name..") else: device = opts.device logger = CyLogger(level=level) logger.initializeLogs() if detach(device, logger): logger.log(lp.INFO, r"Successfully detached disk: " + str(device).strip()) else: logger.log(lp.WARNING, r"Couldn't detach disk: " + str(device).strip()) raise Exception(r"Cannot eject disk: " + str(device).strip())
try: myout, myerr = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=None).communicate() except OSError, err: print traceback.format_exc(err) print str(err) else: sys.stdout.flush() print "myout: " + str(myout) print "\n" print "myerr: " + str(myerr) logger = CyLogger(debug_mode=True) logger.initializeLogs() rw = RunWith(logger) cmd = [ "/usr/bin/osascript", "-e", '\'do shell script "{0}" user name "{1}" password "{2}" with administrator privileges\'' .format(subcmd, user, userpass) ] rw.setCommand(cmd) rw.waitNpassThruStdout() for line in rw.getStdout().split("\n"): print line + "\n"
class GenericTestUtilities(object): """ Generic class based Yutilities for ramdisk testing... @author: Roy Nielsen """ def __init__(self): """ Initialization Method... """ self.logger = CyLogger() self.getLibc() ################################################ ##### Helper Methods @classmethod def getLibc(self): """ """ self.osFamily = sys.platform.lower() if self.osFamily and self.osFamily.startswith("darwin"): ##### # For Mac try: self.libc = ctypes.CDLL("/usr/lib/libc.dylib") except: raise Exception("DAMN IT JIM!!!") else: print "Loading Mac dylib......................................" elif self.osFamily and self.osFamily.startswith("linux"): ##### # For Linux possible_paths = ["/lib/x86_64-linux-gnu/libc.so.6", "/lib/i386-linux-gnu/libc.so.6", "/usr/lib64/libc.so.6"] for path in possible_paths: if os.path.exists(path): self.libcPath = path self.libc = ctypes.CDLL(self.libcPath) print " Found libc!!!" break else: self.libc = self._pass() try: self.libc.sync() print":::::Syncing..............." except: raise Exception("..............................Cannot Sync.") print "OS Family: " + str(self.osFamily) ################################################ def findLinuxLibC(self): """ Find Linux Libc library... @author: Roy Nielsen """ possible_paths = ["/lib/x86_64-linux-gnu/libc.so.6", "/lib/i386-linux-gnu/libc.so.6"] for path in possible_paths: if os.path.exists(path): self.libcPath = path self.libc = ctypes.CDLL(self.libcPath) break ################################################ @classmethod def _pass(self): """ Filler if a library didn't load properly """ pass ################################################ def touch(self, fname="", message_level="normal"): """ Python implementation of the touch command.. @author: Roy Nielsen """ if re.match("^\s*$", str(fname)): self.logger.log(lp.WARNING, "Cannot touch a file without a filename....") else: try: os.utime(fname, None) except: try: open(fname, 'a').close() except Exception, err: self.logger.log(lp.WARNING, "Cannot open to touch: " + str(fname))
class RunWith(object): """ Class that will run commands in various ways. @method setCommand(self, command=[]) @method communicate(self) @method wait(self) @method timeout(self, seconds=0) @method runAs(self, user="", password="") @method runAsWithSudo(self, user="", password="") @method getStdout(self) @method getStderr(self) @method getReturnCode(self) @WARNING - Known to work on Mac, may or may not work on other platforms @author: Roy Nielsen """ def __init__(self, logger=False): if not logger: self.logger = CyLogger() else: self.logger = logger self.command = None self.output = None self.error = None self.module_version = '20160224.184019.673753' self.returncode = None self.printcmd = None self.myshell = None ##### # setting up to call ctypes to do a filesystem sync self.libc = getLibc() def setCommand(self, command, myshell=False): """ initialize a command to run @author: Roy Nielsen """ if command: self.command = command ##### # Handle Popen's shell, or "myshell"... if isinstance(self.command, types.ListType) : self.printcmd = " ".join(self.command) if isinstance(self.command, types.StringTypes) : self.printcmd = self.command self.myshell = myshell ############################################################################ def getStdout(self): """ Getter for the standard output of the last command. @author: Roy Nielsen """ return self.output ############################################################################ def getStderr(self): """ Getter for the standard error of the last command. @author: Roy Nielsen """ return self.error ############################################################################ def getReturnCode(self): """ Getter for the return code of the last command. @author: Roy Nielsen """ return self.returncode ############################################################################ def getNlogReturns(self): """ Getter for the retval, reterr & retcode of the last command. Will also log the values @author: Roy Nielsen """ self.logger.log(lp.INFO, "Output: " + str(self.output)) self.logger.log(lp.INFO, "Error: " + str(self.error)) self.logger.log(lp.INFO, "Return code: " + str(self.returncode)) return self.output, self.error, self.returncode ############################################################################ def getNprintReturns(self): """ Getter for the retval, reterr & retcode of the last command. Will also print the values @author: Roy Nielsen """ print "Output: " + str(self.output) print "Error: " + str(self.error) print "Return code: " + str(self.returncode) return self.output, self.error, self.returncode ############################################################################ def communicate(self) : """ Use the subprocess module to execute a command, returning the output of the command @author: Roy Nielsen """ if self.command: try: proc = Popen(self.command, stdout=PIPE, stderr=PIPE, shell=self.myshell) self.libc.sync() self.output, self.error = proc.communicate() self.libc.sync() except Exception, err : self.logger.log(lp.WARNING, "- Unexpected Exception: " + \ str(err) + " command: " + self.printcmd) self.logger.log(lp.WARNING, "stderr: " + str(self.error)) raise err else : self.logger.log(lp.DEBUG, self.printcmd + " Returned with error/returncode: " + str(proc.returncode)) proc.stdout.close() finally: