testFileTools.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2005
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "File Upload test - VSC version"
00012 __author__   = "S. Tuvi <Stuvi@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "2005/07/15 00:08:27"
00014 __updated__  = "$Date: 2006/03/08 00:40:22 $"
00015 __version__  = "$Revision: 1.6 $"
00016 __release__  = "$Name: HEAD $"
00017 __credits__  = "SLAC"
00018 
00019 import LICOS.copyright_SLAC
00020 
00021 import logging as log
00022 import            os
00023 import            sys
00024 
00025 from LICOS.tests.lib.testAppBase  import TestAppBase
00026 #import LICOS.tests.lib.fileTools  as fileTools
00027 import LICOS.lib.cmdTlmDb.FILE    as FILE
00028 from LICOS.lib.LATconstants       import *
00029 
00030 #  Example application implementation.
00031 class testFileTools(TestAppBase):
00032   "Test file and memory tools"
00033   def __init__(self, common):
00034     TestAppBase.__init__(self, common)
00035     log.debug("%s.__init__()" % self.getName())
00036 
00037   def getName(self):
00038     return self.__class__.__name__
00039 
00040   def testStartRun(self):
00041     self.setCompletionStatus(self.COMPL_STATUS_PASSED)
00042 
00043     self.__diskFile = os.path.join(ONLINE_ROOT, 'LICOS/tests/data/12346_hdr')
00044     self.__noHeader = os.path.join(ONLINE_ROOT, 'LICOS/tests/data/12346')
00045     self.__tempFile = os.path.expandvars('/tmp/licos_download_delete')
00046 
00047     self.__testDirectory = 37
00048     self.__testDirectory = 2
00049     self.__testDevice    = FSW_DEVICE_RAM
00050     self.__testDevice    = FSW_DEVICE_EE1
00051 
00052     # compute a semi-arbitrary file ID.  Directory/fileNum up to user.
00053     self.__testID   = FILE.FswFileID( device     = self.__testDevice,
00054                                       directory  = self.__testDirectory,
00055                                       fileNumber = 12346)
00056 
00057     self.__testID2  = FILE.FswFileID( device     = self.__testDevice,
00058                                       directory  = self.__testDirectory,
00059                                       fileNumber = 33333)
00060 
00061     # directory file IDs have filenumper=0
00062     self.__testDir  = FILE.FswFileID( device     = self.__testDevice,
00063                                       directory  = self.__testDirectory,
00064                                       fileNumber = 0)
00065 
00066     # root ids have directory=127
00067     self.__rootID   = FILE.FswFileID( device     = self.__testDevice,
00068                                       directory  = FSW_DIR_ROOT,
00069                                       fileNumber = 0)
00070 
00071     # build compressed version of self.__diskFile in memory
00072     tmp = FILE.FswFileFromDisk(self.__testID2, self.__noHeader)
00073     self.__zipFile  = FILE.addFmxHeader(tmp, doCompress=True)
00074 
00075     self.lcat.setCmdConfirmCallback(self.lcat.quietConfirmation)
00076 
00077     pass
00078 
00079   def testRun(self):
00080 
00081     status  = 0
00082 
00083 #    log.info("%s: Testing creation of a directory on the RAM disk" % \
00084 #              ( self.getName() ) )
00085 #    status |= self.testCreateDirectory()
00086 #
00087     log.info("%s: Testing dump of RAM directory" % \
00088               ( self.getName() ) )
00089     status |= self.testDirectory()
00090 
00091 #    log.info("%s: Testing upload of disk file %s to RAM" % \
00092 #              ( self.getName(), self.__diskFile ) )
00093 #    status |= self.testFileUpload()
00094 #
00095 #    log.info("%s: Testing download of file" % \
00096 #              ( self.getName() ) )
00097 #    status |= self.testFileDownload()
00098 #
00099 #    log.info("%s: Testing copy of file" % \
00100 #              ( self.getName() ) )
00101 #    status |= self.testFileCopy()
00102 #
00103 #    log.info("%s: Testing dump of RAM directory" % \
00104 #              ( self.getName() ) )
00105 #    status |= self.testDirectory()
00106 
00107     #log.info("%s: Testing deletion of RAM directory" % \
00108     #          ( self.getName() ) )
00109     #status |= self.testDeleteDirectory()
00110 
00111 #    log.info("%s: Testing dump of RAM directory" % \
00112 #              ( self.getName() ) )
00113 #    status |= self.testDirectory()
00114 
00115 
00116     self.setCompletionStatus(self.COMPL_STATUS_PASSED)
00117     # End of run
00118 
00119   def testStopRun(self):
00120     pass
00121 
00122 
00123   def testFileUpload(self):
00124     """! Test the bare file upload from disk.
00125 
00126     \return Status
00127     """
00128     try:
00129 
00130       uplFile = self.lcat.FILE.uploadFileFromDisk(fileName = self.__diskFile,
00131                                                   unit     = FSW_UNIT_SIU,
00132                                                   fileID   = self.__testID)
00133 
00134       if uplFile is None:
00135         return self.COMPL_STATUS_FAILED
00136 
00137       uplFile = self.lcat.FILE.uploadFileFromData(fileData = self.__zipFile.binary(),
00138                                                   unit     = FSW_UNIT_SIU,
00139                                                   fileID   = self.__zipFile.fileID())
00140 
00141       if uplFile is None:
00142         return self.COMPL_STATUS_FAILED
00143 
00144       return self.COMPL_STATUS_PASSED
00145     except:
00146       log.exception("Exception in testFileUpload")
00147       return self.COMPL_STATUS_FAILED
00148 
00149   def testFileDownload(self):
00150     """!\brief test the bare file download system
00151 
00152     \return status
00153     """
00154     try:
00155       fswFile = self.lcat.FILE.downloadFile(unit     = FSW_UNIT_SIU,
00156                                             fileID   = self.__testID)
00157 
00158       f = open(self.__tempFile, 'wb')
00159       fswFile.write(f)
00160       f.close()
00161 
00162       return self.COMPL_STATUS_PASSED
00163     except:
00164       log.exception("Exception in testFileDownload")
00165       return self.COMPL_STATUS_FAILED
00166 
00167   def testDirectory(self):
00168     """!\brief test the directory structure
00169 
00170     \return status
00171     """
00172     try:
00173 
00174       # Two types of directory lists.
00175       # 1: list of the files in a directory
00176       directory = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00177                                                 directoryID = self.__testID)
00178 
00179       directory.dump(sys.stdout)
00180 
00181       # 2: list of the directories on a unit (on a device)
00182       directory2 = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00183                                                  directoryID = self.__rootID,
00184                                                  root = True)
00185 
00186       directory2.dump(sys.stdout)
00187 
00188       return self.COMPL_STATUS_PASSED
00189     except:
00190       log.exception("Exception in testDirectory")
00191       return self.COMPL_STATUS_FAILED
00192 
00193   def testDeleteDirectory(self):
00194     """!\brief test deleting a directory structure
00195 
00196     \return status
00197     """
00198 
00199     try:
00200       d0 = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00201                                          directoryID = self.__rootID,
00202                                          root = True)
00203 
00204       # deleting a directory deletes the files in it if force is set.
00205       status = self.lcat.FILE.deleteDirectory( unit = FSW_UNIT_SIU,
00206                                                directoryID = self.__testDir,
00207                                                force = False)
00208 
00209       status = self.lcat.FILE.deleteDirectory( unit = FSW_UNIT_SIU,
00210                                                directoryID = self.__testDir,
00211                                                force = True)
00212 
00213       d1 = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00214                                          directoryID = self.__rootID,
00215                                          root = True)
00216 
00217       if len(d0.files()) <= len(d1.files()):
00218         log.error("directory deletion didn't work.  Check any previous warnings")
00219         return self.COMPL_STATUS_FAILED
00220 
00221       return self.COMPL_STATUS_PASSED
00222     except:
00223       log.exception("Exception in testDeleteDirectory")
00224       return self.COMPL_STATUS_FAILED
00225 
00226   def testCreateDirectory(self):
00227     """!\brief test deleting a directory structure
00228 
00229     \return status
00230     """
00231     try:
00232       oldRoot = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00233                                               directoryID = self.__rootID,
00234                                               root = True)
00235       oldRoot.dump(sys.stdout)
00236 
00237       status = self.lcat.FILE.createDirectory(unit =FSW_UNIT_SIU,
00238                                               directoryID = self.__testDir)
00239 
00240       newRoot = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00241                                               directoryID = self.__rootID,
00242                                               root = True)
00243       newRoot.dump(sys.stdout)
00244 
00245       if len(oldRoot.files()) < len(newRoot.files()):
00246         return self.COMPL_STATUS_PASSED
00247       else:
00248         log.error( "old root directory had %d directories, new has %d" % \
00249                    (len(oldRoot.files()), len(newRoot.files())) )
00250 
00251         return self.COMPL_STATUS_FAILED
00252 
00253     except:
00254       log.exception("Exception in testDeleteDirectory")
00255       return self.COMPL_STATUS_FAILED
00256 
00257   def testFileCopy(self):
00258     """!\brief test copying a file
00259 
00260     \return status
00261     """
00262     try:
00263       newID = FILE.FswFileID( device     = self.__testDevice,
00264                               directory  = self.__testDirectory,
00265                               fileNumber = 654321)
00266 
00267       d0 = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00268                                          directoryID = self.__testID)
00269 
00270       status = self.lcat.FILE.copyFile( unit = FSW_UNIT_SIU,
00271                                         source = self.__testID,
00272                                         dest = newID)
00273 
00274       d1 = self.lcat.FILE.dumpDirectory( unit = FSW_UNIT_SIU,
00275                                          directoryID = newID)
00276 
00277 
00278       print "old directory had %d files, new has %d" % \
00279             (len(d0.files()), len(d1.files()))
00280 
00281 
00282       return self.COMPL_STATUS_PASSED
00283     except:
00284       log.exception("Exception in testDeleteDirectory")
00285       return self.COMPL_STATUS_FAILED
00286 
00287 

Generated on Thu Apr 27 20:52:44 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO