def _verify_size(self, unit, report): """ Verifies the size of the given unit if the sync is configured to do so. If the verification fails, the error is noted in this instance's progress report and the error is re-raised. :param unit: domain model instance of the package that was downloaded :type unit: pulp_rpm.plugins.db.models.RpmBase :param report: report handed to this listener by the downloader :type report: nectar.report.DownloadReport :raises verification.VerificationException: if the size of the content is incorrect """ if not self.sync.config.get(importer_constants.KEY_VALIDATE): return try: with open(report.destination) as fp: verification.verify_size(fp, unit.size) except verification.VerificationException, e: error_report = { constants.UNIT_KEY: unit.unit_key, constants.ERROR_CODE: constants.ERROR_SIZE_VERIFICATION, constants.ERROR_KEY_EXPECTED_SIZE: unit.size, constants.ERROR_KEY_ACTUAL_SIZE: e[0] } self.sync.progress_report['content'].failure(unit, error_report) raise
def test_size(self): # Setup expected_size = 9 test_file = StringIO('Test data') # Test - Should not raise an exception verification.verify_size(test_file, expected_size)
def _verify_size(self, model, report): """ Verifies the size of the given unit if the sync is configured to do so. If the verification fails, the error is noted in this instance's progress report and the error is re-raised. :param model: domain model instance of the package that was downloaded :type model: pulp_rpm.common.models.RPM :param report: report handed to this listener by the downloader :type report: nectar.report.DownloadReport :raises verification.VerificationException: if the size of the content is incorrect """ if not self.sync_call_config.get(importer_constants.KEY_VALIDATE): return try: with open(report.destination) as dest_file: verification.verify_size(dest_file, model.metadata['size']) except verification.VerificationException, e: error_report = { constants.UNIT_KEY: model.unit_key, constants.ERROR_CODE: constants.ERROR_SIZE_VERIFICATION, constants.ERROR_KEY_EXPECTED_SIZE: model.metadata['size'], constants.ERROR_KEY_ACTUAL_SIZE: e[0] } self.progress_report['content'].failure(model, error_report) raise
def verify_size(self, location): """ Verify size of the file at the specified location :param location: path to the unit location :type location: str """ if self.size is not None: with open(location) as fp: verification.verify_size(fp, self.size)