def init_config(self, config=None): self.config = config self.nginx_manager = nginx.Nginx(config) self.consul_manager = consul_manager.ConsulManager(config) self.host_manager_name = self._get_conf("HOST_MANAGER", "cloudstack") self.lb_manager_name = self._get_conf("LB_MANAGER", "networkapi_cloudstack") self.task_manager = TaskManager(config) self.lock_manager = lock.Lock(app.backend.client) self.hc = hc.Dumb() self.storage = storage.MongoDBStorage(config) self.acl_manager = acl.Dumb(self.consul_manager) if check_option_enable(self._get_conf("CHECK_ACL_API", None)): self.acl_manager = acl.AclManager(config, self.consul_manager, lock.Lock(app.backend.client)) hc_url = self._get_conf("HCAPI_URL", None) if hc_url: self.hc = hc.HCAPI(self.storage, url=hc_url, user=self._get_conf("HCAPI_USER"), password=self._get_conf("HCAPI_PASSWORD"), hc_format=self._get_conf( "HCAPI_FORMAT", "http://{}:8080/")) self.retry_countdown = os.environ.get("RETRY_COUNTDOWN", None) if self.retry_countdown: self.retry_countdown = int(self.retry_countdown) self.max_retries = os.environ.get("MAX_RETRIES", None) if self.max_retries: self.max_retries = int(self.max_retries)
def test_create_lock_when_empty_locks(self): lock_manager = lock.Lock(self.redis_conn) self.assertEqual(len(lock_manager.redis_locks), 0) lock_acquire = lock_manager.lock("lock1", 60) self.assertEqual(len(lock_manager.redis_locks), 1) self.assertTrue(lock_acquire) self.assertEqual(lock_manager.redis_locks[0].name, "lock1")
def test_unlock_and_release_lock(self): lock_manager = lock.Lock(self.redis_conn) lock_manager.lock("lock1", 60) lock1 = lock_manager.redis_locks[0] lock_manager.unlock("lock1") self.assertEqual(len(lock_manager.redis_locks), 0) with self.assertRaises(redis.exceptions.LockError) as cm: lock1.release() self.assertEqual(cm.exception.message, "Cannot release an unlocked lock")
def test_create_multiple_locks(self): lock_manager = lock.Lock(self.redis_conn) lock_acquire_1 = lock_manager.lock("lock1", 60) lock_acquire_2 = lock_manager.lock("lock2", 60) lock_acquire_3 = lock_manager.lock("lock3", 60) self.assertEqual(len(lock_manager.redis_locks), 3) self.assertTrue(lock_acquire_1) self.assertTrue(lock_acquire_2) self.assertTrue(lock_acquire_3)
def __init__(self, config=None): self.config = config self.storage = storage.MongoDBStorage(config) self.consul_manager = consul_manager.ConsulManager(config) self.nginx_manager = nginx.Nginx(config) self.task_manager = tasks.TaskManager(config) self.service_name = os.environ.get("RPAAS_SERVICE_NAME", "rpaas") self.acl_manager = acl.Dumb(self.consul_manager) if check_option_enable(os.environ.get("CHECK_ACL_API", None)): self.acl_manager = acl.AclManager(config, self.consul_manager, lock.Lock(tasks.app.backend.client))
def test_extend_lock_extra_time(self): lock_manager = lock.Lock(self.redis_conn) lock_acquire = lock_manager.lock("lock1", 1) time.sleep(2) with self.assertRaises(redis.exceptions.LockError) as cm: lock_manager.extend_lock("lock1", 30) self.assertEqual(cm.exception.message, "Cannot extend a lock that's no longer owned") self.assertTrue(lock_acquire) lock_1 = lock_manager.redis_locks[0] lock_1.acquire(blocking=False) lock_manager.extend_lock("lock1", 30) time.sleep(3) self.assertFalse(lock_1.acquire(blocking=False))
def test_unlock_and_release_lock_with_multiple_locks(self): lock_manager = lock.Lock(self.redis_conn) lock_acquire_1 = lock_manager.lock("lock1", 60) lock_acquire_2 = lock_manager.lock("lock2", 60) lock_acquire_3 = lock_manager.lock("lock3", 60) self.assertTrue(lock_acquire_1) self.assertTrue(lock_acquire_2) self.assertTrue(lock_acquire_3) lock2 = lock_manager.redis_locks[1] self.assertEqual(lock2.name, "lock2") lock_manager.unlock("lock2") self.assertEqual(len(lock_manager.redis_locks), 2) lock_acquire_1 = lock_manager.lock("lock1", 60) lock_acquire_2 = lock_manager.lock("lock2", 60) lock_acquire_3 = lock_manager.lock("lock3", 60) self.assertFalse(lock_acquire_1) self.assertTrue(lock_acquire_2) self.assertFalse(lock_acquire_3)
def setUp(self): self.master_token = "rpaas-test" os.environ.setdefault("RPAAS_SERVICE_NAME", "rpaas-acl") os.environ.setdefault("CONSUL_HOST", "127.0.0.1") os.environ.setdefault("CONSUL_TOKEN", self.master_token) self.consul = consul.Consul(token=self.master_token) self.consul.kv.delete("rpaas-acl", recurse=True) self.storage = consul_manager.ConsulManager(os.environ) self.redis_conn = redis.StrictRedis() self.redis_conn.flushall() self.lock_manager = lock.Lock(self.redis_conn) self.lock_name = "acl_manager:rpaas-acl" self.config = { "ACL_API_HOST": "http://aclapihost", "ACL_API_USER": "******", "ACL_API_PASSWORD": "******", "RPAAS_SERVICE_NAME": "rpaas-acl" }
def test_create_lock_try_to_acquire_lock_in_use(self): lock_manager = lock.Lock(self.redis_conn) lock_acquire = lock_manager.lock("lock1", 60) lock_acquire = lock_manager.lock("lock1", 60) self.assertEqual(len(lock_manager.redis_locks), 1) self.assertFalse(lock_acquire)