def test_diff2(self): """Another diff test - this one involves multivol support (requires rdiff to be installed to pass)""" sel1 = selection.Select(Path("testfiles/dir2")) diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()), "testfiles/output/dir2.sigtar") sigtar_fp = open("testfiles/output/dir2.sigtar", "rb") sel2 = selection.Select(Path("testfiles/dir3")) delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp) diffdir.write_block_iter(delta_tar, "testfiles/output/dir2dir3.difftar") buffer = "" tf = tarfile.TarFile("testfiles/output/dir2dir3.difftar", "r") for tarinfo in tf: if tarinfo.name.startswith("multivol_diff/"): buffer += tf.extractfile(tarinfo).read() assert 3000000 < len(buffer) < 4000000 fout = open("testfiles/output/largefile.delta", "wb") fout.write(buffer) fout.close() assert not os.system("rdiff patch testfiles/dir2/largefile " "testfiles/output/largefile.delta " "testfiles/output/largefile.patched") dir3large = open("testfiles/dir3/largefile", "rb").read() patchedlarge = open("testfiles/output/largefile.patched", "rb").read() assert dir3large == patchedlarge
def test_diff(self): """Test making a diff""" sel1 = selection.Select(Path("testfiles/dir1")) diffdir.write_block_iter(diffdir.SigTarBlockIter(sel1.set_iter()), "testfiles/output/dir1.sigtar") sigtar_fp = open("testfiles/output/dir1.sigtar", "rb") sel2 = selection.Select(Path("testfiles/dir2")) delta_tar = diffdir.DirDelta(sel2.set_iter(), sigtar_fp) diffdir.write_block_iter(delta_tar, "testfiles/output/dir1dir2.difftar") changed_files = ["diff/changeable_permission", "diff/regular_file", "snapshot/symbolic_link/", "deleted/deleted_file", "snapshot/directory_to_file", "snapshot/file_to_directory/"] for tarinfo in tarfile.TarFile("testfiles/output/dir1dir2.difftar", "r"): tiname = util.get_tarinfo_name(tarinfo) if tiname in changed_files: changed_files.remove(tiname) assert not changed_files, ("Following files not found:\n" + "\n".join(changed_files))
def test_dirdelta_write_sig(self): """Test simultaneous delta and sig generation Generate signatures and deltas of dirs1, 2, 3, 4 and compare those produced by DirDelta_WriteSig and other methods. """ deltadir1 = Path("testfiles/output/dir.deltatar1") # @UnusedVariable deltadir2 = Path("testfiles/output/dir.deltatar2") # @UnusedVariable cur_full_sigs = Path("testfiles/output/fullsig.dir1") cur_dir = Path("testfiles/dir1") get_sel = lambda cur_dir: selection.Select(cur_dir).set_iter() diffdir.write_block_iter(diffdir.SigTarBlockIter(get_sel(cur_dir)), cur_full_sigs) sigstack = [cur_full_sigs] for dirname in ['dir2', 'dir3', 'dir4']: # print "Processing ", dirname old_dir = cur_dir # @UnusedVariable cur_dir = Path("testfiles/" + dirname) old_full_sigs = cur_full_sigs cur_full_sigs = Path("testfiles/output/fullsig." + dirname) delta1 = Path("testfiles/output/delta1." + dirname) delta2 = Path("testfiles/output/delta2." + dirname) incsig = Path("testfiles/output/incsig." + dirname) # Write old-style delta to deltadir1 diffdir.write_block_iter(diffdir.DirDelta(get_sel(cur_dir), old_full_sigs.open("rb")), delta1) # Write new signature and delta to deltadir2 and sigdir2, compare block_iter = diffdir.DirDelta_WriteSig( get_sel(cur_dir), [p.open("rb") for p in sigstack], incsig.open("wb")) sigstack.append(incsig) diffdir.write_block_iter(block_iter, delta2) # print delta1.name, delta2.name compare_tar(delta1.open("rb"), delta2.open("rb")) assert not os.system("cmp %s %s" % (delta1.name, delta2.name)) # Write old-style signature to cur_full_sigs diffdir.write_block_iter(diffdir.SigTarBlockIter(get_sel(cur_dir)), cur_full_sigs)
def test_empty_diff(self): """Test producing a diff against same sig; should be len 0""" self.empty_diff_schema("testfiles/various_file_types") select = selection.Select(Path("testfiles/various_file_types")) select.set_iter() sigtar = diffdir.SigTarBlockIter(select) diffdir.write_block_iter(sigtar, "testfiles/output/sigtar") sigtar_fp = open("testfiles/output/sigtar") select2 = selection.Select(Path("testfiles/various_file_types")) select2.set_iter() diffdir.write_block_iter(diffdir.DirDelta(select2, sigtar_fp), "testfiles/output/difftar") size = os.stat("testfiles/output/difftar").st_size # @UnusedVariable
def test_empty_diff(self): u"""Test producing a diff against same sig; should be len 0""" self.empty_diff_schema(u"{0}/testfiles/various_file_types".format(_runtest_dir)) select = selection.Select(Path(u"{0}/testfiles/various_file_types".format(_runtest_dir))) select.set_iter() sigtar = diffdir.SigTarBlockIter(select) diffdir.write_block_iter(sigtar, u"{0}/testfiles/output/sigtar".format(_runtest_dir)) sigtar_fp = open(u"{0}/testfiles/output/sigtar".format(_runtest_dir), u"rb") select2 = selection.Select(Path(u"{0}/testfiles/various_file_types".format(_runtest_dir))) select2.set_iter() diffdir.write_block_iter(diffdir.DirDelta(select2, sigtar_fp), u"{0}/testfiles/output/difftar".format(_runtest_dir)) size = os.stat(u"{0}/testfiles/output/difftar".format(_runtest_dir)).st_size
def empty_diff_schema(self, dirname): """Given directory name, make sure can tell when nothing changes""" select = selection.Select(Path(dirname)) select.set_iter() sigtar = diffdir.SigTarBlockIter(select) diffdir.write_block_iter(sigtar, "testfiles/output/sigtar") sigtar_fp = open("testfiles/output/sigtar") select2 = selection.Select(Path(dirname)) select2.set_iter() diffdir.write_block_iter(diffdir.DirDelta(select2, sigtar_fp), "testfiles/output/difftar") size = os.stat("testfiles/output/difftar").st_size assert size == 0 or size == 10240, size # 10240 is size of one record if size != 0: fin = open("testfiles/output/difftar", "rb") diff_buf = fin.read() assert not fin.close() assert diff_buf == '\0' * 10240
def test_root(self): """Test changing uid/gid, devices""" self.deltmp() os.system("cp -pR testfiles/root1 testfiles/output/sequence") seq_path = Path("testfiles/output/sequence") new_path = Path("testfiles/root2") sig = Path("testfiles/output/sig.tar") diff = Path("testfiles/output/diff.tar") diffdir.write_block_iter(diffdir.DirSig(self.get_sel(seq_path)), sig) deltablock = diffdir.DirDelta(self.get_sel(new_path), sig.open("rb")) diffdir.write_block_iter(deltablock, diff) patchdir.Patch(seq_path, diff.open("rb")) # since we are not running as root, don't even both comparing, # just make sure file5 exists and file4 doesn't. file5 = seq_path.append("file5") assert file5.isreg() file4 = seq_path.append("file4") assert file4.type is None
def total_sequence(self, filelist): """Test signatures, diffing, and patching on directory list""" assert len(filelist) >= 2 self.deltmp() assert not os.system("cp -pR %s testfiles/output/sequence" % (filelist[0], )) seq_path = Path("testfiles/output/sequence") sig = Path("testfiles/output/sig.tar") diff = Path("testfiles/output/diff.tar") for dirname in filelist[1:]: new_path = Path(dirname) diffdir.write_block_iter( diffdir.DirSig(selection.Select(seq_path).set_iter()), sig) diffdir.write_block_iter( diffdir.DirDelta( selection.Select(new_path).set_iter(), sig.open("rb")), diff) patchdir.Patch(seq_path, diff.open("rb")) assert seq_path.compare_recursive(new_path, 1)
def total_sequence(self, filelist): u"""Test signatures, diffing, and patching on directory list""" assert len(filelist) >= 2 sig = Path(u"testfiles/output/sig.tar") diff = Path(u"testfiles/output/diff.tar") seq_path = Path(u"testfiles/output/sequence") new_path, old_path = None, None # set below in for loop # Write initial full backup to diff.tar for dirname in filelist: old_path, new_path = new_path, Path(dirname) if old_path: sigblock = diffdir.DirSig(self.get_sel(seq_path)) diffdir.write_block_iter(sigblock, sig) deltablock = diffdir.DirDelta(self.get_sel(new_path), sig.open(u"rb")) else: deltablock = diffdir.DirFull(self.get_sel(new_path)) diffdir.write_block_iter(deltablock, diff) patchdir.Patch(seq_path, diff.open(u"rb")) # print "#########", seq_path, new_path assert seq_path.compare_recursive(new_path, 1)