def test_rebuild_cache(self): """classifier - test rebuilding cache.""" from invenio.modules.classifier import reader info = reader._get_ontology(self.taxonomy_name) self.assertTrue(info[0]) cache = reader._get_cache_path(info[0]) if os.path.exists(cache): ctime = os.stat(cache)[stat.ST_CTIME] else: ctime = -1 time.sleep(0.5) # sleep a bit for timing issues rex = reader.get_regular_expressions( self.taxonomy_name, rebuild=True) self.assertTrue(os.path.exists(cache)) ntime = os.stat(cache)[stat.ST_CTIME] self.assertTrue((ntime > ctime)) self.assertEqual(len(rex[0]) + len(rex[1]), 63)
def test_cache_accessibility(self): """classifier - test cache accessibility/writability""" from flask import current_app from invenio.modules.classifier.registry import taxonomies from invenio.modules.classifier import reader from invenio.modules.classifier.errors import TaxonomyError # we will do tests with a copy of test taxonomy, in case anything goes # wrong... orig_name, orig_taxonomy_path, orig_taxonomy_url = reader._get_ontology( self.taxonomy_name) taxonomy_name = self.taxonomy_name + '.copy' taxonomy_path = os.path.join( current_app.config['CFG_TMPDIR'], taxonomy_name + '.rdf') shutil.copy(orig_taxonomy_path, taxonomy_path) taxonomies[taxonomy_name] = taxonomy_path assert(os.path.exists(taxonomy_path)) name, taxonomy_path, taxonomy_url = reader._get_ontology( taxonomy_name) cache = reader._get_cache_path( os.path.basename(taxonomy_path)) assert name if os.path.exists(cache): os.remove(cache) reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) assert(os.path.exists(cache)) # set cache unreadable os.chmod(cache, 000) self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=False, no_cache=False ) # set cache unreadable and test writing os.chmod(cache, 000) self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=True, no_cache=False ) # set cache readable and test writing os.chmod(cache, 600) self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=True, no_cache=False ) # set cache writable only os.chmod(cache, 200) reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) reader.get_regular_expressions( taxonomy_name, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) os.chmod(cache, 600) os.remove(cache) open(cache, 'w').close() reader.get_regular_expressions( taxonomy_name, rebuild=False, no_cache=False) # set cache readable/writable but corrupted (must rebuild itself) open(cache, 'w').close() try: os.rename(taxonomy_path, taxonomy_path + 'x') open(taxonomy_path, 'w').close() self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=False, no_cache=False ) finally: os.rename(taxonomy_path + 'x', taxonomy_path) # make cache ok, but corrupt source reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) try: os.rename(taxonomy_path, taxonomy_path + 'x') open(taxonomy_path, 'w').close() time.sleep(.1) # touch the taxonomy to be older os.utime(cache, (time.time() + 100, time.time() + 100)) reader.get_regular_expressions( taxonomy_name, rebuild=False, no_cache=False) finally: os.rename(taxonomy_path + 'x', taxonomy_path) # make cache ok (but old), and corrupt source reader.get_regular_expressions( taxonomy_name, rebuild=True, no_cache=False) try: os.rename(taxonomy_path, taxonomy_path + 'x') open(taxonomy_path, 'w').close() self.assertRaises( TaxonomyError, reader.get_regular_expressions, taxonomy_name, rebuild=False, no_cache=False ) finally: os.rename(taxonomy_path + 'x', taxonomy_path) name, taxonomy_path, taxonomy_url = reader._get_ontology( taxonomy_name) cache = reader._get_cache_path(name) os.remove(taxonomy_path) os.remove(cache)