def create_storage(self, log_server): """Create a SQLiteTempDB object pointing to the temporary storage of a given log server. If the temporary storage does not exist, creates one. Args: log_server: the server name. """ with self.__mgr.get_connection() as conn: cursor = conn.cursor() try: database_name = self.__get_db_name(cursor, log_server) except database.KeyError: try: cursor.execute( "INSERT INTO database_mapping(server_name) " "VALUES (?)", (log_server, )) database_name = self.__database_id_to_name( cursor.lastrowid) except sqlite3.IntegrityError as e: raise database.KeyError("Failed to create a table mapping " "for server %s: is a concurrent " "factory running?\n%s" % (log_server, e)) return SQLiteTempDB( sqlitecon.SQLiteConnectionManager(self.__database_dir + "/" + database_name))
def test_no_keepalive_returns_new_connection(self): mgr = sqlitecon.SQLiteConnectionManager(":memory:", keepalive=False) with mgr.get_connection() as conn: conn.execute("CREATE TABLE words(word TEXT)") conn.execute("INSERT INTO words VALUES (?)", ("hello", )) with mgr.get_connection() as conn: self.assertRaises(sqlite3.OperationalError, conn.execute, "SELECT * FROM words")
def test_keepalive_returns_same_connection(self): mgr = sqlitecon.SQLiteConnectionManager(":memory:", keepalive=True) with mgr.get_connection() as conn: conn.execute("CREATE TABLE words(word TEXT)") conn.execute("INSERT INTO words VALUES (?)", ("hello", )) with mgr.get_connection() as conn: results = conn.execute("SELECT * FROM words") self.assertEqual("hello", results.next()["word"])
def setUp(self): if not FLAGS.verbose_tests: logging.disable(logging.CRITICAL) self.db = sqlite_log_db.SQLiteLogDB( sqlitecon.SQLiteConnectionManager(":memory:", keepalive=True)) self.temp_db = sqlite_temp_db.SQLiteTempDB( sqlitecon.SQLiteConnectionManager(":memory:", keepalive=True)) # We can't simply use DB in memory with keepalive True, because different # thread is writing to the database which results in an sqlite exception. self.cert_db = mock.MagicMock() default_state = client_pb2.MonitorState() default_state.verified_sth.CopyFrom(self._DEFAULT_STH) self.state_keeper = InMemoryStateKeeper(default_state) self.verifier = mock.Mock() self.hasher = merkle.TreeHasher() # Make sure the DB knows about the default log server. log = client_pb2.CtLogMetadata() log.log_server = "log_server" self.db.add_log(log)
def setUp(self): self.database = sqlite_cert_db.SQLiteCertDB( sqlitecon.SQLiteConnectionManager(":memory:", keepalive=True))
def setUp(self): self.database_dir = tempfile.mkdtemp() self.factory = sqlite_temp_db.SQLiteTempDBFactory( sqlitecon.SQLiteConnectionManager(":memory:", keepalive=True), self.database_dir) self.temp_stores = set()
logging.basicConfig(level=FLAGS.log_level) create_directory(FLAGS.ct_sqlite_temp_dir) create_directory(FLAGS.monitor_state_dir) try: list_ = requests.get(tld_list.TLD_LIST_ADDR, timeout=5) if list_.status_code == 200: create_directory(FLAGS.tld_list_dir) with open('/'.join((FLAGS.tld_list_dir, "tld_list")), 'w') as f: f.write(list_.content) except requests.exceptions.RequestException: logging.warning("Couldn't fetch top level domain list") sqlite_log_db = sqlite_log_db.SQLiteLogDB( sqlitecon.SQLiteConnectionManager(FLAGS.ct_sqlite_db)) sqlite_temp_db_factory = sqlite_temp_db.SQLiteTempDBFactory( sqlitecon.SQLiteConnectionManager(FLAGS.ct_sqlite_temp_dir + "/meta"), FLAGS.ct_sqlite_temp_dir) sqlite_cert_db = sqlite_cert_db.SQLiteCertDB( sqlitecon.SQLiteConnectionManager(FLAGS.ct_sqlite_cert_db)) ctlogs = client_pb2.CtLogs() with open(FLAGS.ctlog_config, "r") as config: log_config = config.read() text_format.Merge(log_config, ctlogs) ct_server_list = [] for log in ctlogs.ctlog:
def test_get_connection(self): mgr = sqlitecon.SQLiteConnectionManager(":memory:") self.assertIsInstance(mgr.get_connection(), sqlitecon.SQLiteConnection)