示例#1
0
 def test_bind_instance_multiple_bind_hosts(self, LoadBalancer):
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.bind("x", "apphost.com")
     binding_data = self.storage.find_binding("x")
     self.assertDictEqual(
         binding_data, {
             "_id": "x",
             "app_host": "apphost.com",
             "paths": [{
                 "path": "/",
                 "destination": "apphost.com"
             }]
         })
     LoadBalancer.find.assert_called_with("x")
     manager.consul_manager.write_location.assert_called_with(
         "x", "/", destination="apphost.com")
     manager.consul_manager.reset_mock()
     manager.bind("x", "apphost.com")
     self.assertEqual(0, len(manager.consul_manager.mock_calls))
     with self.assertRaises(rpaas.manager.BindError):
         manager.bind("x", "another.host.com")
     self.assertEqual(0, len(manager.consul_manager.mock_calls))
示例#2
0
 def test_new_instance_with_plan(self, nginx):
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.new_instance("x", plan_name="small")
     host = self.Host.create.return_value
     config = copy.deepcopy(self.config)
     config.update(self.plan["config"])
     config[
         "HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x,consul_token:abc-123"
     lb = self.LoadBalancer.create.return_value
     self.Host.create.assert_called_with("my-host-manager", "x", config)
     self.LoadBalancer.create.assert_called_with("my-lb-manager", "x",
                                                 config)
     lb.add_host.assert_called_with(host)
     self.assertEquals(manager.storage.find_task("x").count(), 0)
     nginx.Nginx.assert_called_once_with(config)
     nginx_manager = nginx.Nginx.return_value
     nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name,
                                                            timeout=600)
     metadata = manager.storage.find_instance_metadata("x")
     self.assertEqual(
         {
             "_id": "x",
             "plan_name": "small",
             "consul_token": "abc-123"
         }, metadata)
示例#3
0
 def test_unbind_and_bind_instance_with_extra_path(self, LoadBalancer):
     self.storage.store_binding("inst", "app.host.com")
     self.storage.replace_binding_path("inst", "/me", "somewhere.com")
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.unbind("inst", "app.host.com")
     manager.bind("inst", "app2.host.com")
     binding_data = self.storage.find_binding("inst")
     self.assertDictEqual(
         binding_data, {
             "_id":
             "inst",
             "app_host":
             "app2.host.com",
             "paths": [{
                 "path": "/me",
                 "destination": "somewhere.com",
                 "content": None
             }, {
                 "path": "/",
                 "destination": "app2.host.com"
             }]
         })
     LoadBalancer.find.assert_called_with("inst")
     manager.consul_manager.remove_location.assert_called_with("inst", "/")
     manager.consul_manager.write_location.assert_called_with(
         "inst", "/", destination="app2.host.com")
示例#4
0
    def test_list_block(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.consul_manager.list_blocks.return_value = [{
            u'block_name':
            'server',
            u'content':
            'something nice in server'
        }, {
            u'block_name':
            'http',
            u'content':
            'something nice in http'
        }]
        blocks = manager.list_blocks("inst")

        self.assertDictEqual(blocks[0], {
            'block_name': 'server',
            'content': 'something nice in server'
        })
        self.assertDictEqual(blocks[1], {
            'block_name': 'http',
            'content': 'something nice in http'
        })
        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.list_blocks.assert_called_with("inst")
示例#5
0
 def test_node_status(self, LoadBalancer):
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     lb.hosts[0].dns_name = '10.1.1.1'
     lb.hosts[1].dns_name = '10.2.2.2'
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.node_hostname.side_effect = ['vm-1', 'vm-2']
     manager.consul_manager.node_status.return_value = {
         'vm-1': 'OK',
         'vm-2': 'DEAD'
     }
     node_status = manager.node_status("x")
     LoadBalancer.find.assert_called_with("x")
     self.assertDictEqual(
         node_status, {
             'vm-1': {
                 'status': 'OK',
                 'address': '10.1.1.1'
             },
             'vm-2': {
                 'status': 'DEAD',
                 'address': '10.2.2.2'
             }
         })
示例#6
0
    def test_add_route_with_content(self, LoadBalancer):
        self.storage.store_binding("inst", "app.host.com")
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.add_route("inst", "/somewhere", None,
                          "location /x { something; }")

        LoadBalancer.find.assert_called_with("inst")
        binding_data = self.storage.find_binding("inst")
        self.assertDictEqual(
            binding_data, {
                "_id":
                "inst",
                "app_host":
                "app.host.com",
                "paths": [{
                    "path": "/",
                    "destination": "app.host.com",
                }, {
                    "path": "/somewhere",
                    "destination": None,
                    "content": "location /x { something; }",
                }]
            })
        manager.consul_manager.write_location.assert_called_with(
            "inst",
            "/somewhere",
            destination=None,
            content="location /x { something; }")
示例#7
0
 def test_remove_instance(self):
     self.storage.store_instance_metadata("x",
                                          plan_name="small",
                                          consul_token="abc-123")
     self.storage.store_le_certificate("x", "foobar.com")
     self.storage.store_le_certificate("x", "example.com")
     self.storage.store_le_certificate("y", "test.com")
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.remove_instance("x")
     config = copy.deepcopy(self.config)
     config.update(self.plan["config"])
     self.LoadBalancer.find.assert_called_with("x", config)
     for h in lb.hosts:
         h.destroy.assert_called_once()
     lb.destroy.assert_called_once()
     self.assertEquals(self.storage.find_task("x").count(), 0)
     self.assertIsNone(self.storage.find_instance_metadata("x"))
     self.assertEquals([
         cert for cert in self.storage.find_le_certificates({"name": "x"})
     ], [])
     self.assertEquals([
         cert['name']
         for cert in self.storage.find_le_certificates({"name": "y"})
     ][0], "y")
     manager.consul_manager.destroy_token.assert_called_with("abc-123")
     manager.consul_manager.destroy_instance.assert_called_with("x")
示例#8
0
    def test_add_route_with_content(self, LoadBalancer):
        self.storage.store_binding("inst", "app.host.com")
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.add_route("inst", "/somewhere", None, "location /x { something; }")

        LoadBalancer.find.assert_called_with("inst")
        binding_data = self.storage.find_binding("inst")
        self.assertDictEqual(binding_data, {
            "_id": "inst",
            "app_host": "app.host.com",
            "paths": [
                {
                    "path": "/",
                    "destination": "app.host.com",
                },
                {
                    "path": "/somewhere",
                    "destination": None,
                    "content": "location /x { something; }",
                }
            ]
        })
        manager.consul_manager.write_location.assert_called_with("inst", "/somewhere", destination=None,
                                                                 content="location /x { something; }")
示例#9
0
 def test_bind_instance_with_route(self, LoadBalancer):
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     lb.hosts[0].dns_name = "h1"
     lb.hosts[1].dns_name = "h2"
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.add_route("x", "/somewhere", "my.other.host", None)
     manager.bind("x", "apphost.com")
     binding_data = self.storage.find_binding("x")
     self.assertDictEqual(
         binding_data, {
             "_id":
             "x",
             "app_host":
             "apphost.com",
             "paths": [{
                 "path": "/somewhere",
                 "destination": "my.other.host",
                 "content": None
             }, {
                 "path": "/",
                 "destination": "apphost.com"
             }]
         })
     LoadBalancer.find.assert_called_with("x")
     manager.consul_manager.write_location.assert_any_call(
         "x", "/somewhere", destination="my.other.host", content=None)
     manager.consul_manager.write_location.assert_any_call(
         "x", "/", destination="apphost.com")
示例#10
0
    def test_list_block(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.consul_manager.list_blocks.return_value = [
            '9796', [{u'LockIndex': 0,
                      u'ModifyIndex': 9796,
                      u'Value': 'something nice in server',
                      u'Flags': 0,
                      u'Key': u'test-suite-rpaas/myrpaas/blocks/server/ROOT',
                      u'CreateIndex': 9796},
                     {u'LockIndex': 0,
                      u'ModifyIndex': 9796,
                      u'Value': 'something nice in http',
                      u'Flags': 0,
                      u'Key': u'test-suite-rpaas/myrpaas/blocks/http/ROOT',
                      u'CreateIndex': 9796}]
        ]
        blocks = manager.list_blocks("inst")

        self.assertDictEqual(blocks[0], {'block_name': 'server',
                                         'content': 'something nice in server'})
        self.assertDictEqual(blocks[1], {'block_name': 'http',
                                         'content': 'something nice in http'})
        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.list_blocks.assert_called_with("inst")
示例#11
0
 def test_remove_instance_remove_task_on_exception(self):
     self.storage.store_instance_metadata("x", plan_name="small")
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(side_effect=Exception("test"))]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.remove_instance("x")
     self.assertEquals(self.storage.find_task("x").count(), 0)
示例#12
0
 def test_remove_instance_remove_task_on_exception(self):
     self.storage.store_instance_metadata("x", plan_name="small")
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(side_effect=Exception("test"))]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.remove_instance("x")
     self.assertEquals(self.storage.find_task("x").count(), 0)
示例#13
0
    def test_delete_block(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.delete_block("inst", "http")

        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.remove_block.assert_called_with("inst", "http")
示例#14
0
    def test_delete_block(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.delete_block("inst", "http")

        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.remove_block.assert_called_with("inst", "http")
示例#15
0
    def test_update_certificate(self, LoadBalancer):
        self.storage.store_binding("inst", "app.host.com")
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.update_certificate("inst", "cert", "key")

        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.set_certificate.assert_called_with("inst", "cert", "key")
示例#16
0
    def test_add_block_with_content(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.add_block("inst", "server", "location /x { something; }")

        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.write_block.assert_called_with(
            "inst", "server", "location /x { something; }")
示例#17
0
 def test_unbind_instance(self, LoadBalancer):
     self.storage.store_binding("inst", "app.host.com")
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.unbind("inst", "app.host.com")
     binding_data = self.storage.find_binding("inst")
     self.assertDictEqual(binding_data, {"_id": "inst", "paths": []})
     LoadBalancer.find.assert_called_with("inst")
     manager.consul_manager.remove_location.assert_called_with("inst", "/")
示例#18
0
    def test_update_certificate(self, LoadBalancer):
        self.storage.store_binding("inst", "app.host.com")
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.update_certificate("inst", "cert", "key")

        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.set_certificate.assert_called_with(
            "inst", "cert", "key")
示例#19
0
    def test_empty_list_blocks(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.consul_manager.list_blocks.return_value = []
        blocks = manager.list_blocks("inst")

        self.assertEqual(blocks, [])
        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.list_blocks.assert_called_with("inst")
示例#20
0
 def test_new_instance_host_create_fail_and_raises(self, nginx):
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     lb = self.LoadBalancer.create.return_value
     self.Host.create.side_effect = Exception("Host create failure")
     host = self.Host.create.return_value
     manager.new_instance("x")
     lb.add_host.assert_not_called()
     lb.destroy.assert_not_called()
     host.destroy.assert_not_called()
     self.assertEqual(self.storage.find_task("x").count(), 0)
示例#21
0
    def test_empty_list_blocks(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.consul_manager.list_blocks.return_value = []
        blocks = manager.list_blocks("inst")

        self.assertEqual(blocks, [])
        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.list_blocks.assert_called_with("inst")
示例#22
0
    def test_add_block_with_content(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.add_block("inst", "server", "location /x { something; }")

        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.write_block.assert_called_with(
            "inst", "server", "location /x { something; }"
        )
示例#23
0
 def test_node_status_no_hostname(self, LoadBalancer):
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     lb.hosts[0].dns_name = '10.1.1.1'
     lb.hosts[1].dns_name = '10.2.2.2'
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.node_hostname.side_effect = ['vm-1', None]
     manager.consul_manager.node_status.return_value = {'vm-1': 'OK', 'vm-2': 'DEAD'}
     node_status = manager.node_status("x")
     LoadBalancer.find.assert_called_with("x")
     self.assertDictEqual(node_status, {'vm-1': {'status': 'OK', 'address': '10.1.1.1'},
                                        'vm-2': {'status': 'DEAD'}})
示例#24
0
 def test_unbind_instance(self, LoadBalancer):
     self.storage.store_binding("inst", "app.host.com")
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.unbind("inst", "app.host.com")
     binding_data = self.storage.find_binding("inst")
     self.assertDictEqual(binding_data, {
         "_id": "inst",
         "paths": []
     })
     LoadBalancer.find.assert_called_with("inst")
     manager.consul_manager.remove_location.assert_called_with("inst", "/")
示例#25
0
 def test_new_instance_lb_create_fail_and_rollback(self, nginx):
     config = copy.deepcopy(self.config)
     config["RPAAS_ROLLBACK_ON_ERROR"] = "1"
     manager = Manager(config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     self.LoadBalancer.create.side_effect = Exception("LB create failure")
     lb = self.LoadBalancer.create.return_value
     host = self.Host.create.return_value
     manager.new_instance("x")
     lb.add_host.assert_not_called()
     lb.destroy.assert_not_called()
     host.destroy.assert_called_once()
     self.assertEqual(self.storage.find_task("x").count(), 0)
示例#26
0
 def test_remove_instance_no_token(self):
     self.storage.store_task("x")
     self.storage.store_instance_metadata("x", plan_name="small")
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.remove_instance("x")
     self.LoadBalancer.find.assert_called_with("x", self.config)
     for h in lb.hosts:
         h.destroy.assert_called_once()
     lb.destroy.assert_called_once()
     self.assertEquals(self.storage.find_task("x").count(), 0)
     self.assertIsNone(self.storage.find_instance_metadata("x"))
     manager.consul_manager.destroy_token.assert_not_called()
示例#27
0
 def test_bind_instance(self, LoadBalancer):
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     lb.hosts[0].dns_name = "h1"
     lb.hosts[1].dns_name = "h2"
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.bind("x", "apphost.com")
     binding_data = self.storage.find_binding("x")
     self.assertDictEqual(binding_data, {
         "_id": "x",
         "app_host": "apphost.com",
         "paths": [{"path": "/", "destination": "apphost.com"}]
     })
     LoadBalancer.find.assert_called_with("x")
     manager.consul_manager.write_location.assert_called_with("x", "/", destination="apphost.com")
示例#28
0
 def test_scale_instance_down_with_healing_enabled(self, consul_manager):
     consul = consul_manager.ConsulManager.return_value
     config = copy.deepcopy(self.config)
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     lb.hosts[0].dns_name = '10.2.2.2'
     self.storage.store_instance_metadata("x", consul_token="abc-123")
     self.addCleanup(self.storage.remove_instance_metadata, "x")
     consul.node_hostname.return_value = 'rpaas-2'
     manager = Manager(config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.scale_instance("x", 1)
     lb.hosts[0].destroy.assert_called_once
     lb.remove_host.assert_called_once_with(lb.hosts[0])
     consul.remove_node.assert_called_once_with('x', 'rpaas-2')
示例#29
0
 def test_scale_instance_down_with_healing_enabled(self, consul_manager):
     consul = consul_manager.ConsulManager.return_value
     config = copy.deepcopy(self.config)
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     lb.hosts[0].dns_name = '10.2.2.2'
     self.storage.store_instance_metadata("x", consul_token="abc-123")
     self.addCleanup(self.storage.remove_instance_metadata, "x")
     consul.node_hostname.return_value = 'rpaas-2'
     manager = Manager(config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.scale_instance("x", 1)
     lb.hosts[0].destroy.assert_called_once
     lb.remove_host.assert_called_once_with(lb.hosts[0])
     consul.remove_node.assert_called_once_with('x', 'rpaas-2')
示例#30
0
 def test_remove_instance_no_token(self):
     self.storage.store_instance_metadata("x", plan_name="small")
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.remove_instance("x")
     config = copy.deepcopy(self.config)
     config.update(self.plan["config"])
     self.LoadBalancer.find.assert_called_with("x", config)
     for h in lb.hosts:
         h.destroy.assert_called_once()
     lb.destroy.assert_called_once()
     self.assertEquals(self.storage.find_task("x").count(), 0)
     self.assertIsNone(self.storage.find_instance_metadata("x"))
     manager.consul_manager.destroy_token.assert_not_called()
示例#31
0
 def test_unbind_instance_with_extra_path(self, LoadBalancer):
     self.storage.store_binding("inst", "app.host.com")
     self.storage.replace_binding_path("inst", "/me", "somewhere.com")
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.unbind("inst", "app.host.com")
     binding_data = self.storage.find_binding("inst")
     self.assertDictEqual(binding_data, {
         "_id": "inst",
         "paths": [
             {"path": "/me", "destination": "somewhere.com", "content": None}
         ]
     })
     LoadBalancer.find.assert_called_with("inst")
     manager.consul_manager.remove_location.assert_called_with("inst", "/")
示例#32
0
 def test_new_instance(self, nginx):
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.new_instance("x")
     host = self.Host.create.return_value
     lb = self.LoadBalancer.create.return_value
     config = copy.deepcopy(self.config)
     config["HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x,consul_token:abc-123"
     self.Host.create.assert_called_with("my-host-manager", "x", config)
     self.LoadBalancer.create.assert_called_with("my-lb-manager", "x", config)
     lb.add_host.assert_called_with(host)
     self.assertEquals(self.storage.find_task("x").count(), 0)
     nginx.Nginx.assert_called_once_with(config)
     nginx_manager = nginx.Nginx.return_value
     nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name, timeout=600)
     manager.consul_manager.write_healthcheck.assert_called_once_with("x")
示例#33
0
 def test_remove_instance(self):
     self.storage.store_instance_metadata("x", plan_name="small", consul_token="abc-123")
     lb = self.LoadBalancer.find.return_value
     lb.hosts = [mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.remove_instance("x")
     config = copy.deepcopy(self.config)
     config.update(self.plan["config"])
     self.LoadBalancer.find.assert_called_with("x", config)
     for h in lb.hosts:
         h.destroy.assert_called_once()
     lb.destroy.assert_called_once()
     self.assertEquals(self.storage.find_task("x").count(), 0)
     self.assertIsNone(self.storage.find_instance_metadata("x"))
     manager.consul_manager.destroy_token.assert_called_with("abc-123")
     manager.consul_manager.destroy_instance.assert_called_with("x")
示例#34
0
    def test_delete_route(self, LoadBalancer):
        self.storage.store_binding("inst", "app.host.com")
        self.storage.replace_binding_path("inst", "/arrakis", "dune.com")
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.delete_route("inst", "/arrakis")

        LoadBalancer.find.assert_called_with("inst")
        binding_data = self.storage.find_binding("inst")
        self.assertDictEqual(binding_data, {
            "_id": "inst",
            "app_host": "app.host.com",
            "paths": [{"path": "/", "destination": "app.host.com"}]
        })
        manager.consul_manager.remove_location.assert_called_with("inst", "/arrakis")
示例#35
0
 def test_bind_instance_multiple_bind_hosts(self, LoadBalancer):
     lb = LoadBalancer.find.return_value
     lb.hosts = [mock.Mock(), mock.Mock()]
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.bind("x", "apphost.com")
     binding_data = self.storage.find_binding("x")
     self.assertDictEqual(binding_data, {
         "_id": "x",
         "app_host": "apphost.com",
         "paths": [{"path": "/", "destination": "apphost.com"}]
     })
     LoadBalancer.find.assert_called_with("x")
     manager.consul_manager.write_location.assert_called_with("x", "/", destination="apphost.com")
     manager.consul_manager.reset_mock()
     manager.bind("x", "apphost.com")
     self.assertEqual(0, len(manager.consul_manager.mock_calls))
     with self.assertRaises(rpaas.manager.BindError):
         manager.bind("x", "another.host.com")
     self.assertEqual(0, len(manager.consul_manager.mock_calls))
示例#36
0
 def test_scale_instance_up_no_token(self, nginx):
     lb = self.LoadBalancer.find.return_value
     lb.name = "x"
     lb.hosts = [mock.Mock(), mock.Mock()]
     config = copy.deepcopy(self.config)
     config["HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x,consul_token:abc-123"
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.scale_instance("x", 5)
     self.Host.create.assert_called_with("my-host-manager", "x", config)
     self.assertEqual(self.Host.create.call_count, 3)
     lb.add_host.assert_called_with(self.Host.create.return_value)
     self.assertEqual(lb.add_host.call_count, 3)
     nginx_manager = nginx.Nginx.return_value
     created_host = self.Host.create.return_value
     expected_calls = [mock.call(created_host.dns_name, timeout=600),
                       mock.call(created_host.dns_name, timeout=600),
                       mock.call(created_host.dns_name, timeout=600)]
     self.assertEqual(expected_calls, nginx_manager.wait_healthcheck.call_args_list)
示例#37
0
    def test_list_block(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.consul_manager.list_blocks.return_value = [
                {u'block_name': 'server',
                 u'content': 'something nice in server'},
                {u'block_name': 'http',
                 u'content': 'something nice in http'}
        ]
        blocks = manager.list_blocks("inst")

        self.assertDictEqual(blocks[0], {'block_name': 'server',
                                         'content': 'something nice in server'})
        self.assertDictEqual(blocks[1], {'block_name': 'http',
                                         'content': 'something nice in http'})
        LoadBalancer.find.assert_called_with("inst")
        manager.consul_manager.list_blocks.assert_called_with("inst")
示例#38
0
 def test_new_instance_nginx_wait_healthcheck_fail_and_rollback(
         self, nginx, hc):
     config = copy.deepcopy(self.config)
     config["RPAAS_ROLLBACK_ON_ERROR"] = "1"
     manager = Manager(config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     lb = self.LoadBalancer.create.return_value
     host = self.Host.create.return_value
     dumb_hc = hc.return_value
     nginx_manager = nginx.Nginx.return_value
     nginx_manager.wait_healthcheck.side_effect = Exception("Nginx timeout")
     manager.new_instance("x")
     self.LoadBalancer.create.assert_called_once()
     lb.add_host.assert_called_once()
     dumb_hc.add_url.assert_not_called()
     lb.destroy.assert_called_once()
     host.destroy.assert_called_once()
     lb.remove_host.assert_not_called()
     dumb_hc.destroy.assert_called_once()
     self.assertEqual(self.storage.find_task("x").count(), 0)
示例#39
0
 def test_new_instance_with_extra_tags(self, nginx):
     config = copy.deepcopy(self.config)
     config["INSTANCE_EXTRA_TAGS"] = "enable_monitoring:1"
     manager = Manager(config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.new_instance("x")
     host = self.Host.create.return_value
     config["HOST_TAGS"] = (
         "rpaas_service:test-suite-rpaas,rpaas_instance:x,"
         "consul_token:abc-123,enable_monitoring:1")
     del config["INSTANCE_EXTRA_TAGS"]
     lb = self.LoadBalancer.create.return_value
     self.Host.create.assert_called_with("my-host-manager", "x", config)
     self.LoadBalancer.create.assert_called_with("my-lb-manager", "x",
                                                 config)
     lb.add_host.assert_called_with(host)
     self.assertEquals(manager.storage.find_task("x").count(), 0)
     nginx.Nginx.assert_called_once_with(config)
     nginx_manager = nginx.Nginx.return_value
     nginx_manager.wait_healthcheck.assert_called_once_with(host.dns_name,
                                                            timeout=600)
示例#40
0
    def test_add_route_no_binding_creates_one(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.add_route("inst", "/somewhere", "my.other.host", None)

        LoadBalancer.find.assert_called_with("inst")
        binding_data = self.storage.find_binding("inst")
        self.assertDictEqual(
            binding_data, {
                "_id":
                "inst",
                "paths": [{
                    "path": "/somewhere",
                    "destination": "my.other.host",
                    "content": None,
                }]
            })
        manager.consul_manager.write_location.assert_called_with(
            "inst", "/somewhere", destination="my.other.host", content=None)
示例#41
0
    def test_delete_route(self, LoadBalancer):
        self.storage.store_binding("inst", "app.host.com")
        self.storage.replace_binding_path("inst", "/arrakis", "dune.com")
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.delete_route("inst", "/arrakis")

        LoadBalancer.find.assert_called_with("inst")
        binding_data = self.storage.find_binding("inst")
        self.assertDictEqual(
            binding_data, {
                "_id": "inst",
                "app_host": "app.host.com",
                "paths": [{
                    "path": "/",
                    "destination": "app.host.com"
                }]
            })
        manager.consul_manager.remove_location.assert_called_with(
            "inst", "/arrakis")
示例#42
0
    def test_add_route_no_binding_creates_one(self, LoadBalancer):
        lb = LoadBalancer.find.return_value
        lb.hosts = [mock.Mock(), mock.Mock()]

        manager = Manager(self.config)
        manager.consul_manager = mock.Mock()
        manager.add_route("inst", "/somewhere", "my.other.host", None)

        LoadBalancer.find.assert_called_with("inst")
        binding_data = self.storage.find_binding("inst")
        self.assertDictEqual(binding_data, {
            "_id": "inst",
            "paths": [
                {
                    "path": "/somewhere",
                    "destination": "my.other.host",
                    "content": None,
                }
            ]
        })
        manager.consul_manager.write_location.assert_called_with("inst", "/somewhere",
                                                                 destination="my.other.host",
                                                                 content=None)
示例#43
0
 def test_scale_instance_up_no_token(self, nginx):
     lb = self.LoadBalancer.find.return_value
     lb.name = "x"
     lb.hosts = [mock.Mock(), mock.Mock()]
     config = copy.deepcopy(self.config)
     config[
         "HOST_TAGS"] = "rpaas_service:test-suite-rpaas,rpaas_instance:x,consul_token:abc-123"
     manager = Manager(self.config)
     manager.consul_manager = mock.Mock()
     manager.consul_manager.generate_token.return_value = "abc-123"
     manager.scale_instance("x", 5)
     self.Host.create.assert_called_with("my-host-manager", "x", config)
     self.assertEqual(self.Host.create.call_count, 3)
     lb.add_host.assert_called_with(self.Host.create.return_value)
     self.assertEqual(lb.add_host.call_count, 3)
     nginx_manager = nginx.Nginx.return_value
     created_host = self.Host.create.return_value
     expected_calls = [
         mock.call(created_host.dns_name, timeout=600),
         mock.call(created_host.dns_name, timeout=600),
         mock.call(created_host.dns_name, timeout=600)
     ]
     self.assertEqual(expected_calls,
                      nginx_manager.wait_healthcheck.call_args_list)