def test_mongodb_uri_environ(self, mongo_mock): from redisapi.storage import MongoStorage storage = MongoStorage() storage.conn() mongo_mock.assert_called_with("mongodb://localhost:27017/") os.environ["MONGODB_URI"] = "0.0.0.0" self.addCleanup(self.remove_env, "MONGODB_URI") storage = MongoStorage() storage.conn() mongo_mock.assert_called_with("0.0.0.0")
def test_remove_instance(self): from redisapi.storage import MongoStorage storage = MongoStorage() instance = Instance( "xname", "plan", [{"host": "host", "container_id": "id", "port": "port"}]) storage.add_instance(instance) result = storage.find_instance_by_name(instance.name) endpoint = instance.endpoints[0] self.assertEqual(endpoint["container_id"], result.endpoints[0]["container_id"]) storage.remove_instance(instance) length = storage.conn()['redisapi']['instances'].find( {"name": instance.name}).count() self.assertEqual(length, 0)
def test_remove_instance(self): from redisapi.storage import MongoStorage storage = MongoStorage() instance = Instance("xname", "plan", [{ "host": "host", "container_id": "id", "port": "port" }]) storage.add_instance(instance) result = storage.find_instance_by_name(instance.name) endpoint = instance.endpoints[0] self.assertEqual(endpoint["container_id"], result.endpoints[0]["container_id"]) storage.remove_instance(instance) length = storage.conn()['redisapi']['instances'].find({ "name": instance.name }).count() self.assertEqual(length, 0)
class DockerManagerTest(unittest.TestCase): def remove_env(self, env): if env in os.environ: del os.environ[env] def setUp(self): os.environ["SENTINEL_HOSTS"] = '["http://host1.com:4243", \ "http://*****:*****@mock.patch("pyzabbix.ZabbixAPI") def test_hc_zabbix(self, zabix_mock): os.environ["ZABBIX_URL"] = "url" os.environ["ZABBIX_USER"] = "******" os.environ["ZABBIX_PASSWORD"] = "******" os.environ["HEALTH_CHECKER"] = "zabbix" self.addCleanup(self.remove_env, "HEALTH_CHECKER") os.environ["ZABBIX_HOST"] = "2" os.environ["ZABBIX_INTERFACE"] = "1" from redisapi.hc import ZabbixHealthCheck from redisapi.managers import DockerManager manager = DockerManager() self.assertIsInstance(manager.health_checker(), ZabbixHealthCheck) def test_add_instance(self): add_mock = mock.Mock() self.manager.config_sentinels = mock.Mock() self.manager.health_checker.return_value = add_mock client_mock = mock.Mock() client_mock.return_value = mock.Mock(base_url="http://*****:*****@mock.patch("redis.StrictRedis") def test_config_sentinels(self, redis_mock): master = {"host": "localhost", "port": "3333"} self.manager.config_sentinels("master_name", master) calls = [] sentinels = [ {"host": u"host1.com", "port": u"4243"}, {"host": u"localhost", "port": u"4243"}, {"host": u"host2.com", "port": u"4243"}, ] for sentinel in sentinels: host, port = sentinel["host"], sentinel["port"] sentinel_calls = [ mock.call(host=host, port=port), mock.call().sentinel( 'monitor', 'master_name', 'localhost', '3333', '1'), mock.call().sentinel( 'set', 'master_name', 'down-after-milliseconds', '5000'), mock.call().sentinel( 'set', 'master_name', 'failover-timeout', '60000'), mock.call().sentinel( 'set', 'master_name', 'parallel-syncs', '1'), ] calls.extend(sentinel_calls) redis_mock.assert_has_calls(calls) @mock.patch("redis.StrictRedis") def test_remove_from_sentinel(self, redis_mock): self.manager.remove_from_sentinel("master_name") calls = [] sentinels = [ {"host": u"host1.com", "port": u"4243"}, {"host": u"localhost", "port": u"4243"}, {"host": u"host2.com", "port": u"4243"}, ] for sentinel in sentinels: host, port = sentinel["host"], sentinel["port"] sentinel_calls = [ mock.call(host=host, port=port), mock.call().sentinel( 'remove', 'master_name'), ] calls.extend(sentinel_calls) redis_mock.assert_has_calls(calls) def test_port_range_start(self): self.assertEqual(49153, self.manager.port_range_start) def test_get_port_new_host(self): self.assertEqual(49153, self.manager.get_port_by_host("newhost")) def test_get_port_host_with_containers(self): instance = Instance( name="name", plan="basic", endpoints=[{"host": "newhost", "port": 49153, "container_id": "12"}], ) self.storage.add_instance(instance) self.assertEqual(49154, self.manager.get_port_by_host("newhost"))
class DockerManagerTest(unittest.TestCase): def remove_env(self, env): if env in os.environ: del os.environ[env] def setUp(self): os.environ["SENTINEL_HOSTS"] = '["http://host1.com:4243", \ "http://*****:*****@mock.patch("pyzabbix.ZabbixAPI") def test_hc_zabbix(self, zabix_mock): os.environ["ZABBIX_URL"] = "url" os.environ["ZABBIX_USER"] = "******" os.environ["ZABBIX_PASSWORD"] = "******" os.environ["HEALTH_CHECKER"] = "zabbix" self.addCleanup(self.remove_env, "HEALTH_CHECKER") os.environ["ZABBIX_HOST"] = "2" os.environ["ZABBIX_INTERFACE"] = "1" from redisapi.hc import ZabbixHealthCheck from redisapi.managers import DockerManager manager = DockerManager() self.assertIsInstance(manager.health_checker(), ZabbixHealthCheck) def test_add_instance(self): add_mock = mock.Mock() self.manager.config_sentinels = mock.Mock() self.manager.health_checker.return_value = add_mock client_mock = mock.Mock() client_mock.return_value = mock.Mock(base_url="http://*****:*****@mock.patch("redis.StrictRedis") def test_config_sentinels(self, redis_mock): master = {"host": "localhost", "port": "3333"} self.manager.config_sentinels("master_name", master) calls = [] sentinels = [ { "host": u"host1.com", "port": u"4243" }, { "host": u"localhost", "port": u"4243" }, { "host": u"host2.com", "port": u"4243" }, ] for sentinel in sentinels: host, port = sentinel["host"], sentinel["port"] sentinel_calls = [ mock.call(host=host, port=port), mock.call().sentinel('monitor', 'master_name', 'localhost', '3333', '1'), mock.call().sentinel('set', 'master_name', 'down-after-milliseconds', '5000'), mock.call().sentinel('set', 'master_name', 'failover-timeout', '60000'), mock.call().sentinel('set', 'master_name', 'parallel-syncs', '1'), ] calls.extend(sentinel_calls) redis_mock.assert_has_calls(calls) @mock.patch("redis.StrictRedis") def test_remove_from_sentinel(self, redis_mock): self.manager.remove_from_sentinel("master_name") calls = [] sentinels = [ { "host": u"host1.com", "port": u"4243" }, { "host": u"localhost", "port": u"4243" }, { "host": u"host2.com", "port": u"4243" }, ] for sentinel in sentinels: host, port = sentinel["host"], sentinel["port"] sentinel_calls = [ mock.call(host=host, port=port), mock.call().sentinel('remove', 'master_name'), ] calls.extend(sentinel_calls) redis_mock.assert_has_calls(calls) def test_port_range_start(self): self.assertEqual(49153, self.manager.port_range_start) def test_get_port_new_host(self): self.assertEqual(49153, self.manager.get_port_by_host("newhost")) def test_get_port_host_with_containers(self): instance = Instance( name="name", plan="basic", endpoints=[{ "host": "newhost", "port": 49153, "container_id": "12" }], ) self.storage.add_instance(instance) self.assertEqual(49154, self.manager.get_port_by_host("newhost"))