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.mock_kv_store = self._panoptes_context.get_kv_store(PanoptesTestKeyValueStore)
def test_context_key_value_store(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=[PanoptesTestKeyValueStore]) self.assertIsInstance(panoptes_context, PanoptesContext) kv = panoptes_context.get_kv_store(PanoptesTestKeyValueStore) self.assertIsInstance(kv, PanoptesTestKeyValueStore) self.assertTrue(kv.set('test', 'test')) self.assertEqual(kv.get('test'), 'test') self.assertEqual(kv.get('non.existent.key'), None) with self.assertRaises(AssertionError): kv.set(None, None) with self.assertRaises(AssertionError): kv.set('test', None) with self.assertRaises(AssertionError): kv.set(None, 'test') with self.assertRaises(AssertionError): kv.set(1, 'test') with self.assertRaises(AssertionError): kv.set('test', 1) with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=[None]) with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=['test']) with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=[PanoptesMockRedis]) panoptes_context = PanoptesContext(self.panoptes_test_conf_file) with self.assertRaises(PanoptesContextError): panoptes_context.get_kv_store(PanoptesTestKeyValueStore)
def test_context_key_value_store_timeout(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=[PanoptesTestKeyValueStore]) kv = panoptes_context.get_kv_store(PanoptesTestKeyValueStore) with self.assertRaises(TimeoutError): kv.set('test', 'test') with self.assertRaises(TimeoutError): kv.get('test')
def test_get_panoptes_logger(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file) assert isinstance(panoptes_context._get_panoptes_logger(), logging.Logger) # Test error raised when instantiating logger fails mock_get_child = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.context.PanoptesContext._PanoptesContext__rootLogger.getChild', mock_get_child): with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file)
def test_zookeeper_client(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, create_zookeeper_client=True) assert isinstance(panoptes_context.zookeeper_client, FakeClient) mock_kazoo_client = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.context.kazoo.client.KazooClient', mock_kazoo_client): with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file, create_zookeeper_client=True)
def test_message_producer(self): mock_kafka_client = MagicMock(return_value=MockKafkaClient(kafka_brokers={'localhost:9092'})) with patch('yahoo_panoptes.framework.context.KafkaClient', mock_kafka_client): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, create_message_producer=True, async_message_producer=False) self.assertIsNotNone(panoptes_context.message_producer) # Test error in message queue producer mock_panoptes_message_queue_producer = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.context.PanoptesMessageQueueProducer', mock_panoptes_message_queue_producer): with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file, create_message_producer=True, async_message_producer=True)
def test_get_kafka_client(self): mock_kafka_client = MockKafkaClient(kafka_brokers={'localhost:9092'}) mock_kafka_client_init = Mock(return_value=mock_kafka_client) with patch('yahoo_panoptes.framework.context.KafkaClient', mock_kafka_client_init): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, create_message_producer=True, async_message_producer=False) self.assertEqual(panoptes_context.kafka_client, mock_kafka_client)
def setUp(self): self.my_dir, self.panoptes_test_conf_file = get_test_conf_file() self._panoptes_resource = PanoptesResource( resource_site="test", resource_class="test", resource_subclass="test", resource_type="test", resource_id="test", resource_endpoint="test", resource_creation_timestamp=_TIMESTAMP, resource_plugin="test") self._panoptes_context = PanoptesContext( self.panoptes_test_conf_file, key_value_store_class_list=[ PanoptesTestKeyValueStore, PanoptesResourcesKeyValueStore, PanoptesPollingPluginKeyValueStore, PanoptesSecretsStore, PanoptesPollingPluginAgentKeyValueStore, PanoptesDiscoveryPluginAgentKeyValueStore, PanoptesDiscoveryPluginKeyValueStore ], create_message_producer=False, async_message_producer=False, create_zookeeper_client=True) self._runner_class = PanoptesPluginWithEnrichmentRunner self._log_capture = LogCapture( attributes=TestPanoptesPluginRunner.extract)
def test_polling_no_matching_transform_type(self, message_producer, message_producer_property, kv_store): mock_panoptes_plugin = MockPanoptesObject() mock_panoptes_plugin.config = ConfigObj( pwd + u'/config_files/test_panoptes_polling_plugin_conf_diff_transform_callback.ini' ) kv_store.return_value = PanoptesMockRedis() mock_message_producer = MockPanoptesMessageProducer() message_producer.return_value = mock_message_producer message_producer_property.return_value = message_producer_property panoptes_context = PanoptesContext(config_file=os.path.join( path, u'config_files/test_panoptes_config.ini')) for i in range(1, 9): panoptes_metrics = self.prepare_panoptes_metrics_group_set( u'{}/metric_group_sets/interface_plugin_counter_{}.json'. format(pwd, i)) _process_metrics_group_set(context=panoptes_context, results=panoptes_metrics, plugin=mock_panoptes_plugin) self.assertEqual(panoptes_context.message_producer.messages, [])
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_get_redis_connection(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file) # Test get_redis_connection with self.assertRaises(IndexError): panoptes_context.get_redis_connection("default", shard=1) self.assertIsNotNone(panoptes_context.get_redis_connection("dummy", shard=1)) # Test redis shard count error self.assertEqual(panoptes_context.get_redis_shard_count('dummy'), 1) with self.assertRaises(KeyError): panoptes_context.get_redis_shard_count('dummy', fallback_to_default=False)
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._panoptes_resource = PanoptesResource(resource_site='test', resource_class='test', resource_subclass='test', resource_type='test', resource_id='test', resource_endpoint='test', resource_plugin='test')
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._runner_class = PanoptesPluginRunner self._log_capture = LogCapture(attributes=self.extract)
def test_context_config_file(self): # Test invalid inputs for config_file with self.assertRaises(AssertionError): PanoptesContext('') with self.assertRaises(AssertionError): PanoptesContext(1) with self.assertRaises(PanoptesContextError): PanoptesContext(config_file='non.existent.config.file') # Test that the default config file is loaded if no config file is present in the arguments or environment with patch('yahoo_panoptes.framework.const.DEFAULT_CONFIG_FILE_PATH', self.panoptes_test_conf_file): panoptes_context = PanoptesContext() self.assertEqual(panoptes_context.config_object.redis_urls[0].url, 'redis://:password@localhost:6379/0') del panoptes_context # Test that the config file from environment is loaded, if present os.environ[const.CONFIG_FILE_ENVIRONMENT_VARIABLE] = self.panoptes_test_conf_file panoptes_context = PanoptesContext() self.assertEqual(panoptes_context.config_object.redis_urls[0].url, 'redis://:password@localhost:6379/0') del panoptes_context # Test bad config configuration files for f in glob.glob(os.path.join(self.my_dir, 'config_files/test_panoptes_config_bad_*.ini')): with self.assertRaises(PanoptesContextError): print 'Going to load bad configuration file: %s' % f PanoptesContext(f)
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_context(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file) self.assertIsInstance(panoptes_context, PanoptesContext) self.assertEqual(panoptes_context.config_object.redis_urls[0].url, 'redis://:password@localhost:6379/0') self.assertEqual(str(panoptes_context.config_object.redis_urls[0]), 'redis://:**@localhost:6379/0') self.assertEqual(panoptes_context.config_object.zookeeper_servers, set(['localhost:2181'])) self.assertEqual(panoptes_context.config_object.kafka_brokers, set(['localhost:9092'])) self.assertIsInstance(panoptes_context.config_dict, dict) self.assertIsInstance(panoptes_context.logger, _loggerClass) self.assertIsInstance(panoptes_context.redis_pool, MockRedis) with self.assertRaises(AttributeError): panoptes_context.kafka_client with self.assertRaises(AttributeError): panoptes_context.zookeeper_client del panoptes_context # Test bad config configuration files for f in glob.glob(os.path.join(self.my_dir, 'config_files/test_panoptes_config_bad_*.ini')): with self.assertRaises(PanoptesContextError): print 'Going to load bad configuration file: %s' % f PanoptesContext(f) with self.assertRaises(PanoptesContextError): PanoptesContext(config_file='non.existent.config.file')
def test_context(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file) self.assertIsInstance(panoptes_context, PanoptesContext) self.assertEqual(panoptes_context.config_object.redis_urls[0].url, 'redis://:password@localhost:6379/0') self.assertEqual(str(panoptes_context.config_object.redis_urls[0]), 'redis://:**@localhost:6379/0') self.assertEqual(panoptes_context.config_object.zookeeper_servers, set(['localhost:2181'])) self.assertEqual(panoptes_context.config_object.kafka_brokers, set(['localhost:9092'])) self.assertIsInstance(panoptes_context.config_dict, dict) self.assertIsInstance(panoptes_context.logger, _loggerClass) self.assertIsInstance(panoptes_context.redis_pool, MockRedis) with self.assertRaises(AttributeError): panoptes_context.kafka_client with self.assertRaises(AttributeError): panoptes_context.zookeeper_client del panoptes_context
def setUp(self): self.my_dir, self.panoptes_test_conf_file = get_test_conf_file() self._panoptes_context = PanoptesContext(self.panoptes_test_conf_file) self._panoptes_consumer_arguments = { u'panoptes_context': self._panoptes_context, u'consumer_type': 0, u'topics': [u'panoptes-metrics'], u'client_id': 1337, u'group': u'panoptes-consumer-group', u'keys': [u'interface-metrics'], u'poll_timeout': 200, u'callback': panoptes_consumer_callback, u'validate': False }
def test_polling_kafka_produces_to_the_correct_topics( self, message_producer, message_producer_property): spec_paths = [ u'config_files/test_panoptes_config.ini', # Produces to site topics but not Global u'config_files/test_panoptes_config_kafka_true.ini' # Sends enrichment results to both site & global topics ] expected_results = [{ u'test_site-processed': 1 }, { u'panoptes-metrics': 1, u'test_site-processed': 1 }] all_tests_pass = True for i in range(len(spec_paths)): mock_panoptes_plugin = MockPanoptesObject() mock_panoptes_plugin.config = ConfigObj( pwd + u'/config_files/test_panoptes_polling_plugin_conf.ini') mock_message_producer = MockPanoptesMessageProducer() message_producer.return_value = mock_message_producer message_producer_property.return_value = message_producer_property panoptes_context = PanoptesContext( config_file=os.path.join(path, spec_paths[i])) metrics_group_set = self.prepare_panoptes_metrics_group_set() _process_metrics_group_set(context=panoptes_context, results=metrics_group_set, plugin=mock_panoptes_plugin) published_kafka_messages = panoptes_context.message_producer.messages actual_result = {} for message in published_kafka_messages: if message[u'topic'] in actual_result: actual_result[message[u'topic']] += 1 else: actual_result[message[u'topic']] = 1 all_tests_pass &= (actual_result == expected_results[i]) self.assertTrue(all_tests_pass)
def test_context_message_bus(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, create_zookeeper_client=True) self.assertIsInstance(panoptes_context, PanoptesContext) self.assertIsInstance(panoptes_context.zookeeper_client, FakeClient)
def test_plugininfo_repr(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) panoptes_resource = PanoptesResource(resource_site='test', resource_class='test', resource_subclass='test', resource_type='test', resource_id='test', resource_endpoint='test', resource_plugin='test') panoptes_plugininfo = PanoptesPluginInfo("plugin_name", "plugin_path") panoptes_plugininfo.panoptes_context = panoptes_context panoptes_plugininfo.data = panoptes_resource panoptes_plugininfo.kv_store_class = PanoptesTestKeyValueStore panoptes_plugininfo.last_executed = "1458947997" panoptes_plugininfo.last_results = "1458948005" repr_string = "PanoptesPluginInfo: Normalized name: plugin__name, Config file: None, "\ "Panoptes context: " \ "[PanoptesContext: KV Stores: [PanoptesTestKeyValueStore], "\ "Config: ConfigObj({'main': {'sites': " \ "['local'], 'plugins_extension': 'panoptes-plugin', 'plugins_skew': 1}, " \ "'log': " \ "{'config_file': 'tests/config_files/test_panoptes_logging.ini', " \ "'rate': 1000, " \ "'per': 1, " \ "'burst': 10000, " \ "'formatters': {'keys': ['root_log_format', 'log_file_format', 'discovery_plugins_format']}}, " \ "'redis': {'default': {'namespace': 'panoptes', "\ "'shards': {'shard1': {'host': 'localhost', 'port': 6379, 'db': 0, 'password': '******'}}}}, "\ "'kafka': {'topic_key_delimiter': ':', 'topic_name_delimiter': '-', " \ "'brokers': {'broker1': {'host': 'localhost', 'port': 9092}}, " \ "'topics': " \ "{'metrics': {'raw_topic_name_suffix': 'metrics', " \ "'transformed_topic_name_suffix': 'processed'}}}, " \ "'zookeeper': {'connection_timeout': 30, 'servers': {'server1': {'host': " \ "'localhost', 'port': 2181}}}, " \ "'discovery': " \ "{'plugins_path': 'tests/plugins/discovery', " \ "'plugin_scan_interval': 60, " \ "'celerybeat_max_loop_interval': 5}, " \ "'polling': " \ "{'plugins_path': 'tests/plugins/polling', " \ "'plugin_scan_interval': 60, " \ "'celerybeat_max_loop_interval': 5}, " \ "'enrichment': " \ "{'plugins_path': 'tests/plugins/enrichment', " \ "'plugin_scan_interval': 60, " \ "'celerybeat_max_loop_interval': 5}, " \ "'snmp': " \ "{'port': 10161, " \ "'connection_factory_module': 'yahoo_panoptes.framework.utilities.snmp.connection', " \ "'connection_factory_class': 'PanoptesSNMPConnectionFactory', " \ "'community': '**', 'timeout': 5, 'retries': 1, 'non_repeaters': 0, 'max_repetitions': 25, " \ "'proxy_port': 10161, 'community_string_key': 'snmp_community_string'}}), "\ "Redis pool set: False, " \ "Message producer set: False, " \ "Kafka client set: False, " \ "Zookeeper client set: False], " \ "KV store class: PanoptesTestKeyValueStore, " \ "Last executed timestamp: 1458947997, " \ "Last executed key: " \ "plugin_metadata:plugin__name:be7eabbca3b05b9aaa8c81201aa0ca3e:last_executed, " \ "Last results timestamp: 1458948005, " \ "Last results key: plugin_metadata:plugin__name:be7eabbca3b05b9aaa8c81201aa0ca3e:last_results, " \ "Data: Data object passed, " \ "Lock: Lock is set" self.assertEqual(repr(panoptes_plugininfo), repr_string)
def test_get_lock(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, create_zookeeper_client=True) # Test bad input with self.assertRaises(AssertionError): panoptes_context.get_lock('path/to/node', 1, 1, "identifier") with self.assertRaises(AssertionError): panoptes_context.get_lock('/path/to/node', 0, 1, "identifier") with self.assertRaises(AssertionError): panoptes_context.get_lock('/path/to/node', 1, -1, "identifier") with self.assertRaises(AssertionError): panoptes_context.get_lock('/path/to/node', 1, 1) # Test non-callable listener with self.assertRaises(AssertionError): panoptes_context.get_lock("/path/to/node", timeout=1, retries=1, identifier="test", listener=object()) # Test lock acquisition/release among multiple contenders lock = panoptes_context.get_lock("/path/to/node", timeout=1, retries=1, identifier="test") self.assertIsNotNone(lock) lock.release() lock2 = panoptes_context.get_lock("/path/to/node", timeout=1, retries=0, identifier="test") self.assertIsNotNone(lock2) lock3 = panoptes_context.get_lock("/path/to/node", timeout=1, retries=1, identifier="test") self.assertIsNone(lock3) lock2.release() # Test adding a listener for the lock once acquired lock4 = panoptes_context.get_lock("/path/to/node", timeout=1, retries=1, identifier="test", listener=object) self.assertIsNotNone(lock4) mock_zookeeper_client = MockZookeeperClient() with patch('yahoo_panoptes.framework.context.PanoptesContext.zookeeper_client', mock_zookeeper_client): self.assertIsNone(panoptes_context.get_lock("/path/to/node", timeout=5, retries=1, identifier="test"))
def test_panoptes_resources_key_value_store(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file) panoptes_resources_kv_store = PanoptesResourcesKeyValueStore(panoptes_context) self.assertEqual(panoptes_resources_kv_store.redis_group, const.RESOURCE_MANAGER_REDIS_GROUP) self.assertEqual(panoptes_resources_kv_store.namespace, const.RESOURCE_MANAGER_KEY_VALUE_NAMESPACE)
def test_panoptes_resource_store(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=[PanoptesTestKeyValueStore]) with self.assertRaises(Exception): PanoptesResourceStore(panoptes_context) mock_kv_store = Mock(return_value=PanoptesTestKeyValueStore(panoptes_context)) with patch('yahoo_panoptes.framework.resources.PanoptesContext.get_kv_store', mock_kv_store): panoptes_resource_store = PanoptesResourceStore(panoptes_context) panoptes_resource_store.add_resource("test_plugin_signature", self.__panoptes_resource) resource_key = "plugin|test|site|test|class|test|subclass|test|type|test|id|test|endpoint|test" resource_value = panoptes_resource_store.get_resource(resource_key) self.assertEqual(self.__panoptes_resource, resource_value) panoptes_resource_2 = PanoptesResource(resource_site='test', resource_class='test', resource_subclass='test', resource_type='test', resource_id='test2', resource_endpoint='test', resource_plugin='test', resource_ttl=RESOURCE_MANAGER_RESOURCE_EXPIRE, resource_creation_timestamp=_TIMESTAMP) panoptes_resource_store.add_resource("test_plugin_signature", panoptes_resource_2) self.assertIn(self.__panoptes_resource, panoptes_resource_store.get_resources()) self.assertIn(panoptes_resource_2, panoptes_resource_store.get_resources()) panoptes_resource_store.delete_resource("test_plugin_signature", panoptes_resource_2) self.assertNotIn(panoptes_resource_2, panoptes_resource_store.get_resources()) panoptes_resource_3 = PanoptesResource(resource_site='test', resource_class='test', resource_subclass='test', resource_type='test', resource_id='test3', resource_endpoint='test', resource_plugin='test3', resource_ttl=RESOURCE_MANAGER_RESOURCE_EXPIRE, resource_creation_timestamp=_TIMESTAMP) panoptes_resource_store.add_resource("test_plugin_signature", panoptes_resource_3) self.assertIn(panoptes_resource_3, panoptes_resource_store.get_resources(site='test', plugin_name='test3')) self.assertNotIn(self.__panoptes_resource, panoptes_resource_store.get_resources(site='test', plugin_name='test3')) # Test key not found mock_find_keys = Mock( return_value=['dummy', 'plugin|test|site|test|class|test|subclass|test|type|test|id|test|endpoint|test']) with patch('yahoo_panoptes.framework.resources.PanoptesKeyValueStore.find_keys', mock_find_keys): self.assertEqual(1, len(panoptes_resource_store.get_resources())) # Test resource store methods raise correct errors mock_get = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.resources.PanoptesKeyValueStore.get', mock_get): with self.assertRaises(PanoptesResourceError): panoptes_resource_store.get_resource('test3') # Test bad input with self.assertRaises(AssertionError): panoptes_resource_store.get_resource("") with self.assertRaises(AssertionError): panoptes_resource_store.get_resource(1) with self.assertRaises(AssertionError): panoptes_resource_store.add_resource("", panoptes_resource_2) with self.assertRaises(AssertionError): panoptes_resource_store.add_resource("test_plugin_signature", None) with self.assertRaises(AssertionError): panoptes_resource_store.add_resource("test_plugin_signature", PanoptesResourceStore(panoptes_context)) with self.assertRaises(AssertionError): panoptes_resource_store.delete_resource("", panoptes_resource_2) with self.assertRaises(AssertionError): panoptes_resource_store.delete_resource("test_plugin_signature", None) with self.assertRaises(AssertionError): panoptes_resource_store.delete_resource("test_plugin_signature", PanoptesResourceStore(panoptes_context)) with self.assertRaises(AssertionError): panoptes_resource_store.get_resources("", "test_plugin_name") with self.assertRaises(AssertionError): panoptes_resource_store.get_resources("test_site", "") with self.assertRaises(AssertionError): panoptes_resource_store.get_resources(1, "test_plugin_name") with self.assertRaises(AssertionError): panoptes_resource_store.get_resources("test_site", 1) # Test non-existent key with self.assertRaises(PanoptesResourceError): panoptes_resource_store.get_resource('tes') mock_set = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.resources.PanoptesKeyValueStore.set', mock_set): with self.assertRaises(PanoptesResourceError): panoptes_resource_store.add_resource("test_plugin_signature", panoptes_resource_2) mock_delete = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.resources.PanoptesKeyValueStore.delete', mock_delete): with self.assertRaises(PanoptesResourceError): panoptes_resource_store.delete_resource("test_plugin_signature", panoptes_resource_2) with self.assertRaises(PanoptesResourceError): panoptes_resource_store._deserialize_resource("tes", "null") with self.assertRaises(PanoptesResourceError): panoptes_resource_store._deserialize_resource(resource_key, "null")
def test_get_kv_store_error(self, mock_init): mock_init.side_effect = Exception with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file, key_value_store_class_list=[PanoptesTestKeyValueStore])
def test_resource_dsl_parsing(self): panoptes_context = PanoptesContext(self.panoptes_test_conf_file) test_query = 'resource_class = "network" AND resource_subclass = "load-balancer"' test_result = ( 'SELECT resources.*, group_concat(key,"|"), group_concat(value,"|") ' + 'FROM resources ' + 'LEFT JOIN resource_metadata ON resources.id = resource_metadata.id ' + 'WHERE (resources.resource_class = "network" ' + 'AND resources.resource_subclass = "load-balancer") ' + 'GROUP BY resource_metadata.id ' + 'ORDER BY resource_metadata.id' ) panoptes_resource_dsl = PanoptesResourceDSL(test_query, panoptes_context) self.assertEqual(panoptes_resource_dsl.sql, test_result) # This very long query tests all code paths with the DSL parser: test_query = 'resource_class = "network" AND resource_subclass = "load-balancer" OR \ resource_metadata.os_version LIKE "4%" AND resource_site NOT IN ("test_site") \ AND resource_endpoint IN ("test1","test2") AND resource_type != "a10" OR ' \ 'resource_metadata.make NOT LIKE \ "A10%" AND resource_metadata.model NOT IN ("test1", "test2")' test_result = ( 'SELECT resources.*,group_concat(key,"|"),group_concat(value,"|") FROM (SELECT resource_metadata.id ' + 'FROM resources,resource_metadata WHERE (resources.resource_class = "network" ' + 'AND resources.resource_subclass = "load-balancer" ' + 'AND resources.resource_site NOT IN ("test_site") ' + 'AND resources.resource_endpoint IN ("test1","test2") ' + 'AND resources.resource_type != "a10" ' + 'AND ((resource_metadata.key = "os_version" ' + 'AND resource_metadata.value LIKE "4%")) ' + 'AND resource_metadata.id = resources.id) ' + 'UNION SELECT resource_metadata.id ' + 'FROM resources,resource_metadata WHERE (resource_metadata.key = "make" ' + 'AND resource_metadata.value NOT LIKE "A10%") ' + 'AND resource_metadata.id = resources.id ' + 'INTERSECT SELECT resource_metadata.id ' + 'FROM resources,resource_metadata WHERE (resource_metadata.key = "model" ' + 'AND resource_metadata.value NOT IN ("test1","test2")) ' + 'AND resource_metadata.id = resources.id ' + 'GROUP BY resource_metadata.id ' + 'ORDER BY resource_metadata.id) AS filtered_resources, ' + 'resources, resource_metadata WHERE resources.id = filtered_resources.id ' + 'AND resource_metadata.id = filtered_resources.id GROUP BY resource_metadata.id') panoptes_resource_dsl = PanoptesResourceDSL(test_query, panoptes_context) self.assertEqual(panoptes_resource_dsl.sql, test_result) panoptes_resource_dsl = PanoptesResourceDSL('resource_site = "local"', panoptes_context) self.assertIsInstance(panoptes_resource_dsl.tokens, ParseResults) with self.assertRaises(AssertionError): PanoptesResourceDSL(None, panoptes_context) with self.assertRaises(AssertionError): PanoptesResourceDSL('', None) with self.assertRaises(AssertionError): PanoptesResourceDSL('', panoptes_context) with self.assertRaises(ParseException): PanoptesResourceDSL('resources_site = local', panoptes_context)
def test_context_redis_bad_connection(self): with self.assertRaises(ConnectionError): PanoptesContext(self.panoptes_test_conf_file)
def test_root_logger_error(self): mock_get_logger = Mock(side_effect=Exception) with patch('yahoo_panoptes.framework.context.logging.getLogger', mock_get_logger): with self.assertRaises(PanoptesContextError): PanoptesContext(self.panoptes_test_conf_file)
def setUp(self): self.my_dir, self.panoptes_test_conf_file = get_test_conf_file() self._panoptes_context = PanoptesContext(self.panoptes_test_conf_file)
def test_context_del_methods(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) panoptes_context.__del__() with self.assertRaises(AttributeError): kv_stores = panoptes_context.__kv_stores with self.assertRaises(AttributeError): redis_pool = panoptes_context.__redis_pool with self.assertRaises(AttributeError): message_producer = panoptes_context.__message_producer with self.assertRaises(AttributeError): kafka_client = panoptes_context.__kafka_client with self.assertRaises(AttributeError): zookeeper_client = panoptes_context.__zookeeper_client 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) with self.assertRaises(AttributeError): del panoptes_context.__kv_stores panoptes_context.__del__() with self.assertRaises(AttributeError): del panoptes_context.__redis_pool panoptes_context.__del__() with self.assertRaises(AttributeError): del panoptes_context.__message_producer panoptes_context.__del__() with self.assertRaises(AttributeError): del panoptes_context.__kafka_client panoptes_context.__del__() with self.assertRaises(AttributeError): del panoptes_context.__zookeeper_client panoptes_context.__del__()