Beispiel #1
0
 def test_mongodb_dbaas_uri_environ(self, mongo_mock):
     from redisapi.storage import MongoStorage
     os.environ["DBAAS_MONGODB_ENDPOINT"] = "0.0.0.1"
     self.addCleanup(self.remove_env, "DBAAS_MONGODB_ENDPOINT")
     storage = MongoStorage()
     storage.db()
     mongo_mock.assert_called_with("0.0.0.1")
Beispiel #2
0
 def test_mongodb_dbaas_uri_environ(self, mongo_mock):
     from redisapi.storage import MongoStorage
     os.environ["DBAAS_MONGODB_ENDPOINT"] = "0.0.0.1"
     self.addCleanup(self.remove_env, "DBAAS_MONGODB_ENDPOINT")
     storage = MongoStorage()
     storage.db()
     mongo_mock.assert_called_with("0.0.0.1")
Beispiel #3
0
 def test_mongodb_dbaas_database_name_from_url(self, mongo_mock):
     from redisapi.storage import MongoStorage
     os.environ["DBAAS_MONGODB_ENDPOINT"] = "0.0.0.1"
     self.addCleanup(self.remove_env, "DBAAS_MONGODB_ENDPOINT")
     storage = MongoStorage()
     storage.db()
     mongo_mock.assert_called_with("0.0.0.1")
     mongo_mock.return_value.get_default_database.assert_called_with()
Beispiel #4
0
 def test_mongodb_dbaas_database_name_from_url(self, mongo_mock):
     from redisapi.storage import MongoStorage
     os.environ["DBAAS_MONGODB_ENDPOINT"] = "0.0.0.1"
     self.addCleanup(self.remove_env, "DBAAS_MONGODB_ENDPOINT")
     storage = MongoStorage()
     storage.db()
     mongo_mock.assert_called_with("0.0.0.1")
     mongo_mock.return_value.get_default_database.assert_called_with()
Beispiel #5
0
 def setUp(self):
     os.environ["REDIS_IMAGE"] = "redisapi"
     self.addCleanup(self.remove_env, "REDIS_IMAGE")
     os.environ["DOCKER_HOSTS"] = '["http://host1.com:4243", \
         "http://localhost:4243", "http://host2.com:4243"]'
     self.addCleanup(self.remove_env, "DOCKER_HOSTS")
     os.environ["SENTINEL_HOSTS"] = '["http://host1.com:4243", \
         "http://localhost:4243", "http://host2.com:4243"]'
     self.addCleanup(self.remove_env, "SENTINEL_HOSTS")
     self.manager = DockerHaManager()
     self.storage = MongoStorage()
Beispiel #6
0
    def test_mongodb_dbaas_database_name_environ(self, mongo_mock):
        from redisapi.storage import MongoStorage
        os.environ["DBAAS_MONGODB_ENDPOINT"] = "0.0.0.1"
        os.environ["DATABASE_NAME"] = "xxxx"
        self.addCleanup(self.remove_env, "DBAAS_MONGODB_ENDPOINT")

        def error():
            from pymongo.errors import ConfigurationError
            raise ConfigurationError()
        mongo_mock.return_value.get_default_database.side_effect = error
        storage = MongoStorage()
        storage.db()
        mongo_mock.assert_called_with("0.0.0.1")
        mongo_mock.return_value.__getitem__.assert_called_with("xxxx")
Beispiel #7
0
    def test_mongodb_dbaas_database_name_environ(self, mongo_mock):
        from redisapi.storage import MongoStorage
        os.environ["DBAAS_MONGODB_ENDPOINT"] = "0.0.0.1"
        os.environ["DATABASE_NAME"] = "xxxx"
        self.addCleanup(self.remove_env, "DBAAS_MONGODB_ENDPOINT")

        def error():
            from pymongo.errors import ConfigurationError
            raise ConfigurationError()

        mongo_mock.return_value.get_default_database.side_effect = error
        storage = MongoStorage()
        storage.db()
        mongo_mock.assert_called_with("0.0.0.1")
        mongo_mock.return_value.__getitem__.assert_called_with("xxxx")
Beispiel #8
0
 def test_bind_app(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/bind-app",
         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)
Beispiel #9
0
    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")
Beispiel #10
0
    def setUp(self):
        os.environ["SENTINEL_HOSTS"] = '["http://host1.com:4243", \
            "http://localhost:4243", "http://host2.com:4243"]'

        self.addCleanup(self.remove_env, "SENTINEL_HOSTS")
        os.environ["REDIS_SERVER_HOST"] = "localhost"
        self.addCleanup(self.remove_env, "REDIS_SERVER_HOST")
        os.environ["REDIS_IMAGE"] = "redisapi"
        self.addCleanup(self.remove_env, "REDIS_IMAGE")
        os.environ["DOCKER_HOSTS"] = '["http://host1.com:4243", \
            "http://localhost:4243"]'

        self.addCleanup(self.remove_env, "DOCKER_HOSTS")
        from redisapi.managers import DockerManager
        self.manager = DockerManager()
        client_mock = mock.Mock()
        client_mock.return_value = mock.Mock()
        self.manager.client = client_mock
        self.manager.health_checker = mock.Mock()
        self.storage = MongoStorage()
Beispiel #11
0
 def setUp(self):
     os.environ["REDIS_IMAGE"] = "redisapi"
     self.addCleanup(self.remove_env, "REDIS_IMAGE")
     os.environ["DOCKER_HOSTS"] = '["http://host1.com:4243", \
         "http://localhost:4243", "http://host2.com:4243"]'
     self.addCleanup(self.remove_env, "DOCKER_HOSTS")
     os.environ["SENTINEL_HOSTS"] = '["http://host1.com:4243", \
         "http://localhost:4243", "http://host2.com:4243"]'
     self.addCleanup(self.remove_env, "SENTINEL_HOSTS")
     self.manager = DockerHaManager()
     self.storage = MongoStorage()
Beispiel #12
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)
Beispiel #13
0
 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)
Beispiel #14
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.db()['instances'].find({
         "name": instance.name
     }).count()
     self.assertEqual(length, 0)
Beispiel #15
0
 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)
Beispiel #16
0
    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")
Beispiel #17
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 setUp(self):
     os.environ["SENTINEL_HOSTS"] = '["http://host1.com:4243", \
         "http://localhost:4243", "http://host2.com:4243"]'
     self.addCleanup(self.remove_env, "SENTINEL_HOSTS")
     os.environ["REDIS_SERVER_HOST"] = "localhost"
     self.addCleanup(self.remove_env, "REDIS_SERVER_HOST")
     os.environ["REDIS_IMAGE"] = "redisapi"
     self.addCleanup(self.remove_env, "REDIS_IMAGE")
     os.environ["DOCKER_HOSTS"] = '["http://host1.com:4243", \
         "http://localhost:4243"]'
     self.addCleanup(self.remove_env, "DOCKER_HOSTS")
     from redisapi.managers import DockerManager
     self.manager = DockerManager()
     client_mock = mock.Mock()
     client_mock.return_value = mock.Mock()
     self.manager.client = client_mock
     self.manager.health_checker = mock.Mock()
     self.storage = MongoStorage()
Beispiel #19
0
class DockerHaManagerTest(unittest.TestCase):

    def remove_env(self, env):
        if env in os.environ:
            del os.environ[env]

    def setUp(self):
        os.environ["REDIS_IMAGE"] = "redisapi"
        self.addCleanup(self.remove_env, "REDIS_IMAGE")
        os.environ["DOCKER_HOSTS"] = '["http://host1.com:4243", \
            "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_slave_of(self, redis_mock):
        redis_instance_mock = mock.Mock()
        redis_mock.return_value = redis_instance_mock
        master = {"host": "localhost", "port": "3333"}
        slave = {"host": "myhost", "port": "9999"}
        self.manager.slave_of(master, slave)
        redis_mock.assert_called_with(
            host=str(slave["host"]), port=str(slave["port"]))
        redis_instance_mock.slaveof.assert_called_with(
            master["host"], master["port"])

    def test_add_instance(self):
        add_mock = mock.Mock()
        self.manager.health_checker = mock.Mock()
        self.manager.health_checker.return_value = add_mock
        self.manager.slave_of = mock.Mock()
        self.manager.get_port_by_host = mock.Mock()
        self.manager.get_port_by_host.return_value = 49153
        self.manager.config_sentinels = mock.Mock()
        client_mock = mock.Mock()
        client_mock.return_value = mock.Mock(base_url="http://*****:*****@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_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_grant(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.assertEqual(["10.0.0.1", "10.0.0.2"], access_mngr.permits[instance.name])

    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])

    def test_port_range_start(self):
        self.assertEqual(49153, self.manager.port_range_start)

    def test_get_port(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"))
Beispiel #20
0
class DockerHaManagerTest(unittest.TestCase):
    def remove_env(self, env):
        if env in os.environ:
            del os.environ[env]

    def setUp(self):
        os.environ["REDIS_IMAGE"] = "redisapi"
        self.addCleanup(self.remove_env, "REDIS_IMAGE")
        os.environ["DOCKER_HOSTS"] = '["http://host1.com:4243", \
            "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_slave_of(self, redis_mock):
        redis_instance_mock = mock.Mock()
        redis_mock.return_value = redis_instance_mock
        master = {"host": "localhost", "port": "3333"}
        slave = {"host": "myhost", "port": "9999"}
        self.manager.slave_of(master, slave)
        redis_mock.assert_called_with(host=str(slave["host"]),
                                      port=str(slave["port"]))
        redis_instance_mock.slaveof.assert_called_with(master["host"],
                                                       master["port"])

    def test_add_instance(self):
        add_mock = mock.Mock()
        self.manager.health_checker = mock.Mock()
        self.manager.health_checker.return_value = add_mock
        self.manager.slave_of = mock.Mock()
        self.manager.get_port_by_host = mock.Mock()
        self.manager.get_port_by_host.return_value = 49153
        self.manager.config_sentinels = mock.Mock()
        client_mock = mock.Mock()
        client_mock.return_value = mock.Mock(base_url="http://*****:*****@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_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_grant(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.assertEqual(["10.0.0.1", "10.0.0.2"],
                         access_mngr.permits[instance.name])

    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])

    def test_port_range_start(self):
        self.assertEqual(49153, self.manager.port_range_start)

    def test_get_port(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"))
Beispiel #22
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"))