Beispiel #1
0
    def test_pick_name(self):
        """Test pick_name function under normal parameters."""

        expected_hostname = 'mongodb-slave-1'

        fake_service = 'mongodb'
        fake_service_type = 'masterful'
        fake_instance_id = 'i-test'

        fake_row = {
                'hostname': 'mongodb-master',
                'ip': '12.123.234.3',
                'service': 'mongodb',
                'service_type': 'masterful',
                'instance_id': 'i-d23lk3kjl'}

        fake_db = self.mox.CreateMockAnything()
        fake_db.servers = self.mox.CreateMockAnything()
        # I'm cheating here by using a list instead of an iterable obj.

        fake_registrar = registrar.Registrar()
        self.mox.StubOutWithMock(aerostat, 'hostname_exists')
        self.mox.StubOutWithMock(fake_registrar, 'hostname_instance_exists')
        self.mox.StubOutWithMock(fake_registrar, 'check_dup')
        self.mox.StubOutWithMock(fake_registrar, 'get_smallest_gap')

        aerostat.hostname_exists(
                fake_db, 'mongodb-master').AndReturn(True)
        fake_registrar.hostname_instance_exists(
                fake_db, 'mongodb-master').AndReturn(True)
        fake_registrar.check_dup(fake_db, fake_instance_id).AndReturn(False)
        fake_registrar.get_smallest_gap(fake_db, fake_service).AndReturn('mongodb-slave-1')

        # I'm cheating here by using a list instead of an iterable obj.
        fake_db.servers.find(
                {'service': fake_service}).AndReturn([fake_row])

        self.mox.ReplayAll()

        test_hostname = fake_registrar.pick_name(fake_db, fake_service,
                fake_service_type, fake_instance_id)

        self.assertEqual(test_hostname, expected_hostname)
Beispiel #2
0
    def test_pick_name(self):
        """Test pick_name function under normal parameters."""

        expected_hostname = "mongodb-slave-1"

        fake_service = "mongodb"
        fake_service_type = "masterful"
        fake_instance_id = "i-test"

        fake_row = {
            "hostname": "mongodb-master",
            "ip": "12.123.234.3",
            "service": "mongodb",
            "service_type": "masterful",
            "instance_id": "i-d23lk3kjl",
        }

        fake_db = self.mox.CreateMockAnything()
        fake_db.servers = self.mox.CreateMockAnything()
        # I'm cheating here by using a list instead of an iterable obj.

        fake_registrar = registrar.Registrar()
        self.mox.StubOutWithMock(aerostat, "hostname_exists")
        self.mox.StubOutWithMock(fake_registrar, "hostname_instance_exists")
        self.mox.StubOutWithMock(fake_registrar, "check_dup")
        self.mox.StubOutWithMock(fake_registrar, "get_smallest_gap")

        aerostat.hostname_exists(fake_db, "mongodb-master").AndReturn(True)
        fake_registrar.hostname_instance_exists(fake_db, "mongodb-master").AndReturn(True)
        fake_registrar.check_dup(fake_db, fake_instance_id).AndReturn(False)
        fake_registrar.get_smallest_gap(fake_db, fake_service).AndReturn("mongodb-slave-1")

        # I'm cheating here by using a list instead of an iterable obj.
        fake_db.servers.find({"service": fake_service}).AndReturn([fake_row])

        self.mox.ReplayAll()

        test_hostname = fake_registrar.pick_name(fake_db, fake_service, fake_service_type, fake_instance_id)

        self.assertEqual(test_hostname, expected_hostname)
Beispiel #3
0
    def test_register_name(self):
        """Test registration of new hostnames."""

        fake_hostname = 'mongodb-slave-1'
        fake_ip = '12.123.234.5'
        fake_instance_id = 'i-23426'
        fake_service = 'mongodb'
        fake_service_type = 'masterful'
        fake_aliases = []

        fake_row = {
                'hostname': 'mongodb-slave-1',
                'ip': '12.123.234.5',
                'service': 'mongodb',
                'service_type': 'masterful',
                'instance_id': 'i-23426',
                'aliases': []}

        fake_hostname_exists = False

        fake_db = self.mox.CreateMockAnything()
        fake_db.servers = self.mox.CreateMockAnything()
        fake_db.servers.insert(fake_row).AndReturn(None)

        fake_registrar = registrar.Registrar()
        self.mox.StubOutWithMock(aerostat, 'hostname_exists')
        aerostat.hostname_exists(fake_db, fake_hostname).AndReturn(
                fake_hostname_exists)

        self.mox.ReplayAll()

        test_value = fake_registrar.register_name(
                fake_db, fake_hostname, fake_ip,
                fake_instance_id, fake_service, fake_service_type, fake_aliases)

        self.assertTrue(test_value)
Beispiel #4
0
    def test_hostname_exists(self):
        """test hostname_exists function for negative and positive cases."""

        test_hostname1 = 'cassandra-0'
        test_hostname2 = 'cassandra-1'

        fake_db = self.mox.CreateMockAnything()
        fake_db.servers = self.mox.CreateMockAnything()
        fake_cursor_true = self.mox.CreateMockAnything()
        fake_cursor_true.count().AndReturn(1)

        fake_cursor_false = self.mox.CreateMockAnything()
        fake_cursor_false.count().AndReturn(0)

        fake_db.servers.find(
                {'hostname': test_hostname1}).AndReturn(fake_cursor_true)
        fake_db.servers.find(
                {'hostname': test_hostname2}).AndReturn(fake_cursor_false)

        self.mox.ReplayAll()

        self.assertTrue(aerostat.hostname_exists(fake_db, test_hostname1))
        self.assertFalse(aerostat.hostname_exists(
                fake_db, test_hostname2))
Beispiel #5
0
    def test_register_name(self):
        """Test registration of new hostnames."""

        fake_hostname = "mongodb-slave-1"
        fake_ip = "12.123.234.5"
        fake_instance_id = "i-23426"
        fake_service = "mongodb"
        fake_service_type = "masterful"
        fake_aliases = []

        fake_row = {
            "hostname": "mongodb-slave-1",
            "ip": "12.123.234.5",
            "service": "mongodb",
            "service_type": "masterful",
            "instance_id": "i-23426",
            "aliases": [],
        }

        fake_hostname_exists = False

        fake_db = self.mox.CreateMockAnything()
        fake_db.servers = self.mox.CreateMockAnything()
        fake_db.servers.insert(fake_row).AndReturn(None)

        fake_registrar = registrar.Registrar()
        self.mox.StubOutWithMock(aerostat, "hostname_exists")
        aerostat.hostname_exists(fake_db, fake_hostname).AndReturn(fake_hostname_exists)

        self.mox.ReplayAll()

        test_value = fake_registrar.register_name(
            fake_db, fake_hostname, fake_ip, fake_instance_id, fake_service, fake_service_type, fake_aliases
        )

        self.assertTrue(test_value)
Beispiel #6
0
    def test_pick_name_duplicate_inst_no_host(self):
        """test pick_name when there is a duplicate, but no hostname."""

        # This tests the case where a master name swap is underway.
        # the <service>-master hostname is removed from the former instance.
        # then added to the new master. Meanwhile, the old master needs
        # to have a name picked. So we ignore the fact that its instance_id
        # is already in the database as long as it has no hostname.

        # Replace fallen master.
        expected_hostname1 = 'mongodb-master'
        # You were master, replace gap in slave names.
        expected_hostname2 = 'mongodb-slave-1'

        fake_service = 'mongodb'
        fake_service_type = 'masterful'
        fake_instance_id1 = 'i-test'
        fake_instance_id2 = 'i-test2'

        # Missing Master
        fake_row1 = {'hostname': '', 'ip': '12.123.234.3',
                'service': 'mongodb', 'service_type': 'masterful',
                'instance_id': 'i-test'}

        # Missing Slave
        fake_row2 = [
                fake_row1,
                {'hostname': 'mongodb-master', 'ip': '12.1.1.2',
                 'service': 'mongodb', 'service_type': 'masterful',
                 'instance_id': 'i-test2'}]

        fake_db = self.mox.CreateMockAnything()

        fake_db.servers = self.mox.CreateMockAnything()
        fake_db.servers.find(
                {'service': fake_service}).AndReturn([fake_row1])
        fake_db.servers.find(
                {'service': fake_service}).AndReturn(fake_row2)

        fake_registrar = registrar.Registrar()

        self.mox.StubOutWithMock(aerostat, 'get_hostname')
        aerostat.get_hostname(fake_db, fake_instance_id1).AndReturn(
                '')
        aerostat.get_hostname(fake_db, fake_instance_id2).AndReturn(
                '')

        self.mox.StubOutWithMock(fake_registrar, 'check_dup')
        fake_registrar.check_dup(fake_db, fake_instance_id1).AndReturn(True)
        fake_registrar.check_dup(fake_db, fake_instance_id2).AndReturn(True)

        # Called for Slave.
        self.mox.StubOutWithMock(aerostat, 'hostname_exists')
        aerostat.hostname_exists(
                fake_db, 'mongodb-master').AndReturn(True)

        self.mox.StubOutWithMock(fake_registrar, 'hostname_instance_exists')
        fake_registrar.hostname_instance_exists(
                fake_db, 'mongodb-master').AndReturn(True)

        self.mox.StubOutWithMock(fake_registrar, 'get_smallest_gap')
        fake_registrar.get_smallest_gap(fake_db, fake_service).AndReturn(expected_hostname2)

        self.mox.ReplayAll()

        test_hostname1 = fake_registrar.pick_name(
                fake_db, fake_service,
                fake_service_type, fake_instance_id1)

        test_hostname2 = fake_registrar.pick_name(
                fake_db, fake_service,
                fake_service_type, fake_instance_id2)

        self.assertEqual(test_hostname1, expected_hostname1)
        self.assertEqual(test_hostname2, expected_hostname2)
Beispiel #7
0
    def test_pick_name_duplicate_inst_no_host(self):
        """test pick_name when there is a duplicate, but no hostname."""

        # This tests the case where a master name swap is underway.
        # the <service>-master hostname is removed from the former instance.
        # then added to the new master. Meanwhile, the old master needs
        # to have a name picked. So we ignore the fact that its instance_id
        # is already in the database as long as it has no hostname.

        # Replace fallen master.
        expected_hostname1 = "mongodb-master"
        # You were master, replace gap in slave names.
        expected_hostname2 = "mongodb-slave-1"

        fake_service = "mongodb"
        fake_service_type = "masterful"
        fake_instance_id1 = "i-test"
        fake_instance_id2 = "i-test2"

        # Missing Master
        fake_row1 = {
            "hostname": "",
            "ip": "12.123.234.3",
            "service": "mongodb",
            "service_type": "masterful",
            "instance_id": "i-test",
        }

        # Missing Slave
        fake_row2 = [
            fake_row1,
            {
                "hostname": "mongodb-master",
                "ip": "12.1.1.2",
                "service": "mongodb",
                "service_type": "masterful",
                "instance_id": "i-test2",
            },
        ]

        fake_db = self.mox.CreateMockAnything()

        fake_db.servers = self.mox.CreateMockAnything()
        fake_db.servers.find({"service": fake_service}).AndReturn([fake_row1])
        fake_db.servers.find({"service": fake_service}).AndReturn(fake_row2)

        fake_registrar = registrar.Registrar()

        self.mox.StubOutWithMock(aerostat, "get_hostname")
        aerostat.get_hostname(fake_db, fake_instance_id1).AndReturn("")
        aerostat.get_hostname(fake_db, fake_instance_id2).AndReturn("")

        self.mox.StubOutWithMock(fake_registrar, "check_dup")
        fake_registrar.check_dup(fake_db, fake_instance_id1).AndReturn(True)
        fake_registrar.check_dup(fake_db, fake_instance_id2).AndReturn(True)

        # Called for Slave.
        self.mox.StubOutWithMock(aerostat, "hostname_exists")
        aerostat.hostname_exists(fake_db, "mongodb-master").AndReturn(True)

        self.mox.StubOutWithMock(fake_registrar, "hostname_instance_exists")
        fake_registrar.hostname_instance_exists(fake_db, "mongodb-master").AndReturn(True)

        self.mox.StubOutWithMock(fake_registrar, "get_smallest_gap")
        fake_registrar.get_smallest_gap(fake_db, fake_service).AndReturn(expected_hostname2)

        self.mox.ReplayAll()

        test_hostname1 = fake_registrar.pick_name(fake_db, fake_service, fake_service_type, fake_instance_id1)

        test_hostname2 = fake_registrar.pick_name(fake_db, fake_service, fake_service_type, fake_instance_id2)

        self.assertEqual(test_hostname1, expected_hostname1)
        self.assertEqual(test_hostname2, expected_hostname2)