def test_is_healthy(self):
        private_ip_address = '127.0.0.22'
        private_dns_name = 'ip-{}.eu-west-1.compute.internal'.format(
            private_ip_address.replace('.', '-'))
        url = 'http://' + private_ip_address
        peer_urls = ['{}:{}'.format(url, EtcdMember.DEFAULT_PEER_PORT)]
        me = EtcdMember({
            'id':
            'ifoobari7',
            'name':
            'i-sadfjhg',
            'clientURLs': [
                '{}:{}'.format(private_ip_address,
                               EtcdMember.DEFAULT_CLIENT_PORT)
            ],
            'peerURLs':
            peer_urls
        })
        me.private_ip_address = private_ip_address
        self.assertFalse(self.cluster.is_healthy(me))
        self.cluster.members[-1].instance_id = 'foo'
        self.cluster.members[-1].name = ''
        self.assertFalse(self.cluster.is_healthy(me))

        self.cluster.members[-1].peer_urls = peer_urls
        self.assertTrue(self.cluster.is_healthy(me))
        self.cluster.members.pop()
        self.assertTrue(self.cluster.is_healthy(me))
Esempio n. 2
0
 def test_delete_member(self, res):
     sg = Mock()
     sg.tags = [{
         'Key': 'aws:cloudformation:stack-name',
         'Value': 'etc-cluster'
     }, {
         'Key': 'aws:autoscaling:groupName',
         'Value': 'etc-cluster-postgres'
     }]
     sg.revoke_ingress.side_effect = Exception
     res.return_value.security_groups.all.return_value = [sg]
     member = EtcdMember({
         'id':
         'ifoobari7',
         'name':
         'i-sadfjhg',
         'clientURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
         'peerURLs': [
             'http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(
                 EtcdMember.DEFAULT_PEER_PORT)
         ]
     })
     member.peer_urls[0] = member.peer_urls[0].replace('2', '1')
     self.assertFalse(self.ec2_member.delete_member(member))
Esempio n. 3
0
 def set_up(self):
     self.old_requests_post = requests.post
     self.old_requests_get = requests.get
     self.old_requests_delete = requests.delete
     requests.post = requests_post
     requests.get = requests_get
     requests.delete = requests_delete
     self.ec2 = Instance()
     self.ec2.id = 'i-foobar'
     self.ec2.private_ip_address = '127.0.0.1'
     self.ec2.private_dns_name = 'ip-127-0-0-1.eu-west-1.compute.internal'
     self.ec2.tags = {
         'aws:cloudformation:stack-name': 'etc-cluster',
         'aws:autoscaling:groupName': 'etc-cluster-postgres'
     }
     self.ec2_member = EtcdMember(self.ec2)
     self.etcd = {
         'id':
         'deadbeef',
         'name':
         'i-foobar2',
         'clientURLs': [],
         'peerURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     }
     self.etcd_member = EtcdMember(self.etcd)
Esempio n. 4
0
 def test_add_member(self):
     member = EtcdMember({
         'id': '',
         'name': '',
         'clientURLs': [],
         'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     })
     self.assertEqual(self.ec2_member.add_member(member), True)
     member.addr = '127.0.0.4'
     self.assertEqual(self.ec2_member.add_member(member), False)
 def test_add_member(self):
     member = EtcdMember({
         'id': '',
         'name': '',
         'clientURLs': [],
         'peerURLs': ['http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     })
     self.assertTrue(self.ec2_member.add_member(member))
     member.peer_urls[0] = member.peer_urls[0].replace('2', '4')
     self.assertFalse(self.ec2_member.add_member(member))
 def setUp(self):
     self.ec2 = MockInstance('i-foobar', '127.0.0.1')
     self.ec2_member = EtcdMember(self.ec2)
     self.etcd = {
         'id': 'deadbeef',
         'name': 'i-foobar2',
         'clientURLs': [],
         'peerURLs': ['http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     }
     self.etcd_member = EtcdMember(self.etcd)
 def setUp(self):
     self.ec2 = MockInstance('i-foobar', '127.0.0.1')
     self.ec2_member = EtcdMember(self.ec2)
     self.etcd = {
         'id': 'deadbeef',
         'name': 'i-foobar2',
         'clientURLs': [],
         'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     }
     self.etcd_member = EtcdMember(self.etcd)
Esempio n. 8
0
 def test_add_member(self):
     member = EtcdMember({
         'id':
         '',
         'name':
         '',
         'clientURLs': [],
         'peerURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     })
     self.assertEqual(self.ec2_member.add_member(member), True)
     member.addr = '127.0.0.4'
     self.assertEqual(self.ec2_member.add_member(member), False)
Esempio n. 9
0
 def setUp(self):
     self.ec2 = MockInstance('i-foobar', '127.0.0.1')
     self.ec2_member = EtcdMember(self.ec2)
     self.etcd = {
         'id':
         'deadbeef',
         'name':
         'i-foobar2',
         'clientURLs': [],
         'peerURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     }
     self.etcd_member = EtcdMember(self.etcd)
Esempio n. 10
0
 def test_add_member(self):
     member = EtcdMember({
         'id':
         '',
         'name':
         '',
         'clientURLs': [],
         'peerURLs': [
             'http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(
                 EtcdMember.DEFAULT_PEER_PORT)
         ],
     })
     self.assertTrue(self.ec2_member.add_member(member))
     member.peer_urls[0] = member.peer_urls[0].replace('2', '4')
     self.assertFalse(self.ec2_member.add_member(member))
Esempio n. 11
0
 def setUp(self):
     self.ec2 = MockInstance('i-foobar', '127.0.0.1')
     self.ec2_member = EtcdMember(self.ec2)
     self.etcd = {
         'id':
         'deadbeef',
         'name':
         'i-foobar2',
         'clientURLs': [],
         'peerURLs': [
             'http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(
                 EtcdMember.DEFAULT_PEER_PORT)
         ],
     }
     self.etcd_member = EtcdMember(self.etcd)
 def test_delete_member(self, res):
     sg = Mock()
     sg.tags = [
         {'Key': 'aws:cloudformation:stack-name', 'Value': 'etc-cluster'},
         {'Key': 'aws:autoscaling:groupName', 'Value': 'etc-cluster-postgres'}
     ]
     sg.revoke_ingress.side_effect = Exception
     res.return_value.security_groups.all.return_value = [sg]
     member = EtcdMember({
         'id': 'ifoobari7',
         'name': 'i-sadfjhg',
         'clientURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
         'peerURLs': ['http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(EtcdMember.DEFAULT_PEER_PORT)]
     })
     member.peer_urls[0] = member.peer_urls[0].replace('2', '1')
     self.assertFalse(self.ec2_member.delete_member(member))
 def test_is_healthy(self):
     public_dns_name = 'ec2-52-0-0-128.eu-west-1.compute.amazonaws.com'
     url = 'http://' + public_dns_name
     peer_urls = ['{}:{}'.format(url, EtcdMember.DEFAULT_PEER_PORT)]
     me = EtcdMember({
         'id': 'ifoobari0815',
         'name': 'i-sadfjhg',
         'clientURLs': ['{}:{}'.format(url, EtcdMember.DEFAULT_CLIENT_PORT)],
         'peerURLs': peer_urls
     })
     me.public_dns_name = public_dns_name
     self.assertFalse(self.cluster.is_healthy(me))
     self.cluster.members[-1].instance_id = 'foo'
     self.cluster.members[-1].name = ''
     self.assertFalse(self.cluster.is_healthy(me))
     self.cluster.members[-1].peer_urls = peer_urls
     self.assertTrue(self.cluster.is_healthy(me))
     self.cluster.members.pop()
     self.assertTrue(self.cluster.is_healthy(me))
Esempio n. 14
0
 def set_up(self):
     self.old_requests_post = requests.post
     self.old_requests_get = requests.get
     self.old_requests_delete = requests.delete
     requests.post = requests_post
     requests.get = requests_get
     requests.delete = requests_delete
     self.ec2 = Instance()
     self.ec2.id = 'i-foobar'
     self.ec2.private_ip_address = '127.0.0.1'
     self.ec2.private_dns_name = 'ip-127-0-0-1.eu-west-1.compute.internal'
     self.ec2.tags = {'aws:cloudformation:stack-name': 'etc-cluster',
                      'aws:autoscaling:groupName': 'etc-cluster-postgres'}
     self.ec2_member = EtcdMember(self.ec2)
     self.etcd = {
         'id': 'deadbeef',
         'name': 'i-foobar2',
         'clientURLs': [],
         'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     }
     self.etcd_member = EtcdMember(self.etcd)
Esempio n. 15
0
 def test_delete_member(self):
     member = EtcdMember({
         'id':
         'ifoobari7',
         'name':
         'i-sadfjhg',
         'clientURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
         'peerURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     })
     self.assertEqual(self.ec2_member.delete_member(member), False)
    def test_is_healthy(self):
        private_ip_address = "127.0.0.22"
        private_dns_name = "ip-{}.eu-west-1.compute.internal".format(private_ip_address.replace(".", "-"))
        url = "http://" + private_ip_address
        peer_urls = ["{}:{}".format(url, EtcdMember.DEFAULT_PEER_PORT)]
        me = EtcdMember(
            {
                "id": "ifoobari7",
                "name": "i-sadfjhg",
                "clientURLs": ["{}:{}".format(private_ip_address, EtcdMember.DEFAULT_CLIENT_PORT)],
                "peerURLs": peer_urls,
            }
        )
        me.private_ip_address = private_ip_address
        self.assertFalse(self.cluster.is_healthy(me))
        self.cluster.members[-1].instance_id = "foo"
        self.cluster.members[-1].name = ""
        self.assertFalse(self.cluster.is_healthy(me))

        self.cluster.members[-1].peer_urls = peer_urls
        self.assertTrue(self.cluster.is_healthy(me))
        self.cluster.members.pop()
        self.assertTrue(self.cluster.is_healthy(me))
Esempio n. 17
0
 def test_is_healthy(self):
     public_dns_name = 'ec2-52-0-0-128.eu-west-1.compute.amazonaws.com'
     url = 'http://' + public_dns_name
     peer_urls = ['{}:{}'.format(url, EtcdMember.DEFAULT_PEER_PORT)]
     me = EtcdMember({
         'id':
         'ifoobari0815',
         'name':
         'i-sadfjhg',
         'clientURLs':
         ['{}:{}'.format(url, EtcdMember.DEFAULT_CLIENT_PORT)],
         'peerURLs':
         peer_urls
     })
     me.public_dns_name = public_dns_name
     self.assertFalse(self.cluster.is_healthy(me))
     self.cluster.members[-1].instance_id = 'foo'
     self.cluster.members[-1].name = ''
     self.assertFalse(self.cluster.is_healthy(me))
     self.cluster.members[-1].peer_urls = peer_urls
     self.assertTrue(self.cluster.is_healthy(me))
     self.cluster.members.pop()
     self.assertTrue(self.cluster.is_healthy(me))
 def test_is_healthy(self):
     me = EtcdMember({
         'id':
         'ifoobari7',
         'name':
         'i-sadfjhg',
         'clientURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
         'peerURLs':
         ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
     })
     self.assertFalse(self.cluster.is_healthy(me))
     self.cluster.members[-1].instance_id = 'foo'
     self.cluster.members[-1].name = ''
     self.assertFalse(self.cluster.is_healthy(me))
     self.cluster.members[-1].peer_urls = ['http://127.0.0.2:2380']
     self.assertTrue(self.cluster.is_healthy(me))
     self.cluster.members.pop()
     self.assertTrue(self.cluster.is_healthy(me))
Esempio n. 19
0
class TestEtcdMember(unittest.TestCase):
    def __init__(self, method_name='runTest'):
        self.setUp = self.set_up
        super(TestEtcdMember, self).__init__(method_name)

    def set_up(self):
        self.old_requests_post = requests.post
        self.old_requests_get = requests.get
        self.old_requests_delete = requests.delete
        requests.post = requests_post
        requests.get = requests_get
        requests.delete = requests_delete
        self.ec2 = Instance()
        self.ec2.id = 'i-foobar'
        self.ec2.private_ip_address = '127.0.0.1'
        self.ec2.private_dns_name = 'ip-127-0-0-1.eu-west-1.compute.internal'
        self.ec2.tags = {
            'aws:cloudformation:stack-name': 'etc-cluster',
            'aws:autoscaling:groupName': 'etc-cluster-postgres'
        }
        self.ec2_member = EtcdMember(self.ec2)
        self.etcd = {
            'id':
            'deadbeef',
            'name':
            'i-foobar2',
            'clientURLs': [],
            'peerURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        }
        self.etcd_member = EtcdMember(self.etcd)

    def test_get_addr_from_urls(self):
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2:3']),
                         '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2']),
                         '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http//1.2']),
                         None)

    def test_set_info_from_ec2_instance(self):
        self.assertEqual(self.etcd_member.addr, '127.0.0.2')
        self.etcd_member.set_info_from_ec2_instance(self.ec2)
        self.etcd_member.name = ''
        self.etcd_member.set_info_from_ec2_instance(self.ec2)

    def test_set_info_from_etcd(self):
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar'
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar2'

    def test_add_member(self):
        member = EtcdMember({
            'id':
            '',
            'name':
            '',
            'clientURLs': [],
            'peerURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertEqual(self.ec2_member.add_member(member), True)
        member.addr = '127.0.0.4'
        self.assertEqual(self.ec2_member.add_member(member), False)

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

    def test_delete_member(self):
        member = EtcdMember({
            'id':
            'ifoobari7',
            'name':
            'i-sadfjhg',
            'clientURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
            'peerURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertEqual(self.ec2_member.delete_member(member), False)

    def test_get_leader(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_leader(), 'ifoobari1')

    def test_get_members(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_members(), [])
Esempio n. 20
0
class TestEtcdMember(unittest.TestCase):
    def setUp(self):
        self.ec2 = MockInstance('i-foobar', '127.0.0.1')
        self.ec2_member = EtcdMember(self.ec2)
        self.etcd = {
            'id':
            'deadbeef',
            'name':
            'i-foobar2',
            'clientURLs': [],
            'peerURLs': [
                'http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(
                    EtcdMember.DEFAULT_PEER_PORT)
            ],
        }
        self.etcd_member = EtcdMember(self.etcd)

    def test_get_addr_from_urls(self):
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2:3']),
                         '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2']),
                         '1.2')
        self.assertIsNone(self.ec2_member.get_addr_from_urls(['http//1.2']))

    def test_set_info_from_ec2_instance(self):
        self.etcd_member.set_info_from_ec2_instance(self.ec2)
        self.etcd_member.name = ''
        self.etcd_member.set_info_from_ec2_instance(self.ec2)

    def test_set_info_from_etcd(self):
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar'
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['peerURLs'] = [
            'http://127.0.0.100:{}'.format(EtcdMember.DEFAULT_PEER_PORT)
        ]
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['peerURLs'] = [
            'http://127.0.0.1:{}'.format(EtcdMember.DEFAULT_PEER_PORT)
        ]
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['peerURLs'] = []
        self.ec2_member.set_info_from_etcd(self.etcd)

    @patch('requests.post', requests_post)
    def test_add_member(self):
        member = EtcdMember({
            'id':
            '',
            'name':
            '',
            'clientURLs': [],
            'peerURLs': [
                'http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(
                    EtcdMember.DEFAULT_PEER_PORT)
            ],
        })
        self.assertTrue(self.ec2_member.add_member(member))
        member.peer_urls[0] = member.peer_urls[0].replace('2', '4')
        self.assertFalse(self.ec2_member.add_member(member))

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

    @patch('boto3.resource')
    @patch('requests.delete', requests_delete)
    @patch('etcd.EtcdCluster.is_multiregion', Mock(return_value=True))
    def test_delete_member(self, res):
        sg = Mock()
        sg.tags = [{
            'Key': 'aws:cloudformation:stack-name',
            'Value': 'etc-cluster'
        }, {
            'Key': 'aws:autoscaling:groupName',
            'Value': 'etc-cluster-postgres'
        }]
        sg.revoke_ingress.side_effect = Exception
        res.return_value.security_groups.all.return_value = [sg]
        member = EtcdMember({
            'id':
            'ifoobari7',
            'name':
            'i-sadfjhg',
            'clientURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
            'peerURLs': [
                'http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(
                    EtcdMember.DEFAULT_PEER_PORT)
            ]
        })
        member.peer_urls[0] = member.peer_urls[0].replace('2', '1')
        self.assertFalse(self.ec2_member.delete_member(member))

    @patch('requests.get', requests_get)
    def test_get_leader(self):
        self.ec2_member.private_ip_address = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_leader(), 'ifoobari1')

    @patch('requests.get', requests_get)
    def test_get_members(self):
        self.ec2_member.private_ip_address = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_members(), [])
class TestEtcdMember(unittest.TestCase):

    def setUp(self):
        self.ec2 = MockInstance('i-foobar', '127.0.0.1')
        self.ec2_member = EtcdMember(self.ec2)
        self.etcd = {
            'id': 'deadbeef',
            'name': 'i-foobar2',
            'clientURLs': [],
            'peerURLs': ['http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        }
        self.etcd_member = EtcdMember(self.etcd)

    def test_get_addr_from_urls(self):
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2:3']), '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2']), '1.2')
        self.assertIsNone(self.ec2_member.get_addr_from_urls(['http//1.2']))

    def test_set_info_from_ec2_instance(self):
        self.etcd_member.set_info_from_ec2_instance(self.ec2)
        self.etcd_member.name = ''
        self.etcd_member.set_info_from_ec2_instance(self.ec2)

    def test_set_info_from_etcd(self):
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar'
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['peerURLs'] = ['http://127.0.0.100:{}'.format(EtcdMember.DEFAULT_PEER_PORT)]
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['peerURLs'] = ['http://127.0.0.1:{}'.format(EtcdMember.DEFAULT_PEER_PORT)]
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['peerURLs'] = []
        self.ec2_member.set_info_from_etcd(self.etcd)

    @patch('requests.post', requests_post)
    def test_add_member(self):
        member = EtcdMember({
            'id': '',
            'name': '',
            'clientURLs': [],
            'peerURLs': ['http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertTrue(self.ec2_member.add_member(member))
        member.peer_urls[0] = member.peer_urls[0].replace('2', '4')
        self.assertFalse(self.ec2_member.add_member(member))

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

    @patch('boto3.resource')
    @patch('requests.delete', requests_delete)
    @patch('etcd.EtcdCluster.is_multiregion', Mock(return_value=True))
    def test_delete_member(self, res):
        sg = Mock()
        sg.tags = [
            {'Key': 'aws:cloudformation:stack-name', 'Value': 'etc-cluster'},
            {'Key': 'aws:autoscaling:groupName', 'Value': 'etc-cluster-postgres'}
        ]
        sg.revoke_ingress.side_effect = Exception
        res.return_value.security_groups.all.return_value = [sg]
        member = EtcdMember({
            'id': 'ifoobari7',
            'name': 'i-sadfjhg',
            'clientURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
            'peerURLs': ['http://ip-127-0-0-2.eu-west-1.compute.internal:{}'.format(EtcdMember.DEFAULT_PEER_PORT)]
        })
        member.peer_urls[0] = member.peer_urls[0].replace('2', '1')
        self.assertFalse(self.ec2_member.delete_member(member))

    @patch('requests.get', requests_get)
    def test_get_leader(self):
        self.ec2_member.private_ip_address = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_leader(), 'ifoobari1')

    @patch('requests.get', requests_get)
    def test_get_members(self):
        self.ec2_member.private_ip_address = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_members(), [])
class TestEtcdMember(unittest.TestCase):

    def setUp(self):
        self.ec2 = MockInstance('i-foobar', '127.0.0.1')
        self.ec2_member = EtcdMember(self.ec2)
        self.etcd = {
            'id': 'deadbeef',
            'name': 'i-foobar2',
            'clientURLs': [],
            'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        }
        self.etcd_member = EtcdMember(self.etcd)

    def test_get_addr_from_urls(self):
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2:3']), '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2']), '1.2')
        self.assertIsNone(self.ec2_member.get_addr_from_urls(['http//1.2']))

    def test_set_info_from_ec2_instance(self):
        self.assertEqual(self.etcd_member.addr, '127.0.0.2')
        self.etcd_member.set_info_from_ec2_instance(self.ec2)
        self.etcd_member.name = ''
        self.etcd_member.set_info_from_ec2_instance(self.ec2)

    def test_set_info_from_etcd(self):
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar'
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar2'

    @patch('requests.post', requests_post)
    def test_add_member(self):
        member = EtcdMember({
            'id': '',
            'name': '',
            'clientURLs': [],
            'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertTrue(self.ec2_member.add_member(member))
        member.addr = '127.0.0.4'
        self.assertFalse(self.ec2_member.add_member(member))

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

    @patch('requests.delete', requests_delete)
    def test_delete_member(self):
        member = EtcdMember({
            'id': 'ifoobari7',
            'name': 'i-sadfjhg',
            'clientURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
            'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertFalse(self.ec2_member.delete_member(member))

    @patch('requests.get', requests_get)
    def test_get_leader(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_leader(), 'ifoobari1')

    @patch('requests.get', requests_get)
    def test_get_members(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_members(), [])
Esempio n. 23
0
class TestEtcdMember(unittest.TestCase):

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

    def set_up(self):
        self.old_requests_post = requests.post
        self.old_requests_get = requests.get
        self.old_requests_delete = requests.delete
        requests.post = requests_post
        requests.get = requests_get
        requests.delete = requests_delete
        self.ec2 = Instance()
        self.ec2.id = 'i-foobar'
        self.ec2.private_ip_address = '127.0.0.1'
        self.ec2.private_dns_name = 'ip-127-0-0-1.eu-west-1.compute.internal'
        self.ec2.tags = {'aws:cloudformation:stack-name': 'etc-cluster',
                         'aws:autoscaling:groupName': 'etc-cluster-postgres'}
        self.ec2_member = EtcdMember(self.ec2)
        self.etcd = {
            'id': 'deadbeef',
            'name': 'i-foobar2',
            'clientURLs': [],
            'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        }
        self.etcd_member = EtcdMember(self.etcd)

    def test_get_addr_from_urls(self):
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2:3']), '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2']), '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http//1.2']), None)

    def test_set_info_from_ec2_instance(self):
        self.assertEqual(self.etcd_member.addr, '127.0.0.2')
        self.etcd_member.set_info_from_ec2_instance(self.ec2)
        self.etcd_member.name = ''
        self.etcd_member.set_info_from_ec2_instance(self.ec2)

    def test_set_info_from_etcd(self):
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar'
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar2'

    def test_add_member(self):
        member = EtcdMember({
            'id': '',
            'name': '',
            'clientURLs': [],
            'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertEqual(self.ec2_member.add_member(member), True)
        member.addr = '127.0.0.4'
        self.assertEqual(self.ec2_member.add_member(member), False)

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

    def test_delete_member(self):
        member = EtcdMember({
            'id': 'ifoobari7',
            'name': 'i-sadfjhg',
            'clientURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
            'peerURLs': ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertEqual(self.ec2_member.delete_member(member), False)

    def test_get_leader(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_leader(), 'ifoobari1')

    def test_get_members(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_members(), [])
Esempio n. 24
0
class TestEtcdMember(unittest.TestCase):
    def setUp(self):
        self.ec2 = MockInstance('i-foobar', '127.0.0.1')
        self.ec2_member = EtcdMember(self.ec2)
        self.etcd = {
            'id':
            'deadbeef',
            'name':
            'i-foobar2',
            'clientURLs': [],
            'peerURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        }
        self.etcd_member = EtcdMember(self.etcd)

    def test_get_addr_from_urls(self):
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2:3']),
                         '1.2')
        self.assertEqual(self.ec2_member.get_addr_from_urls(['http://1.2']),
                         '1.2')
        self.assertIsNone(self.ec2_member.get_addr_from_urls(['http//1.2']))

    def test_set_info_from_ec2_instance(self):
        self.assertEqual(self.etcd_member.addr, '127.0.0.2')
        self.etcd_member.set_info_from_ec2_instance(self.ec2)
        self.etcd_member.name = ''
        self.etcd_member.set_info_from_ec2_instance(self.ec2)

    def test_set_info_from_etcd(self):
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar'
        self.ec2_member.set_info_from_etcd(self.etcd)
        self.etcd['name'] = 'i-foobar2'

    @patch('requests.post', requests_post)
    def test_add_member(self):
        member = EtcdMember({
            'id':
            '',
            'name':
            '',
            'clientURLs': [],
            'peerURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertTrue(self.ec2_member.add_member(member))
        member.addr = '127.0.0.4'
        self.assertFalse(self.ec2_member.add_member(member))

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

    @patch('requests.delete', requests_delete)
    def test_delete_member(self):
        member = EtcdMember({
            'id':
            'ifoobari7',
            'name':
            'i-sadfjhg',
            'clientURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_CLIENT_PORT)],
            'peerURLs':
            ['http://127.0.0.2:{}'.format(EtcdMember.DEFAULT_PEER_PORT)],
        })
        self.assertFalse(self.ec2_member.delete_member(member))

    @patch('requests.get', requests_get)
    def test_get_leader(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_leader(), 'ifoobari1')

    @patch('requests.get', requests_get)
    def test_get_members(self):
        self.ec2_member.addr = '127.0.0.7'
        self.assertEqual(self.ec2_member.get_members(), [])