def test_all_algorithm(self): testStr = b'quellochetepare' for alg in fsdb.config.ACCEPTED_HASH_ALG: mFsdb = Fsdb(os.path.join(self.fsdb_tmp_path, "test_alg"), hash_alg=alg, depth=0) digest = mFsdb.add(BytesIO(testStr)) with mFsdb[digest] as f: self.assertEqual(f.read(), testStr)
def test_direct_relative_path(self): '''A relative path should be accepted and sanitized''' absdir = os.path.dirname(self.fsdb_tmp_path) os.chdir(absdir) relPath = os.path.relpath(self.fsdb_tmp_path, absdir) fsdb = Fsdb(relPath) self.assertEqual(fsdb.fsdbRoot, self.fsdb_tmp_path)
def test_deep_retrocompatibility(self): conf = fsdb.config.get_defaults() conf['deep'] = conf.pop('depth') + 1 confFile = os.path.join(self.fsdb_tmp_path, Fsdb.CONFIG_FILE) fsdb.config.writeConf(confFile, conf) myFsdb = Fsdb(self.fsdb_tmp_path) self.assertEqual(myFsdb._conf['depth'], fsdb.config.get_defaults()['depth'] + 1)
def test_creation_with_params(self): fsdb = Fsdb(self.fsdb_tmp_path, fmode="600", dmode="700", depth=10, hash_alg="sha512") self.assertEqual(fsdb._conf['fmode'], int("600", 8)) self.assertEqual(fsdb._conf['dmode'], int("700", 8)) self.assertEqual(fsdb._conf['depth'], 10) self.assertEqual(fsdb._conf['hash_alg'], "sha512")
def __init__(self, conf={}): defaults = {'FSDB_PATH': None, 'ES_HOSTS': None, 'ES_INDEXNAME': None} defaults.update(conf) self._config = defaults log.debug('initializing with this config: ' + dumps(self._config)) # initialize fsdb if self._config['FSDB_PATH']: self.__fsdb = Fsdb(self._config['FSDB_PATH']) else: log.warning( 'It has not been set any value for FSDB_PATH, file operations will not be supported' ) # initialize elasticsearch if not self._config['ES_INDEXNAME']: raise ValueError('ES_INDEXNAME cannot be empty') self.__db = None
def get_aqi24(days): dfrom = datetime.now() - timedelta(days=int(days)) dto = datetime.now() db = Fsdb() return jsonify(db.getRange(dfrom, dto))
def test_write_default_config(self): Fsdb(self.fsdb_tmp_path) confFile = os.path.join(self.fsdb_tmp_path, Fsdb.CONFIG_FILE) conf = fsdb.config.loadConf(confFile) self.assertEqual(conf, fsdb.config.get_defaults())
def get_range(sfrom, sto): dfrom = datetime.strptime(sfrom, "%Y-%m-%d %H:%M:%S") dto = datetime.strptime(sto, "%Y-%m-%d %H:%M:%S") db = Fsdb() return jsonify(db.getRange(dfrom, dto))
def test_wrong_algorithm(self): Fsdb(self.fsdb_tmp_path, hash_alg="verystrangealgorithm")
def test_dmode_passing(self): dmode = "700" fsdb = Fsdb(self.fsdb_tmp_path, fmode=dmode) self.assertEqual(fsdb._conf['dmode'], int(dmode, 8))
def test_fmode_other_session(self): fmode = "600" Fsdb(self.fsdb_tmp_path, fmode=fmode) fsdb = Fsdb(self.fsdb_tmp_path, fmode="000") self.assertEqual(fsdb._conf['fmode'], int(fmode, 8))
def test_negative_depth(self): Fsdb(self.fsdb_tmp_path, depth=-5, hash_alg="sha1")
class FsdbTest(unittest.TestCase): def setUp(self): self.FSDB_TMP_PATH="/tmp/fsdb" self.fsdb = Fsdb(self.FSDB_TMP_PATH+"/fsdbRoot") def createTestFile(self): testFilePath = os.path.join(self.FSDB_TMP_PATH, "testFile_"+randomID(4)) with open(testFilePath, 'w') as f: f.write("test"+randomID(7)) return testFilePath def test_creation_without_params(self): fsdbRootPath = os.path.join(self.FSDB_TMP_PATH, "fsdbRoot_"+randomID(4)) fsdb = Fsdb(fsdbRootPath) def test_creation_with_params(self): fsdbRootPath = os.path.join(self.FSDB_TMP_PATH, "fsdbRoot_"+randomID(4)) fsdb = Fsdb(fsdbRootPath, mode="0770", deep=5, hash_alg="sha1") def test_add(self): self.fsdb.add(self.createTestFile()) def test_file_exists(self): testFilePath = self.createTestFile() digest = self.fsdb.add(testFilePath) self.assertTrue(self.fsdb.exists(digest)) def test_file_not_exists(self): self.assertFalse(self.fsdb.exists(randomID(20))) def test_get_file_path(self): testFilePath = self.createTestFile() digest = self.fsdb.add(testFilePath) self.assertIsInstance(self.fsdb.getFilePath(digest),basestring) self.assertTrue(os.path.isabs(self.fsdb.getFilePath(digest))) def test_same_file_after_retrieval(self): testFilePath = self.createTestFile() digest = self.fsdb.add(testFilePath) storedFilePath = self.fsdb.getFilePath(digest) self.assertTrue( filecmp.cmp(testFilePath, storedFilePath, shallow=False) ) def test_remove_existing_file(self): testFilePath = self.createTestFile() digest = self.fsdb.add(testFilePath) self.fsdb.remove(digest) def test_remove_not_existing_file(self): with self.assertRaisesRegexp(OSError,"No such file or directory"): self.fsdb.remove(randomID(20)) def tearDown(self): shutil.rmtree(self.FSDB_TMP_PATH)
def test_undirect_relative_path(self): '''A relative path containing back reference (/../) should be accepted and sanitized''' relPath = os.path.relpath(self.fsdb_tmp_path) fsdb = Fsdb(relPath) self.assertEqual(fsdb.fsdbRoot, self.fsdb_tmp_path)
continue # try: sp25d = sp_25[0] sp25t = sp_25[1] sp25v = sp_25[2] sp25time = sp25d + " " + sp25t if sp25time == sp10time: print("Value for " + p10time + " is: " + sp25v) return sp25v line25 = fppm25.readline() fppm25.close() db = Fsdb() lasttime = db.lastTime() line10 = fppm10.readline() print(line10) while line10: sp_10 = line10.split(" ") print("sp10: " + str(sp_10)) if len(sp_10) != 3: line10 = fppm10.readline() continue sp10time = "" sp25v = "" sp10v = "" # try:
def get_rangeFromPl(sfrom): dfrom = datetime.strptime(sfrom, "%Y-%m-%d %H:%M:%S") dto = sto = datetime.now() #datetime.strptime(sto, "%Y-%m-%d %H:%M:%S") db = Fsdb() return jsonify(db.getRangePL(dfrom, dto))
def test_creation_without_params(self): Fsdb(self.fsdb_tmp_path)
def setUp(self): self.FSDB_TMP_PATH="/tmp/fsdb" self.fsdb = Fsdb(self.FSDB_TMP_PATH+"/fsdbRoot")
def setUp(self): self.fsdb_tmp_path = tempfile.mkdtemp(prefix="fsdb_test") self.fsdb = Fsdb(os.path.join(self.fsdb_tmp_path, "fsdbRoot"))
def test_fmode_passing(self): fmode = "600" fsdb = Fsdb(self.fsdb_tmp_path, fmode=fmode) self.assertEqual(fsdb._conf['fmode'], int(fmode, 8))
def writeDb(p10, p25): db = Fsdb() db.write(p10, p25)
def get_lastdaysPl(days): db = Fsdb() return jsonify(db.getLastDaysPl(days))