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...")
Beispiel #2
0
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 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...")
Beispiel #4
0
class RamDiskFactory(object):
    """
    Retrieve and OS specific ramdisk, and provide an interface to manage it.

    Keeps a reference to a list of ramdisks.  When calling getRamdisk(new), if
    "new" is true, the method will add the ramdisk to the list of ramdisks.

    @parameter message_level: Level of logging a person wishes to log at.
                              see logMessage in the log_message module.

    @method: getRamdisk: Will return either a new ramdisk, or make the
                         self.activeRamdisk the ramdisk with the name of the
                         passed in mountpoint (if found).  Otherwise, the
                         self.activeRamdisk is initialized to None.

    @method getModuleVersion: gets the version of this module.

    @method unmountActiveRamdisk: Unmounts the active ramdisk.

    @method unmountRamdisk: Unmounts the mountpoint that is passed in.

    @author: Roy Nielsen
    """
    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 getRamdisk(self, size=0, mountpoint="", ramdiskType=""):
        """
        Getter for the ramdisk instance.
        
        @var: ramdisks - a list of ramdisks this factory has created
        
        @param: size - size of the ramdisk to create. If zero, it looks for 
        @author: Roy Nielsen
        """
        if not ramdiskType in self.validRamdiskTypes:
            raise BadRamdiskTypeException("Not a valid ramdisk type")

        if size and mountpoint and ramdiskType:
            #####
            # Determine OS and ramdisk type, create ramdisk accordingly
            if self.myosfamily == "darwin":
                #####
                # Found MacOS
                from macRamdisk import RamDisk
                self.activeRamdisk = RamDisk(size, mountpoint, self.logger)

            elif self.myosfamily == "linux" and ramdiskType == "loop":
                #####
                # Found Linux with a loopback ramdisk request
                from linuxLoopRamdisk import RamDisk
                self.activeRamdisk = RamDisk(mountpoint, self.logger)

            elif self.myosfamily == "linux" and ramdiskType == "tmpfs":
                #####
                # Found Linux with a tmpfs ramdisk request.
                from linuxTmpfsRamdisk import RamDisk
                self.activeRamdisk = RamDisk(size, mountpoint, self.logger)

            else:
                #####
                # Bad method input parameters...
                self.activeRamdisk = None

            #####
            # Append the newly assigned self.activeRamdisk to the self.ramdisks
            # list
            self.ramdisks.append(self.activeRamdisk)

        elif not size and mountpoint:
            #####
            # Look for the ramdisk with "mountpoint" and return that instance.
            for ramdisk in self.ramdisks:
                if re.match("^%s$" % mountpoint, ramdisk.getMountPoint()):
                    self.activeRamdisk = ramdisk
                    break

        return self.activeRamdisk

    ############################################################################

    def getModuleVersion(self):
        """
        Getter for the version of this  module.

        @author: Roy Nielsen
        """
        return self.module_version

    ############################################################################

    def unmountActiveRamdisk(self):
        """
        Eject the currently active ramdisk in the Factory.

        @return: success - successful = True, unsuccessful = False

        @author: Roy Nielsen
        """
        success = False

        success = self.activeRamdisk.unmount(self.logger)

        return success

    ############################################################################

    def unmountRamdisk(self, mountpoint=""):
        """
        Eject the ramdisk in the list with the passed in mountpoint.

        @param mountpoint: the mountpoint to eject.

        @return: True if successful, False if not successful

        @author: Roy Nielsen
        """
        success = False
        if mountpoint:
            for ramdisk in self.ramdisks:
                if re.match("^%s$" % mountpoint, ramdisk.getMountPoint()):
                    self.activeRamdisk = ramdisk
                    success = self.unmountActiveRamdisk()
                    break

        return success
Beispiel #5
0
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...")