IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 23, 2010, 3:38:03 PM (16 years ago)
Author:
Serge CHASTEL
Message:

Test report / product

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/sc_branches/psps_testing/testers/batch_file.py

    r29116 r29227  
    1 # Strings managed as files
    2 from StringIO import StringIO
    31# Regular expressions
    42import re
     
    75import os
    86from utilities.file_manipulation import FileManipulation
     7from utilities.test_report import TestReport
     8from utilities.test_product import TestProduct
     9# Sub-tests
    910from testers.batch_manifest_file import BatchManifestFileTester
    1011from testers.fits_file import FitsFileTester
     
    1314# Global definitions
    1415from utilities.configuration import Configuration
     16# Logging
     17from utilities.psps_logger import PsPsLogger
    1518
    1619class BatchFileTester:
     
    2225      of the files contained in the archive
    2326
    24     >>> print BatchFileTester("data/psut/ok/B00029152.tar.gz").test()
     27    >>> print BatchFileTester("data/psut/ok/B00000010.tar.gz").test()
    2528    PSDC-940-006-01, 3.4.1: OK
    2629    PSDC-940-006-01, 3.4: OK
     
    4245        dummy/
    4346        """
     47        PsPsLogger.debug('Creating BatchFileTester object with filename = [%s]' % filename)
     48        TestReport.info('Tests performed on file [%s]' % filename)
    4449        self._filename = filename
    4550        self.expected_basename = os.path.basename(self._filename
    4651                                                  ).replace(Configuration.EXTENSION,
    4752                                                            '') + '/'
     53        PsPsLogger.debug(' BatchFileTester object expected basename = [%s]'
     54                           % self.expected_basename)
    4855
    4956    def test(self, start_dependant_tests = True):
     
    5259        tests are succesful.
    5360        """
    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
    6575            tempDir = tempfile.mkdtemp()
    6676            command = '/bin/tar xvfz %s -C %s' % (self._filename, tempDir)
     77            PsPsLogger.debug('Unpacking data (%s)' % command)
    6778            (status,
    6879             child_stdout,
     
    7182            batchManifestFileTester = BatchManifestFileTester(self.expected_basename,
    7283                                                              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
    8198            # End of test... Delete the temporary directory
    8299            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
    84103
    85104    def _test_file_name(self, requirement):
     
    94113
    95114        >>> 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
    98117
    99118        >>> 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
    102121
    103122        >>> 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
    106125
    107126        >>> 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
    110129
    111130        >>> 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
    114133
    115134        >>> 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
    118137       
    119138        >>> 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')
    124143        basename = os.path.basename(self._filename)
     144        product = TestProduct()
    125145        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 )
    127148        else:
    128             report.write('%s: OK' % requirement)
    129         return (report.getvalue(), True)
     149            TestReport.ok(requirement)
     150            product.success = True
     151        return product
    130152
    131153    def _test_file_format(self, requirement):
     
    152174              (named BatchManifest.xml) and exactly one FITS file.
    153175
    154         >>> batchFileTester = BatchFileTester('data/psut/ok/B00029152.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
    157179
    158180        >>> 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
    161183
    162184        >>> 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
    165187
    166188        >>> 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
    169191
    170192        >>> 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
    173195
    174196        >>> 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')
    178201        (status,
    179202         child_stdout,
    180203         child_stderr) = FileManipulation.system('/bin/tar tfz ' + self._filename)
     204        product = TestProduct()
    181205        # Test 1: if the archive is not valid
    182206        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
    184210        # Now check the results of the standard output which must be:
    185211        #   <basename>/
     
    199225            if status == 0:
    200226                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
    202231                status = 1
    203232            elif status == 1:
     
    207236                    xml_file_in_archive = True
    208237                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
    210241        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
    212245        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################################################################
    216257if __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.