def _register_fetch_name(self, db, filename): f = utils.hash_string(filename) + "_" + \ utils.str_time_stamp() fetch_name = utils.join_path(self.cache_fetch_name, f) self.fetch_name = fetch_name db.insert_cachename(fetch_name, self.SIGNATURE, filename) return utils.join_path(self.cache_path, fetch_name)
def test_012_cachename(self): fil = "φ012" f_path = self.get_path(fil) with open(f_path, "w") as f: f.write("content") state = self.db.get_state(self.s.SLAVE, fil) handle = self.slave.prepare_target(state) hidden_filename = utils.join_path( handle.cache_hide_name, utils.hash_string(handle.objname)) hidden_path = handle.get_path_in_cache(hidden_filename) self.assertFalse(os.path.isfile(hidden_path)) client_db = database.get_db(self.slave.client_dbtuple) self.assertIsNone(client_db.get_cachename(hidden_filename)) handle.move_file() self.assertTrue(os.path.isfile(hidden_path)) self.assertIsNotNone(client_db.get_cachename(hidden_filename)) handle.move_file() self.assertTrue(os.path.isfile(hidden_path)) shutil.move(hidden_path, f_path) self.assertIsNotNone(client_db.get_cachename(hidden_filename)) handle.move_file() self.assertTrue(os.path.isfile(hidden_path)) # open file to cause busy error f = open(hidden_path, "r") with self.assertRaises(common.BusyError): handle.hide_file()
def _register_hidden_name(self, db, filename): f = utils.hash_string(filename) hide_filename = utils.join_path(self.cache_hide_name, f) self.hidden_filename = hide_filename self.hidden_path = self.get_path_in_cache(self.hidden_filename) if db.get_cachename(hide_filename): return False db.insert_cachename(hide_filename, self.SIGNATURE, filename) return True
def _register_stage_name(self, db, filename): f = utils.hash_string(filename) stage_filename = utils.join_path(self.cache_stage_name, f) self.stage_filename = stage_filename stage_path = self.get_path_in_cache(stage_filename) self.staged_path = stage_path if db.get_cachename(stage_filename): return False db.insert_cachename(stage_filename, self.SIGNATURE, filename) return True
def test_001_probe_and_sync(self): # initial upload to pithos f1 = "φ001" f1_content1 = "content1" r1 = self.pithos.upload_from_string( f1, f1_content1) etag1 = r1['etag'] state = self.db.get_state(self.s.MASTER, f1) self.assertEqual(state.serial, -1) self.assertEqual(state.info, {}) # probe pithos self.s.probe_file(self.s.MASTER, f1) m = self.assert_message(messaging.UpdateMessage) self.assertEqual(m.archive, self.s.MASTER) self.assertEqual(m.serial, 0) state = self.db.get_state(self.s.MASTER, f1) self.assertEqual(state.serial, 0) self.assertEqual(state.info["pithos_etag"], etag1) # get local state state = self.db.get_state(self.s.SLAVE, f1) self.assertEqual(state.serial, -1) assert state.info == {} # sync self.s.decide_file_sync(f1) self.s.launch_syncs() dstate = self.db.get_state(self.s.DECISION, f1) self.assertEqual(dstate.serial, 0) self.assert_message(messaging.SyncMessage) # check local synced file self.assert_message(messaging.AckSyncMessage) state = self.db.get_state(self.s.SLAVE, f1) assert state.serial == 0 info = state.info assert info['localfs_size'] == len(f1_content1) f1_path = self.get_path(f1) self.assertEqual(hash_file(f1_path), utils.hash_string(f1_content1)) dstate = self.db.get_state(self.s.DECISION, f1) sstate = self.db.get_state(self.s.SYNC, f1) self.assertEqual(dstate.info, sstate.info) self.assertEqual(sstate.serial, 0)
def hash_file(fil): with open(fil) as f: return utils.hash_string(f.read())
def get_instance(elems): data = "".join(elems) return utils.hash_string(data)