def testCopyManySmall(self): local = [] numFiles = 5 tempf = tempfile.mkdtemp() try: for i in xrange(numFiles): local_file = utils.maketempfile(1000, dir=tempf) local.append(local_file) self.store.send(local_file, "test/") # Make sure each file made it files = self.store.list("test") for i in xrange(numFiles): self.assertTrue(os.path.basename(local[i]) in files) for i in xrange(numFiles): remote_contents = self.store.get_contents(os.path.join("test", os.path.basename(local[i]))) local_contents = open(local[i]).read() self.assertEqual(local_contents, remote_contents) # Make sure list is only returning those files self.assertEqual(len(files), numFiles) finally: shutil.rmtree(tempf) self.store.remove_dir("test")
def test(self): """ Test whether this Storage is valid Raises an exception if there is a problem. """ log.info("Beginning Store Test") self.connect() # Create a root dir to test from. # There may be multiple simultaneous backups using it, # and there may be multiple machines doing those backups. # So we can't juts use locks. We use a large random number, # and watch for anyone else using it. remotedir = "__test%s" % "".join(random.choice(string.letters + string.digits) for dummy in xrange(10)) if self.exists(remotedir): # Uh oh... very unlikely but possible. raise Exception("Test folder in use") try: tempf = utils.maketempfile(256) temps = open(tempf).read() # Create a remote folder log.debug("Building temp dir") self.make_dir(remotedir) if not self.exists(remotedir): raise Exception("Unable to create temporary test folder") # Attempt to send log.debug("Sending temp file") remotefile = self.send(tempf, remotedir + os.sep) # Make sure it exists if not self.exists(remotefile): raise Exception("Cannot copy to remote folder") # Get it back and check it is consistent contents = self.get_contents(remotefile) if contents != temps: raise Exception("Writing and reading a file fails") # Attempt to delete (make sure we have delete permission self.remove_file(remotefile) if self.exists(remotefile): raise Exception("Unable to delete remote file") except Exception as e: raise Exception("Test of store '%s' failed (%s)" % (self.name, e)) finally: # Clean out the dir too if self.exists(remotedir): self.remove_dir(remotedir) if self.exists(remotedir): raise Exception("Unable to remove test dir") if os.path.exists(tempf): os.remove(tempf) self.disconnect() log.debug("Success! store test")
def testSpeed(self): tempf = utils.maketempfile(128000) try: with Timer("\n" + self.store.__class__.__name__ + " 128k Copy"): self.store.send(tempf, "test/") finally: os.remove(tempf) self.store.remove_dir("test")
def testDeleteFile(self): tempf = utils.maketempfile(1037) try: remote = self.store.send(tempf, "/") self.assertTrue(self.store.exists(remote)) self.store.remove_file(remote) self.assertFalse(self.store.exists(remote)) finally: os.remove(tempf)
def testSize(self): tempf = utils.maketempfile(1037) try: remote = self.store.send(tempf, "test/") size = self.store.size(remote) self.assertEqual(size, 1037) finally: os.remove(tempf) self.store.remove_dir("test")
def testCopyFolder(self): temp_path = utils.maketempfile(100) fname = os.path.basename(temp_path) try: self.store.send(temp_path, "test/") self.assertTrue(self.store.exists(os.path.join("test", fname))) self.store.get(os.path.join("test", fname), self.working_folder + os.sep) self.assertTrue(os.path.isfile(os.path.join(self.working_folder, fname))) finally: os.remove(temp_path)
def testRead(self): testfile = utils.maketempfile(1037) try: remote = self.store.send(testfile, "test/") fd = self.store.open(remote, "r") remote_data = fd.read() fd.close() self.assertEqual(remote_data, open(testfile).read()) finally: os.remove(testfile)
def testCopyLarge(self): temp_path = utils.maketempfile(100000) temp_path2 = temp_path + "2" try: remote = self.store.send(temp_path, "test/") self.store.get(remote, temp_path2) self.assertTrue(filecmp.cmp(temp_path, temp_path2, shallow=False)) finally: os.remove(temp_path) os.remove(temp_path2) # local_path will be removed during tearDown self.store.remove_dir("test")
def testQueuedSend(self): if isinstance(self.store, FolderStore) or isinstance(self.store, FTPStore): # These classes stream rather than queue return fname = utils.maketempfile(1031) contents = open(fname).read() try: self.store.send(fname, "test/send1", block=False) self.store.flush() remote = self.store.get_contents("test/send1") self.assertEqual(contents, remote) self.assertFalse(os.path.isfile(fname)) finally: # In case sometihng failed - we ensure the file is gone if os.path.isfile(fname): os.remove(fname)
def testCopy(self): temp_path = utils.maketempfile(1000) temp_path2 = temp_path + "2" try: remote = self.store.send(temp_path, "test/") self.store.get(remote, temp_path2) self.assertTrue(filecmp.cmp(temp_path, temp_path2, shallow=False)) finally: os.remove(temp_path) if os.path.exists(temp_path2): os.remove(temp_path2) # local_path will be removed during tearDown try: # This could fail if there was an exception above self.store.remove_dir("test") except: pass
def testRecursiveDelete(self): self.store.make_dir("test") self.assertTrue(self.store.exists("test")) t = utils.maketempfile(1037) try: remote1 = self.store.send(t, "test/") remote2 = self.store.send(t, "test2/") self.assertTrue(self.store.exists(remote1)) self.assertTrue(self.store.exists(remote2)) self.store.remove_dir("test") # Make sure both the folder and file are gone self.assertFalse(self.store.exists(remote1)) self.assertFalse(self.store.exists("test")) # Make sure the delete didn't touch any other files self.assertTrue(self.store.exists(remote2)) finally: os.remove(t) self.store.remove_dir("test2")
def testBadEncrypt(self): fname = utils.maketempfile(1024) self.assertRaises(CipherError, decrypt_file, self.password, fname)
def setUp(self): self.testfile1 = utils.maketempfile(1024) self.password = "******"