def setUp(self, res):
     res.return_value.instances.filter.return_value = instances()
     self.manager = EtcdManager()
     self.manager.get_my_instance()
     self.manager.instance_id = 'i-deadbeef3'
     self.manager.region = 'eu-west-1'
     self.keeper = HouseKeeper(self.manager, 'test.')
     self.members_changed = self.keeper.members_changed()
Пример #2
0
 def set_up(self):
     subprocess.Popen = Popen
     requests.get = requests_get
     requests.put = requests_put
     requests.delete = requests_delete
     boto.ec2.connect_to_region = boto_ec2_connect_to_region
     boto.route53.connect_to_region = boto_route53_connect_to_region
     self.manager = EtcdManager()
     self.manager.get_my_instace()
     self.manager.instance_id = 'i-deadbeef3'
     self.manager.region = 'eu-west-1'
     self.keeper = HouseKeeper(self.manager, 'test.')
     self.members_changed = self.keeper.members_changed()
Пример #3
0
class TestHouseKeeper(unittest.TestCase):
    def __init__(self, method_name='runTest'):
        self.setUp = self.set_up
        super(TestHouseKeeper, self).__init__(method_name)

    def set_up(self):
        subprocess.Popen = Popen
        requests.get = requests_get
        requests.put = requests_put
        requests.delete = requests_delete
        boto.ec2.connect_to_region = boto_ec2_connect_to_region
        boto.route53.connect_to_region = boto_route53_connect_to_region
        self.manager = EtcdManager()
        self.manager.get_my_instace()
        self.manager.instance_id = 'i-deadbeef3'
        self.manager.region = 'eu-west-1'
        self.keeper = HouseKeeper(self.manager, 'test.')
        self.members_changed = self.keeper.members_changed()

    def test_members_changed(self):
        self.assertEqual(self.members_changed, True)
        self.keeper.members['blabla'] = True
        self.assertEqual(self.keeper.members_changed(), True)

    def test_is_leader(self):
        self.assertEqual(self.keeper.is_leader(), True)

    def test_acquire_lock(self):
        self.assertEqual(self.keeper.acquire_lock(), True)

    def test_remove_unhealthy_members(self):
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertEqual(
            self.keeper.remove_unhealthy_members(autoscaling_members), None)

    def test_update_route53_records(self):
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertEqual(
            self.keeper.update_route53_records(autoscaling_members), None)
        self.keeper.hosted_zone = 'bla'
        self.assertEqual(
            self.keeper.update_route53_records(autoscaling_members), None)
        self.keeper.hosted_zone = 'test2'
        self.assertEqual(
            self.keeper.update_route53_records(autoscaling_members), None)

    def test_cluster_unhealthy(self):
        self.assertEqual(self.keeper.cluster_unhealthy(), True)

    def test_run(self):
        time.sleep = raise_exception
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.manager.etcd_pid = 1
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.is_leader = raise_exception
        self.assertRaises(Exception, self.keeper.run)
class TestHouseKeeper(unittest.TestCase):

    def __init__(self, method_name='runTest'):
        self.setUp = self.set_up
        super(TestHouseKeeper, self).__init__(method_name)

    def set_up(self):
        subprocess.Popen = Popen
        requests.get = requests_get
        requests.put = requests_put
        requests.delete = requests_delete
        boto.ec2.connect_to_region = boto_ec2_connect_to_region
        boto.route53.connect_to_region = boto_route53_connect_to_region
        self.manager = EtcdManager()
        self.manager.get_my_instace()
        self.manager.instance_id = 'i-deadbeef3'
        self.manager.region = 'eu-west-1'
        self.keeper = HouseKeeper(self.manager, 'test.')
        self.members_changed = self.keeper.members_changed()

    def test_members_changed(self):
        self.assertEqual(self.members_changed, True)
        self.keeper.members['blabla'] = True
        self.assertEqual(self.keeper.members_changed(), True)
        self.assertEqual(self.keeper.members_changed(), False)

    def test_is_leader(self):
        self.assertEqual(self.keeper.is_leader(), True)

    def test_acquire_lock(self):
        self.assertEqual(self.keeper.acquire_lock(), True)

    def test_remove_unhealthy_members(self):
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertEqual(self.keeper.remove_unhealthy_members(autoscaling_members), None)

    def test_update_route53_records(self):
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertEqual(self.keeper.update_route53_records(autoscaling_members), None)
        self.keeper.hosted_zone = 'bla'
        self.assertEqual(self.keeper.update_route53_records(autoscaling_members), None)
        self.keeper.hosted_zone = 'test2'
        self.assertEqual(self.keeper.update_route53_records(autoscaling_members), None)

    def test_cluster_unhealthy(self):
        self.assertEqual(self.keeper.cluster_unhealthy(), True)

    def test_run(self):
        time.sleep = raise_exception
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.manager.etcd_pid = 1
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.is_leader = raise_exception
        self.assertRaises(Exception, self.keeper.run)
 def setUp(self, res):
     res.return_value.instances.filter.return_value = instances()
     self.manager = EtcdManager()
     self.manager.get_my_instance()
     self.manager.instance_id = 'i-deadbeef3'
     self.manager.region = 'eu-west-1'
     self.keeper = HouseKeeper(self.manager, 'test.')
     self.members_changed = self.keeper.members_changed()
Пример #6
0
 def set_up(self):
     subprocess.Popen = Popen
     requests.get = requests_get
     requests.put = requests_put
     requests.delete = requests_delete
     boto.ec2.connect_to_region = boto_ec2_connect_to_region
     boto.route53.connect_to_region = boto_route53_connect_to_region
     self.manager = EtcdManager()
     self.manager.get_my_instace()
     self.manager.instance_id = 'i-deadbeef3'
     self.manager.region = 'eu-west-1'
     self.keeper = HouseKeeper(self.manager, 'test.')
     self.members_changed = self.keeper.members_changed()
class TestHouseKeeper(unittest.TestCase):
    @patch('requests.get', requests_get)
    @patch('boto3.resource')
    def setUp(self, res):
        res.return_value.instances.filter.return_value = instances()
        self.manager = EtcdManager()
        self.manager.get_my_instance()
        self.manager.instance_id = 'i-deadbeef3'
        self.manager.region = 'eu-west-1'
        self.keeper = HouseKeeper(self.manager, 'test.')
        self.members_changed = self.keeper.members_changed()

    @patch('requests.get', requests_get)
    def test_members_changed(self):
        self.assertTrue(self.members_changed)
        self.keeper.members['blabla'] = True
        self.assertTrue(self.keeper.members_changed())
        self.assertFalse(self.keeper.members_changed())

    @patch('requests.get', requests_get)
    def test_is_leader(self):
        self.assertTrue(self.keeper.is_leader())

    @patch('requests.put', requests_put)
    def test_acquire_lock(self):
        self.assertTrue(self.keeper.acquire_lock())

    @patch('requests.delete', requests_delete)
    @patch('boto3.resource')
    def test_remove_unhealthy_members(self, res):
        res.return_value.instances.filter.return_value = instances()
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertIsNone(
            self.keeper.remove_unhealthy_members(autoscaling_members))

    @patch('boto3.resource')
    @patch('boto3.client')
    def test_update_route53_records(self, cli, res):
        cli.return_value.list_hosted_zones_by_name.return_value = {
            'HostedZones': [{
                'Id': '',
                'Name': 'test.'
            }]
        }
        res.return_value.instances.filter.return_value = instances()
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertIsNone(
            self.keeper.update_route53_records(autoscaling_members))
        self.keeper.hosted_zone = 'bla'
        self.assertRaises(Exception, self.keeper.update_route53_records,
                          autoscaling_members)

    @patch('subprocess.Popen', Popen)
    def test_cluster_unhealthy(self):
        self.assertTrue(self.keeper.cluster_unhealthy())

    @patch('logging.exception', Mock(side_effect=Exception))
    @patch('os.kill', Mock())
    @patch('time.sleep', Mock(side_effect=Exception))
    @patch('requests.get', requests_get)
    @patch('requests.put', requests_put)
    @patch('requests.delete', requests_delete)
    @patch('subprocess.Popen', Popen)
    @patch('boto3.resource')
    @patch('boto3.client')
    def test_run(self, cli, res):
        cli.return_value.list_hosted_zones_by_name.return_value = {
            'HostedZones': [{
                'Id': '',
                'Name': 'test.'
            }]
        }
        res.return_value.instances.filter.return_value = instances()
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.manager.etcd_pid = 1
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.is_leader = Mock(side_effect=Exception)
        self.assertRaises(Exception, self.keeper.run)
        with patch('time.sleep', Mock()):
            self.keeper.is_leader = Mock(return_value=False)
            self.keeper.manager.run_old = True
            self.keeper.cluster_unhealthy = Mock(
                side_effect=[False, True, False])
            self.assertRaises(Exception, self.keeper.run)
            self.keeper.cluster_unhealthy = Mock(side_effect=[False] +
                                                 [True] * 100)
            self.assertRaises(Exception, self.keeper.run)
class TestHouseKeeper(unittest.TestCase):

    @patch('requests.get', requests_get)
    @patch('boto3.resource')
    def setUp(self, res):
        res.return_value.instances.filter.return_value = instances()
        self.manager = EtcdManager()
        self.manager.get_my_instance()
        self.manager.instance_id = 'i-deadbeef3'
        self.manager.region = 'eu-west-1'
        self.keeper = HouseKeeper(self.manager, 'test.')
        self.members_changed = self.keeper.members_changed()

    @patch('requests.get', requests_get)
    def test_members_changed(self):
        self.assertTrue(self.members_changed)
        self.keeper.members['blabla'] = True
        self.assertTrue(self.keeper.members_changed())
        self.assertFalse(self.keeper.members_changed())

    @patch('requests.get', requests_get)
    def test_is_leader(self):
        self.assertTrue(self.keeper.is_leader())

    @patch('requests.put', requests_put)
    def test_acquire_lock(self):
        self.assertTrue(self.keeper.acquire_lock())

    @patch('requests.delete', requests_delete)
    @patch('boto3.resource')
    def test_remove_unhealthy_members(self, res):
        res.return_value.instances.filter.return_value = instances()
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertIsNone(self.keeper.remove_unhealthy_members(autoscaling_members))

    @patch('boto3.resource')
    @patch('boto3.client')
    def test_update_route53_records(self, cli, res):
        cli.return_value.list_hosted_zones_by_name.return_value = {'HostedZones': [{'Id': '', 'Name': 'test.'}]}
        res.return_value.instances.filter.return_value = instances()
        autoscaling_members = self.manager.get_autoscaling_members()
        self.assertIsNone(self.keeper.update_route53_records(autoscaling_members))
        self.keeper.hosted_zone = 'bla'
        self.assertRaises(Exception, self.keeper.update_route53_records, autoscaling_members)

    @patch('subprocess.Popen', Popen)
    def test_cluster_unhealthy(self):
        self.assertTrue(self.keeper.cluster_unhealthy())

    @patch('logging.exception', Mock(side_effect=Exception))
    @patch('os.kill', Mock())
    @patch('time.sleep', Mock(side_effect=Exception))
    @patch('requests.get', requests_get)
    @patch('requests.put', requests_put)
    @patch('requests.delete', requests_delete)
    @patch('subprocess.Popen', Popen)
    @patch('boto3.resource')
    @patch('boto3.client')
    def test_run(self, cli, res):
        cli.return_value.list_hosted_zones_by_name.return_value = {'HostedZones': [{'Id': '', 'Name': 'test.'}]}
        res.return_value.instances.filter.return_value = instances()
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.manager.etcd_pid = 1
        self.assertRaises(Exception, self.keeper.run)
        self.keeper.is_leader = Mock(side_effect=Exception)
        self.assertRaises(Exception, self.keeper.run)
        with patch('time.sleep', Mock()):
            self.keeper.is_leader = Mock(return_value=False)
            self.keeper.manager.runv2 = True
            self.keeper.cluster_unhealthy = Mock(side_effect=[False, True, False])
            self.assertRaises(Exception, self.keeper.run)
            self.keeper.cluster_unhealthy = Mock(side_effect=[False] + [True]*100)
            self.assertRaises(Exception, self.keeper.run)