def test_bind(self): instance = Instance( name="name", plan='basic', endpoints=[ { "host": "localhost", "port": "4242", "container_id": "12" }, { "host": "host.com", "port": "422", "container_id": "12" }, ], ) result = self.manager.bind(instance) expected_redis = json.dumps(['localhost:4242', 'host.com:422']) expected_sentinels = json.dumps([ u'http://host1.com:4243', u'http://localhost:4243', u'http://host2.com:4243' ]) self.assertEqual(result['REDIS_HOSTS'], expected_redis) self.assertEqual(result['SENTINEL_HOSTS'], expected_sentinels) self.assertEqual(result['REDIS_MASTER'], instance.name)
def test_remove_instance(self): remove_mock = mock.Mock() self.manager.remove_from_sentinel = mock.Mock() self.manager.health_checker = mock.Mock() self.manager.health_checker.return_value = remove_mock self.manager.client = mock.Mock() self.manager.client.return_value = mock.Mock() instance = Instance( name="name", plan="basic", endpoints=[ {"host": "host", "port": 123, "container_id": "12"}, {"host": "host", "port": 123, "container_id": "12"}, {"host": "host", "port": 123, "container_id": "12"}, ], ) self.storage.add_instance(instance) self.manager.remove_instance(instance) remove_mock.remove.assert_called_with("host", 123) self.manager.client.assert_called_with("http://host:4243") self.manager.client().stop.assert_called_with( instance.endpoints[0]["container_id"]) self.manager.client().remove_container.assert_called( instance.endpoints[0]["container_id"]) self.storage.remove_instance(instance) self.manager.remove_from_sentinel.assert_called_with( instance.name)
def test_to_json(self): host = "host" id = "id" name = "name" port = "port" plan = "plan" endpoints = [{"port": port, "host": host, "container_id": id}] instance = Instance( name=name, plan=plan, endpoints=endpoints, ) expected = { 'name': name, 'plan': plan, 'endpoints': endpoints, } self.assertDictEqual(instance.to_json(), expected)
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"))
def test_bind_returns_the_server_host_and_port(self): instance = Instance( name='ble', plan='development', endpoints=[{"host": "localhost", "port": "6379"}], ) envs = self.manager.bind(instance) self.assertEqual( {"REDIS_HOST": "localhost", "REDIS_PORT": "6379"}, envs )
def test_bind_returns_the_REDIS_SERVER_PORT_when_its_defined(self): instance = Instance( name='ble', plan='development', endpoints=[{"host": "localhost", "port": "12345"}], ) envs = self.manager.bind(instance) want = { "REDIS_HOST": "localhost", "REDIS_PORT": "12345", } self.assertEqual(want, envs)
def test_find_instances_by_host(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_instances_by_host("host") self.assertEqual(instance.endpoints[0]["container_id"], result[0].endpoints[0]["container_id"]) storage.remove_instance(instance)
def test_manager_by_instance(self): os.environ["REDIS_IMAGE"] = "redisapi" os.environ["DOCKER_HOSTS"] = "[]" os.environ["SENTINEL_HOSTS"] = "[]" instance = Instance( name='name', plan='plus', endpoints=[{ "host": "host", "port": "port", "container_id": "id" }], ) manager = manager_by_instance(instance) self.assertIsInstance(manager, DockerHaManager)
def test_instance(self): host = "host" id = "id" name = "name" port = "port" plan = "plan" endpoints = [{"port": port, "host": host, "container_id": id}] instance = Instance( name=name, plan=plan, endpoints=endpoints, ) self.assertEqual(instance.name, name) self.assertEqual(instance.plan, plan) self.assertListEqual(instance.endpoints, endpoints)
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.db()['instances'].find({ "name": instance.name }).count() self.assertEqual(length, 0)
def test_bind(self): storage = MongoStorage() instance = Instance( name='myinstance', plan='development', endpoints=[{ "host": "host", "port": "port", "container_id": "id" }], ) storage.add_instance(instance) response = self.app.post("/resources/myinstance", data={"hostname": "something.tsuru.io"}) self.assertEqual(201, response.status_code) j = json.loads(response.data) endpoint = instance.endpoints[0] self.assertDictEqual( { "REDIS_HOST": endpoint["host"], "REDIS_PORT": endpoint["port"] }, j)
def test_revoke(self): instance = Instance( name="name", plan='basic', endpoints=[ { "host": "localhost", "port": "4242", "container_id": "12" }, { "host": "host.com", "port": "422", "container_id": "12" }, ], ) self.manager.grant(instance, "10.0.0.1") self.manager.grant(instance, "10.0.0.2") access_mngr = self.manager.access_manager self.manager.revoke(instance, "10.0.0.1") self.assertEqual(["10.0.0.2"], access_mngr.permits[instance.name])