Skip to content

Commit

Permalink
Support Python 3 (#2999)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmuesch authored Jan 23, 2019
1 parent 4400118 commit af38b5c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 86 deletions.
30 changes: 15 additions & 15 deletions iis/datadog_checks/iis/iis.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# (C) Datadog, Inc. 2010-2017
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
from datadog_checks.checks import AgentCheck
from datadog_checks.checks.win import PDHBaseCheck
from six import iteritems

from datadog_checks.base import AgentCheck, PDHBaseCheck, ensure_unicode
from datadog_checks.utils.containers import hash_mutable


Expand Down Expand Up @@ -48,7 +49,6 @@ def __init__(self, name, init_config, agentConfig, instances):
PDHBaseCheck.__init__(self, name, init_config, agentConfig, instances=instances, counter_list=DEFAULT_COUNTERS)

def check(self, instance):

sites = instance.get('sites')
if sites is None:
expected_sites = set()
Expand All @@ -59,17 +59,17 @@ def check(self, instance):
if "_Total" not in expected_sites:
expected_sites.add("_Total")

self.log.debug("expected sites is %s" % str(expected_sites))
self.log.debug("expected sites is {}".format(str(expected_sites)))
key = hash_mutable(instance)
for inst_name, dd_name, metric_func, counter in self._metrics[key]:
try:
try:
vals = counter.get_all_values()
except Exception as e:
self.log.error("Failed to get_all_values %s %s" % (inst_name, dd_name))
self.log.error("Failed to get_all_values {} {}".format(inst_name, dd_name))
continue

for sitename, val in vals.iteritems():
for sitename, val in iteritems(vals):
tags = []
if key in self._tags:
tags = list(self._tags[key])
Expand All @@ -78,41 +78,41 @@ def check(self, instance):
if not counter.is_single_instance():
# Skip any sites we don't specifically want.
if not sites:
tags.append("site:{0}".format(self.normalize(sitename)))
tags.append("site:{0}".format(ensure_unicode(self.normalize(sitename))))
# always report total
elif sitename == "_Total":
tags.append("site:{0}".format(self.normalize(sitename)))
tags.append("site:{0}".format(ensure_unicode(self.normalize(sitename))))
elif sitename not in sites:
continue
else:
tags.append("site:{0}".format(self.normalize(sitename)))
tags.append("site:{0}".format(ensure_unicode(self.normalize(sitename))))
except Exception as e:
self.log.error("Caught exception %s setting tags" % str(e))
self.log.error("Caught exception {} setting tags".format(str(e)))

try:
metric_func(dd_name, val, tags)
except Exception as e:
self.log.error("metric_func: %s %s %s" % (dd_name, str(val), str(e)))
self.log.error("metric_func: {} {} {}".format(dd_name, str(val), str(e)))
pass

if dd_name == "iis.uptime":
uptime = int(val)
status = AgentCheck.CRITICAL if uptime == 0 else AgentCheck.OK
self.service_check(self.SERVICE_CHECK, status, tags)
if sitename in expected_sites:
self.log.debug("Removing %s from expected sites" % sitename)
self.log.debug("Removing {} from expected sites".format(sitename))
expected_sites.remove(sitename)
else:
self.log.warning("site not in expected_sites %s" % sitename)
self.log.warning("site not in expected_sites {}".format(sitename))

except Exception as e:
# don't give up on all of the metrics because one failed
self.log.error("IIS Failed to get metric data for %s %s: %s" % (inst_name, dd_name, str(e)))
self.log.error("IIS Failed to get metric data for {} {}: {}" .format(inst_name, dd_name, str(e)))
pass

for site in expected_sites:
tags = []
if key in self._tags:
tags = list(self._tags[key])
tags.append("site:{0}".format(self.normalize(site)))
tags.append("site:{}".format(ensure_unicode(self.normalize(site))))
self.service_check(self.SERVICE_CHECK, AgentCheck.CRITICAL, tags)
2 changes: 1 addition & 1 deletion iis/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_requirements(fpath):
return f.readlines()


CHECKS_BASE_REQ = 'datadog_checks_base'
CHECKS_BASE_REQ = 'datadog_checks_base>=4.2.0'

setup(
name='datadog-iis',
Expand Down
27 changes: 27 additions & 0 deletions iis/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# (C) Datadog, Inc. 2010-2017
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
CHECK_NAME = 'iis'
MINIMAL_INSTANCE = {
'host': '.',
}

INSTANCE = {
'host': '.',
'sites': ['Default Web Site', 'Exchange Back End', 'Non Existing Website'],
}

INVALID_HOST_INSTANCE = {
'host': 'nonexistinghost'
}

WIN_SERVICES_MINIMAL_CONFIG = {
'host': ".",
'tags': ["mytag1", "mytag2"]
}

WIN_SERVICES_CONFIG = {
'host': ".",
'tags': ["mytag1", "mytag2"],
'sites': ["Default Web Site", "Exchange Back End", "Failing site"]
}
11 changes: 11 additions & 0 deletions iis/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# (C) Datadog, Inc. 2010-2017
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)
import pytest

from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture, initialize_pdh_tests # noqa: F401


@pytest.fixture(scope="function", autouse=True)
def setup_check():
initialize_pdh_tests()
100 changes: 33 additions & 67 deletions iis/tests/test_iis.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
# (C) Datadog, Inc. 2010-2017
# All rights reserved
# Licensed under Simplified BSD License (see LICENSE)

import pytest
import re
from datadog_checks.stubs import aggregator

from datadog_checks.iis import IIS
from datadog_checks.iis.iis import DEFAULT_COUNTERS
# for reasons unknown, flake8 says that pdh_mocks_fixture is unused, even though
# it's used below. noqa to suppress that error.
from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture, initialize_pdh_tests # noqa: F401


@pytest.fixture
def Aggregator():
aggregator.reset()
return aggregator

from datadog_test_libs.win.pdh_mocks import pdh_mocks_fixture # noqa: F401

CHECK_NAME = 'iis'
MINIMAL_INSTANCE = {
'host': '.',
}

INSTANCE = {
'host': '.',
'sites': ['Default Web Site', 'Exchange Back End', 'Non Existing Website'],
}

INVALID_HOST_INSTANCE = {
'host': 'nonexistinghost'
}
from .common import (
CHECK_NAME,
MINIMAL_INSTANCE,
INSTANCE,
INVALID_HOST_INSTANCE,
WIN_SERVICES_MINIMAL_CONFIG,
WIN_SERVICES_CONFIG
)


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_basic_check(Aggregator, pdh_mocks_fixture):
initialize_pdh_tests()
def test_basic_check(aggregator, pdh_mocks_fixture):
instance = MINIMAL_INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
Expand All @@ -45,19 +31,18 @@ def test_basic_check(Aggregator, pdh_mocks_fixture):
for metric_def in DEFAULT_COUNTERS:
metric = metric_def[3]
for site_tag in site_tags:
Aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag)], count=1)
aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag)], count=1)

for site_tag in site_tags:
Aggregator.assert_service_check('iis.site_up', IIS.OK,
aggregator.assert_service_check('iis.site_up', IIS.OK,
tags=["site:{0}".format(site_tag)], count=1)

Aggregator.assert_all_metrics_covered()
aggregator.assert_all_metrics_covered()


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_check_on_specific_websites(Aggregator, pdh_mocks_fixture):
initialize_pdh_tests()
def test_check_on_specific_websites(aggregator, pdh_mocks_fixture):
instance = INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
Expand All @@ -66,87 +51,68 @@ def test_check_on_specific_websites(Aggregator, pdh_mocks_fixture):
for metric_def in DEFAULT_COUNTERS:
metric = metric_def[3]
for site_tag in site_tags:
Aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag)], count=1)
aggregator.assert_metric(metric, tags=["site:{0}".format(site_tag)], count=1)

for site_tag in site_tags:
Aggregator.assert_service_check('iis.site_up', IIS.OK,
aggregator.assert_service_check('iis.site_up', IIS.OK,
tags=["site:{0}".format(site_tag)], count=1)

Aggregator.assert_service_check('iis.site_up', IIS.CRITICAL,
aggregator.assert_service_check('iis.site_up', IIS.CRITICAL,
tags=["site:{0}".format('Non_Existing_Website')], count=1)

Aggregator.assert_all_metrics_covered()
aggregator.assert_all_metrics_covered()


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_service_check_with_invalid_host(Aggregator, pdh_mocks_fixture):
initialize_pdh_tests()
def test_service_check_with_invalid_host(aggregator, pdh_mocks_fixture):
instance = INVALID_HOST_INSTANCE
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)

Aggregator.assert_service_check('iis.site_up', IIS.CRITICAL, tags=["site:{0}".format('Total')])


WIN_SERVICES_MINIMAL_CONFIG = {
'host': ".",
'tags': ["mytag1", "mytag2"]
}

WIN_SERVICES_CONFIG = {
'host': ".",
'tags': ["mytag1", "mytag2"],
'sites': ["Default Web Site", "Exchange Back End", "Failing site"]
}
aggregator.assert_service_check('iis.site_up', IIS.CRITICAL, tags=["site:{0}".format('Total')])


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_check(Aggregator, pdh_mocks_fixture):
def test_check(aggregator, pdh_mocks_fixture):
"""
Returns the right metrics and service checks
"""
# Set up & run the check
config = {
'instances': [WIN_SERVICES_CONFIG]
}
initialize_pdh_tests()
instance = WIN_SERVICES_CONFIG
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)

# Test metrics
# ... normalize site-names
default_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", config['instances'][0]['sites'][0])
ok_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", config['instances'][0]['sites'][1])
fail_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", config['instances'][0]['sites'][2])
default_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", WIN_SERVICES_CONFIG['sites'][0])
ok_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", WIN_SERVICES_CONFIG['sites'][1])
fail_site_name = re.sub(r"[,\+\*\-/()\[\]{}\s]", "_", WIN_SERVICES_CONFIG['sites'][2])

for site_name in [default_site_name, ok_site_name, 'Total']:
for metric_def in DEFAULT_COUNTERS:
mname = metric_def[3]
Aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_name)], count=1)
aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_name)], count=1)

Aggregator.assert_service_check('iis.site_up', status=IIS.OK,
aggregator.assert_service_check('iis.site_up', status=IIS.OK,
tags=["mytag1", "mytag2", "site:{0}".format(site_name)], count=1)

Aggregator.assert_service_check('iis.site_up', status=IIS.CRITICAL,
aggregator.assert_service_check('iis.site_up', status=IIS.CRITICAL,
tags=["mytag1", "mytag2", "site:{0}".format(fail_site_name)], count=1)

# Check completed with no warnings
# self.assertFalse(logger.warning.called)

Aggregator.assert_all_metrics_covered()
aggregator.assert_all_metrics_covered()


# flake8 then says this is a redefinition of unused, which it's not.
@pytest.mark.usefixtures("pdh_mocks_fixture") # noqa: F811
def test_check_without_sites_specified(Aggregator, pdh_mocks_fixture):
def test_check_without_sites_specified(aggregator, pdh_mocks_fixture):
"""
Returns the right metrics and service checks for the `_Total` site
"""
# Run check
initialize_pdh_tests()
instance = WIN_SERVICES_MINIMAL_CONFIG
c = IIS(CHECK_NAME, {}, {}, [instance])
c.check(instance)
Expand All @@ -156,9 +122,9 @@ def test_check_without_sites_specified(Aggregator, pdh_mocks_fixture):
mname = metric_def[3]

for site_tag in site_tags:
Aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_tag)], count=1)
aggregator.assert_metric(mname, tags=["mytag1", "mytag2", "site:{0}".format(site_tag)], count=1)

for site_tag in site_tags:
Aggregator.assert_service_check('iis.site_up', status=IIS.OK,
aggregator.assert_service_check('iis.site_up', status=IIS.OK,
tags=["mytag1", "mytag2", "site:{0}".format(site_tag)], count=1)
Aggregator.assert_all_metrics_covered()
aggregator.assert_all_metrics_covered()
4 changes: 1 addition & 3 deletions iis/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
minversion = 2.0
basepython = py27
envlist =
iis
{py27,py36}-iis
flake8

[testenv]
usedevelop = true
platform = win32

[testenv:iis]
deps =
-e../datadog_checks_base[deps]
../datadog_checks_tests_helper
Expand Down

0 comments on commit af38b5c

Please sign in to comment.