Example #1
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(), [])
Example #2
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(), [])
Example #3
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(), [])
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(), [])
Example #5
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://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(), [])