Example #1
0
 def get_inst_service_status(self, status_id, statuses):
     answers = []
     for i, status in enumerate(statuses):
         inst_svc_status = InstanceServiceStatus(status, id="%s-%s" % (status_id, i))
         inst_svc_status.save = MagicMock(return_value=None)
         answers.append(inst_svc_status)
     return answers
Example #2
0
 def setUp(self):
     super(BaseDbStatusTest, self).setUp()
     util.init_db()
     self.orig_dbaas_time_sleep = dbaas.time.sleep
     self.FAKE_ID = randint(1, 10000)
     InstanceServiceStatus.create(instance_id=self.FAKE_ID, status=rd_instance.ServiceStatuses.NEW)
     dbaas.CONF.guest_id = self.FAKE_ID
    def test_upgrade(self, task_upgrade):
        instance_model = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestUpgradeInstance",
            datastore_version_id=self.datastore_version1.id)
        instance_model.set_task_status(InstanceTasks.NONE)
        instance_model.save()
        instance_status = InstanceServiceStatus(
            ServiceStatuses.RUNNING,
            id=str(uuid.uuid4()),
            instance_id=instance_model.id)
        instance_status.save()
        self.assertIsNotNone(instance_model)
        instance = models.load_instance(models.Instance, self.context,
                                        instance_model.id)

        try:
            instance.upgrade(self.datastore_version2)

            self.assertEqual(self.datastore_version2.id,
                             instance.db_info.datastore_version_id)
            self.assertEqual(InstanceTasks.UPGRADING,
                             instance.db_info.task_status)
            self.assertTrue(task_upgrade.called)
        finally:
            instance_status.delete()
            instance_model.delete()
Example #4
0
 def build_db_instance(self, status, task_status=InstanceTasks.NONE):
     version = datastore_models.DBDatastoreVersion.get_by(name='5.5')
     instance = DBInstance(InstanceTasks.NONE,
                           name='test_name',
                           id=str(uuid.uuid4()),
                           flavor_id='flavor_1',
                           datastore_version_id=
                           version.id,
                           compute_instance_id='compute_id_1',
                           server_id='server_id_1',
                           tenant_id='tenant_id_1',
                           server_status=
                           rd_instance.ServiceStatuses.
                           BUILDING.api_status,
                           deleted=False)
     instance.save()
     service_status = InstanceServiceStatus(
         rd_instance.ServiceStatuses.RUNNING,
         id=str(uuid.uuid4()),
         instance_id=instance.id,
     )
     service_status.save()
     instance.set_task_status(task_status)
     instance.server_status = status
     instance.save()
     return instance, service_status
Example #5
0
 def tearDown(self):
     super(MySqlAppStatusTest, self).tearDown()
     dbaas.utils.execute_with_timeout = self.orig_utils_execute_with_timeout
     dbaas.load_mysqld_options = self.orig_load_mysqld_options
     dbaas.os.path.exists = self.orig_dbaas_os_path_exists
     dbaas.time.sleep = self.orig_dbaas_time_sleep
     InstanceServiceStatus.find_by(instance_id=self.FAKE_ID).delete()
     dbaas.CONF.guest_id = None
Example #6
0
 def setUp(self):
     super(MySqlAppTest, self).setUp()
     self.orig_utils_execute_with_timeout = dbaas.utils.execute_with_timeout
     self.orig_time_sleep = time.sleep
     util.init_db()
     self.FAKE_ID = randint(1, 10000)
     InstanceServiceStatus.create(instance_id=self.FAKE_ID, status=rd_instance.ServiceStatuses.NEW)
     self.appStatus = FakeAppStatus(self.FAKE_ID, rd_instance.ServiceStatuses.NEW)
     self.mySqlApp = MySqlApp(self.appStatus)
     time.sleep = Mock()
Example #7
0
 def setUp(self):
     super(MySqlAppStatusTest, self).setUp()
     util.init_db()
     self.orig_utils_execute_with_timeout = dbaas.utils.execute_with_timeout
     self.orig_load_mysqld_options = dbaas.load_mysqld_options
     self.orig_dbaas_os_path_exists = dbaas.os.path.exists
     self.orig_dbaas_time_sleep = dbaas.time.sleep
     self.FAKE_ID = randint(1, 10000)
     InstanceServiceStatus.create(instance_id=self.FAKE_ID, status=rd_instance.ServiceStatuses.NEW)
     dbaas.CONF.guest_id = self.FAKE_ID
Example #8
0
    def update_statuses_on_time_out(self):

        if CONF.update_status_on_fail:
            #Updating service status
            service = InstanceServiceStatus.find_by(instance_id=self.id)
            service.set_status(ServiceStatuses.
                               FAILED_TIMEOUT_GUESTAGENT)
            service.save()
            LOG.error(_("Service status: %(status)s") %
                      {'status': ServiceStatuses.
                       FAILED_TIMEOUT_GUESTAGENT.api_status})
            LOG.error(_("Service error description: %(desc)s") %
                      {'desc': ServiceStatuses.
                       FAILED_TIMEOUT_GUESTAGENT.description})
            #Updating instance status
            db_info = DBInstance.find_by(name=self.name)
            db_info.set_task_status(InstanceTasks.
                                    BUILDING_ERROR_TIMEOUT_GA)
            db_info.save()
            LOG.error(_("Trove instance status: %(action)s") %
                      {'action': InstanceTasks.
                       BUILDING_ERROR_TIMEOUT_GA.action})
            LOG.error(_("Trove instance status description: %(text)s") %
                      {'text': InstanceTasks.
                       BUILDING_ERROR_TIMEOUT_GA.db_text})
    def monitor_ha(self, context):
        """Monitors the status of MySQL masters to make sure they are up."""
        LOG.debug("Monitoring Trove Replica Sources (Masters)")

        db_infos = t_models.DBInstance.find_all(deleted=False)
        masters_to_watch = [(instance.slave_of_id, instance) for instance
                            in db_infos.all() if instance.slave_of_id and
                            instance.task_status == InstanceTasks.NONE]

        LOG.debug("Monitoring %s",  masters_to_watch)
        for (master_id, slave) in masters_to_watch:
            master = t_models.DBInstance.find_by(deleted=False, id=master_id)
            service = InstanceServiceStatus.find_by(instance_id=master_id)

            if self._engage_failover(service):
                LOG.debug("Engage FAILOVER from %s to %s NOW!",
                          master_id, slave.id)
                master = t_models.DBInstance.find_by(id=master_id,
                                                     deleted=False)

                self._create_and_auth_clients()

                # Failover Slave to Master by detaching replica source on slave
                self.trove_client.instances.edit(slave.id,
                                                 detach_replica_source=True)

                # Update DNS records for master and slave
                master_dns, slave_dns = self._update_dns_records(master, slave)

                # Finally update the hostnames in trove to
                # reflect the updated DNS information
                self._reflect_dns_updates_in_trove(master, master_dns,
                                                   slave, slave_dns)
 def _init(self):
     self.mock = mox.Mox()
     self.instance_id = 500
     context = trove_testtools.TroveTestContext(self)
     self.db_info = DBInstance.create(
         name="instance",
         flavor_id=OLD_FLAVOR_ID,
         tenant_id=999,
         volume_size=None,
         datastore_version_id=test_config.dbaas_datastore_version_id,
         task_status=InstanceTasks.RESIZING)
     self.server = self.mock.CreateMock(Server)
     self.instance = models.BuiltInstanceTasks(
         context,
         self.db_info,
         self.server,
         datastore_status=InstanceServiceStatus.create(
             instance_id=self.db_info.id,
             status=rd_instance.ServiceStatuses.RUNNING))
     self.instance.server.flavor = {'id': OLD_FLAVOR_ID}
     self.guest = self.mock.CreateMock(guest.API)
     self.instance._guest = self.guest
     self.instance.refresh_compute_server_info = lambda: None
     self.instance._refresh_datastore_status = lambda: None
     self.mock.StubOutWithMock(self.instance, 'update_db')
     self.mock.StubOutWithMock(self.instance,
                               'set_datastore_status_to_paused')
     self.poll_until_mocked = False
     self.action = None
Example #11
0
    def setUp(self):
        util.init_db()

        self.datastore = datastore_models.DBDatastore.create(
            id=str(uuid.uuid4()), name="name", default_version_id=str(uuid.uuid4())
        )

        self.datastore_version = datastore_models.DBDatastoreVersion.create(
            id=self.datastore.default_version_id,
            name="name",
            image_id=str(uuid.uuid4()),
            packages=str(uuid.uuid4()),
            datastore_id=self.datastore.id,
            manager="mysql",
            active=1,
        )

        self.master = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestMasterInstance",
            datastore_version_id=self.datastore_version.id,
        )
        self.master.set_task_status(InstanceTasks.NONE)
        self.master.save()
        self.master_status = InstanceServiceStatus(
            ServiceStatuses.RUNNING, id=str(uuid.uuid4()), instance_id=self.master.id
        )
        self.master_status.save()

        self.safe_nova_client = models.create_nova_client
        models.create_nova_client = nova.fake_create_nova_client
        super(TestReplication, self).setUp()
Example #12
0
    def _service_is_active(self):
        """
        Check that the database guest is active.

        This function is meant to be called with poll_until to check that
        the guest is alive before sending a 'create' message. This prevents
        over billing a customer for a instance that they can never use.

        Returns: boolean if the service is active.
        Raises: TroveError if the service is in a failure state.
        """
        service = InstanceServiceStatus.find_by(instance_id=self.id)
        status = service.get_status()
        if status == rd_instance.ServiceStatuses.RUNNING:
            return True
        elif status not in [rd_instance.ServiceStatuses.NEW,
                            rd_instance.ServiceStatuses.BUILDING]:
            raise TroveError(_("Service not active, status: %s") % status)

        c_id = self.db_info.compute_instance_id
        nova_status = self.nova_client.servers.get(c_id).status
        if nova_status in [InstanceStatus.ERROR,
                           InstanceStatus.FAILED]:
            raise TroveError(_("Server not active, status: %s") % nova_status)
        return False
Example #13
0
 def update_db():
     status = InstanceServiceStatus.find_by(instance_id=self.id)
     if instance_name.endswith('GUEST_ERROR'):
         status.status = rd_instance.ServiceStatuses.FAILED
     else:
         status.status = rd_instance.ServiceStatuses.RUNNING
     status.save()
     AgentHeartBeat.create(instance_id=self.id)
Example #14
0
    def _set_status(self, new_status="RUNNING"):
        from trove.instance.models import InstanceServiceStatus

        print("Setting status to %s" % new_status)
        states = {"RUNNING": rd_instance.ServiceStatuses.RUNNING, "SHUTDOWN": rd_instance.ServiceStatuses.SHUTDOWN}
        status = InstanceServiceStatus.find_by(instance_id=self.id)
        status.status = states[new_status]
        status.save()
Example #15
0
 def _set_task_status(self, new_status='RUNNING'):
     from trove.instance.models import InstanceServiceStatus
     print("Setting status to %s" % new_status)
     states = {'RUNNING': rd_instance.ServiceStatuses.RUNNING,
               'SHUTDOWN': rd_instance.ServiceStatuses.SHUTDOWN,
               }
     status = InstanceServiceStatus.find_by(instance_id=self.id)
     status.status = states[new_status]
     status.save()
Example #16
0
 def setUp(self):
     super(MySqlAppTest, self).setUp()
     self.orig_utils_execute_with_timeout = dbaas.utils.execute_with_timeout
     self.orig_time_sleep = dbaas.time.sleep
     util.init_db()
     self.FAKE_ID = randint(1, 10000)
     InstanceServiceStatus.create(instance_id=self.FAKE_ID,
                                  status=rd_instance.ServiceStatuses.NEW)
     self.appStatus = FakeAppStatus(self.FAKE_ID,
                                    rd_instance.ServiceStatuses.NEW)
     self.mySqlApp = MySqlApp(self.appStatus)
     mysql_service = {'cmd_start': Mock(),
                      'cmd_stop': Mock(),
                      'cmd_enable': Mock(),
                      'cmd_disable': Mock(),
                      'bin': Mock()}
     dbaas.operating_system.service_discovery = Mock(return_value=
                                                     mysql_service)
     dbaas.time.sleep = Mock()
Example #17
0
 def __call__(self):
     audit_start, audit_end = NotificationTransformer._get_audit_period()
     messages = []
     db_infos = instance_models.DBInstance.find_all(deleted=False)
     for db_info in db_infos:
         service_status = InstanceServiceStatus.find_by(
             instance_id=db_info.id)
         instance = SimpleMgmtInstance(None, db_info, None, service_status)
         message = self.transform_instance(instance, audit_start, audit_end)
         messages.append(message)
     return messages
Example #18
0
 def _instance_ids_with_failures(ids):
     LOG.debug("Checking for service status failures for "
               "instance ids: %s" % ids)
     failed_instance_ids = []
     for instance_id in ids:
         status = InstanceServiceStatus.find_by(
             instance_id=instance_id).get_status()
         if (status == ServiceStatuses.FAILED or
            status == ServiceStatuses.FAILED_TIMEOUT_GUESTAGENT):
                 failed_instance_ids.append(instance_id)
     return failed_instance_ids
Example #19
0
    def test_upgrade(self, task_upgrade):
        instance_model = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestUpgradeInstance",
            datastore_version_id=self.datastore_version1.id)
        instance_model.set_task_status(InstanceTasks.NONE)
        instance_model.save()
        instance_status = InstanceServiceStatus(ServiceStatuses.RUNNING,
                                                id=str(uuid.uuid4()),
                                                instance_id=instance_model.id)
        instance_status.save()
        self.assertIsNotNone(instance_model)
        instance = models.load_instance(models.Instance, self.context,
                                        instance_model.id)

        try:
            instance.upgrade(self.datastore_version2)

            self.assertEqual(self.datastore_version2.id,
                             instance.db_info.datastore_version_id)
            self.assertEqual(InstanceTasks.UPGRADING,
                             instance.db_info.task_status)
            self.assertTrue(task_upgrade.called)
        finally:
            instance_status.delete()
            instance_model.delete()
Example #20
0
    def test_add_query_routers_new_cluster(self, mock_dv, mock_ds,
                                           mock_password, mock_guest):
        query_router = BaseInstance(Mock(), self.dbinst3, Mock(),
                                    InstanceServiceStatus(ServiceStatuses.NEW))
        mock_password.return_value = 'pwd'

        ret_val = self.clustertasks._add_query_routers([query_router],
                                                       ['10.0.0.5'],
                                                       new_cluster=True)
        mock_guest.assert_called_with(query_router)
        mock_guest().add_config_servers.assert_called_with(['10.0.0.5'])
        mock_guest().create_admin_user.assert_called_with('pwd')
        self.assertEqual(True, ret_val)
    def test_create_shard_failure(self, mock_dv, mock_ds, mock_ip, mock_guest,
                                  mock_init_rs, mock_update):
        member1 = BaseInstance(Mock(), self.dbinst1, Mock(),
                               InstanceServiceStatus(ServiceStatuses.NEW))
        member2 = BaseInstance(Mock(), self.dbinst2, Mock(),
                               InstanceServiceStatus(ServiceStatuses.NEW))
        members = [member1, member2]
        mock_ip.side_effect = ["10.0.0.2"]

        query_router = [
            BaseInstance(Mock(), self.dbinst3, Mock(),
                         InstanceServiceStatus(ServiceStatuses.NEW))
        ]
        mock_guest().get_replica_set_name.return_value = 'testrs'
        mock_add_shard = Mock(side_effect=Exception("Boom!"))
        mock_guest().add_shard = mock_add_shard

        ret_val = self.clustertasks._create_shard(query_router, members)

        mock_init_rs.assert_called_with(member1, [member2])
        mock_update.assert_called_with(self.cluster_id, shard_id="shard-1")
        self.assertEqual(False, ret_val)
Example #22
0
 def setUp(self):
     super(SimpleInstanceTest, self).setUp()
     db_info = DBInstance(InstanceTasks.BUILDING, name="TestInstance")
     self.instance = SimpleInstance(None, db_info,
                                    InstanceServiceStatus(
                                        ServiceStatuses.BUILDING),
                                    ds_version=Mock(), ds=Mock())
     db_info.addresses = {"private": [{"addr": "123.123.123.123"}],
                          "internal": [{"addr": "10.123.123.123"}],
                          "public": [{"addr": "15.123.123.123"}]}
     self.orig_conf = CONF.network_label_regex
     self.orig_ip_regex = CONF.ip_regex
     self.orig_black_list_regex = CONF.black_list_regex
 def test_create_cluster(self, mock_dv, mock_ds, mock_find_all, mock_load,
                         mock_ready, mock_ip, mock_guest, mock_reset_task):
     mock_find_all.return_value.all.return_value = [self.dbinst1]
     mock_load.return_value = BaseInstance(Mock(),
                                           self.dbinst1, Mock(),
                                           InstanceServiceStatus(
                                               ServiceStatuses.NEW))
     mock_ip.return_value = "10.0.0.2"
     self.clustertasks.create_cluster(Mock(), self.cluster_id)
     mock_guest.return_value.install_cluster.assert_called_with(['10.0.0.2']
                                                                )
     mock_reset_task.assert_called()
     mock_guest.return_value.cluster_complete.assert_called()
Example #24
0
    def test_create_shard(self, mock_dv, mock_ds, mock_ip, mock_guest,
                          mock_init_rs):
        member1 = BaseInstance(Mock(), self.dbinst1, Mock(),
                               InstanceServiceStatus(ServiceStatuses.NEW))
        member2 = BaseInstance(Mock(), self.dbinst2, Mock(),
                               InstanceServiceStatus(ServiceStatuses.NEW))
        members = [member1, member2]
        mock_ip.side_effect = ["10.0.0.2"]

        query_router = [
            BaseInstance(Mock(), self.dbinst3, Mock(),
                         InstanceServiceStatus(ServiceStatuses.NEW))
        ]
        mock_guest().get_replica_set_name.return_value = 'testrs'
        mock_add_shard = Mock()
        mock_guest().add_shard = mock_add_shard

        ret_val = self.clustertasks._create_shard(query_router, members)

        mock_init_rs.assert_called_with(member1, [member2])
        mock_add_shard.assert_called_with("testrs", "10.0.0.2")
        self.assertEqual(True, ret_val)
Example #25
0
    def test_add_query_routers_failure(self,
                                       mock_logging, mock_dv, mock_ds,
                                       mock_password, mock_guest, mock_update):
        query_router = BaseInstance(
            Mock(), self.dbinst3, Mock(),
            InstanceServiceStatus(ServiceStatuses.NEW)
        )
        mock_guest.side_effect = Exception("Boom!")

        ret_val = self.clustertasks._add_query_routers([query_router],
                                                       ['10.0.0.5'])
        mock_update.assert_called_with(self.cluster_id)
        self.assertFalse(ret_val)
Example #26
0
 def test_create_shard_failure(self, mock_dv, mock_ds, mock_ip, mock_guest,
                               mock_update):
     member1 = BaseInstance(Mock(), self.dbinst1, Mock(),
                            InstanceServiceStatus(ServiceStatuses.NEW))
     member2 = BaseInstance(Mock(), self.dbinst2, Mock(),
                            InstanceServiceStatus(ServiceStatuses.NEW))
     members = [member1, member2]
     query_routers = [
         BaseInstance(Mock(), self.dbinst3, Mock(),
                      InstanceServiceStatus(ServiceStatuses.NEW))
     ]
     mock_ip.return_value = "10.0.0.2"
     mock_guest.return_value.add_shard = Mock(
         side_effect=Exception("Boom!"))
     ret_val = self.clustertasks._create_shard(query_routers,
                                               "rs1",
                                               members,
                                               self.cluster_id,
                                               shard_id="shard-1")
     mock_guest.return_value.add_shard.assert_called_with("rs1", "10.0.0.2")
     mock_update.assert_called_with(self.cluster_id, "shard-1")
     self.assertEqual(False, ret_val)
Example #27
0
    def test_grow_cluster_query_router(self,
                                       mock_dv,
                                       mock_ds,
                                       mock_load,
                                       mock_ip,
                                       mock_add_query_router):
        query_router = BaseInstance(
            Mock(), self.dbinst3, Mock(),
            InstanceServiceStatus(ServiceStatuses.NEW)
        )
        config_server = BaseInstance(
            Mock(), self.dbinst4, Mock(),
            InstanceServiceStatus(ServiceStatuses.NEW)
        )
        mock_load.side_effect = [query_router, config_server]
        mock_ip.return_value = '10.0.0.5'
        mock_add_query_router.return_value = True

        self._run_grow_cluster(new_instances_ids=[query_router.id])

        mock_add_query_router.assert_called_with(
            [query_router], ['10.0.0.5']
        )
Example #28
0
 def build_db_instance(self, status, task_status=InstanceTasks.NONE):
     instance = DBInstance(InstanceTasks.NONE,
                           name='test_name',
                           id=str(uuid.uuid4()),
                           flavor_id='flavor_1',
                           datastore_version_id=self.version.id,
                           compute_instance_id='compute_id_1',
                           server_id='server_id_1',
                           tenant_id='tenant_id_1',
                           server_status=rd_instance.ServiceStatuses.
                           BUILDING.api_status,
                           deleted=False)
     instance.save()
     service_status = InstanceServiceStatus(
         rd_instance.ServiceStatuses.RUNNING,
         id=str(uuid.uuid4()),
         instance_id=instance.id,
     )
     service_status.save()
     instance.set_task_status(task_status)
     instance.server_status = status
     instance.save()
     return instance, service_status
Example #29
0
    def test_grow_cluster_shard(self,
                                mock_dv,
                                mock_ds,
                                mock_running_qr_id,
                                mock_load,
                                mock_create_shard):
        mock_running_qr_id.return_value = '3'
        member1 = BaseInstance(Mock(), self.dbinst1, Mock(),
                               InstanceServiceStatus(ServiceStatuses.NEW))
        member2 = BaseInstance(Mock(), self.dbinst2, Mock(),
                               InstanceServiceStatus(ServiceStatuses.NEW))
        query_router = BaseInstance(
            Mock(), self.dbinst3, Mock(),
            InstanceServiceStatus(ServiceStatuses.NEW)
        )
        mock_load.side_effect = [member1, member2, query_router]
        mock_create_shard.return_value = True

        self._run_grow_cluster(new_instances_ids=[member1.id, member2.id])

        mock_create_shard.assert_called_with(
            query_router, [member1, member2]
        )
Example #30
0
    def setUp(self):
        util.init_db()

        self.datastore = datastore_models.DBDatastore.create(
            id=str(uuid.uuid4()),
            name='name' + str(uuid.uuid4()),
            default_version_id=str(uuid.uuid4()))

        self.datastore_version = datastore_models.DBDatastoreVersion.create(
            id=self.datastore.default_version_id,
            name='name' + str(uuid.uuid4()),
            image_id=str(uuid.uuid4()),
            packages=str(uuid.uuid4()),
            datastore_id=self.datastore.id,
            manager='mysql',
            active=1)

        self.databases = []

        self.users = []

        self.master = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestMasterInstance",
            datastore_version_id=self.datastore_version.id,
            volume_size=2)
        self.master.set_task_status(InstanceTasks.NONE)
        self.master.save()
        self.master_status = InstanceServiceStatus(ServiceStatuses.RUNNING,
                                                   id=str(uuid.uuid4()),
                                                   instance_id=self.master.id)
        self.master_status.save()

        self.safe_nova_client = models.create_nova_client
        models.create_nova_client = nova.fake_create_nova_client
        super(TestReplication, self).setUp()
Example #31
0
 def test_create_cluster_fail(self, mock_logging, mock_dv, mock_ds,
                              mock_find_all, mock_load, mock_ready, mock_ip,
                              mock_reset_task, mock_update_status):
     mock_find_all.return_value.all.return_value = [self.dbinst1]
     mock_load.return_value = BaseInstance(
         Mock(), self.dbinst1, Mock(),
         InstanceServiceStatus(ServiceStatuses.NEW))
     mock_ip.return_value = "10.0.0.2"
     guest_client = Mock()
     guest_client.install_cluster = Mock(side_effect=GuestError("Error"))
     with patch.object(ClusterTasks, 'get_guest',
                       return_value=guest_client):
         self.clustertasks.create_cluster(Mock(), self.cluster_id)
         mock_update_status.assert_called_with('1232')
         mock_reset_task.assert_called_with()
Example #32
0
 def setUp(self):
     super(SimpleInstanceTest, self).setUp()
     self.context = trove_testtools.TroveTestContext(self, is_admin=True)
     db_info = DBInstance(
         InstanceTasks.BUILDING, name="TestInstance")
     self.instance = SimpleInstance(
         None, db_info, InstanceServiceStatus(
             ServiceStatuses.BUILDING), ds_version=Mock(), ds=Mock(),
         locality='affinity')
     self.instance.context = self.context
     db_info.addresses = {"private": [{"addr": "123.123.123.123"}],
                          "internal": [{"addr": "10.123.123.123"}],
                          "public": [{"addr": "15.123.123.123"}]}
     self.orig_conf = CONF.network_label_regex
     self.orig_ip_regex = CONF.ip_regex
     self.orig_black_list_regex = CONF.black_list_regex
Example #33
0
 def test_add_shard_cluster(self, mock_find_all, mock_all_instances_ready,
                            mock_load, mock_dv, mock_ds, mock_add_shard,
                            mock_guest, mock_reset_task):
     mock_find_all.return_value.all.return_value = [
         self.dbinst1, self.dbinst2, self.dbinst3, self.dbinst4
     ]
     mock_load.return_value = BaseInstance(
         Mock(), self.dbinst1, Mock(),
         InstanceServiceStatus(ServiceStatuses.NEW))
     mock_all_instances_ready.return_value = True
     mock_add_shard.return_value = True
     mock_guest.return_value.cluster_complete.return_value = Mock()
     self.clustertasks.add_shard_cluster(Mock(), self.cluster_id, "shard-1",
                                         "rs1")
     mock_guest.return_value.cluster_complete.assert_called_with()
     mock_reset_task.assert_called_with()
 def setUp(self):
     util.init_db()
     self.db_info = FakeDBInstance()
     self.status = InstanceServiceStatus(ServiceStatuses.RUNNING)
     self.datastore = models.DBDatastore.create(
         id=str(uuid.uuid4()),
         name='mysql' + str(uuid.uuid4()),
         default_version_id=self.db_info.datastore_version_id)
     self.version = models.DBDatastoreVersion.create(
         id=self.db_info.datastore_version_id,
         datastore_id=self.datastore.id,
         name='5.7' + str(uuid.uuid4()),
         manager='mysql',
         image_id=str(uuid.uuid4()),
         active=1,
         packages="mysql-server-5.7")
     super(BaseInstanceStatusTestCase, self).setUp()
Example #35
0
    def test_add_query_routers(self,
                               mock_dv,
                               mock_ds,
                               mock_guest):
        password = '******'
        query_router = BaseInstance(
            Mock(), self.dbinst3, Mock(),
            InstanceServiceStatus(ServiceStatuses.NEW)
        )

        ret_val = self.clustertasks._add_query_routers([query_router],
                                                       ['10.0.0.5'],
                                                       admin_password=password)
        mock_guest.assert_called_with(query_router)
        mock_guest().add_config_servers.assert_called_with(['10.0.0.5'])
        mock_guest().store_admin_password.assert_called_with(password)
        self.assertTrue(ret_val)
Example #36
0
    def test_tranformer(self):
        transformer = mgmtmodels.NotificationTransformer(context=self.context)
        status = rd_instance.ServiceStatuses.BUILDING.api_status
        db_instance = MockMgmtInstanceTest.build_db_instance(
            status, InstanceTasks.BUILDING)

        when(DatabaseModelBase).find_all(deleted=False).thenReturn(
            [db_instance])
        when(DatabaseModelBase).find_by(instance_id='1').thenReturn(
            InstanceServiceStatus(rd_instance.ServiceStatuses.BUILDING))

        payloads = transformer()
        self.assertIsNotNone(payloads)
        self.assertThat(len(payloads), Equals(1))
        payload = payloads[0]
        self.assertThat(payload['audit_period_beginning'], Not(Is(None)))
        self.assertThat(payload['audit_period_ending'], Not(Is(None)))
        self.assertThat(payload['state'], Equals(status.lower()))
Example #37
0
 def __call__(self):
     audit_start, audit_end = NotificationTransformer._get_audit_period()
     messages = []
     db_infos = instance_models.DBInstance.find_all(deleted=False)
     for db_info in db_infos:
         try:
             service_status = InstanceServiceStatus.find_by(instance_id=db_info.id)
         except exception.ModelNotFoundError:
             # There is a small window of opportunity during when the db
             # resource for an instance exists, but no InstanceServiceStatus
             # for it has yet been created. We skip sending the notification
             # message for all such instances. These instance are too new
             # and will get picked up the next round of notifications.
             LOG.debug("InstanceServiceStatus not found for %s. " "Will wait to send notification." % db_info.id)
             continue
         instance = SimpleMgmtInstance(None, db_info, None, service_status)
         message = self.transform_instance(instance, audit_start, audit_end)
         messages.append(message)
     return messages
Example #38
0
 def _all_status_ready(ids):
     LOG.debug("Checking service status of instance ids: %s" % ids)
     for instance_id in ids:
         status = InstanceServiceStatus.find_by(
             instance_id=instance_id).get_status()
         if (status == ServiceStatuses.FAILED or
            status == ServiceStatuses.FAILED_TIMEOUT_GUESTAGENT):
                 # if one has failed, no need to continue polling
                 LOG.debug("Instance %s in %s, exiting polling." % (
                     instance_id, status))
                 return True
         if (status != ServiceStatuses.RUNNING and
            status != ServiceStatuses.BUILD_PENDING):
                 # if one is not in a ready state, continue polling
                 LOG.debug("Instance %s in %s, continue polling." % (
                     instance_id, status))
                 return False
     LOG.debug("Instances are ready, exiting polling for: %s" % ids)
     return True
Example #39
0
 def _all_status_ready(ids):
     LOG.debug("Checking service status of instance ids: %s" % ids)
     for instance_id in ids:
         status = InstanceServiceStatus.find_by(
             instance_id=instance_id).get_status()
         if (status == ServiceStatuses.FAILED or status
                 == ServiceStatuses.FAILED_TIMEOUT_GUESTAGENT):
             # if one has failed, no need to continue polling
             LOG.debug("Instance %s in %s, exiting polling." %
                       (instance_id, status))
             return True
         if (status != ServiceStatuses.RUNNING
                 and status != ServiceStatuses.BUILD_PENDING):
             # if one is not in a ready state, continue polling
             LOG.debug("Instance %s in %s, continue polling." %
                       (instance_id, status))
             return False
     LOG.debug("Instances are ready, exiting polling for: %s" % ids)
     return True
Example #40
0
 def __init__(self, host_info):
     self.name = host_info.name
     self.percent_used = host_info.percentUsed
     self.total_ram = host_info.totalRAM
     self.used_ram = host_info.usedRAM
     self.instances = host_info.instances
     for instance in self.instances:
         instance["server_id"] = instance["uuid"]
         del instance["uuid"]
         try:
             db_info = DBInstance.find_by(compute_instance_id=instance["server_id"])
             instance["id"] = db_info.id
             instance["tenant_id"] = db_info.tenant_id
             status = InstanceServiceStatus.find_by(instance_id=db_info.id)
             instance_info = SimpleInstance(None, db_info, status)
             instance["status"] = instance_info.status
         except exception.TroveError as re:
             LOG.error(re)
             LOG.error("Compute Instance ID found with no associated RD " "instance: %s" % instance["server_id"])
             instance["id"] = None
Example #41
0
 def __call__(self):
     audit_start, audit_end = NotificationTransformer._get_audit_period()
     messages = []
     db_infos = instance_models.DBInstance.find_all(deleted=False)
     for db_info in db_infos:
         try:
             service_status = InstanceServiceStatus.find_by(
                 instance_id=db_info.id)
         except exception.ModelNotFoundError:
             # There is a small window of opportunity during when the db
             # resource for an instance exists, but no InstanceServiceStatus
             # for it has yet been created. We skip sending the notification
             # message for all such instances. These instance are too new
             # and will get picked up the next round of notifications.
             LOG.debug("InstanceServiceStatus not found for %s. "
                       "Will wait to send notification." % db_info.id)
             continue
         instance = SimpleMgmtInstance(None, db_info, None, service_status)
         message = self.transform_instance(instance, audit_start, audit_end)
         messages.append(message)
     return messages
Example #42
0
 def __init__(self, host_info):
     self.name = host_info.name
     self.percent_used = host_info.percentUsed
     self.total_ram = host_info.totalRAM
     self.used_ram = host_info.usedRAM
     self.instances = host_info.instances
     for instance in self.instances:
         instance['server_id'] = instance['uuid']
         del instance['uuid']
         try:
             db_info = DBInstance.find_by(
                 compute_instance_id=instance['server_id'])
             instance['id'] = db_info.id
             instance['tenant_id'] = db_info.tenant_id
             status = InstanceServiceStatus.find_by(instance_id=db_info.id)
             instance_info = SimpleInstance(None, db_info, status)
             instance['status'] = instance_info.status
         except exception.TroveError as re:
             LOG.error(re)
             LOG.error("Compute Instance ID found with no associated RD "
                       "instance: %s" % instance['server_id'])
             instance['id'] = None
Example #43
0
    def _service_is_active(self):
        """
        Check that the database guest is active.

        This function is meant to be called with poll_until to check that
        the guest is alive before sending a 'create' message. This prevents
        over billing a customer for a instance that they can never use.

        Returns: boolean if the service is active.
        Raises: TroveError if the service is in a failure state.
        """
        service = InstanceServiceStatus.find_by(instance_id=self.id)
        status = service.get_status()
        if status == ServiceStatuses.RUNNING:
            return True
        elif status not in [ServiceStatuses.NEW, ServiceStatuses.BUILDING]:
            raise TroveError("Service not active, status: %s" % status)

        c_id = self.db_info.compute_instance_id
        nova_status = self.nova_client.servers.get(c_id).status
        if nova_status in [InstanceStatus.ERROR, InstanceStatus.FAILED]:
            raise TroveError("Server not active, status: %s" % nova_status)
        return False
Example #44
0
class TestReplication(trove_testtools.TestCase):
    def setUp(self):
        util.init_db()

        self.datastore = datastore_models.DBDatastore.create(
            id=str(uuid.uuid4()),
            name='name' + str(uuid.uuid4()),
            default_version_id=str(uuid.uuid4()))

        self.datastore_version = datastore_models.DBDatastoreVersion.create(
            id=self.datastore.default_version_id,
            name='name' + str(uuid.uuid4()),
            image_id=str(uuid.uuid4()),
            packages=str(uuid.uuid4()),
            datastore_id=self.datastore.id,
            manager='mysql',
            active=1)

        self.databases = []

        self.users = []

        self.master = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestMasterInstance",
            datastore_version_id=self.datastore_version.id,
            volume_size=2)
        self.master.set_task_status(InstanceTasks.NONE)
        self.master.save()
        self.master_status = InstanceServiceStatus(ServiceStatuses.RUNNING,
                                                   id=str(uuid.uuid4()),
                                                   instance_id=self.master.id)
        self.master_status.save()

        self.safe_nova_client = models.create_nova_client
        models.create_nova_client = nova.fake_create_nova_client

        self.swift_verify_patch = patch.object(models.Backup,
                                               'verify_swift_auth_token')
        self.addCleanup(self.swift_verify_patch.stop)
        self.swift_verify_patch.start()

        super(TestReplication, self).setUp()

    def tearDown(self):
        self.master.delete()
        self.master_status.delete()
        self.datastore.delete()
        self.datastore_version.delete()
        models.create_nova_client = self.safe_nova_client
        super(TestReplication, self).tearDown()

    @patch('trove.instance.models.LOG')
    def test_replica_of_not_active_master(self, mock_logging):
        self.master.set_task_status(InstanceTasks.BUILDING)
        self.master.save()
        self.master_status.set_status(ServiceStatuses.BUILDING)
        self.master_status.save()
        self.assertRaises(exception.UnprocessableEntity,
                          Instance.create,
                          None,
                          'name',
                          1,
                          "UUID", [], [],
                          self.datastore,
                          self.datastore_version,
                          2,
                          None,
                          slave_of_id=self.master.id)

    @patch('trove.instance.models.LOG')
    def test_replica_with_invalid_slave_of_id(self, mock_logging):
        self.assertRaises(exception.NotFound,
                          Instance.create,
                          None,
                          'name',
                          1,
                          "UUID", [], [],
                          self.datastore,
                          self.datastore_version,
                          2,
                          None,
                          slave_of_id=str(uuid.uuid4()))

    def test_create_replica_from_replica(self):
        self.replica_datastore_version = Mock(
            spec=datastore_models.DBDatastoreVersion)
        self.replica_datastore_version.id = "UUID"
        self.replica_datastore_version.manager = 'mysql'
        self.replica_info = DBInstance(
            InstanceTasks.NONE,
            id="UUID",
            name="TestInstance",
            datastore_version_id=self.replica_datastore_version.id,
            slave_of_id=self.master.id)
        self.replica_info.save()
        self.assertRaises(exception.Forbidden,
                          Instance.create,
                          None,
                          'name',
                          2,
                          "UUID", [], [],
                          self.datastore,
                          self.datastore_version,
                          2,
                          None,
                          slave_of_id=self.replica_info.id)

    def test_create_replica_with_users(self):
        self.users.append({"name": "testuser", "password": "******"})
        self.assertRaises(exception.ReplicaCreateWithUsersDatabasesError,
                          Instance.create,
                          None,
                          'name',
                          2,
                          "UUID", [],
                          self.users,
                          self.datastore,
                          self.datastore_version,
                          1,
                          None,
                          slave_of_id=self.master.id)

    def test_create_replica_with_databases(self):
        self.databases.append({"name": "testdb"})
        self.assertRaises(exception.ReplicaCreateWithUsersDatabasesError,
                          Instance.create,
                          None,
                          'name',
                          1,
                          "UUID",
                          self.databases, [],
                          self.datastore,
                          self.datastore_version,
                          2,
                          None,
                          slave_of_id=self.master.id)

    def test_replica_volume_size_smaller_than_master(self):
        self.assertRaises(exception.Forbidden,
                          Instance.create,
                          None,
                          'name',
                          1,
                          "UUID", [], [],
                          self.datastore,
                          self.datastore_version,
                          1,
                          None,
                          slave_of_id=self.master.id)
class TestReplication(TestCase):
    def setUp(self):
        util.init_db()

        self.datastore = datastore_models.DBDatastore.create(
            id=str(uuid.uuid4()),
            name='name',
            default_version_id=str(uuid.uuid4()))

        self.datastore_version = datastore_models.DBDatastoreVersion.create(
            id=self.datastore.default_version_id,
            name='name',
            image_id=str(uuid.uuid4()),
            packages=str(uuid.uuid4()),
            datastore_id=self.datastore.id,
            manager='mysql',
            active=1)

        self.master = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestMasterInstance",
            datastore_version_id=self.datastore_version.id)
        self.master.set_task_status(InstanceTasks.NONE)
        self.master.save()
        self.master_status = InstanceServiceStatus(ServiceStatuses.RUNNING,
                                                   id=str(uuid.uuid4()),
                                                   instance_id=self.master.id)
        self.master_status.save()

        self.safe_nova_client = models.create_nova_client
        models.create_nova_client = nova.fake_create_nova_client
        super(TestReplication, self).setUp()

    def tearDown(self):
        self.master.delete()
        self.master_status.delete()
        self.datastore.delete()
        self.datastore_version.delete()
        models.create_nova_client = self.safe_nova_client
        super(TestReplication, self).tearDown()

    def test_replica_of_not_active_master(self):
        self.master.set_task_status(InstanceTasks.BUILDING)
        self.master.save()
        self.master_status.set_status(ServiceStatuses.BUILDING)
        self.master_status.save()
        self.assertRaises(exception.UnprocessableEntity,
                          Instance.create,
                          None,
                          'name',
                          1,
                          "UUID", [], [],
                          None,
                          self.datastore_version,
                          1,
                          None,
                          slave_of_id=self.master.id)

    def test_replica_with_invalid_slave_of_id(self):
        self.assertRaises(exception.NotFound,
                          Instance.create,
                          None,
                          'name',
                          1,
                          "UUID", [], [],
                          None,
                          self.datastore_version,
                          1,
                          None,
                          slave_of_id=str(uuid.uuid4()))

    def test_create_replica_from_replica(self):
        self.replica_datastore_version = Mock(
            spec=datastore_models.DBDatastoreVersion)
        self.replica_datastore_version.id = "UUID"
        self.replica_datastore_version.manager = 'mysql'
        self.replica_info = DBInstance(
            InstanceTasks.NONE,
            id="UUID",
            name="TestInstance",
            datastore_version_id=self.replica_datastore_version.id,
            slave_of_id=self.master.id)
        self.replica_info.save()
        self.assertRaises(exception.Forbidden,
                          Instance.create,
                          None,
                          'name',
                          2,
                          "UUID", [], [],
                          None,
                          self.datastore_version,
                          1,
                          None,
                          slave_of_id=self.replica_info.id)
Example #46
0
 def setUp(self):
     self.db_info.task_status = FakeInstanceTask()
     self.db_info.server_status = "ACTIVE"
     self.ds_status = InstanceServiceStatus(ServiceStatuses.HEALTHY)
     super(InstanceStatusTest, self).setUp()
 def _create(self,group_id,type):
     id = self.instance_id
     InstanceServiceStatus.create(status=ServiceStatuses.RUNNING,instance_id=id)
     return InstanceGroupItem.create(self.context,self.tenant_id,id,group_id,type)
Example #48
0
 def tearDown(self):
     super(MySqlAppTest, self).tearDown()
     dbaas.utils.execute_with_timeout = self.orig_utils_execute_with_timeout
     time.sleep = self.orig_time_sleep
     InstanceServiceStatus.find_by(instance_id=self.FAKE_ID).delete()
Example #49
0
    def setUp(self):
        super(BuiltInstanceTasksTest, self).setUp()
        self.new_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'}
        stub_nova_server = MagicMock()
        self.rpc_patches = patch.multiple(
            rpc, get_notifier=MagicMock(), get_client=MagicMock())
        self.rpc_mocks = self.rpc_patches.start()
        self.addCleanup(self.rpc_patches.stop)
        db_instance = DBInstance(InstanceTasks.NONE,
                                 id=INST_ID,
                                 name='resize-inst-name',
                                 datastore_version_id='1',
                                 datastore_id='id-1',
                                 flavor_id='6',
                                 manager='mysql',
                                 created=datetime.datetime.utcnow(),
                                 updated=datetime.datetime.utcnow(),
                                 compute_instance_id='computeinst-id-1',
                                 tenant_id='testresize-tenant-id',
                                 volume_size='1',
                                 volume_id=VOLUME_ID)

        # this is used during the final check of whether the resize successful
        db_instance.server_status = 'ACTIVE'
        self.db_instance = db_instance
        self.dm_dv_load_by_uuid_patch = patch.object(
            datastore_models.DatastoreVersion, 'load_by_uuid', MagicMock(
                return_value=datastore_models.DatastoreVersion(db_instance)))
        self.dm_dv_load_by_uuid_mock = self.dm_dv_load_by_uuid_patch.start()
        self.addCleanup(self.dm_dv_load_by_uuid_patch.stop)
        self.dm_ds_load_patch = patch.object(
            datastore_models.Datastore, 'load', MagicMock(
                return_value=datastore_models.Datastore(db_instance)))
        self.dm_ds_load_mock = self.dm_ds_load_patch.start()
        self.addCleanup(self.dm_ds_load_patch.stop)

        self.instance_task = taskmanager_models.BuiltInstanceTasks(
            trove.common.context.TroveContext(),
            db_instance,
            stub_nova_server,
            InstanceServiceStatus(ServiceStatuses.RUNNING,
                                  id='inst-stat-id-0'))

        self.instance_task._guest = MagicMock(spec=trove.guestagent.api.API)
        self.instance_task._nova_client = MagicMock(
            spec=novaclient.client)
        self.stub_server_mgr = MagicMock(
            spec=novaclient.v2.servers.ServerManager)
        self.stub_running_server = MagicMock(
            spec=novaclient.v2.servers.Server)
        self.stub_running_server.status = 'ACTIVE'
        self.stub_running_server.flavor = {'id': 6, 'ram': 512}
        self.stub_verifying_server = MagicMock(
            spec=novaclient.v2.servers.Server)
        self.stub_verifying_server.status = 'VERIFY_RESIZE'
        self.stub_verifying_server.flavor = {'id': 8, 'ram': 768}
        self.stub_server_mgr.get = MagicMock(
            return_value=self.stub_verifying_server)
        self.instance_task._nova_client.servers = self.stub_server_mgr
        stub_flavor_manager = MagicMock(
            spec=novaclient.v2.flavors.FlavorManager)
        self.instance_task._nova_client.flavors = stub_flavor_manager

        nova_flavor = novaclient.v2.flavors.Flavor(stub_flavor_manager,
                                                   self.new_flavor,
                                                   True)
        stub_flavor_manager.get = MagicMock(return_value=nova_flavor)

        self.instance_task._volume_client = MagicMock(spec=cinderclient)
        self.instance_task._volume_client.volumes = Mock(
            spec=cinderclient_volumes.VolumeManager)

        answers = (status for status in
                   self.get_inst_service_status('inst_stat-id',
                                                [ServiceStatuses.SHUTDOWN,
                                                 ServiceStatuses.RUNNING,
                                                 ServiceStatuses.RUNNING,
                                                 ServiceStatuses.RUNNING]))

        def side_effect_func(*args, **kwargs):
            if 'instance_id' in kwargs:
                return next(answers)
            elif ('id' in kwargs and 'deleted' in kwargs
                  and not kwargs['deleted']):
                return db_instance
            else:
                return MagicMock()

        self.dbm_dbmb_patch = patch.object(
            trove.db.models.DatabaseModelBase, 'find_by',
            MagicMock(side_effect=side_effect_func))
        self.dbm_dbmb_mock = self.dbm_dbmb_patch.start()
        self.addCleanup(self.dbm_dbmb_patch.stop)

        self.template_patch = patch.object(
            template, 'SingleInstanceConfigTemplate',
            MagicMock(spec=template.SingleInstanceConfigTemplate))
        self.template_mock = self.template_patch.start()
        self.addCleanup(self.template_patch.stop)
        db_instance.save = MagicMock(return_value=None)
        self.tbmb_running_patch = patch.object(
            trove.backup.models.Backup, 'running',
            MagicMock(return_value=None))
        self.tbmb_running_mock = self.tbmb_running_patch.start()
        self.addCleanup(self.tbmb_running_patch.stop)

        if 'volume' in self._testMethodName:
            self._stub_volume_client()

        stub_floating_ips_manager = MagicMock(
            spec=novaclient.v2.floating_ips.FloatingIPManager)
        self.instance_task._nova_client.floating_ips = (
            stub_floating_ips_manager)
        floatingip = novaclient.v2.floating_ips.FloatingIP(
            stub_floating_ips_manager, {'ip': '192.168.10.1'}, True)
        stub_floating_ips_manager.list = MagicMock(return_value=[floatingip])
Example #50
0
 def set_server_running():
     instance = DBInstance.find_by(compute_instance_id=id)
     LOG.debug("Setting server %s to running" % instance.id)
     status = InstanceServiceStatus.find_by(instance_id=instance.id)
     status.status = rd_instance.ServiceStatuses.RUNNING
     status.save()
Example #51
0
 def _set_service_status_to_paused(self):
     status = InstanceServiceStatus.find_by(instance_id=self.id)
     status.set_status(inst_models.ServiceStatuses.PAUSED)
     status.save()
Example #52
0
 def _refresh_compute_service_status(self):
     """Refreshes the service status info for an instance."""
     service = InstanceServiceStatus.find_by(instance_id=self.id)
     self.service_status = service.get_status()
Example #53
0
 def _refresh_compute_service_status(self):
     """Refreshes the service status info for an instance."""
     service = InstanceServiceStatus.find_by(instance_id=self.id)
     self.service_status = service.get_status()
Example #54
0
    def setUp(self):
        super(BuiltInstanceTasksTest, self).setUp()
        self.new_flavor = {'id': 8, 'ram': 768, 'name': 'bigger_flavor'}
        stub_nova_server = MagicMock()
        rpc.get_notifier = MagicMock()
        rpc.get_client = MagicMock()
        db_instance = DBInstance(InstanceTasks.NONE,
                                 id=INST_ID,
                                 name='resize-inst-name',
                                 datastore_version_id='1',
                                 datastore_id='id-1',
                                 flavor_id='6',
                                 manager='mysql',
                                 created=datetime.datetime.utcnow(),
                                 updated=datetime.datetime.utcnow(),
                                 compute_instance_id='computeinst-id-1',
                                 tenant_id='testresize-tenant-id',
                                 volume_size='1',
                                 volume_id=VOLUME_ID)

        # this is used during the final check of whether the resize successful
        db_instance.server_status = 'ACTIVE'
        self.db_instance = db_instance
        datastore_models.DatastoreVersion.load_by_uuid = MagicMock(
            return_value=datastore_models.DatastoreVersion(db_instance))
        datastore_models.Datastore.load = MagicMock(
            return_value=datastore_models.Datastore(db_instance))

        self.instance_task = taskmanager_models.BuiltInstanceTasks(
            trove.common.context.TroveContext(),
            db_instance,
            stub_nova_server,
            InstanceServiceStatus(ServiceStatuses.RUNNING,
                                  id='inst-stat-id-0'))

        self.instance_task._guest = MagicMock(spec=trove.guestagent.api.API)
        self.instance_task._nova_client = MagicMock(
            spec=novaclient.v2.Client)
        self.stub_server_mgr = MagicMock(
            spec=novaclient.v2.servers.ServerManager)
        self.stub_running_server = MagicMock(
            spec=novaclient.v2.servers.Server)
        self.stub_running_server.status = 'ACTIVE'
        self.stub_running_server.flavor = {'id': 6, 'ram': 512}
        self.stub_verifying_server = MagicMock(
            spec=novaclient.v2.servers.Server)
        self.stub_verifying_server.status = 'VERIFY_RESIZE'
        self.stub_verifying_server.flavor = {'id': 8, 'ram': 768}
        self.stub_server_mgr.get = MagicMock(
            return_value=self.stub_verifying_server)
        self.instance_task._nova_client.servers = self.stub_server_mgr
        stub_flavor_manager = MagicMock(
            spec=novaclient.v2.flavors.FlavorManager)
        self.instance_task._nova_client.flavors = stub_flavor_manager

        nova_flavor = novaclient.v2.flavors.Flavor(stub_flavor_manager,
                                                   self.new_flavor,
                                                   True)
        stub_flavor_manager.get = MagicMock(return_value=nova_flavor)

        answers = (status for status in
                   self.get_inst_service_status('inst_stat-id',
                                                [ServiceStatuses.SHUTDOWN,
                                                 ServiceStatuses.RUNNING,
                                                 ServiceStatuses.RUNNING,
                                                 ServiceStatuses.RUNNING]))

        def side_effect_func(*args, **kwargs):
            if 'instance_id' in kwargs:
                return answers.next()
            elif ('id' in kwargs and 'deleted' in kwargs
                  and not kwargs['deleted']):
                return db_instance
            else:
                return MagicMock()
        trove.db.models.DatabaseModelBase.find_by = MagicMock(
            side_effect=side_effect_func)

        template.SingleInstanceConfigTemplate = MagicMock(
            spec=template.SingleInstanceConfigTemplate)
        db_instance.save = MagicMock(return_value=None)
        trove.backup.models.Backup.running = MagicMock(return_value=None)

        if 'volume' in self._testMethodName:
            self._stub_volume_client()
Example #55
0
 def set_server_running():
     instance = DBInstance.find_by(compute_instance_id=id)
     LOG.debug("Setting server %s to running" % instance.id)
     status = InstanceServiceStatus.find_by(instance_id=instance.id)
     status.status = rd_instance.ServiceStatuses.RUNNING
     status.save()
Example #56
0
 def assert_reported_status(self, expected_status):
     service_status = InstanceServiceStatus.find_by(
         instance_id=self.FAKE_ID)
     self.assertEqual(expected_status, service_status.status)
Example #57
0
 def _load_status(self):
     return InstanceServiceStatus.find_by(instance_id=self.id)
Example #58
0
class TestReplication(TestCase):
    def setUp(self):
        util.init_db()

        self.datastore = datastore_models.DBDatastore.create(
            id=str(uuid.uuid4()), name="name", default_version_id=str(uuid.uuid4())
        )

        self.datastore_version = datastore_models.DBDatastoreVersion.create(
            id=self.datastore.default_version_id,
            name="name",
            image_id=str(uuid.uuid4()),
            packages=str(uuid.uuid4()),
            datastore_id=self.datastore.id,
            manager="mysql",
            active=1,
        )

        self.master = DBInstance(
            InstanceTasks.NONE,
            id=str(uuid.uuid4()),
            name="TestMasterInstance",
            datastore_version_id=self.datastore_version.id,
        )
        self.master.set_task_status(InstanceTasks.NONE)
        self.master.save()
        self.master_status = InstanceServiceStatus(
            ServiceStatuses.RUNNING, id=str(uuid.uuid4()), instance_id=self.master.id
        )
        self.master_status.save()

        self.safe_nova_client = models.create_nova_client
        models.create_nova_client = nova.fake_create_nova_client
        super(TestReplication, self).setUp()

    def tearDown(self):
        self.master.delete()
        self.master_status.delete()
        self.datastore.delete()
        self.datastore_version.delete()
        models.create_nova_client = self.safe_nova_client
        super(TestReplication, self).tearDown()

    def test_replica_of_not_active_master(self):
        self.master.set_task_status(InstanceTasks.BUILDING)
        self.master.save()
        self.master_status.set_status(ServiceStatuses.BUILDING)
        self.master_status.save()
        self.assertRaises(
            exception.UnprocessableEntity,
            Instance.create,
            None,
            "name",
            1,
            "UUID",
            [],
            [],
            None,
            self.datastore_version,
            1,
            None,
            slave_of_id=self.master.id,
        )

    def test_replica_with_invalid_slave_of_id(self):
        self.assertRaises(
            exception.NotFound,
            Instance.create,
            None,
            "name",
            1,
            "UUID",
            [],
            [],
            None,
            self.datastore_version,
            1,
            None,
            slave_of_id=str(uuid.uuid4()),
        )

    def test_create_replica_from_replica(self):
        self.replica_datastore_version = Mock(spec=datastore_models.DBDatastoreVersion)
        self.replica_datastore_version.id = "UUID"
        self.replica_datastore_version.manager = "mysql"
        self.replica_info = DBInstance(
            InstanceTasks.NONE,
            id="UUID",
            name="TestInstance",
            datastore_version_id=self.replica_datastore_version.id,
            slave_of_id=self.master.id,
        )
        self.replica_info.save()
        self.assertRaises(
            exception.Forbidden,
            Instance.create,
            None,
            "name",
            2,
            "UUID",
            [],
            [],
            None,
            self.datastore_version,
            1,
            None,
            slave_of_id=self.replica_info.id,
        )
Example #59
0
 def _set_service_status_to_paused(self):
     status = InstanceServiceStatus.find_by(instance_id=self.id)
     status.set_status(rd_instance.ServiceStatuses.PAUSED)
     status.save()
Example #60
0
 def tearDown(self):
     super(BaseDbStatusTest, self).tearDown()
     dbaas.time.sleep = self.orig_dbaas_time_sleep
     InstanceServiceStatus.find_by(instance_id=self.FAKE_ID).delete()
     dbaas.CONF.guest_id = None