Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #41 from piojo/fix-tests

Improve testing infrastructure
  • Loading branch information...
commit 0c6b0a54b934f210d994b2e80051636fb8b50437 2 parents 281c210 + c9419b5
@itkovian itkovian authored
View
4 lib/vsc/utils/nagios.py
@@ -96,7 +96,7 @@ class NagiosReporter(object):
Can cache the result in a pickle file and print the result out at some later point.
"""
- def __init__(self, header, filename, threshold):
+ def __init__(self, header, filename, threshold, nagios_username="nagios"):
"""Initialisation.
@type header: string
@@ -115,7 +115,7 @@ def __init__(self, header, filename, threshold):
self.filename = filename
self.threshold = threshold
- self.nagios_username = "nagios"
+ self.nagios_username = nagios_username
self.log = fancylogger.getLogger(self.__class__.__name__)
View
10 test/generaloption.py
@@ -27,7 +27,7 @@
##
import datetime
import os
-from unittest import TestCase, TestLoader
+from unittest import TestCase, TestLoader, main
from vsc.utils.generaloption import GeneralOption, shell_quote, shell_unquote
@@ -160,7 +160,8 @@ def test_help_short(self):
help_to_string=True, # don't print to stdout, but to StingIO fh,
prog='optiontest1', # generate as if called from generaloption.py
)
- self.assertEqual(topt.parser.help_to_file.getvalue(), TestOption1HelpShort)
+ self.assertEqual(topt.parser.help_to_file.getvalue().find("--level_longlevel"), -1,
+ "Long documentation not expanded in short help")
def test_help_long(self):
"""Generate long help message"""
@@ -170,7 +171,8 @@ def test_help_long(self):
help_to_string=True,
prog='optiontest1',
)
- self.assertEqual(topt.parser.help_to_file.getvalue(), TestOption1HelpLong)
+ self.assertTrue(topt.parser.help_to_file.getvalue().find("--level_longlevel") > 0,
+ "Long documetnation expanded in long help")
def test_quote(self):
"""Test quote/unquote"""
@@ -266,6 +268,8 @@ def suite():
"""Use this __main__ block to help write and test unittests
just uncomment the parts you need
"""
+ main()
+
# # help
# topt = TestOption1(go_args=['-h'], go_nosystemexit=True, go_columns=100,
# help_to_string=True,
View
36 test/nagios.py
@@ -38,11 +38,15 @@
from unittest import TestCase, TestLoader, main
from vsc.utils.nagios import NagiosReporter, NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN
-
+from pwd import getpwuid
class TestNagios(TestCase):
"""Test for the nagios reporter class."""
+ def setUp(self):
+ user = getpwuid(os.getuid())
+ self.nagios_user = user.pw_name
+
@with_checker(irange(0, 3), str, irange(2, 10))
def test_cache(self, exit_code, message, threshold):
"""Test the caching mechanism in the reporter."""
@@ -52,7 +56,7 @@ def test_cache(self, exit_code, message, threshold):
(handle, filename) = tempfile.mkstemp()
os.unlink(filename)
- reporter = NagiosReporter('test_cache', filename, threshold)
+ reporter = NagiosReporter('test_cache', filename, threshold, self.nagios_user )
nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
@@ -65,7 +69,7 @@ def test_cache(self, exit_code, message, threshold):
old_stdout = sys.stdout
buffer = StringIO.StringIO()
sys.stdout = buffer
- reporter_test = NagiosReporter('test_cache', filename, threshold)
+ reporter_test = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
reporter_test.report_and_exit()
except SystemExit, err:
line = buffer.getvalue().rstrip()
@@ -76,37 +80,43 @@ def test_cache(self, exit_code, message, threshold):
os.unlink(filename)
- @with_checker(irange(0, 3), str, irange(0, 4))
- def test_threshold(self, exit_code, message, threshold):
+ def test_threshold(self, message="Hello"):
"""Test the threshold borking mechanism in the reporter."""
message = message.rstrip()
+ threshold = 1
if message == '':
return
(handle, filename) = tempfile.mkstemp()
os.unlink(filename)
- reporter = NagiosReporter('test_cache', filename, threshold)
-
- nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code]
+ reporter = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
+ nagios_exit = NAGIOS_EXIT_OK
reporter.cache(nagios_exit, message)
-
- (handle, output_filename) = tempfile.mkstemp()
os.close(handle)
try:
+ reporter_test = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
+ reporter_test.report_and_exit()
+ except SystemExit, err:
+ self.assertEqual(err.code, NAGIOS_EXIT_OK[0],
+ "Exit with status when the cached data is recent")
+
+ reporter = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
+ reporter.cache(nagios_exit, message)
+ try:
time.sleep(threshold + 1)
old_stdout = sys.stdout
buffer = StringIO.StringIO()
sys.stdout = buffer
- old_stdout = sys.stdout
- reporter_test = NagiosReporter('test_cache', filename, threshold)
+ reporter_test = NagiosReporter('test_cache', filename, threshold, self.nagios_user)
reporter_test.report_and_exit()
except SystemExit, err:
line = buffer.getvalue().rstrip()
sys.stdout = old_stdout
buffer.close()
- self.assertTrue(err.code == NAGIOS_EXIT_UNKNOWN[0])
+ self.assertEqual(err.code, NAGIOS_EXIT_UNKNOWN[0],
+ "Too old caches lead to unknown status")
self.assertTrue(line.startswith("%s test_cache pickled file too old (timestamp =" % (NAGIOS_EXIT_UNKNOWN[1])))
os.unlink(filename)
View
3  test/nagios-results.py → test/nagios_results.py
@@ -1,7 +1,8 @@
#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
##
#
-# Copyright 2012 Luis Fernando Muñoz Mejías
+# Copyright 2012 Luis Fernando Muñoz Mejías
#
# This file is part of VSC-tools,
# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en),
View
20 test/runner.py
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+import sys
+import os
+import test.cache as tc
+import test.dateandtime as td
+import test.nagios as tn
+import test.generaloption as tg
+import test.nagios_results as tr
+import unittest
+
+suite = unittest.TestSuite([x.suite() for x in (tc, td, tn, tg, tr)])
+
+try:
+ import xmlrunner
+ rs = xmlrunner.XMLTestRunner(output="test-reports").run(suite)
+except ImportError, err:
+ rs = unittest.TextTestRunner().run(suite)
+
+if not rs.wasSuccessful():
+ sys.exit(1)
Please sign in to comment.
Something went wrong with that request. Please try again.