Skip to content

Commit

Permalink
Merge pull request #105 from yahoo/fix_test_no_service_active_tests
Browse files Browse the repository at this point in the history
Fix test_no_service_active
  • Loading branch information
varunvarma committed Oct 8, 2019
2 parents 3feb747 + 4dcae02 commit ec5a3a1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
50 changes: 44 additions & 6 deletions tests/plugins/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import logging
import os
import subprocess
import re

from mock import Mock, patch, create_autospec
from yahoo_panoptes.framework.context import PanoptesContext
from yahoo_panoptes.framework.enrichment import PanoptesEnrichmentCache, PanoptesEnrichmentCacheKeyValueStore
from yahoo_panoptes.framework.plugins.panoptes_base_plugin import PanoptesPluginRuntimeError, \
PanoptesPluginConfigurationError
from yahoo_panoptes.framework.plugins.context import PanoptesPluginWithEnrichmentContext, PanoptesPluginContext
from yahoo_panoptes.plugins.polling.utilities.polling_status import DEVICE_METRICS_STATES
from yahoo_panoptes.framework.resources import PanoptesResource
from yahoo_panoptes.framework.utilities.helpers import ordered
from yahoo_panoptes.framework.utilities.secrets import PanoptesSecretsStore
Expand Down Expand Up @@ -230,7 +230,7 @@ def set_panoptes_resource(self):
if self._resource_backplane:
self._panoptes_resource.add_metadata('backplane', self._resource_backplane)

def set_enrichment_cache(self):
def set_enrichment_cache(self, resource_endpoint=None):
if self.use_enrichment:
self._enrichment_data_file = 'data/' + self.enrichment_data_file
self._enrichment_kv = self._panoptes_context.get_kv_store(PanoptesEnrichmentCacheKeyValueStore)
Expand All @@ -248,6 +248,8 @@ def set_enrichment_cache(self):
with open(enrichment_data_file) as enrichment_data:
for line in enrichment_data:
key, value = line.strip().split('=>')
if resource_endpoint is not None:
value = self.update_enrichment_ip(value, resource_endpoint)
self._enrichment_kv.set(key, value)
except Exception as e:
raise IOError('Failed to load enrichment data file {}: {}'.format(enrichment_data_file, repr(e)))
Expand Down Expand Up @@ -306,6 +308,32 @@ def set_expected_results(self):
expected_result_file = os.path.join(os.path.abspath(self.path), self._results_data_file)
self._expected_results = json.load(open(expected_result_file))

def update_enrichment_ip(self, enrichment, resource_endpoint):
"""
The PluginPollingGenericSNMPMetrics Plugin initially calls the function _get_config() when started.
This function loads the enrichment from the PanoptesEnrichmentCacheKeyValueStore
using the resource_id, namespace, and enrichment key. The enrichment key is the resource_endpoint.
no_service_active tests to ensure that a proper error (timeout -> ping error) is returned when
snmp_bulk is called with an invalid IP. In order to get to this stage in the tests the enrichment must
loaded in the generic snmp runner. In order to test this, the resource_endpoint is changed to
be invalid. However this IP must also be changed in the test enrichment file so the generic snmp runner
is able to load the enrichment from the cache.
"""
modified_enrichment = json.loads(enrichment)

if len(modified_enrichment) == 0 or len(modified_enrichment['data'].keys()) == 0:
return enrichment

ip = modified_enrichment['data'].keys()[0]

if re.search(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b', ip):
modified_enrichment['data'][resource_endpoint] = modified_enrichment['data'][ip]
del modified_enrichment['data'][ip]

return json.dumps(modified_enrichment)

return enrichment

@patch('time.time', mock_time)
@patch('yahoo_panoptes.framework.resources.time', mock_time)
@patch('redis.StrictRedis', PanoptesMockRedis)
Expand Down Expand Up @@ -424,7 +452,9 @@ def test_inactive_port(self):

def test_no_service_active(self):
"""Tests a valid resource_endpoint with no service active"""
self._resource_endpoint = '127.0.0.2'
self._resource_endpoint = '192.0.2.1' # RFC 5737
self.set_enrichment_cache(resource_endpoint=self._resource_endpoint)

self._snmp_conf['timeout'] = self._snmp_failure_timeout
self.set_panoptes_resource()
self.set_plugin_context()
Expand All @@ -433,8 +463,16 @@ def test_no_service_active(self):

if self._plugin_conf.get('enrichment'):
if self._plugin_conf['enrichment'].get('preload'):
with self.assertRaises(PanoptesPluginRuntimeError):
plugin.run(self._plugin_context)
result = plugin.run(self._plugin_context)
result = self._remove_timestamps(result)

ping_failure = False
for i in range(len(result)):
if len(result[i]['metrics']) >= 1 and result[i]['metrics'][0]['metric_value'] \
== DEVICE_METRICS_STATES.PING_FAILURE:
ping_failure = True

self.assertTrue(ping_failure)

self._resource_endpoint = '127.0.0.1'
self._snmp_conf['timeout'] = self.snmp_timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ class TestPluginPollingCiscoIOS(helpers.SNMPPollingPluginTestFramework, unittest
def test_inactive_port(self):
pass

def test_no_service_active(self):
pass


class TestPluginPollingCiscoIOSEnrichmentFromFile(helpers.SNMPPollingPluginTestFramework, unittest.TestCase):
plugin_class = plugin_polling_generic_snmp.PluginPollingGenericSNMPMetrics
Expand Down Expand Up @@ -90,9 +87,6 @@ class TestPluginPollingCiscoIOSEnrichmentFromFile(helpers.SNMPPollingPluginTestF
def test_inactive_port(self):
pass

def test_no_service_active(self):
pass


class TestPluginPollingCiscoIOSEnrichmentFromFileBad(TestPluginPollingCiscoIOSEnrichmentFromFile, unittest.TestCase):
plugin_conf = {
Expand Down Expand Up @@ -188,6 +182,11 @@ def test_basic_operations(self):
plugin = self.plugin_class()
plugin.run(self._plugin_context)

def test_no_service_active(self):
# Since the enrichment is defined in both the config and via Key-Value store a
# PanoptesEnrichmentCacheError error will be thrown.
pass


class TestPluginPollingCiscoIOS4900M(TestPluginPollingCiscoIOS):
snmp_community = "4900M"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ class TestPluginPollingCiscoNXOS3048(SNMPPollingPluginTestFramework, unittest.Te
def test_inactive_port(self):
pass

def test_no_service_active(self):
pass


class TestPluginPollingCiscoNXOSn3k(TestPluginPollingCiscoNXOS3048, unittest.TestCase):
snmp_community = 'n3k_3048T'
Expand Down

0 comments on commit ec5a3a1

Please sign in to comment.