Skip to content

Commit

Permalink
[tests] split tests/ into two subdirectories
Browse files Browse the repository at this point in the history
Separate agent core tests and all integration tests.
Also :
- remove unused `run_check` in checks/__init__.py, which was importing
  from tests.common...
- add a Resources class in tests.integrations.common to ease the use of
  fixtures
  • Loading branch information
degemer committed Apr 22, 2015
1 parent 0999057 commit 744dd7e
Show file tree
Hide file tree
Showing 103 changed files with 149 additions and 421 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ node_modules/*
venv/*
*.swp
*.log
!nagios.log
*.pid
*.deb
*.rpm
Expand Down
26 changes: 0 additions & 26 deletions checks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,32 +682,6 @@ def agent_formatter(metric, value, timestamp, tags, hostname, device_name=None,
return (metric, int(timestamp), value)


def run_check(name, path=None):
from tests.common import get_check

# 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 create_service_check(check_name, status, tags=None, timestamp=None,
hostname=None, check_run_id=None, message=None):
""" Create a service_check dict. See AgentCheck.service_check() for
Expand Down
Empty file added tests/agent/__init__.py
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 0 additions & 4 deletions tests/test_autorestart.py → tests/agent/test_autorestart.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import time
import unittest
import logging
import subprocess
import shlex
import os
import signal

from nose.plugins.skip import SkipTest

from daemon import AgentSupervisor

class TestAutoRestart(unittest.TestCase):
""" Test the auto-restart and forking of the agent """
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ def check(self, instance):
raise Exception("failure")



def test_check_status_fail():
instances = [
{'pass':True},
{'pass':False},
{'pass':True}
]

check = DummyAgentCheck('dummy_agent_check', {}, {}, instances)
instance_statuses = check.run()
assert len(instance_statuses) == 3
assert instance_statuses[0].status == STATUS_OK
assert instance_statuses[1].status == STATUS_ERROR
assert instance_statuses[2].status == STATUS_OK


def test_check_status_pass():
instances = [
{'pass':True},
{'pass':True},
]

check = DummyAgentCheck('dummy_agent_check', {}, {}, instances)
instances_status = check.run()
assert len(instances_status) == 2
for i in instances_status:
assert i.status == STATUS_OK


def test_persistence():
i1 = InstanceStatus(1, STATUS_OK)
chk1 = CheckStatus("dummy", [i1], 1, 2)
Expand All @@ -53,6 +53,7 @@ def test_persistence():
assert chk2.metric_count == 1
assert chk2.event_count == 2


def test_persistence_fail():

# Assert remove doesn't crap out if a file doesn't exist.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_common.py → tests/agent/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
AgentCheck,
Check,
CheckException,
CheckException,
Infinity,
UnknownValue,
)
from checks.collector import Collector
from tests.common import load_check
from tests.integrations.common import load_check
from util import get_hostname

logger = logging.getLogger()


class TestCore(unittest.TestCase):
"Tests to validate the core check logic"

Expand Down Expand Up @@ -193,7 +193,7 @@ def test_no_proxy(self):
from requests.utils import get_environ_proxies
import dogstatsd
from os import environ as env

env["http_proxy"] = "http://localhost:3128"
env["https_proxy"] = env["http_proxy"]
env["HTTP_PROXY"] = env["http_proxy"]
Expand Down
6 changes: 4 additions & 2 deletions tests/test_config.py → tests/agent/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## -*- coding: latin-1 -*-
# -*- coding: latin-1 -*-
import unittest
import os
import os.path
Expand All @@ -8,11 +8,13 @@

from util import PidFile, is_valid_hostname, Platform, windows_friendly_colon_split


class TestConfig(unittest.TestCase):
def testWhiteSpaceConfig(self):
"""Leading whitespace confuse ConfigParser
"""
agentConfig = get_config(cfg_path=os.path.join(os.path.dirname(os.path.realpath(__file__)), "badconfig.conf"))
agentConfig = get_config(cfg_path=os.path.join(os.path.dirname(os.path.realpath(__file__)),
'resources', 'badconfig.conf'))
self.assertEquals(agentConfig["dd_url"], "https://app.datadoghq.com")
self.assertEquals(agentConfig["api_key"], "1234")
self.assertEquals(agentConfig["nagios_log"], "/var/log/nagios3/nagios.log")
Expand Down
File renamed without changes.
24 changes: 11 additions & 13 deletions tests/test_datadog.py → tests/agent/test_datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import unittest
from tempfile import NamedTemporaryFile
import re
import os

from checks.datadog import Dogstreams, EventDefaults, point_sorter
from checks.datadog import Dogstreams, EventDefaults

log = logging.getLogger('datadog.test')

Expand Down Expand Up @@ -55,7 +54,7 @@ def parse_line(self, line):
"RECOVERY": "success"
}
def parse_events(logger, line):
""" Expecting lines like this:
""" Expecting lines like this:
2012-05-14 12:46:01 [ERROR] - host0 is down (broke its collarbone)
"""
match = log_event_pattern.match(line)
Expand All @@ -78,7 +77,7 @@ class TailTestCase(unittest.TestCase):
def setUp(self):
self.log_file = NamedTemporaryFile()
self.logger = logging.getLogger('test.dogstream')

def _write_log(self, log_data):
for data in log_data:
print >> self.log_file, data
Expand All @@ -101,7 +100,7 @@ def setUp(self):
log.info("Test config: %s" % self.config)
self.dogstream = Dogstreams.init(self.logger, self.config)
self.maxDiff = None

def test_dogstream_gauge(self):
log_data = [
# bucket 0
Expand All @@ -116,21 +115,21 @@ def test_dogstream_gauge(self):
('test.metric.a', '1000000006', '7', 'metric_type=gauge'),
('test.metric.a', '1000000007', '8', 'metric_type=gauge'),
]

expected_output = {
"dogstream": [
('test.metric.a', 1000000000, 5.0, self.gauge),
('test.metric.a', 1000000005, 8.0, self.gauge),
]
}

self._write_log((' '.join(data) for data in log_data))

actual_output = self.dogstream.check(self.config, move_end=False)
self.assertEquals(expected_output, actual_output)
for metric, timestamp, val, attr in expected_output['dogstream']:
assert isinstance(val, float)

def test_dogstream_counter(self):
log_data = [
# bucket 0
Expand All @@ -145,14 +144,14 @@ def test_dogstream_counter(self):
('test.metric.a', '1000000006', '7', 'metric_type=counter'),
('test.metric.a', '1000000007', '8', 'metric_type=counter'),
]

expected_output = {
"dogstream": [
('test.metric.a', 1000000000, 42, self.counter),
('test.metric.a', 1000000005, 27, self.counter),
]
}

self._write_log((' '.join(data) for data in log_data))

actual_output = self.dogstream.check(self.config, move_end=False)
Expand All @@ -170,9 +169,9 @@ def test_dogstream_bad_input(self):
expected_output = {"dogstream":
[('test_metric.e', 1000000000, 10, self.gauge)]
}

self._write_log(log_data)

actual_output = self.dogstream.check(self.config, move_end=False)
self.assertEquals(expected_output, actual_output)

Expand Down Expand Up @@ -280,7 +279,6 @@ def test_dogstream_events(self):

]
}

self._write_log(log_data)

dogstream = Dogstreams.init(self.logger, {'dogstreams': '%s:tests.test_datadog:parse_events' % self.log_file.name})
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/test_flare.py → tests/agent/test_flare.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_mocked_version():
def get_mocked_temp():
return os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'resources',
'flare'
)

Expand Down
File renamed without changes.
File renamed without changes.
21 changes: 12 additions & 9 deletions tests/test_modules.py → tests/agent/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

log = logging.getLogger('datadog.test')

TARGET_MODULE = 'tests.agent.resources.target_module'
default_target = 'DEFAULT'
specified_target = 'SPECIFIED'
has_been_mutated = False


class TestModuleLoad(unittest.TestCase):

def setUp(self):
sys.modules[__name__].has_been_mutated = True
if 'tests.target_module' in sys.modules:
del sys.modules['tests.target_module']
if TARGET_MODULE in sys.modules:
del sys.modules[TARGET_MODULE]

def tearDown(self):
sys.modules[__name__].has_been_mutated = False
Expand All @@ -27,16 +29,16 @@ def test_cached_module(self):

def test_cache_population(self):
"""Python module cache should be populated"""
self.assertTrue(not 'tests.target_module' in sys.modules)
modules.load('tests.target_module')
self.assertTrue('tests.target_module' in sys.modules)
self.assertTrue(TARGET_MODULE not in sys.modules)
modules.load(TARGET_MODULE)
self.assertTrue(TARGET_MODULE in sys.modules)

def test_modname_load_default(self):
"""When the specifier contains no module name, any provided default
should be used"""
self.assertEquals(
modules.load(
'tests.target_module',
TARGET_MODULE,
'default_target'),
'DEFAULT'
)
Expand All @@ -46,16 +48,17 @@ def test_modname_load_specified(self):
should be overridden"""
self.assertEquals(
modules.load(
'tests.target_module:specified_target',
'{0}:specified_target'.format(TARGET_MODULE),
'default_target'),
'SPECIFIED'
)

def test_pathname_load_finds_package(self):
""""Loading modules by absolute path should correctly set the name of
the loaded module to include any package containing it."""
m = modules.load(os.getcwd() + '/tests/target_module.py')
self.assertEquals(m.__name__, 'tests.target_module')
m = modules.load(os.path.join(os.getcwd(),
TARGET_MODULE.replace('.', '/')))
self.assertEquals(m.__name__, TARGET_MODULE)

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from Queue import Empty
import unittest
import time
from tests.common import load_check
from tests.integrations.common import load_check
import nose.tools as nt
from config import AGENT_VERSION
from util import headers as agent_headers
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 744dd7e

Please sign in to comment.