def test_db_migration_failure_inside(self): # This tests verify that an exception happeing inside a migration causes the # migration to fail and that on this condition the database results unmodified. def epilogue(self): raise Exception('failure') update_50.MigrationScript.epilogue = epilogue path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'db', 'populated') helpers.init_state() self.db_path = os.path.join(Settings.working_path, 'db') self.final_db_file = os.path.abspath( os.path.join(Settings.working_path, 'globaleaks.db')) set_db_uri('sqlite:' + self.final_db_file) shutil.rmtree(self.db_path, True) os.mkdir(self.db_path) srcdb = os.path.join(path, 'glbackend-49.db') dstdb = os.path.join(Settings.working_path, 'globaleaks.db') shutil.copyfile(srcdb, dstdb) ret = update_db() self.assertEqual(ret, -1) self.assertTrue(filecmp.cmp(srcdb, dstdb))
def _deferred_start(self): ret = update_db() if ret == -1: reactor.stop() return if ret == 0: create_db() init_db() sync_clean_untracked_files() sync_refresh_memory_variables() sync_initialize_snimap() self.state.orm_tp.start() reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown) for sock in self.state.http_socks: listen_tcp_on_sock(reactor, sock.fileno(), self.api_factory) for sock in self.state.https_socks: listen_tls_on_sock(reactor, fd=sock.fileno(), contextFactory=self.state.snimap, factory=self.api_factory) self.start_jobs() self.print_listening_interfaces()
def _test(self, path, version): f = 'glbackend-%d.db' % version helpers.init_state() self.db_path = os.path.join(Settings.working_path, 'db') self.final_db_file = os.path.abspath( os.path.join(Settings.working_path, 'globaleaks.db')) set_db_uri('sqlite:' + self.final_db_file) shutil.rmtree(self.db_path, True) os.mkdir(self.db_path) dbpath = os.path.join(path, f) if version < 41: dbfile = os.path.join(self.db_path, f) Settings.db_file_path = os.path.join(Settings.working_path, 'db', 'glbackend-%d.db' % version) else: dbfile = os.path.join(Settings.working_path, 'globaleaks.db') shutil.copyfile(dbpath, dbfile) # TESTS PRECONDITIONS preconditions = getattr(self, 'preconditions_%d' % version, None) if preconditions is not None: preconditions() ret = update_db() # TESTS POSTCONDITIONS postconditions = getattr(self, 'postconditions_%d' % version, None) if postconditions is not None: postconditions() self.assertNotEqual(ret, -1)
def _test(self, path, version): f = 'glbackend-%d.db' % version helpers.init_glsettings_for_unit_tests() GLSettings.db_path = os.path.join(GLSettings.ramdisk_path, 'db_test') self.start_db_file = os.path.abspath( os.path.join(GLSettings.db_path, 'glbackend-%d.db' % version)) self.final_db_file = os.path.abspath( os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION)) self.start_db_uri = GLSettings.make_db_uri(self.start_db_file) GLSettings.db_uri = GLSettings.make_db_uri(self.final_db_file) shutil.rmtree(GLSettings.db_path, True) os.mkdir(GLSettings.db_path) dbpath = os.path.join(path, f) dbfile = os.path.join(GLSettings.db_path, f) shutil.copyfile(dbpath, dbfile) # TESTS PRECONDITIONS preconditions = getattr(self, 'preconditions_%d' % version, None) if preconditions is not None: preconditions() ret = update_db() # TESTS POSTCONDITIONS postconditions = getattr(self, 'postconditions_%d' % version, None) if postconditions is not None: postconditions() shutil.rmtree(GLSettings.db_path) self.assertNotEqual(ret, -1)
def _deferred_start(self): ret = update_db() if ret == -1: reactor.stop() return if ret == 0: create_db() init_db() sync_clean_untracked_files() sync_refresh_memory_variables() self.state.orm_tp.start() reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown) for sock in self.state.http_socks: listen_tcp_on_sock(reactor, sock.fileno(), self.api_factory) self.state.process_supervisor = ProcessSupervisor( self.state.https_socks, '127.0.0.1', 8082) self.state.process_supervisor.maybe_launch_https_workers() self.start_jobs() self.print_listening_interfaces()
def _deferred_start(self): ret = update_db() if ret == -1: reactor.stop() if ret == 0: init_db() sync_clean_untracked_files() sync_refresh_memory_variables() GLSettings.orm_tp.start() reactor.addSystemEventTrigger('after', 'shutdown', GLSettings.orm_tp.stop) arw = APIResourceWrapper() GLSettings.api_factory = Site(arw, logFormatter=timedLogFormatter) for sock in GLSettings.http_socks: listen_tcp_on_sock(reactor, sock.fileno(), GLSettings.api_factory) GLSettings.state.process_supervisor = ProcessSupervisor(GLSettings.https_socks, '127.0.0.1', 8082) yield GLSettings.state.process_supervisor.maybe_launch_https_workers() GLSettings.start_jobs() GLSettings.start_services() GLSettings.print_listening_interfaces()
def test_mig_37_valid_tor_hs_key(self): self._initStartDB(36) from globaleaks.db.migrations import update_37 t = update_37.TOR_DIR update_37.TOR_DIR = GLSettings.db_path pk_path = os.path.join(update_37.TOR_DIR, 'private_key') hn_path = os.path.join(update_37.TOR_DIR, 'hostname') shutil.copy(os.path.join(helpers.DATA_DIR, 'tor/private_key'), pk_path) shutil.copy(os.path.join(helpers.DATA_DIR, 'tor/hostname'), hn_path) ret = update_db() self.assertEqual(ret, None) new_uri = GLSettings.make_db_uri( os.path.join(GLSettings.db_path, GLSettings.db_file_name)) store = Store(create_database(new_uri)) hs = config.NodeFactory(store).get_val('onionservice') pk = config.PrivateFactory(store).get_val('tor_onion_key') self.assertEqual('lftx7dbyvlc5txtl.onion', hs) with open(os.path.join(helpers.DATA_DIR, 'tor/ephemeral_service_key')) as f: saved_key = f.read().strip() self.assertEqual(saved_key, pk) store.close() shutil.rmtree(GLSettings.db_path) update_37.TOR_DIR = t
def test_check_field_constraints(self): # This test case asserts that a migration from db ver 32 up to the latest # db with fields that fail the constraints still functions. self._initStartDB(32) field_dict = helpers.get_dummy_field() field_dict['instance'] = 'reference' field_dict['step_id'] = None field_dict['field_id'] = None db_create_field(self.store, field_dict, u'en') field_dict = helpers.get_dummy_field() field_dict['instance'] = 'instance' db_create_field(self.store, field_dict, u'en') field_dict = helpers.get_dummy_field() field_dict['instance'] = 'template' field_dict['step_id'] = None fld_grp_id = self.store.find(Field, Field.fieldgroup_id is not None)[0].fieldgroup_id field_dict['field_id'] = fld_grp_id db_create_field(self.store, field_dict, u'en') self.store.commit() ret = update_db() shutil.rmtree(GLSettings.db_path) self.assertNotEqual(ret, -1)
def _deferred_start(self): ret = update_db() if ret == -1: reactor.stop() return if ret == 0: create_db() init_db() sync_clean_untracked_files() sync_refresh_memory_variables() self.state.orm_tp.start() reactor.addSystemEventTrigger('before', 'shutdown', self.shutdown) for sock in self.state.http_socks: listen_tcp_on_sock(reactor, sock.fileno(), self.api_factory) self.state.process_supervisor = ProcessSupervisor(self.state.https_socks, '127.0.0.1', 8082) self.state.process_supervisor.maybe_launch_https_workers() self.start_jobs() self.print_listening_interfaces()
def _test(self, path, version): f = 'glbackend-%d.db' % version helpers.init_state() self.db_path = os.path.join(Settings.working_path, 'db') self.final_db_file = os.path.abspath(os.path.join(Settings.working_path, 'globaleaks.db')) set_db_uri('sqlite:' + self.final_db_file) shutil.rmtree(self.db_path, True) os.mkdir(self.db_path) dbpath = os.path.join(path, f) if version < 41: dbfile = os.path.join(self.db_path, f) Settings.db_file_path = os.path.join(Settings.working_path, 'db', 'glbackend-%d.db' % version) else: dbfile = os.path.join(Settings.working_path, 'globaleaks.db') shutil.copyfile(dbpath, dbfile) # TESTS PRECONDITIONS preconditions = getattr(self, 'preconditions_%d' % version, None) if preconditions is not None: preconditions() ret = update_db() # TESTS POSTCONDITIONS postconditions = getattr(self, 'postconditions_%d' % version, None) if postconditions is not None: postconditions() self.assertNotEqual(ret, -1)
def _deferred_start(self): ret = update_db() if ret == -1: reactor.stop() if ret == 0: init_db() sync_clean_untracked_files() sync_refresh_memory_variables() GLSettings.orm_tp.start() reactor.addSystemEventTrigger('after', 'shutdown', GLSettings.orm_tp.stop) arw = APIResourceWrapper() GLSettings.api_factory = Site(arw, logFormatter=timedLogFormatter) for sock in GLSettings.http_socks: listen_tcp_on_sock(reactor, sock.fileno(), GLSettings.api_factory) GLSettings.state.process_supervisor = ProcessSupervisor( GLSettings.https_socks, '127.0.0.1', GLSettings.bind_port) yield GLSettings.state.process_supervisor.maybe_launch_https_workers() GLSettings.start_jobs() print( "GlobaLeaks is now running and accessible at the following urls:") if GLSettings.memory_copy.reachable_via_web: print("- http://%s:%d%s" % (GLSettings.bind_address, GLSettings.bind_port, GLSettings.api_prefix)) if GLSettings.memory_copy.hostname: print("- http://%s:%d%s" % (GLSettings.memory_copy.hostname, GLSettings.bind_port, GLSettings.api_prefix)) else: print("- http://127.0.0.1:%d%s" % (GLSettings.bind_port, GLSettings.api_prefix)) if GLSettings.onionservice is not None: print("- http://%s%s" % (GLSettings.onionservice, GLSettings.api_prefix))
def _test(self, path, f): helpers.init_glsettings_for_unit_tests() GLSettings.db_path = os.path.join(GLSettings.ramdisk_path, 'db_test') final_db_file = os.path.abspath( os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION)) GLSettings.db_uri = GLSettings.make_db_uri(final_db_file) shutil.rmtree(GLSettings.db_path, True) os.mkdir(GLSettings.db_path) dbpath = os.path.join(path, f) dbfile = os.path.join(GLSettings.db_path, f) shutil.copyfile(dbpath, dbfile) ret = update_db() shutil.rmtree(GLSettings.db_path) self.assertNotEqual(ret, -1)