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 23, 2015
1 parent 0999057 commit 7b744a4
Show file tree
Hide file tree
Showing 105 changed files with 167 additions and 435 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
194 changes: 0 additions & 194 deletions tests/cassandra/cfstats

This file was deleted.

5 changes: 0 additions & 5 deletions tests/cassandra/info

This file was deleted.

9 changes: 0 additions & 9 deletions tests/cassandra/info.8

This file was deleted.

10 changes: 0 additions & 10 deletions tests/cassandra/info.opp

This file was deleted.

14 changes: 0 additions & 14 deletions tests/cassandra/tpstats

This file was deleted.

25 changes: 0 additions & 25 deletions tests/cassandra/tpstats.8

This file was deleted.

Empty file added tests/checks/__init__.py
Empty file.
38 changes: 33 additions & 5 deletions tests/common.py → tests/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

log = logging.getLogger('tests')


def get_check_class(name):
checksd_path = get_checksd_path(get_os())
if checksd_path not in sys.path:
Expand All @@ -37,6 +38,7 @@ def get_check_class(name):

return check_class


def load_check(name, config, agentConfig):
checksd_path = get_checksd_path(get_os())
if checksd_path not in sys.path:
Expand Down Expand Up @@ -67,6 +69,7 @@ def load_check(name, config, agentConfig):
except Exception as e:
raise Exception("Check is using old API, {0}".format(e))


def kill_subprocess(process_obj):
try:
process_obj.terminate()
Expand All @@ -76,12 +79,13 @@ def kill_subprocess(process_obj):
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False,
process_obj.pid)
process_obj.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
else:
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:
Expand All @@ -102,10 +106,34 @@ def get_check(name, config_str):
}

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

def read_data_from_file(filename):
return open(os.path.join(os.path.dirname(__file__), 'data', filename)).read()
agentConfig=agentConfig)


class Resources(object):

@staticmethod
def integration_name():
for stack in inspect.stack():
# stack[1] is the file path
file_name = os.path.basename(stack[1])
if 'test_' in file_name:
# test_name.py
# 5 -3
return file_name[5:-3]
raise Exception('No integration test file in stack')

@staticmethod
def directory():
return os.path.join(os.path.dirname(__file__), 'resources',
Resources.integration_name())

@staticmethod
def file(file_name):
return os.path.join(Resources.directory(), file_name)

@staticmethod
def read_file(file_name):
return open(Resources.file(file_name)).read()


class AgentCheckTest(unittest.TestCase):
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# project
from checks import AgentCheck
from tests.common import AgentCheckTest
from tests.checks.common import AgentCheckTest


@attr(requires='apache')
Expand Down
Loading

0 comments on commit 7b744a4

Please sign in to comment.