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
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 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 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 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...")
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()
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...")
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...")
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_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...")
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())
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 :param command: :param myshell: (Default value = False) ''' if command: self.command = command ##### # Handle Popen's shell, or "myshell"... if isinstance(self.command, list): self.printcmd = " ".join(self.command) if isinstance(self.command, str): 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 as 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: self.logger.log(lp.DEBUG, "Done with command: " + self.printcmd) self.returncode = str(proc.returncode) else: self.logger.log(lp.WARNING, "Cannot run a command that is empty...") self.output = None self.error = None self.returncode = None ############################################################################ def wait(self): '''Use subprocess to call a command and wait until it is finished before moving on... @author: Roy Nielsen ''' if self.command: try: proc = Popen(self.command, stdout=PIPE, stderr=PIPE, shell=self.myshell) proc.wait() for line in proc.stdout.readline(): self.output = self.output + line for line in proc.stderr.readline(): self.error = self.error + line except Exception as err: self.logger.log(lp.WARNING, "system_call_retval - Unexpected Exception: " + \ str(err) + " command: " + self.printcmd) raise err else: self.logger.log(lp.DEBUG, self.printcmd + \ " Returned with error/returncode: " + \ str(proc.returncode)) proc.stdout.close() finally: self.logger.log(lp.DEBUG, "Done with command: " + self.printcmd) self.output = proc.stdout self.error = proc.stderr self.returncode = str(proc.returncode) else: self.logger.log(lp.WARNING, "Cannot run a command that is empty...") self.stdout = None self.stderr = None self.returncode = None ############################################################################ def killProc(self, proc, timeout): '''Support function for the "runWithTimeout" function below @author: Roy Nielsen :param proc: :param timeout: ''' timeout["value"] = True proc.kill() ############################################################################ def timeout(self, timout_sec): '''Run a command with a timeout - return: :param timout_sec: :returns: stdout of the process stderr of the process timout - True if the command timed out False if the command completed successfully @author: Roy Nielsen ''' if self.command: try: proc = Popen(self.command, stdout=PIPE, stderr=PIPE, shell=self.myshell) timeout = {"value": False} timer = threading.Timer(timout_sec, self.killProc, [proc, timeout]) timer.start() self.output, self.error = proc.communicate() timer.cancel() self.returncode = proc.returncode except Exception as err: self.logger.log(lp.WARNING, "system_call_retval - Unexpected " + \ "Exception: " + str(err) + \ " command: " + self.printcmd) raise err else: self.logger.log(lp.DEBUG, self.printcmd + \ " Returned with error/returncode: " + \ str(proc.returncode)) proc.stdout.close() finally: self.logger.log(lp.DEBUG, "Done with command: " + self.printcmd) else: self.logger.log(lp.WARNING, "Cannot run a command that is empty...") self.output = None self.error = None self.returncode = None return timeout["value"] ############################################################################ def runAs(self, user="", password=""): '''Use pexpect to run "su" to run a command as another user... Required parameters: user, password, command @author: Roy Nielsen :param user: (Default value = "") :param password: (Default value = "") ''' if re.match("^\s*$", user) or \ re.match("^\s*$", password) or \ not self.command : self.logger.log(lp.WARNING, "Cannot pass in empty parameters...") self.logger.log(lp.WARNING, "user = \"" + str(user) + "\"") self.logger.log(lp.WARNING, "check password...") self.logger.log(lp.WARNING, "command = \"" + str(self.command) + "\"") return (255) else: output = "" internal_command = ["/usr/bin/su", "-", str(user), "-c"] if isinstance(self.command, list): internal_command.append(" ".join(self.command)) #log_message("Trying to execute: \"" + \ # " ".join(internal_command) + "\"", \ # "verbose", message_level) elif isinstance(self.command, str): internal_command.append(self.command) #log_message("Trying to execute: \"" + \ # str(internal_command) + "\"", \ # "verbose", message_level) (master, slave) = pty.openpty() proc = Popen(internal_command, stdin=slave, stdout=slave, stderr=slave, close_fds=True) prompt = os.read(master, 10) if re.match("^Password:"******"\n") line = os.read(master, 512) output = output + line while True: ##### # timeout of 0 means "poll" r, w, e = select.select([master], [], [], 0) if r: line = os.read(master, 512) ##### # Warning, uncomment at your own risk - several programs # print empty lines that will cause this to break and # the output will be all goofed up. #if not line : # break #print output.rstrip() output = output + line elif proc.poll() is not None: break os.close(master) os.close(slave) proc.wait() self.stdout = proc.stdout self.stderr = proc.stderr self.returncode = proc.returncode else: self.stdout = None self.stderr = None self.returncode = None #print output.strip() output = output.strip() #log_message("Leaving runAs with: \"" + str(output) + "\"", # "debug", message_level) return output ############################################################################ def getecho(self, fileDescriptor): '''This returns the terminal echo mode. This returns True if echo is on or False if echo is off. Child applications that are expecting you to enter a password often set ECHO False. See waitnoecho(). Borrowed from pexpect - acceptable to license :param fileDescriptor: ''' attr = termios.tcgetattr(fileDescriptor) if attr[3] & termios.ECHO: return True return False ############################################################################ def waitnoecho(self, fileDescriptor, timeout=3): '''This waits until the terminal ECHO flag is set False. This returns True if the echo mode is off. This returns False if the ECHO flag was not set False before the timeout. This can be used to detect when the child is waiting for a password. Usually a child application will turn off echo mode when it is waiting for the user to enter a password. For example, instead of expecting the "password:"******"", password=""): '''Use pty method to run "su" to run a command as another user... Required parameters: user, password, command @author: Roy Nielsen :param user: (Default value = "") :param password: (Default value = "") ''' self.logger.log(lp.DEBUG, "Starting runAsWithSudo: ") self.logger.log(lp.DEBUG, "\tuser: \"" + str(user) + "\"") self.logger.log(lp.DEBUG, "\tcmd : \"" + str(self.command) + "\"") if re.match("^\s+$", user) or re.match("^\s+$", password) or \ not user or not password or \ not self.command : self.logger.log(lp.WARNING, "Cannot pass in empty parameters...") self.logger.log(lp.WARNING, "user = \"" + str(user) + "\"") self.logger.log(lp.WARNING, "check password...") self.logger.log(lp.WARNING, "command = \"" + str(self.command) + "\"") return (255) else: output = "" internal_command = [ "/usr/bin/su", str("-"), str(user).strip(), str("-c") ] if isinstance(self.command, list): cmd = [] for i in range(len(self.command)): try: cmd.append(str(self.command[i].decode('utf-8'))) except UnicodeDecodeError: cmd.append(str(self.command[i])) internal_command.append(str("/usr/bin/sudo -E -S -s '" + \ " ".join(cmd) + "'")) #log_message("Trying to execute: \"" + \ # " ".join(internal_command) + "\"", \ # "verbose", message_level) #print "Trying to execute: \"" + " ".join(internal_command) + \ # "\"" elif isinstance(self.command, str): internal_command.append(str("/usr/bin/sudo -E -S -s " + \ "'" + \ str(self.command.decode('utf-8'))+ \ "'")) #log_message("Trying to execute: \"" + str(internal_command) + \ # "\"", "verbose", message_level) #print "Trying to execute: \"" + str(internal_command) + "\"" try: (master, slave) = pty.openpty() except Exception as err: self.logger.log(lp.WARNING, "Error trying to open pty: " + str(err)) raise err else: try: proc = Popen(internal_command, stdin=slave, stdout=slave, stderr=slave, close_fds=True) except Exception as err: self.logger.log(lp.WARNING, "Error opening process to pty: " + \ str(err)) raise err else: ##### # Catch the su password prompt # prompt = os.read(master, 512) self.waitnoecho(master, 3) prompt = os.read(master, 512) ##### # pass in the password os.write(master, password.strip() + "\n") ##### # catch the password prompt = os.read(master, 512) ##### # Wait for the next password prompt self.waitnoecho(master, 3) ##### # catch the password prompt prompt = os.read(master, 512) ##### # Enter the sudo password os.write(master, password + "\n") ##### # Catch the password os.read(master, 512) #output = tmp + output while True: ##### # timeout of 0 means "poll" r, w, e = select.select([master], [], [], 0) if r: line = os.read(master, 512) ##### # Warning, uncomment at your own risk - several # programs print empty lines that will cause this # to break and the output will be all goofed up. #if not line : # break #print output.rstrip() output = output + line elif proc.poll() is not None: break #print output.strip() os.close(master) os.close(slave) proc.wait() self.stdout = proc.stdout self.stderr = proc.stderr self.returncode = proc.returncode #print output.strip() #output = output.strip() ##### # UNCOMMENT ONLY WHEN IN DEVELOPMENT AND DEBUGGING OR YOU MAY REVEAL # MORE THAN YOU WANT TO IN THE LOGS!!! #log_message("\n\nLeaving runAs with Sudo: \"" + str(output) + \ # "\"\n\n", "debug", message_level) #print "\n\nLeaving runAs with Sudo: \"" + str(output) + "\"\n\n" return output
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 test_run_commands(unittest.TestCase): """ """ @classmethod 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() @classmethod def tearDownClass(self): """ """ pass def test_RunCommunicateWithBlankCommand(self): self.rw.__init__(self.logger) self.assertRaises(SetCommandTypeError, self.rw.setCommand, "") self.assertRaises(SetCommandTypeError, self.rw.setCommand, []) self.assertRaises(SetCommandTypeError, self.rw.setCommand, None) self.assertRaises(SetCommandTypeError, self.rw.setCommand, True) self.assertRaises(SetCommandTypeError, self.rw.setCommand, {}) def test_setCommand(self): self.rw.__init__(self.logger) command = ['/bin/ls', 1, '.'] self.assertRaises(SetCommandTypeError, self.rw.setCommand, [command]) def test_communicate(self): """ """ self.rw.__init__(self.logger) self.logger.log(lp.DEBUG, "=============== Starting test_communicate...") self.rw.setCommand('/bin/ls /var/spool', myshell=True) _, _, retval = self.rw.communicate(silent=False) self.assertEquals( retval, 0, "Valid [] command execution failed: " + '/bin/ls /var/spool --- retval: ' + str(retval)) self.rw.setCommand(['/bin/ls', '-l', '/usr/local']) _, _, retval = self.rw.communicate(silent=False) self.assertEquals( retval, 0, "Valid [] command execution failed: " + '/bin/ls /var/spool --- retval: ' + str(retval)) self.logger.log(lp.DEBUG, "=============== Ending test_communicate...") def test_wait(self): """ """ self.rw.__init__(self.logger) self.logger.log(lp.DEBUG, "=============== Starting test_wait...") self.rw.setCommand('/bin/ls /var/spool') _, _, retval = self.rw.communicate(silent=False) self.assertEquals( retval, 0, "Valid [] command execution failed: " + '/bin/ls /var/spool --- retval: ' + str(retval)) self.rw.setCommand(['/bin/ls', '-l', '/usr/local']) _, _, retval = self.rw.communicate(silent=False) self.assertEquals( retval, 0, "Valid [] command execution failed: " + '/bin/ls /var/spool --- retval: ' + str(retval)) self.rw.setCommand(['/bin/ls', '/1', '/']) _, _, retcode = self.rw.wait() self.logger.log(lp.WARNING, "retcode: " + str(retcode)) if sys.platform == 'darwin': self.assertEquals(retcode, 1, "Returncode Test failed...") else: self.assertEquals(retcode, 2, "Returncode Test failed...") def test_waitNpassThruStdout(self): """ """ self.rw.__init__(self.logger) self.rw.setCommand(['/bin/ls', '-l', '/usr/local']) _, _, retval = self.rw.waitNpassThruStdout() self.assertEquals( retval, 0, "Valid [] command execution failed: " + '/bin/ls /var/spool --- retval: ' + str(retval)) self.rw.setCommand(['/bin/ls', '/1', '/']) _, _, retval = self.rw.waitNpassThruStdout() if sys.platform == 'darwin': self.assertEquals(retval, 1, "Returncode Test failed...") else: self.assertEquals(retval, 2, "Returncode Test failed...") def test_timeout(self): """ """ self.rw.__init__(self.logger) if os.path.exists("/sbin/ping"): ping = "/sbin/ping" elif os.path.exists('/bin/ping'): ping = "/bin/ping" self.rw.setCommand([ping, '8.8.8.8']) startTime = time.time() self.rw.timeout(3) elapsed = (time.time() - startTime) self.assertTrue(elapsed < 4, "Elapsed time is greater than it should be...") def test_runAs(self): """ """ pass def test_liftDown(self): """ """ pass def test_runAsWithSudo(self): """ """ pass def test_runWithSudo(self): """ """ pass def test_getecho(self): """ """ pass def test_waitnoecho(self): """ """ pass def test_RunThread(self): """ """ pass def test_runMyThreadCommand(self): """ """ pass
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 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 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 :param fname: (Default value = "") :param message_level: (Default value = "normal") ''' 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 as err: self.logger.log(lp.WARNING, "Cannot open to touch: " + str(fname)) ################################################ def mkdirs(self, path=""): '''A function to do an equivalent of "mkdir -p" :param path: (Default value = "") ''' if not path: self.logger.log(lp.WARNING, "Bad path...") else: if not os.path.exists(str(path)): try: os.makedirs(str(path)) except OSError as err1: self.logger.log( lp.WARNING, "OSError exception attempting to create directory: " + str(path)) self.logger.log(lp.WARNING, "Exception: " + str(err1)) except Exception as err2: self.logger.log( lp.WARNING, "Unexpected Exception trying to makedirs: " + str(err2)) ################################################ def mkfile(self, file_path="", file_size=0, pattern="rand", block_size=512, mode=0o777): '''Create a file with "file_path" and "file_size". To be used in file creation benchmarking - filesystem vs ramdisk. :param eter: file_path - Full path to the file to create :param eter: file_size - Size of the file to create, in Mba :param eter: pattern - "rand": write a random pattern "0xXX": where XX is a hex value for a byte :param eter: block_size - size of blocks to write in bytes :param eter: mode - file mode, default 0o777 :param file_path: (Default value = "") :param file_size: (Default value = 0) :param pattern: (Default value = "rand") :param block_size: (Default value = 512) :param mode: (Default value = 0o777) :returns: s: time in miliseconds the write took @author: Roy Nielsen ''' total_time = 0 if file_path and file_size: self.libc.sync() file_size = file_size * 1024 * 1024 if os.path.isdir(file_path): tmpfile_path = os.path.join(file_path, "testfile") else: tmpfile_path = file_path self.logger.log(lp.DEBUG, "Writing to: " + tmpfile_path) try: # Get the number of blocks to create blocks = file_size / block_size # Start timer in miliseconds start_time = datetime.now() # do low level file access... tmpfile = os.open(tmpfile_path, os.O_WRONLY | os.O_CREAT, mode) # do file writes... for i in range(blocks): tmp_buffer = os.urandom(block_size) os.write(tmpfile, str(tmp_buffer)) os.fsync(tmpfile) self.libc.sync() os.close(tmpfile) self.libc.sync() os.unlink(tmpfile_path) self.libc.sync() # capture end time end_time = datetime.now() except Exception as err: self.logger.log(lp.WARNING, "Exception trying to write temp file for " + \ "benchmarking...") self.logger.log(lp.WARNING, "Exception thrown: " + str(err)) total_time = 0 else: total_time = end_time - start_time return total_time
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 = "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() @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.umount(): 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 ##### # 500Mb file size fiveHundred = 500 ##### # 800Mb file size eightHundred = 800 ##### # 1Gb file size oneGig = 1000 my_fs_array = [oneHundred, fiveHundred, eightHundred, 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...") assert_message = "Problem with " + str(file_size) + "mb ramdisk..." self.logger.log(lp.DEBUG, assert_message) self.logger.log( lp.INFO, "Smaller file sizes will fail this test on systems with SSD's..." ) self.assertTrue((fs_time - ram_time).days > -1, assert_message) ################################## 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() try: umount(self.mount) self.logger.log(lp.INFO, r"Successfully detached disk: " + \ str(self.my_ramdisk.mntPoint).strip()) except Exception: message = r"Couldn't detach disk: " + \ str(self.my_ramdisk.myRamdiskDev).strip() + \ " : mntpnt: " + str(self.my_ramdisk.mntPoint) ex_message = message + "\n" + traceback.format_exc() raise Exception(ex_message) ##### # 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...")
# 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 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:
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)
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...")
##### # ... 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(filename="ramdiskTestLog") logger.log(lp.DEBUG, "Modules: " + str(modules)) ##### # ... processing test prefixes if options.prefix: prefix = options.prefix else: prefix = ["test_", "Test_"] bars = BuildAndRunSuite(logger) bars.setPrefix(prefix) bars.run_suite(modules)
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 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_", "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) for item in self.prefix: if re.match("^%s.+\.py$"%item, 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)) try: ################################################ # 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)) except AttributeError, err: pass ##### # calll the run_action to execute the test suite self.run_action()
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()
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 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
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())