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)
class TestEtcdManager(unittest.TestCase): def __init__(self, method_name='runTest'): self.setUp = self.set_up super(TestEtcdManager, self).__init__(method_name) def set_up(self): requests.get = requests_get boto.ec2.connect_to_region = boto_ec2_connect_to_region self.manager = EtcdManager() self.manager.find_my_instace() def test_get_autoscaling_members(self): self.assertEqual(len(self.manager.get_autoscaling_members()), 3) self.assertEqual(self.manager.instance_id, 'i-deadbeef3') self.assertEqual(self.manager.region, 'eu-west-1') def test_clean_data_dir(self): self.manager.clean_data_dir() os.mkdir(self.manager.DATA_DIR) self.manager.clean_data_dir() open(self.manager.DATA_DIR, 'w').close() self.manager.clean_data_dir() os.symlink('foo', self.manager.DATA_DIR) old_unlink = os.unlink os.unlink = raise_exception self.manager.clean_data_dir() os.unlink = old_unlink self.manager.clean_data_dir() def test_load_my_identities(self): requests.get = requests_get_bad_status self.assertRaises(Exception, self.manager.load_my_identities) def test_run(self): os.execv = raise_exception os.fork = lambda: 0 time.sleep = raise_exception self.assertRaises(Exception, self.manager.run) os.fork = lambda: 1 os.waitpid = lambda a, b: (1, 0) self.assertRaises(Exception, self.manager.run)
class TestEtcdManager(unittest.TestCase): @patch('boto3.resource') @patch('requests.get', requests_get) def setUp(self, res): self.manager = EtcdManager() res.return_value.instances.filter.return_value = instances() self.manager.find_my_instance() @patch('boto3.resource') def test_get_autoscaling_members(self, res): res.return_value.instances.filter.return_value = instances() self.assertEqual(len(self.manager.get_autoscaling_members()), 3) self.assertEqual(self.manager.instance_id, 'i-deadbeef3') self.assertEqual(self.manager.region, 'eu-west-1') def test_clean_data_dir(self): self.manager.clean_data_dir() os.mkdir(self.manager.DATA_DIR) self.manager.clean_data_dir() open(self.manager.DATA_DIR, 'w').close() self.manager.clean_data_dir() os.symlink('foo', self.manager.DATA_DIR) with patch('os.unlink', Mock(side_effect=Exception)): self.manager.clean_data_dir() self.manager.clean_data_dir() @patch('requests.get', requests_get_bad_status) def test_load_my_identities(self): self.assertRaises(EtcdClusterException, self.manager.load_my_identities) @patch('time.sleep', Mock()) @patch('requests.get', requests_get) @patch('boto3.resource') def test_register_me(self, res): res.return_value.instances.filter.return_value = instances() cluster = EtcdCluster(self.manager) cluster.load_members() self.manager.me.id = '1' self.manager.register_me(cluster) self.manager.me.id = None cluster.accessible_member.add_member = Mock(return_value=False) self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) self.manager.me.client_urls = ['a'] cluster.accessible_member.delete_member = Mock(return_value=False) self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) cluster.accessible_member.delete_member = cluster.accessible_member.add_member = Mock(return_value=True) self.manager.register_me(cluster) cluster.leader_id = None self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) cluster.accessible_member = None self.manager.register_me(cluster) @patch('boto3.resource') @patch('os.path.exists', Mock(return_value=True)) @patch('os.execv', Mock(side_effect=Exception)) @patch('os.fork', Mock(return_value=0)) @patch('time.sleep', Mock(side_effect=SleepException)) @patch('requests.get', requests_get) def test_run(self, res): res.return_value.instances.filter.return_value = instances() self.assertRaises(SleepException, self.manager.run) with patch('os.fork', Mock(return_value=1)): with patch('os.waitpid', Mock(return_value=(1, 0))): self.assertRaises(SleepException, self.manager.run) with patch.object(EtcdCluster, 'load_members', Mock(side_effect=SystemExit)): self.manager.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.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)
class TestEtcdManager(unittest.TestCase): def __init__(self, method_name='runTest'): self.setUp = self.set_up super(TestEtcdManager, self).__init__(method_name) def set_up(self): time.sleep = lambda e: None requests.get = requests_get boto.ec2.connect_to_region = boto_ec2_connect_to_region self.manager = EtcdManager() self.manager.find_my_instace() def test_get_autoscaling_members(self): self.assertEqual(len(self.manager.get_autoscaling_members()), 3) self.assertEqual(self.manager.instance_id, 'i-deadbeef3') self.assertEqual(self.manager.region, 'eu-west-1') def test_clean_data_dir(self): self.manager.clean_data_dir() os.mkdir(self.manager.DATA_DIR) self.manager.clean_data_dir() open(self.manager.DATA_DIR, 'w').close() self.manager.clean_data_dir() os.symlink('foo', self.manager.DATA_DIR) old_unlink = os.unlink os.unlink = raise_exception self.manager.clean_data_dir() os.unlink = old_unlink self.manager.clean_data_dir() def test_load_my_identities(self): requests.get = requests_get_bad_status self.assertRaises(EtcdClusterException, self.manager.load_my_identities) def test_register_me(self): cluster = EtcdCluster(self.manager) cluster.load_members() self.manager.me.id = '1' self.manager.register_me(cluster) self.manager.me.id = None cluster.accessible_member.add_member = lambda e: False self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) self.manager.me.client_urls = ['a'] cluster.accessible_member.delete_member = lambda e: False self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) cluster.accessible_member.delete_member = cluster.accessible_member.add_member = lambda e: True self.manager.register_me(cluster) cluster.leader_id = None self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) cluster.accessible_member = None self.manager.register_me(cluster) def test_run(self): os.path.exists = lambda e: True os.execv = raise_exception os.fork = lambda: 0 time.sleep = sleep_exception self.assertRaises(SleepException, self.manager.run) os.fork = lambda: 1 os.waitpid = lambda a, b: (1, 0) self.assertRaises(SleepException, self.manager.run) load_members = EtcdCluster.load_members EtcdCluster.load_members = system_exit self.manager.run() EtcdCluster.load_members = load_members
class TestEtcdManager(unittest.TestCase): @patch('boto3.resource') @patch('requests.get', requests_get) def setUp(self, res): self.manager = EtcdManager() res.return_value.instances.filter.return_value = instances() self.manager.find_my_instance() @patch('boto3.resource') def test_get_autoscaling_members(self, res): res.return_value.instances.filter.return_value = instances() self.assertEqual(len(self.manager.get_autoscaling_members()), 3) self.assertEqual(self.manager.instance_id, 'i-deadbeef3') self.assertEqual(self.manager.region, 'eu-west-1') def test_clean_data_dir(self): self.manager.clean_data_dir() os.mkdir(self.manager.DATA_DIR) self.manager.clean_data_dir() open(self.manager.DATA_DIR, 'w').close() self.manager.clean_data_dir() os.symlink('foo', self.manager.DATA_DIR) with patch('os.unlink', Mock(side_effect=Exception)): self.manager.clean_data_dir() self.manager.clean_data_dir() @patch('requests.get', requests_get_bad_status) def test_load_my_identities(self): self.assertRaises(EtcdClusterException, self.manager.load_my_identities) @patch('time.sleep', Mock()) @patch('requests.get', requests_get) @patch('boto3.resource') def test_register_me(self, res): res.return_value.instances.filter.return_value = instances() cluster = EtcdCluster(self.manager) cluster.load_members() self.manager.me.id = '1' self.manager.register_me(cluster) self.manager.me.id = None cluster.accessible_member.add_member = Mock(return_value=False) self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) self.manager.me.client_urls = ['a'] cluster.accessible_member.delete_member = Mock(return_value=False) self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) cluster.accessible_member.delete_member = cluster.accessible_member.add_member = Mock( return_value=True) self.manager.register_me(cluster) cluster.leader_id = None self.assertRaises(EtcdClusterException, self.manager.register_me, cluster) cluster.accessible_member = None self.manager.register_me(cluster) @patch('boto3.resource') @patch('os.path.exists', Mock(return_value=True)) @patch('os.execv', Mock(side_effect=Exception)) @patch('os.fork', Mock(return_value=0)) @patch('time.sleep', Mock(side_effect=SleepException)) @patch('requests.get', requests_get) def test_run(self, res): res.return_value.instances.filter.return_value = instances() self.assertRaises(SleepException, self.manager.run) with patch('os.fork', Mock(return_value=1)): with patch('os.waitpid', Mock(return_value=(1, 0))): self.assertRaises(SleepException, self.manager.run) with patch.object(EtcdCluster, 'load_members', Mock(side_effect=SystemExit)): self.manager.run()