Changeset 29227 for branches/sc_branches/psps_testing/testers/batch_file.py
- Timestamp:
- Sep 23, 2010, 3:38:03 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/sc_branches/psps_testing/testers/batch_file.py
r29116 r29227 1 # Strings managed as files2 from StringIO import StringIO3 1 # Regular expressions 4 2 import re … … 7 5 import os 8 6 from utilities.file_manipulation import FileManipulation 7 from utilities.test_report import TestReport 8 from utilities.test_product import TestProduct 9 # Sub-tests 9 10 from testers.batch_manifest_file import BatchManifestFileTester 10 11 from testers.fits_file import FitsFileTester … … 13 14 # Global definitions 14 15 from utilities.configuration import Configuration 16 # Logging 17 from utilities.psps_logger import PsPsLogger 15 18 16 19 class BatchFileTester: … … 22 25 of the files contained in the archive 23 26 24 >>> print BatchFileTester("data/psut/ok/B000 29152.tar.gz").test()27 >>> print BatchFileTester("data/psut/ok/B00000010.tar.gz").test() 25 28 PSDC-940-006-01, 3.4.1: OK 26 29 PSDC-940-006-01, 3.4: OK … … 42 45 dummy/ 43 46 """ 47 PsPsLogger.debug('Creating BatchFileTester object with filename = [%s]' % filename) 48 TestReport.info('Tests performed on file [%s]' % filename) 44 49 self._filename = filename 45 50 self.expected_basename = os.path.basename(self._filename 46 51 ).replace(Configuration.EXTENSION, 47 52 '') + '/' 53 PsPsLogger.debug(' BatchFileTester object expected basename = [%s]' 54 % self.expected_basename) 48 55 49 56 def test(self, start_dependant_tests = True): … … 52 59 tests are succesful. 53 60 """ 54 report = StringIO() 55 testsOk = True 56 (message, status) = self._test_file_name("PSDC-940-006-01, 3.4.1") 57 report.write(message) 58 testsOk = testsOk and status 59 (message, status) = self._test_file_format("PSDC-940-006-01, 3.4") 60 report.write('\n' + message) 61 testsOk = testsOk and status 62 # Now runs the dependant tests 63 if testsOk and start_dependant_tests: 64 # Uncompress the archive (since it's valid) to some temp dir 61 PsPsLogger.debug('Starting BatchFileTester object test') 62 TestReport.title('Batch file tests') 63 # Set up global test product 64 product = TestProduct() 65 product.success = True 66 # Subtest 67 subproduct = self._test_file_name("PSDC-940-006-01, 3.4.1") 68 product.success = product.success and subproduct.success 69 # Subtest 70 subproduct = self._test_file_format("PSDC-940-006-01, 3.4") 71 product.success = product.success and subproduct.success 72 # Now runs the dependent tests 73 if product.success and start_dependant_tests: 74 # Uncompress the archive (since it is valid) to some temp dir 65 75 tempDir = tempfile.mkdtemp() 66 76 command = '/bin/tar xvfz %s -C %s' % (self._filename, tempDir) 77 PsPsLogger.debug('Unpacking data (%s)' % command) 67 78 (status, 68 79 child_stdout, … … 71 82 batchManifestFileTester = BatchManifestFileTester(self.expected_basename, 72 83 tempDir) 73 (_report, fits_info) = batchManifestFileTester.test() 74 report.write('\n' + _report) 75 # Run the tests on the fits file 76 fitsFileTester = FitsFileTester(fits_info['filename'], 77 self.expected_basename, tempDir, 78 fits_info['filetype']) 79 (_report, info) = fitsFileTester.test() 80 report.write('\n' + _report) 84 subproduct = batchManifestFileTester.test() 85 product.success = product.success and subproduct.success 86 if not product.success: 87 FileManipulation.delete_directory(tempDir) 88 return product 89 # Run the tests on the fits file (subproduct.filename and 90 # subproduct.filetype are supposed to convey such 91 # information) 92 fitsFileTester = FitsFileTester(subproduct.filename, 93 self.expected_basename, 94 tempDir, 95 subproduct.filetype) 96 subproduct = fitsFileTester.test() 97 product.success = product.success and subproduct.success 81 98 # End of test... Delete the temporary directory 82 99 FileManipulation.delete_directory(tempDir) 83 return report.getvalue() 100 else: 101 PsPsLogger.debug('Dependant tests not run (condition on upstream tests is %s and start_dependant_tests variable is %s)' % (str(testsOk), str(start_dependant_tests))) 102 return product 84 103 85 104 def _test_file_name(self, requirement): … … 94 113 95 114 >>> batchFileTester = BatchFileTester('B12345678.tar.gz') 96 >>> print batchFileTester._test_file_name(" REQ_ID")97 ('REQ_ID: OK', True)115 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 116 True 98 117 99 118 >>> batchFileTester = BatchFileTester('B1234567.tar.gz') 100 >>> print batchFileTester._test_file_name(" REQ_ID")101 ("REQ_ID: KO (invalid archive file basename 'B1234567.tar.gz')", False)119 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 120 False 102 121 103 122 >>> batchFileTester = BatchFileTester('B123456789.tar.gz') 104 >>> print batchFileTester._test_file_name(" REQ_ID")105 ("REQ_ID: KO (invalid archive file basename 'B123456789.tar.gz')", False)123 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 124 False 106 125 107 126 >>> batchFileTester = BatchFileTester('B123a5678.tar.gz') 108 >>> print batchFileTester._test_file_name(" REQ_ID")109 ("REQ_ID: KO (invalid archive file basename 'B123a5678.tar.gz')", False)127 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 128 False 110 129 111 130 >>> batchFileTester = BatchFileTester('b12345678.tar.gz') 112 >>> print batchFileTester._test_file_name(" REQ_ID")113 ("REQ_ID: KO (invalid archive file basename 'b12345678.tar.gz')", False)131 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 132 False 114 133 115 134 >>> batchFileTester = BatchFileTester('B12345678.tgz') 116 >>> print batchFileTester._test_file_name(" REQ_ID")117 ("REQ_ID: KO (invalid archive file basename 'B12345678.tgz')", False)135 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 136 False 118 137 119 138 >>> batchFileTester = BatchFileTester('B12345678.TAR.GZ') 120 >>> print batchFileTester._test_file_name(" REQ_ID")121 ("REQ_ID: KO (invalid archive file basename 'B12345678.TAR.GZ')", False)122 """ 123 report = StringIO()139 >>> print batchFileTester._test_file_name("(UNIT_TEST_SAMPLE_REQ)").success 140 False 141 """ 142 PsPsLogger.debug('Running tests on file format') 124 143 basename = os.path.basename(self._filename) 144 product = TestProduct() 125 145 if re.match('B\d{8}\.tar.gz', basename) == None: 126 return ('%s: KO (invalid archive file basename \'%s\')' % (requirement, basename), False) 146 TestReport.ko(requirement, 147 'invalid archive file basename \'%s\'' % basename ) 127 148 else: 128 report.write('%s: OK' % requirement) 129 return (report.getvalue(), True) 149 TestReport.ok(requirement) 150 product.success = True 151 return product 130 152 131 153 def _test_file_format(self, requirement): … … 152 174 (named BatchManifest.xml) and exactly one FITS file. 153 175 154 >>> batchFileTester = BatchFileTester('data/psut/ok/B000 29152.tar.gz')155 >>> print batchFileTester._test_file_format(" REQ_ID")156 ('REQ_ID: OK', True)176 >>> batchFileTester = BatchFileTester('data/psut/ok/B00000010.tar.gz') 177 >>> print batchFileTester._test_file_format("(UNIT_TEST_SAMPLE_REQ)").success 178 True 157 179 158 180 >>> batchFileTester = BatchFileTester('This_file_does_not_exist.tar.gz') 159 >>> print batchFileTester._test_file_format(" REQ_ID")160 ("REQ_ID: KO (invalid tar gzipped archive file 'This_file_does_not_exist.tar.gz')", False)181 >>> print batchFileTester._test_file_format("(UNIT_TEST_SAMPLE_REQ)").success 182 False 161 183 162 184 >>> batchFileTester = BatchFileTester('data/psut/ko/B00000001.tar.gz') 163 >>> print batchFileTester._test_file_format(" REQ_ID")164 ("REQ_ID: KO (invalid directory name in archive 'data/psut/ko/B00000001.tar.gz': Got tmp/ instead of B00000001/)", False)185 >>> print batchFileTester._test_file_format("(UNIT_TEST_SAMPLE_REQ)").success 186 False 165 187 166 188 >>> batchFileTester = BatchFileTester('data/psut/ko/B00000002.tar.gz') 167 >>> print batchFileTester._test_file_format(" REQ_ID")168 ("REQ_ID: KO (missing XML Manifest file in archive 'data/psut/ko/B00000002.tar.gz')", False)189 >>> print batchFileTester._test_file_format("(UNIT_TEST_SAMPLE_REQ)").success 190 False 169 191 170 192 >>> batchFileTester = BatchFileTester('data/psut/ko/B00000003.tar.gz') 171 >>> print batchFileTester._test_file_format(" REQ_ID")172 ("REQ_ID: KO (missing FITS file in archive 'data/psut/ko/B00000003.tar.gz')", False)193 >>> print batchFileTester._test_file_format("(UNIT_TEST_SAMPLE_REQ)").success 194 False 173 195 174 196 >>> batchFileTester = BatchFileTester('data/psut/ko/B00000004.tar.gz') 175 >>> print batchFileTester._test_file_format("REQ_ID") 176 ("REQ_ID: KO (unexpected file 'B00000004/extra' in archive 'data/psut/ko/B00000004.tar.gz')", False) 177 """ 197 >>> print batchFileTester._test_file_format("(UNIT_TEST_SAMPLE_REQ)").success 198 False 199 """ 200 PsPsLogger.debug('Running tests on file format') 178 201 (status, 179 202 child_stdout, 180 203 child_stderr) = FileManipulation.system('/bin/tar tfz ' + self._filename) 204 product = TestProduct() 181 205 # Test 1: if the archive is not valid 182 206 if status != 0: 183 return (requirement + ': KO (invalid tar gzipped archive file \'%s\')' % self._filename, False) 207 TestReport.ko(requirement, 208 'invalid tar gzipped archive file \'%s\'' % self._filename) 209 return product 184 210 # Now check the results of the standard output which must be: 185 211 # <basename>/ … … 199 225 if status == 0: 200 226 if line != self.expected_basename: 201 return (requirement + ': KO (invalid directory name in archive \'%s\': Got %s instead of %s)' % (self._filename, line, self.expected_basename), False) 227 TestReport.ko(requirement, 228 'invalid directory name in archive \'%s\': Got %s instead of %s' 229 % (self._filename, line, self.expected_basename)) 230 return product 202 231 status = 1 203 232 elif status == 1: … … 207 236 xml_file_in_archive = True 208 237 else: 209 return (requirement + ': KO (unexpected file \'%s\' in archive \'%s\')' % (line, self._filename), False) 238 TestReport.ko(requirement, 239 'unexpected file \'%s\' in archive \'%s\'' % (line, self._filename)) 240 return product 210 241 if not xml_file_in_archive: 211 return (requirement + ': KO (missing XML Manifest file in archive \'%s\')' % (self._filename), False) 242 TestReport.ko(requirement, 243 'missing XML Manifest file in archive \'%s\'' % (self._filename)) 244 return product 212 245 if not fits_file_in_archive: 213 return (requirement + ': KO (missing FITS file in archive \'%s\')' % (self._filename), False) 214 return (requirement + ': OK', True) 215 246 TestReport.ko(requirement, 247 'missing FITS file in archive \'%s\'' % (self._filename)) 248 return product 249 product.success = True 250 return product 251 252 ################################################################ 253 # 254 # __main__ 255 # 256 ################################################################ 216 257 if __name__ == '__main__': 217 import doctest 218 doctest.testmod() 258 import logging 259 import sys 260 PsPsLogger.setLevel(logging.DEBUG) 261 current_argument_position = 1 262 while current_argument_position < len(sys.argv): 263 if sys.argv[current_argument_position] == '-v': 264 PsPsLogger.set_stderr() 265 current_argument_position += 1 266 elif sys.argv[current_argument_position] == 'test': 267 print 'Running unittest for BatchFileTester class' 268 import doctest 269 doctest.testmod() 270 sys.exit(0)
Note:
See TracChangeset
for help on using the changeset viewer.
