def start_discovery_plugin_scheduler():
    """
    The entry point for the Discovery Plugin Scheduler

    This method creates a Panoptes Context and the Celery Instance for the Discovery Plugin Scheduler

    Returns:
        None
    """
    global discovery_plugin_scheduler, celery, logger, panoptes_context

    try:
        panoptes_context = PanoptesDiscoveryPluginSchedulerContext()
    except Exception as e:
        sys.exit(u'Could not create a Panoptes Context: %s' % (repr(e)))

    try:
        celery_config = PanoptesCeleryDiscoveryAgentConfig()
    except Exception as e:
        sys.exit(u'Could not create a Celery Config object: %s' % repr(e))

    discovery_plugin_scheduler = PanoptesPluginScheduler(
        panoptes_context, u'discovery', u'Discovery', celery_config,
        const.DISCOVERY_PLUGIN_SCHEDULER_LOCK_ACQUIRE_TIMEOUT,
        discovery_plugin_scheduler_task)
    logger = panoptes_context.logger
    celery = discovery_plugin_scheduler.start()

    if not celery:
        sys.exit(u'Could not start Celery Beat Service')
Beispiel #2
0
def start_enrichment_plugin_scheduler():
    """
    The entry point for the Enrichment Plugin Scheduler

    This method creates a Panoptes Context and the Celery Instance for the Enrichment Plugin Scheduler

    Returns:
        None
    """
    global enrichment_plugin_scheduler, celery, logger, panoptes_context, resources_store

    try:
        panoptes_context = PanoptesEnrichmentPluginSchedulerContext()
    except Exception as e:
        sys.exit(u'Could not create a Panoptes Context: %s' % (repr(e)))

    try:
        celery_config = PanoptesCeleryEnrichmentAgentConfig()
    except Exception as e:
        sys.exit(u'Could not create a Celery Config object: %s' % repr(e))

    enrichment_plugin_scheduler = PanoptesPluginScheduler(
        panoptes_context=panoptes_context,
        plugin_type=u'enrichment',
        plugin_type_display_name=u'Enrichment',
        celery_config=celery_config,
        lock_timeout=const.ENRICHMENT_PLUGIN_SCHEDULER_LOCK_ACQUIRE_TIMEOUT,
        plugin_scheduler_task=enrichment_plugin_scheduler_task)

    logger = panoptes_context.logger
    celery = enrichment_plugin_scheduler.start()

    if not celery:
        sys.exit(u'Could not start Celery Beat Service')
Beispiel #3
0
 def setUp(self):
     self.my_dir, self.panoptes_test_conf_file = get_test_conf_file()
     self._panoptes_context = PanoptesContext(self.panoptes_test_conf_file,
                                              key_value_store_class_list=[PanoptesTestKeyValueStore],
                                              create_message_producer=False, async_message_producer=False,
                                              create_zookeeper_client=True)
     self._celery_config = PanoptesCeleryConfig(app_name="Polling Plugin Test")
     self._scheduler = PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling", self._celery_config, 1,
                                               _callback)
Beispiel #4
0
 def setUp(self):
     self.my_dir, self.panoptes_test_conf_file = get_test_conf_file()
     self._panoptes_context = PanoptesContext(self.panoptes_test_conf_file,
                                              key_value_store_class_list=[PanoptesTestKeyValueStore],
                                              create_message_producer=False, async_message_producer=False,
                                              create_zookeeper_client=True)
     self._celery_config = PanoptesCeleryConfig(app_name=u"Polling Plugin Test")
     self._scheduler = PanoptesPluginScheduler(
         panoptes_context=self._panoptes_context,
         plugin_type=u"polling",
         plugin_type_display_name=u"Polling",
         celery_config=self._celery_config,
         lock_timeout=1,
         plugin_scheduler_task=_callback
     )
Beispiel #5
0
class TestPanoptesPluginScheduler(unittest.TestCase):
    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def setUp(self):
        self.my_dir, self.panoptes_test_conf_file = get_test_conf_file()
        self._panoptes_context = PanoptesContext(self.panoptes_test_conf_file,
                                                 key_value_store_class_list=[PanoptesTestKeyValueStore],
                                                 create_message_producer=False, async_message_producer=False,
                                                 create_zookeeper_client=True)
        self._celery_config = PanoptesCeleryConfig(app_name="Polling Plugin Test")
        self._scheduler = PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling", self._celery_config, 1,
                                                  _callback)

    def test_start_basic_operations(self):
        # Test bad input
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler("test", "polling", "Polling", self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "", "Polling",self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "",self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling", "Test", 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling",self._celery_config, 0,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling", "Test", 1,
                                    object)

        # Test locking error when starting up the scheduler
        mock_lock = MagicMock(side_effect=Exception)
        with patch('yahoo_panoptes.framework.plugins.scheduler.PanoptesLock', mock_lock):
            with self.assertRaises(SystemExit):
                self._scheduler.start()

        celery_app = self._scheduler.start()
        self.assertIsInstance(celery_app, app.base.Celery)

    def test_celery_beat_error(self):
        mock_celery_instance = MagicMock(side_effect=Exception)
        with patch('yahoo_panoptes.framework.plugins.scheduler.PanoptesCeleryInstance', mock_celery_instance):
            celery_app = self._scheduler.start()
            self.assertIsNone(celery_app)
Beispiel #6
0
def start_polling_plugin_scheduler():
    """
    The entry point for the Polling Plugin Scheduler

    This method creates a Panoptes Context and the Celery Instance for the Polling Plugin Scheduler

    Returns:
        None
    """
    global polling_plugin_scheduler, celery, logger, panoptes_context, cached_schedule
    try:
        panoptes_context = PanoptesPollingPluginSchedulerContext()
    except Exception as e:
        sys.exit(u'Could not create a Panoptes Context: %s' % (repr(e)))

    logger = panoptes_context.logger

    try:
        celery_config = PanoptesCeleryPollingAgentConfig()
    except Exception as e:
        sys.exit(u'Could not create a Celery Config object: %s' % repr(e))

    try:
        polling_plugin_scheduler = PanoptesPluginScheduler(
            panoptes_context=panoptes_context,
            plugin_type=u'polling',
            plugin_type_display_name=u'Polling',
            celery_config=celery_config,
            lock_timeout=const.POLLING_PLUGIN_SCHEDULER_LOCK_ACQUIRE_TIMEOUT,
            plugin_scheduler_task=polling_plugin_scheduler_task,
            plugin_subtype=panoptes_context.config_dict[u'polling']
            [u'plugins_subtype'])
    except Exception as e:
        sys.exit(u'Could not create a Plugin Scheduler object: %s' % repr(e))

    cached_schedule = polling_plugin_get_schedule()

    try:
        celery = polling_plugin_scheduler.start()
    except Exception as e:
        sys.exit(u'Could not start the Plugin Scheduler object: %s' % repr(e))

    if not celery:
        sys.exit(u'Could not start Celery Beat Service')
Beispiel #7
0
    def test_shutdown_after_tour_of_duty(self):
        mock_tour_of_duty = create_autospec(PanoptesTourOfDuty)
        mock_tour_of_duty.completed.return_value = True
        mock_tour_of_duty.tasks_completed.return_value = True
        mock_tour_of_duty.time_completed.return_value = True
        mock_tour_of_duty.memory_growth_completed.return_value = True

        with patch(u'yahoo_panoptes.framework.plugins.scheduler.PanoptesTourOfDuty', mock_tour_of_duty):
            self._scheduler = PanoptesPluginScheduler(
                panoptes_context=self._panoptes_context,
                plugin_type=u"polling",
                plugin_type_display_name=u"Polling",
                celery_config=self._celery_config,
                lock_timeout=1,
                plugin_scheduler_task=_callback
            )
            celery_app = self._scheduler.start()
            celery_beat_service = Service(celery_app, max_interval=None, schedule_filename=None,
                                          scheduler_cls=PanoptesCeleryPluginScheduler)
            self._scheduler.run(celery_beat_service)
Beispiel #8
0
    def test_start_basic_operations(self):
        # Test bad input
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler("test", "polling", "Polling", self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "", "Polling",self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "",self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling", "Test", 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling",self._celery_config, 0,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, "polling", "Polling", "Test", 1,
                                    object)

        # Test locking error when starting up the scheduler
        mock_lock = MagicMock(side_effect=Exception)
        with patch('yahoo_panoptes.framework.plugins.scheduler.PanoptesLock', mock_lock):
            with self.assertRaises(SystemExit):
                self._scheduler.start()

        celery_app = self._scheduler.start()
        self.assertIsInstance(celery_app, app.base.Celery)
Beispiel #9
0
class TestPanoptesEnrichmentPluginScheduler(unittest.TestCase):
    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def setUp(self):
        self.my_dir, self.panoptes_test_conf_file = get_test_conf_file()
        self._panoptes_context = PanoptesContext(
            self.panoptes_test_conf_file,
            key_value_store_class_list=[PanoptesTestKeyValueStore],
            create_message_producer=False,
            async_message_producer=False,
            create_zookeeper_client=True)
        self._celery_config = PanoptesCeleryConfig(
            app_name=u"Enrichment Plugin Test")
        self._scheduler = PanoptesPluginScheduler(
            panoptes_context=self._panoptes_context,
            plugin_type=u"enrichment",
            plugin_type_display_name=u"Enrichment",
            celery_config=self._celery_config,
            lock_timeout=1,
            plugin_scheduler_task=_callback)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_basic_operations(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesResourceCache.get_resources',
                    mock_get_resources):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_error_messages(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):

            mock_plugin_manager = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesPluginManager',
                    mock_plugin_manager):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_enrichment_plugin_scheduler_task_bad_plugin(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesEnrichmentPluginInfo.'
                    'execute_frequency', 0):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesEnrichmentPluginInfo.'
                    'resource_filter', None):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_enrichment_plugin_scheduler_task_config_error(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_getmtime = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.framework.plugins.panoptes_base_plugin.os.path.getmtime',
                    mock_getmtime):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_resource_cache_error(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_cache = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesResourceCache',
                    mock_cache):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

            mock_get_resources_exception = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesResourceCache.get_resources',
                    mock_get_resources_exception):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_enrichment_plugin_scheduler_update_error(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_update = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.framework.celery_manager.PanoptesCeleryPluginScheduler.update',
                    mock_update):
                start_enrichment_plugin_scheduler()
                enrichment_plugin_scheduler_task(celery_beat_service)

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_enrichment_plugin_scheduler_context_error(self):
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_context = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesEnrichmentPluginSchedulerContext',
                    mock_context):
                with self.assertRaises(SystemExit):
                    start_enrichment_plugin_scheduler()

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_enrichment_plugin_scheduler_agent_config_error(self):
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_config = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesCeleryEnrichmentAgentConfig',
                    mock_config):
                with self.assertRaises(SystemExit):
                    start_enrichment_plugin_scheduler()

    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_celery_none(self):
        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_start = create_autospec(PanoptesPluginScheduler.start,
                                         return_value=None)
            with patch(
                    'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.PanoptesPluginScheduler.start',
                    mock_start):
                with self.assertRaises(SystemExit):
                    start_enrichment_plugin_scheduler()

    def test_celery_beat_service_connect_function(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)

        self.assertFalse(
            hasattr(celery_beat_service.scheduler, 'panoptes_context'))
        self.assertFalse(
            hasattr(celery_beat_service.scheduler, 'metadata_kv_store_class'))
        self.assertFalse(hasattr(celery_beat_service.scheduler, 'task_prefix'))

        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.enrichment_plugin_scheduler'
        ) as mock_scheduler:
            celery_beat_service_started(sender=celery_beat_service)

            self.assertTrue(
                hasattr(celery_beat_service.scheduler, 'panoptes_context'))
            self.assertIsNotNone(
                celery_beat_service.scheduler.metadata_kv_store_class)
            self.assertIsNotNone(celery_beat_service.scheduler.task_prefix)
            mock_scheduler.run.assert_called_with(celery_beat_service, None)

        with patch(
                'yahoo_panoptes.enrichment.enrichment_plugin_scheduler.enrichment_plugin_scheduler'
        ) as mock_scheduler:
            mock_scheduler.run.side_effect = Exception
            with self.assertRaises(SystemExit):
                celery_beat_service_started(sender=celery_beat_service)
Beispiel #10
0
class TestPanoptesPluginScheduler(unittest.TestCase):
    @patch(u'redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch(u'kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def setUp(self):
        self.my_dir, self.panoptes_test_conf_file = get_test_conf_file()
        self._panoptes_context = PanoptesContext(self.panoptes_test_conf_file,
                                                 key_value_store_class_list=[PanoptesTestKeyValueStore],
                                                 create_message_producer=False, async_message_producer=False,
                                                 create_zookeeper_client=True)
        self._celery_config = PanoptesCeleryConfig(app_name=u"Polling Plugin Test")
        self._scheduler = PanoptesPluginScheduler(
            panoptes_context=self._panoptes_context,
            plugin_type=u"polling",
            plugin_type_display_name=u"Polling",
            celery_config=self._celery_config,
            lock_timeout=1,
            plugin_scheduler_task=_callback
        )

    def test_start_basic_operations(self):
        # Test bad input
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(u"test", u"polling", u"Polling", self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, u"", u"Polling", self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, u"polling", u"", self._celery_config, 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, u"polling", u"Polling", u"Test", 1,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, u"polling", u"Polling", self._celery_config, 0,
                                    _callback)
        with self.assertRaises(AssertionError):
            PanoptesPluginScheduler(self._panoptes_context, u"polling", u"Polling", u"Test", 1,
                                    object)

        # Test locking error when starting up the scheduler
        mock_lock = MagicMock(side_effect=Exception)
        with patch(u'yahoo_panoptes.framework.plugins.scheduler.PanoptesLock', mock_lock):
            with self.assertRaises(SystemExit):
                self._scheduler.start()

        celery_app = self._scheduler.start()
        self.assertIsInstance(celery_app, app.base.Celery)

    def test_redundant_shutdown_signal(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(celery_app, max_interval=None, schedule_filename=None,
                                      scheduler_cls=PanoptesCeleryPluginScheduler)
        self._scheduler.run(celery_beat_service)

        temp_is_set = self._scheduler._shutdown_plugin_scheduler.is_set

        self._scheduler._shutdown_plugin_scheduler.is_set = _mock_is_set_true
        self._scheduler._signal_handler(signal.SIGTERM, None)  # pragma: no cover
        self._scheduler._shutdown()
        self.assertTrue(self._scheduler._t.isAlive())

        with self.assertRaises(SystemExit):
            self._scheduler._shutdown_plugin_scheduler.is_set = temp_is_set
            self._scheduler._signal_handler(signal.SIGTERM, None)  # pragma: no cover

    def test_shutdown_after_tour_of_duty(self):
        mock_tour_of_duty = create_autospec(PanoptesTourOfDuty)
        mock_tour_of_duty.completed.return_value = True
        mock_tour_of_duty.tasks_completed.return_value = True
        mock_tour_of_duty.time_completed.return_value = True
        mock_tour_of_duty.memory_growth_completed.return_value = True

        with patch(u'yahoo_panoptes.framework.plugins.scheduler.PanoptesTourOfDuty', mock_tour_of_duty):
            self._scheduler = PanoptesPluginScheduler(
                panoptes_context=self._panoptes_context,
                plugin_type=u"polling",
                plugin_type_display_name=u"Polling",
                celery_config=self._celery_config,
                lock_timeout=1,
                plugin_scheduler_task=_callback
            )
            celery_app = self._scheduler.start()
            celery_beat_service = Service(celery_app, max_interval=None, schedule_filename=None,
                                          scheduler_cls=PanoptesCeleryPluginScheduler)
            self._scheduler.run(celery_beat_service)

    def test_celery_beat_error(self):
        mock_celery_instance = MagicMock(side_effect=Exception)
        with patch(u'yahoo_panoptes.framework.plugins.scheduler.PanoptesCeleryInstance', mock_celery_instance):
            celery_app = self._scheduler.start()
            self.assertIsNone(celery_app)
class TestPanoptesDiscoveryPluginScheduler(unittest.TestCase):
    @patch('redis.StrictRedis', panoptes_mock_redis_strict_client)
    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def setUp(self):
        self.my_dir, self.panoptes_test_conf_file = get_test_conf_file()
        self._panoptes_context = PanoptesContext(
            self.panoptes_test_conf_file,
            key_value_store_class_list=[PanoptesTestKeyValueStore],
            create_message_producer=False,
            async_message_producer=False,
            create_zookeeper_client=True)
        self._celery_config = PanoptesCeleryConfig(
            app_name="Discovery Plugin Test")
        self._scheduler = PanoptesPluginScheduler(
            panoptes_context=self._panoptes_context,
            plugin_type="polling",
            plugin_type_display_name="Polling",
            celery_config=self._celery_config,
            lock_timeout=1,
            plugin_scheduler_task=_callback)

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_basic_operations(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            start_discovery_plugin_scheduler()
            discovery_plugin_scheduler_task(celery_beat_service)

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_error_messages(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):

            mock_plugin_manager = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.discovery.discovery_plugin_scheduler.PanoptesPluginManager',
                    mock_plugin_manager):
                start_discovery_plugin_scheduler()
                discovery_plugin_scheduler_task(celery_beat_service)

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_discovery_plugin_scheduler_task_exceptions(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            with patch(
                    'yahoo_panoptes.discovery.discovery_plugin_scheduler.PanoptesPluginInfo.execute_frequency',
                    0):
                start_discovery_plugin_scheduler()
                discovery_plugin_scheduler_task(celery_beat_service)

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_discovery_plugin_scheduler_task_config_error(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_getmtime = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.framework.plugins.panoptes_base_plugin.os.path.getmtime',
                    mock_getmtime):
                start_discovery_plugin_scheduler()
                discovery_plugin_scheduler_task(celery_beat_service)

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_discovery_plugin_scheduler_update_error(self):
        celery_app = self._scheduler.start()
        celery_beat_service = Service(
            celery_app,
            max_interval=None,
            schedule_filename=None,
            scheduler_cls=PanoptesCeleryPluginScheduler)
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_update = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.framework.celery_manager.PanoptesCeleryPluginScheduler.update',
                    mock_update):
                start_discovery_plugin_scheduler()
                discovery_plugin_scheduler_task(celery_beat_service)

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_discovery_plugin_scheduler_context_error(self):
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_context = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.discovery.discovery_plugin_scheduler.PanoptesDiscoveryPluginSchedulerContext',
                    mock_context):
                with self.assertRaises(SystemExit):
                    start_discovery_plugin_scheduler()

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_discovery_plugin_scheduler_agent_config_error(self):
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_config = MagicMock(side_effect=Exception)
            with patch(
                    'yahoo_panoptes.discovery.discovery_plugin_scheduler.PanoptesCeleryDiscoveryAgentConfig',
                    mock_config):
                with self.assertRaises(SystemExit):
                    start_discovery_plugin_scheduler()

    @patch('kazoo.client.KazooClient', panoptes_mock_kazoo_client)
    def test_celery_none(self):
        with patch(
                'yahoo_panoptes.discovery.discovery_plugin_scheduler.const.DEFAULT_CONFIG_FILE_PATH',
                self.panoptes_test_conf_file):
            mock_start = create_autospec(PanoptesPluginScheduler.start,
                                         return_value=None)
            with patch(
                    'yahoo_panoptes.discovery.discovery_plugin_scheduler.PanoptesPluginScheduler.start',
                    mock_start):
                with self.assertRaises(SystemExit):
                    start_discovery_plugin_scheduler()