Example #1
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=ServiceStatuses.NEW)
     self.appStatus = FakeAppStatus(self.FAKE_ID, ServiceStatuses.NEW)
     self.mySqlApp = MySqlApp(self.appStatus)
     time.sleep = Mock()
Example #2
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=ServiceStatuses.NEW)
     self.appStatus = FakeAppStatus(self.FAKE_ID, ServiceStatuses.NEW)
     self.mySqlApp = MySqlApp(self.appStatus)
     time.sleep = Mock()
Example #3
0
class MySqlAppTest(testtools.TestCase):

    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=ServiceStatuses.NEW)
        self.appStatus = FakeAppStatus(self.FAKE_ID, ServiceStatuses.NEW)
        self.mySqlApp = MySqlApp(self.appStatus)
        time.sleep = Mock()

    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()

    def assert_reported_status(self, expected_status):
        service_status = InstanceServiceStatus.find_by(instance_id=
                                                       self.FAKE_ID)
        self.assertEqual(expected_status, service_status.status)

    def mysql_starts_successfully(self):
        def start(update_db=False):
            self.appStatus.set_next_status(ServiceStatuses.RUNNING)

        self.mySqlApp.start_mysql.side_effect = start

    def mysql_starts_unsuccessfully(self):
        def start():
            raise RuntimeError("MySQL failed to start!")

        self.mySqlApp.start_mysql.side_effect = start

    def mysql_stops_successfully(self):
        def stop():
            self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.mySqlApp.stop_mysql.side_effect = stop

    def mysql_stops_unsuccessfully(self):
        def stop():
            raise RuntimeError("MySQL failed to stop!")

        self.mySqlApp.stop_mysql.side_effect = stop

    def test_stop_mysql(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.mySqlApp.stop_mysql()
        self.assert_reported_status(ServiceStatuses.NEW)

    def test_stop_mysql_with_db_update(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.mySqlApp.stop_mysql(True)
        self.assert_reported_status(ServiceStatuses.SHUTDOWN)

    def test_stop_mysql_error(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.RUNNING)
        self.mySqlApp.state_change_wait_time = 1
        self.assertRaises(RuntimeError, self.mySqlApp.stop_mysql)

    def test_restart_is_successful(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp.stop_mysql = Mock()
        self.mysql_stops_successfully()
        self.mysql_starts_successfully()

        self.mySqlApp.restart()

        self.assertTrue(self.mySqlApp.stop_mysql.called)
        self.assertTrue(self.mySqlApp.start_mysql.called)
        self.assert_reported_status(ServiceStatuses.RUNNING)

    def test_restart_mysql_wont_start_up(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp.stop_mysql = Mock()
        self.mysql_stops_unsuccessfully()
        self.mysql_starts_unsuccessfully()

        self.assertRaises(RuntimeError, self.mySqlApp.restart)

        self.assertTrue(self.mySqlApp.stop_mysql.called)
        self.assertFalse(self.mySqlApp.start_mysql.called)
        self.assert_reported_status(ServiceStatuses.NEW)

    def test_wipe_ib_logfiles_no_file(self):

        from reddwarf.common.exception import ProcessExecutionError
        dbaas.utils.execute_with_timeout = \
            Mock(side_effect=
                 ProcessExecutionError('No such file or directory'))

        self.mySqlApp.wipe_ib_logfiles()

    def test_wipe_ib_logfiles_error(self):

        from reddwarf.common.exception import ProcessExecutionError
        dbaas.utils.execute_with_timeout = Mock(side_effect=
                                                ProcessExecutionError('Error'))

        self.assertRaises(ProcessExecutionError,
                          self.mySqlApp.wipe_ib_logfiles)

    def test_start_mysql(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.RUNNING)

        self.mySqlApp.start_mysql()
        self.assert_reported_status(ServiceStatuses.NEW)

    def test_start_mysql_with_db_update(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.RUNNING)

        self.mySqlApp.start_mysql(True)
        self.assert_reported_status(ServiceStatuses.RUNNING)

    def test_start_mysql_runs_forever(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.mySqlApp.state_change_wait_time = 1
        self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.assertRaises(RuntimeError, self.mySqlApp.start_mysql)
        self.assert_reported_status(ServiceStatuses.SHUTDOWN)

    def test_start_mysql_error(self):

        self.mySqlApp._enable_mysql_on_boot = Mock()
        from reddwarf.common.exception import ProcessExecutionError
        dbaas.utils.execute_with_timeout = Mock(side_effect=
                                                ProcessExecutionError('Error'))

        self.assertRaises(RuntimeError, self.mySqlApp.start_mysql)

    def test_start_mysql_with_conf_changes(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp._write_mycnf = Mock()
        self.mysql_starts_successfully()

        self.appStatus.status = ServiceStatuses.SHUTDOWN
        self.mySqlApp.start_mysql_with_conf_changes(Mock())

        self.assertTrue(self.mySqlApp._write_mycnf.called)
        self.assertTrue(self.mySqlApp.start_mysql.called)
        self.assertEqual(self.appStatus._get_actual_db_status(),
                         ServiceStatuses.RUNNING)

    def test_start_mysql_with_conf_changes_mysql_is_running(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp._write_mycnf = Mock()

        self.appStatus.status = ServiceStatuses.RUNNING
        self.assertRaises(RuntimeError,
                          self.mySqlApp.start_mysql_with_conf_changes, Mock())
Example #4
0
class MySqlAppTest(testtools.TestCase):
    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=ServiceStatuses.NEW)
        self.appStatus = FakeAppStatus(self.FAKE_ID, ServiceStatuses.NEW)
        self.mySqlApp = MySqlApp(self.appStatus)
        time.sleep = Mock()

    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()

    def assert_reported_status(self, expected_status):
        service_status = InstanceServiceStatus.find_by(
            instance_id=self.FAKE_ID)
        self.assertEqual(expected_status, service_status.status)

    def mysql_starts_successfully(self):
        def start(update_db=False):
            self.appStatus.set_next_status(ServiceStatuses.RUNNING)

        self.mySqlApp.start_mysql.side_effect = start

    def mysql_starts_unsuccessfully(self):
        def start():
            raise RuntimeError("MySQL failed to start!")

        self.mySqlApp.start_mysql.side_effect = start

    def mysql_stops_successfully(self):
        def stop():
            self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.mySqlApp.stop_mysql.side_effect = stop

    def mysql_stops_unsuccessfully(self):
        def stop():
            raise RuntimeError("MySQL failed to stop!")

        self.mySqlApp.stop_mysql.side_effect = stop

    def test_stop_mysql(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.mySqlApp.stop_mysql()
        self.assert_reported_status(ServiceStatuses.NEW)

    def test_stop_mysql_with_db_update(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.mySqlApp.stop_mysql(True)
        self.assert_reported_status(ServiceStatuses.SHUTDOWN)

    def test_stop_mysql_error(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.RUNNING)
        self.mySqlApp.state_change_wait_time = 1
        self.assertRaises(RuntimeError, self.mySqlApp.stop_mysql)

    def test_restart_is_successful(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp.stop_mysql = Mock()
        self.mysql_stops_successfully()
        self.mysql_starts_successfully()

        self.mySqlApp.restart()

        self.assertTrue(self.mySqlApp.stop_mysql.called)
        self.assertTrue(self.mySqlApp.start_mysql.called)
        self.assert_reported_status(ServiceStatuses.RUNNING)

    def test_restart_mysql_wont_start_up(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp.stop_mysql = Mock()
        self.mysql_stops_unsuccessfully()
        self.mysql_starts_unsuccessfully()

        self.assertRaises(RuntimeError, self.mySqlApp.restart)

        self.assertTrue(self.mySqlApp.stop_mysql.called)
        self.assertFalse(self.mySqlApp.start_mysql.called)
        self.assert_reported_status(ServiceStatuses.NEW)

    def test_wipe_ib_logfiles_no_file(self):

        from reddwarf.common.exception import ProcessExecutionError
        processexecerror = ProcessExecutionError('No such file or directory')
        dbaas.utils.execute_with_timeout = Mock(side_effect=processexecerror)

        self.mySqlApp.wipe_ib_logfiles()

    def test_wipe_ib_logfiles_error(self):

        from reddwarf.common.exception import ProcessExecutionError
        mocked = Mock(side_effect=ProcessExecutionError('Error'))
        dbaas.utils.execute_with_timeout = mocked

        self.assertRaises(ProcessExecutionError,
                          self.mySqlApp.wipe_ib_logfiles)

    def test_start_mysql(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.RUNNING)

        self.mySqlApp.start_mysql()
        self.assert_reported_status(ServiceStatuses.NEW)

    def test_start_mysql_with_db_update(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.appStatus.set_next_status(ServiceStatuses.RUNNING)

        self.mySqlApp.start_mysql(True)
        self.assert_reported_status(ServiceStatuses.RUNNING)

    def test_start_mysql_runs_forever(self):

        dbaas.utils.execute_with_timeout = Mock()
        self.mySqlApp.state_change_wait_time = 1
        self.appStatus.set_next_status(ServiceStatuses.SHUTDOWN)

        self.assertRaises(RuntimeError, self.mySqlApp.start_mysql)
        self.assert_reported_status(ServiceStatuses.SHUTDOWN)

    def test_start_mysql_error(self):

        self.mySqlApp._enable_mysql_on_boot = Mock()
        from reddwarf.common.exception import ProcessExecutionError
        mocked = Mock(side_effect=ProcessExecutionError('Error'))
        dbaas.utils.execute_with_timeout = mocked

        self.assertRaises(RuntimeError, self.mySqlApp.start_mysql)

    def test_start_mysql_with_conf_changes(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp._write_mycnf = Mock()
        self.mysql_starts_successfully()

        self.appStatus.status = ServiceStatuses.SHUTDOWN
        self.mySqlApp.start_mysql_with_conf_changes(Mock())

        self.assertTrue(self.mySqlApp._write_mycnf.called)
        self.assertTrue(self.mySqlApp.start_mysql.called)
        self.assertEqual(self.appStatus._get_actual_db_status(),
                         ServiceStatuses.RUNNING)

    def test_start_mysql_with_conf_changes_mysql_is_running(self):

        self.mySqlApp.start_mysql = Mock()
        self.mySqlApp._write_mycnf = Mock()

        self.appStatus.status = ServiceStatuses.RUNNING
        self.assertRaises(RuntimeError,
                          self.mySqlApp.start_mysql_with_conf_changes, Mock())