def test_random_cluster_multi(self): urls = ['http://localhost:8529', 'http://localhost:8530'] cluster = RandomCluster(urls) seen = {} for i in range(100): url = cluster.select_next_url() self.assertIn(url, urls) seen[url] = True if len(seen) == 2: break self.assertEqual(len(seen), 2, seen)
def create_config(): return Config(cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=BasicAuth(username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD), disable_database_delete=False, disable_collection_delete=False)
def test_disable_delete(self): cfg = Config(cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=BasicAuth(username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD)) db = cfg.database(helper.TEST_ARANGO_DB) coll = db.collection('test_coll') self.assertRaises(AssertionError, lambda: coll.force_delete())
def test_code_as_config_basic_auth(self): config = Config( cluster=RandomCluster( urls=['http://127.0.0.1:8529']), # see Cluster Styles timeout_seconds=3, back_off=StepBackOffStrategy([0.1, 0.5, 1, 1, 1]), # see Back Off Strategies auth=BasicAuth(username='******', password=''), ttl_seconds=31622400) self.assertIsNotNone(config)
def test_database_exists(self): cfg = Config( cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=BasicAuth( username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD ) ) db = cfg.database(helper.TEST_ARANGO_DB) self.assertFalse(db.check_if_exists())
def test_code_as_config_jwt(self): config = Config( cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=3, back_off=StepBackOffStrategy(steps=[0.1, 0.5, 1, 1, 1]), ttl_seconds=31622400, auth=JWTAuth( username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD, cache=JWTDiskCache( # See JWT Caches lock_file='.arango_jwt.lock', lock_time_seconds=10, store_file='.arango_jwt'))) self.assertIsNotNone(config) config.prepare()
def create_config(): return Config( cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=JWTAuth( username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD, cache=JWTDiskCache( lock_file='test.jwt.lock', lock_time_seconds=10, store_file='test.jwt' ) ) )
def test_collection_default_ttl(self): cfg = Config(cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=2, auth=BasicAuth(username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD), disable_database_delete=False) db = cfg.database(helper.TEST_ARANGO_DB) self.assertTrue(db.create_if_not_exists()) coll = db.collection('test_coll') self.assertTrue(coll.create_if_not_exists()) coll.create_or_overwrite_doc('test_doc', {'foo': 3}) self.assertEqual(coll.read_doc('test_doc'), {'foo': 3}) self.assertTrue(db.force_delete())
def test_protect(self): cfg = Config(cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=BasicAuth(username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD), disable_database_delete=False, disable_collection_delete=False, protected_collections=['test_coll']) db = cfg.database(helper.TEST_ARANGO_DB) coll = db.collection('test_coll') self.assertRaises(AssertionError, lambda: coll.force_delete()) self.assertTrue(db.create_if_not_exists()) coll = db.collection('test_coll2') self.assertFalse(coll.force_delete()) self.assertTrue(db.force_delete())
def main(): # Normally one would use env_config instead of parsing the environment # variables directly, but we do this here to make the example have as # little magic as possible. urls = os.environ.get('ARANGO_CLUSTER', 'http://localhost:8529').split(',') username = os.environ.get('ARANGO_AUTH_USERNAME', 'root') password = os.environ.get('ARANGO_AUTH_PASSWORD') cfg = Config(cluster=RandomCluster(urls=urls), timeout_seconds=3, back_off=StepBackOffStrategy(steps=[0.1, 0.5, 1, 1, 1]), ttl_seconds=31622400, auth=JWTAuth(username=username, password=password, cache=None)) db: Database = cfg.database('fake_db') assert isinstance(db, Database) assert db.check_if_exists() is False
def test_reset_affinity(self): cfg = Config( cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=JWTAuth( username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD, cache=JWTDiskCache( lock_file='test.jwt.lock', lock_time_seconds=10, store_file='test.jwt' ) ) ) cfg.prepare() th = threading.Thread(target=run_with_reset_affinity, args=(cfg, RuntimeError, False)) th.start() th.join() th = threading.Thread(target=run_with_reset_affinity, args=(cfg, None, True)) th.start() th.join() cfg.auth.reset_affinity() cfg.prepare() proc = multiprocessing.Process( target=run_with_reset_affinity, args=(cfg, None, False) ) proc.start() proc.join() self.assertTrue(True) os.remove('test.jwt') os.remove('test.jwt.lock')
def main(): # Normally one would use env_config instead of parsing the environment # variables directly, but we do this here to make the example have as # little magic as possible. urls = os.environ.get('ARANGO_CLUSTER', 'http://localhost:8529').split(',') username = os.environ.get('ARANGO_AUTH_USERNAME', 'root') password = os.environ.get('ARANGO_AUTH_PASSWORD') cfg = Config( cluster=RandomCluster(urls=urls), timeout_seconds=3, back_off=StepBackOffStrategy(steps=[0.1, 0.5, 1, 1, 1]), ttl_seconds=31622400, auth=JWTAuth( username=username, password=password, cache=JWTDiskCache( # See JWT Caches lock_file='.jwt_disk_example.lock', lock_time_seconds=10, store_file='.jwt_disk_example'))) print(f'Connecting to cluster={urls}, username={username}') cfg.prepare() db: Database = cfg.database('fake_db') assert isinstance(db, Database) assert db.check_if_exists() is False print('Lock file:') with open('.jwt_disk_example.lock') as fin: print(fin.read()) print() print('JWT file:') with open('.jwt_disk_example') as fin: print(fin.read()) os.remove('.jwt_disk_example.lock') os.remove('.jwt_disk_example')
def test_recover(self): self.assertFalse(os.path.exists('test.jwt')) self.assertFalse(os.path.exists('test.jwt.lock')) cfg = Config( cluster=RandomCluster(urls=helper.TEST_CLUSTER_URLS), timeout_seconds=10, back_off=StepBackOffStrategy(steps=[1]), ttl_seconds=None, auth=JWTAuth( username=helper.TEST_USERNAME, password=helper.TEST_PASSWORD, cache=JWTDiskCache( lock_file='test.jwt.lock', lock_time_seconds=10, store_file='test.jwt' ) ) ) cfg.prepare() self.assertTrue(cfg.auth.try_recover_auth_failure()) self.assertFalse(cfg.auth.try_recover_auth_failure()) os.remove('test.jwt') os.remove('test.jwt.lock')
def test_random_cluster_single(self): cluster = RandomCluster(urls=['http://localhost:8529']) self.assertIsNotNone(cluster) url = cluster.select_next_url() self.assertEqual(url, 'http://localhost:8529')