def test_files_object_element(self): xmlfile = os.path.join(self.datadir, "test.xml") with open(xmlfile, 'w') as xml: xml.write('''<?xml version="1.0" encoding="UTF-8" ?> <root xmlns:xlink="http://www.w3.org/1999/xlink"> <object> <storage> <contentLocation> <contentLocationValue>file:///1.txt</contentLocationValue> </contentLocation> </storage> </object> <object> <storage> <contentLocation> <contentLocationValue>file:///2.txt</contentLocationValue> </contentLocation> </storage> </object> </root> ''') found = find_file('1.txt', xmlfile=xmlfile, rootdir=self.datadir) self.assertIsNotNone(found) found = find_file('2.txt', xmlfile=xmlfile, rootdir=self.datadir) self.assertIsNotNone(found) found = find_file('3.txt', xmlfile=xmlfile, rootdir=self.datadir) self.assertIsNone(found)
def test_files_file_element(self): xmlfile = os.path.join(self.datadir, "test.xml") with open(xmlfile, 'w') as xml: xml.write('''<?xml version="1.0" encoding="UTF-8" ?> <root xmlns:xlink="http://www.w3.org/1999/xlink"> <file><FLocat href="file:///1.txt"/></file> <file><FLocat href="file:2.txt"/></file> <file><FLocat href="3.txt"/></file> </root> ''') found = find_file('1.txt', xmlfile=xmlfile, rootdir=self.datadir) self.assertIsNotNone(found) found = find_file('2.txt', xmlfile=xmlfile, rootdir=self.datadir) self.assertIsNotNone(found) found = find_file('4.txt', xmlfile=xmlfile, rootdir=self.datadir) self.assertIsNone(found)
def validate(self, filepath, expected=None): logger.debug('Validating checksum of %s' % filepath) val_obj = Validation.objects.create( filename=filepath, time_started=timezone.now(), validator=self.__class__.__name__, required=self.required, task=self.task, information_package=self.ip, responsible=self.responsible, specification={ 'context': self.context, 'options': self.options, } ) expected = self.options['expected'].format(**self.data) if self.context == 'checksum_str': checksum = expected.lower() elif self.context == 'checksum_file': with open(expected, 'r') as checksum_file: checksum = checksum_file.read().strip() elif self.context == 'xml_file': xml_el, _ = find_file(filepath, xmlfile=expected) checksum = xml_el.checksum passed = False try: actual_checksum = calculate_checksum(filepath, algorithm=self.algorithm, block_size=self.block_size) if actual_checksum != checksum: raise ValidationError("checksum for %s is not valid (%s != %s)" % ( filepath, checksum, actual_checksum )) passed = True except Exception: val_obj.message = traceback.format_exc() raise else: message = 'Successfully validated checksum of %s' % filepath val_obj.message = message logger.info(message) finally: val_obj.time_done = timezone.now() val_obj.passed = passed val_obj.save(update_fields=['time_done', 'passed', 'message'])