def ValidateLogicalPhysicalRepresentation(self, path, xmlfile, skip_files=None, relpath=None): """ Validates the logical and physical representation of objects. """ Validation.objects.filter(task=self.get_processtask()).delete() path, xmlfile, = self.parse_params(path, xmlfile) if skip_files is None: skip_files = [] else: skip_files = self.parse_params(*skip_files) if relpath is not None: rootdir, = self.parse_params(relpath) or path else: if os.path.isdir(path): rootdir = path else: rootdir = os.path.dirname(path) ip = InformationPackage.objects.get(pk=self.ip) validator = DiffCheckValidator(context=xmlfile, exclude=skip_files, options={'rootdir': rootdir}, task=self.get_processtask(), ip=self.ip, responsible=ip.responsible) validator.validate(path) msg = "Successfully validated logical and physical structure of {path} against {xml}".format( path=path, xml=xmlfile ) self.create_success_event(msg)
def run(self, path, xmlfile, skip_files=None, relpath=None): Validation.objects.filter(task=self.task_id).delete() path, xmlfile, = self.parse_params(path, xmlfile) if skip_files is None: skip_files = [] else: skip_files = self.parse_params(*skip_files) if relpath is not None: rootdir, = self.parse_params(relpath) or path else: if os.path.isdir(path): rootdir = path else: rootdir = os.path.dirname(path) ip = InformationPackage.objects.get(pk=self.ip) validator = DiffCheckValidator(context=xmlfile, exclude=skip_files, options={'rootdir': rootdir}, task=self.task_id, ip=self.ip, responsible=ip.responsible) validator.validate(path)
class DiffCheckValidatorTests(TestCase): @classmethod def setUpClass(cls): cls.generator = XMLGenerator() @classmethod def tearDownClass(cls): Path.objects.all().delete() def setUp(self): self.root = os.path.dirname(os.path.realpath(__file__)) self.datadir = os.path.join(self.root, "datadir") self.fname = os.path.join(self.datadir, 'test1.xml') self.options = {'rootdir': self.datadir} self.filesToCreate = { self.fname: { 'data': {}, 'spec': { '-name': 'root', '-children': [ { "-name": "file", "-containsFiles": True, "-attr": [ { "-name": "MIMETYPE", "#content": "{{FMimetype}}", }, { "-name": "CHECKSUM", "#content": "{{FChecksum}}" }, { "-name": "CHECKSUMTYPE", "#content": "{{FChecksumType}}" }, { "-name": "SIZE", "#content": "{{FSize}}" } ], "-children": [ { "-name": "FLocat", "-attr": [ { "-name": "href", "-namespace": "xlink", "#content": "file:///{{href}}" }, ] } ] } ] } } } try: os.mkdir(self.datadir) except OSError as e: if e.errno != 17: raise def tearDown(self): shutil.rmtree(self.datadir) def create_files(self): files = [] for i in range(3): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('%s' % i) files.append(fname) return files def generate_xml(self): self.generator.generate(self.filesToCreate, folderToParse=self.datadir) def test_validation_without_files(self): root = etree.fromstring('<root></root>') with open(self.fname, 'wb') as f: f.write(etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='UTF-8')) self.validator = DiffCheckValidator(context=self.fname, options=self.options) self.validator.validate(self.datadir) def test_validation_with_unchanged_files(self): files = self.create_files() self.generate_xml() self.validator = DiffCheckValidator(context=self.fname, options=self.options) self.validator.validate(self.datadir) def test_validation_with_unchanged_files_multiple_times(self): files = self.create_files() self.generate_xml() self.validator = DiffCheckValidator(context=self.fname, options=self.options) self.validator.validate(self.datadir) self.validator.validate(self.datadir) def test_validation_with_unchanged_files_with_same_content(self): files = [os.path.join(self.datadir, 'first.txt'), os.path.join(self.datadir, 'second.txt')] for f in files: with open(f, 'w') as fp: fp.write('foo') self.generate_xml() self.validator = DiffCheckValidator(context=self.fname, options=self.options) self.validator.validate(self.datadir) def test_validation_with_unchanged_file_in_directory(self): os.mkdir(os.path.join(self.datadir, 'dir1')) files = [os.path.join(self.datadir, 'dir1', 'first.txt')] for f in files: with open(f, 'w') as fp: fp.write('foo') self.generate_xml() self.validator = DiffCheckValidator(context=self.fname, options=self.options) self.validator.validate(self.datadir) def test_validation_with_deleted_file(self): files = self.create_files() self.generate_xml() os.remove(files[0]) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '2 confirmed, 0 added, 0 changed, 0 renamed, 1 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_added_file(self): files = self.create_files() self.generate_xml() added = os.path.join(self.datadir, 'added.txt') with open(added, 'w') as f: f.write('added') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '3 confirmed, 1 added, 0 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_renamed_file(self): files = self.create_files() self.generate_xml() old = files[0] new = os.path.join(self.datadir, 'new.txt') os.rename(old, new) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '2 confirmed, 0 added, 0 changed, 1 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_changed_file(self): files = self.create_files() self.generate_xml() with open(files[0], 'a') as f: f.write('changed') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '2 confirmed, 0 added, 1 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_checksum_attribute_missing(self): files = self.create_files() self.generate_xml() tree = etree.parse(self.fname) file_el = tree.xpath('*[local-name()="file"]')[1] file_el.attrib.pop('CHECKSUM') tree.write(self.fname, xml_declaration=True, encoding='UTF-8') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '2 confirmed, 0 added, 1 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_incorrect_size(self): files = self.create_files() self.generate_xml() tree = etree.parse(self.fname) file_el = tree.xpath('*[local-name()="file"]')[1] file_el.attrib['SIZE'] = str(os.path.getsize(files[1])*2) tree.write(self.fname, xml_declaration=True, encoding='UTF-8') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '2 confirmed, 0 added, 1 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_size_attribute_missing(self): files = self.create_files() self.generate_xml() tree = etree.parse(self.fname) file_el = tree.xpath('*[local-name()="file"]')[1] file_el.attrib.pop('SIZE') tree.write(self.fname, xml_declaration=True, encoding='UTF-8') self.validator = DiffCheckValidator(context=self.fname, options=self.options) self.validator.validate(self.datadir) def test_validation_two_identical_files_one_missing(self): files = [] for i in range(2): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('foo') files.append(fname) self.generate_xml() os.remove(files[0]) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '1 confirmed, 0 added, 0 changed, 0 renamed, 1 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_two_identical_files_one_renamed(self): files = [] for i in range(2): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('foo') files.append(fname) self.generate_xml() old = files[0] new = os.path.join(self.datadir, 'new.txt') os.rename(old, new) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '1 confirmed, 0 added, 0 changed, 1 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_two_identical_files_one_renamed_one_deleted(self): files = [] for i in range(2): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('foo') files.append(fname) self.generate_xml() old = files[0] new = os.path.join(self.datadir, 'new.txt') os.rename(old, new) os.remove(files[1]) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '0 confirmed, 0 added, 0 changed, 1 renamed, 1 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_three_identical_files_two_renamed_one_deleted(self): files = [] for i in range(3): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('foo') files.append(fname) self.generate_xml() old = files[0] new = os.path.join(self.datadir, 'new.txt') os.rename(old, new) old = files[1] new = os.path.join(self.datadir, 'newer.txt') os.rename(old, new) os.remove(files[2]) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '0 confirmed, 0 added, 0 changed, 2 renamed, 1 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_three_identical_files_two_renamed_one_added(self): files = [] for i in range(3): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('foo') files.append(fname) self.generate_xml() old = files[0] new = os.path.join(self.datadir, 'new.txt') os.rename(old, new) old = files[1] new = os.path.join(self.datadir, 'newer.txt') os.rename(old, new) added = os.path.join(self.datadir, 'added.txt') with open(added, 'w') as f: f.write('foo') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '1 confirmed, 1 added, 0 changed, 2 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_two_identical_files_one_changed(self): files = [] for i in range(2): fname = os.path.join(self.datadir, '%s.txt' % i) with open(fname, 'w') as f: f.write('foo') files.append(fname) self.generate_xml() with open(files[0], 'a') as f: f.write('changed') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '1 confirmed, 0 added, 1 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_added_identical_file(self): files = self.create_files() self.generate_xml() added = os.path.join(self.datadir, 'added.txt') with open(added, 'w') as f: with open(files[1]) as f1: f.write(f1.read()) self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '3 confirmed, 1 added, 0 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_added_identical_file_reversed_walk(self): files = self.create_files() self.generate_xml() added = os.path.join(self.datadir, 'added.txt') with open(added, 'w') as f: with open(files[1]) as f1: f.write(f1.read()) for f in files: os.remove(f) self.create_files() self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '3 confirmed, 1 added, 0 changed, 0 renamed, 0 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir) def test_validation_with_all_alterations(self): files = self.create_files() self.generate_xml() with open(files[0], 'a') as f: f.write('changed') os.remove(files[1]) os.rename(files[2], os.path.join(self.datadir, 'new.txt')) added = os.path.join(self.datadir, 'added.txt') with open(added, 'w') as f: f.write('added') self.validator = DiffCheckValidator(context=self.fname, options=self.options) msg = '0 confirmed, 1 added, 1 changed, 1 renamed, 1 deleted$'.format(xml=self.fname) with self.assertRaisesRegexp(ValidationError, msg): self.validator.validate(self.datadir)