Skip to content

Commit

Permalink
[core][windows] restore 'run_check' from 'checks'
Browse files Browse the repository at this point in the history
PR #1571 introduced a bad regression: 'run_check' method was deleted
beside it is still used on Windows to debug.
  • Loading branch information
yannmh committed Jun 9, 2015
1 parent 27a526b commit d6950ce
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 23 deletions.
3 changes: 3 additions & 0 deletions checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
# project
from checks import check_status
from util import LaconicFilter, get_hostname, get_next_id, yLoader
from utils.platform import Platform
from utils.profile import pretty_statistics
if Platform.is_windows():
from utils.debug import run_check # noqa - windows debug purpose

log = logging.getLogger(__name__)

Expand Down
24 changes: 1 addition & 23 deletions tests/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from checks import AgentCheck
from config import get_checksd_path
from util import get_os, get_hostname
from utils.debug import get_check # noqa - FIXME 5.5.0 AgentCheck tests should not use this

log = logging.getLogger('tests')

Expand Down Expand Up @@ -86,29 +87,6 @@ def kill_subprocess(process_obj):
os.kill(process_obj.pid, signal.SIGKILL)


def get_check(name, config_str):
checksd_path = get_checksd_path(get_os())
if checksd_path not in sys.path:
sys.path.append(checksd_path)
check_module = __import__(name)
check_class = None
classes = inspect.getmembers(check_module, inspect.isclass)
for name, clsmember in classes:
if AgentCheck in clsmember.__bases__:
check_class = clsmember
break
if check_class is None:
raise Exception("Unable to import check %s. Missing a class that inherits AgentCheck" % name)

agentConfig = {
'version': '0.1',
'api_key': 'tota'
}

return check_class.from_yaml(yaml_text=config_str, check_name=name,
agentConfig=agentConfig)


class Fixtures(object):
@staticmethod
def integration_name():
Expand Down
63 changes: 63 additions & 0 deletions utils/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# stdlib
import inspect
import os
from pprint import pprint
import sys

# datadog
from config import get_checksd_path, get_confd_path
from util import get_os


def run_check(name, path=None):
"""
Test custom checks on Windows.
"""
# Read the config file

confd_path = path or os.path.join(get_confd_path(get_os()), '%s.yaml' % name)

try:
f = open(confd_path)
except IOError:
raise Exception('Unable to open configuration at %s' % confd_path)

config_str = f.read()
f.close()

# Run the check
check, instances = get_check(name, config_str)
if not instances:
raise Exception('YAML configuration returned no instances.')
for instance in instances:
check.check(instance)
if check.has_events():
print "Events:\n"
pprint(check.get_events(), indent=4)
print "Metrics:\n"
pprint(check.get_metrics(), indent=4)


def get_check(name, config_str):
from checks import AgentCheck

checksd_path = get_checksd_path(get_os())
if checksd_path not in sys.path:
sys.path.append(checksd_path)
check_module = __import__(name)
check_class = None
classes = inspect.getmembers(check_module, inspect.isclass)
for name, clsmember in classes:
if AgentCheck in clsmember.__bases__:
check_class = clsmember
break
if check_class is None:
raise Exception("Unable to import check %s. Missing a class that inherits AgentCheck" % name)

agentConfig = {
'version': '0.1',
'api_key': 'tota'
}

return check_class.from_yaml(yaml_text=config_str, check_name=name,
agentConfig=agentConfig)

0 comments on commit d6950ce

Please sign in to comment.