Skip to content

Commit

Permalink
[snmp] adding tests, minor fix to PR.
Browse files Browse the repository at this point in the history
[snmp] fixing test descriptions.
  • Loading branch information
truthbk committed Jan 20, 2016
1 parent d663cf7 commit 4d0db8b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
8 changes: 5 additions & 3 deletions checks.d/snmp.py
Expand Up @@ -438,11 +438,13 @@ def submit_metric(self, name, snmp_value, forced_type, tags=[]):
if forced_type.lower() == "gauge":
value = int(snmp_value)
self.gauge(metric_name, value, tags)
return
if forced_type.lower() == "counter":
elif forced_type.lower() == "counter":
value = int(snmp_value)
self.rate(metric_name, value, tags)
return
else:
raise Exception("Invalid forced-type in config file: {0}".format(name))

return

# Ugly hack but couldn't find a cleaner way
# Proper way would be to use the ASN1 method isSameTypeWith but it
Expand Down
4 changes: 3 additions & 1 deletion tests/checks/common.py
Expand Up @@ -325,7 +325,7 @@ def _candidates_size_assert(self, candidates, count=None, at_least=1):
raise

def assertMetric(self, metric_name, value=None, tags=None, count=None,
at_least=1, hostname=None, device_name=None):
at_least=1, hostname=None, device_name=None, metric_type=None):
candidates = []
for m_name, ts, val, mdata in self.metrics:
if m_name == metric_name:
Expand All @@ -337,6 +337,8 @@ def assertMetric(self, metric_name, value=None, tags=None, count=None,
continue
if device_name is not None and mdata['device_name'] != device_name:
continue
if metric_type is not None and mdata['type'] != metric_type:
continue

candidates.append((m_name, ts, val, mdata))

Expand Down
65 changes: 65 additions & 0 deletions tests/checks/integration/test_snmp.py
Expand Up @@ -6,6 +6,7 @@

# agent
from checks import AgentCheck
from checks.metric_types import MetricTypes
from tests.checks.common import AgentCheckTest

# This test is dependent of having a fully open snmpd responding at localhost:161
Expand Down Expand Up @@ -64,6 +65,31 @@ class SNMPTestCase(AgentCheckTest):
}
]

FORCED_METRICS = [
{
'OID': "1.3.6.1.2.1.4.24.6.0", # Gauge32
'name': "IAmAGauge32",
'forced_type': 'counter'

}, {
'OID': "1.3.6.1.2.1.4.31.1.1.6.1", # Counter32
'name': "IAmACounter64",
'forced_type': 'gauge'
}
]
INVALID_FORCED_METRICS = [
{
'OID': "1.3.6.1.2.1.4.24.6.0", # Gauge32
'name': "IAmAGauge32",
'forced_type': 'counter'

}, {
'OID': "1.3.6.1.2.1.4.31.1.1.6.1", # Counter32
'name': "IAmACounter64",
'forced_type': 'histogram'
}
]

SCALAR_OBJECTS = [
{
'OID': "1.3.6.1.2.1.7.1.0",
Expand Down Expand Up @@ -303,6 +329,45 @@ def test_invalid_metric(self):
tags=self.CHECK_TAGS, count=1)
self.coverage_report()

def test_forcedtype_metric(self):
"""
Forced Types should be reported as metrics of the forced type
"""
config = {
'instances': [self.generate_instance_config(self.FORCED_METRICS)]
}
self.run_check(config)

for metric in self.FORCED_METRICS:
metric_name = "snmp." + (metric.get('name') or metric.get('symbol'))
if metric.get('forced_type') == MetricTypes.COUNTER:
# rate will be flushed as a gauge, so count should be 0.
self.assertMetric(metric_name, tags=self.CHECK_TAGS,
count=0, metric_type=MetricTypes.GAUGE)
elif metric.get('forced_type') == MetricTypes.GAUGE:
self.assertMetric(metric_name, tags=self.CHECK_TAGS,
count=1, metric_type=MetricTypes.GAUGE)

# # Test service check
self.assertServiceCheck("snmp.can_check", status=AgentCheck.OK,
tags=self.CHECK_TAGS, count=1)
self.coverage_report()

def test_invalid_forcedtype_metric(self):
"""
If a forced types is invalid an exception should be raised
"""
config = {
'instances': [self.generate_instance_config(self.INVALID_FORCED_METRICS)]
}
self.assertRaises(Exception, self.run_check, config)


# # Test service check
self.assertServiceCheck("snmp.can_check", status=AgentCheck.CRITICAL,
tags=self.CHECK_TAGS, count=1)
self.coverage_report()

def test_network_failure(self):
"""
Network failure is reported in service check
Expand Down

0 comments on commit 4d0db8b

Please sign in to comment.