Module: check_mk
Branch: master
Commit: 1ba100ce0d13001d7c6198e127927739c840e305
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=1ba100ce0d1300…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Thu Jun 21 07:53:58 2018 +0200
use new checktestlib objects in 'test_statgrab_cpu_check.py'
Change-Id: I77350fdaaa95715c06e1ea51b8ff2a9b5900784a
---
tests/unit/checks/checktestlib.py | 4 +
tests/unit/checks/test_statgrab_cpu_check.py | 129 +++++++++++++++++++++------
2 files changed, 107 insertions(+), 26 deletions(-)
diff --git a/tests/unit/checks/checktestlib.py b/tests/unit/checks/checktestlib.py
index 35aeb20..85ad02c 100644
--- a/tests/unit/checks/checktestlib.py
+++ b/tests/unit/checks/checktestlib.py
@@ -300,6 +300,8 @@ class MockItemState(object):
In all of these cases, the sanity of the returned values is checked
(i.e. they have to be BasicItemState).
+
+ See for example 'test_statgrab_cpu_check.py'.
"""
TARGET = 'cmk_base.item_state._cached_item_states.get_item_state'
@@ -364,6 +366,8 @@ class assertMKCounterWrapped(object):
with assertMKCounterWrapped():
# do a check that raises such an exception
run_my_check()
+
+ See for example 'test_statgrab_cpu_check.py'.
"""
def __init__(self, msg=None):
self.msg = msg
diff --git a/tests/unit/checks/test_statgrab_cpu_check.py
b/tests/unit/checks/test_statgrab_cpu_check.py
index 96e3d0d..a3a47e7 100644
--- a/tests/unit/checks/test_statgrab_cpu_check.py
+++ b/tests/unit/checks/test_statgrab_cpu_check.py
@@ -1,33 +1,110 @@
import pytest
-from cmk_base.check_api import MKCounterWrapped
-import checktestlib
+from checktestlib import BasicCheckResult, CheckResult, \
+ assertCheckResultsEqual, BasicItemState, \
+ MockItemState, assertMKCounterWrapped
pytestmark = pytest.mark.checks
-info_statgrab_cpu_hpux = lambda t: [[u'idle', u'%d' % int(t*5)],
- [u'iowait', u'%d' % int(t*5)],
- [u'kernel', u'%d' % int(t*5)],
- [u'nice', u'%d' % int(t*5)],
- [u'swap', u'0'],
- [u'systime', u'%d' % int(t*5)],
- [u'total', u'%d' % int(t*30)],
- [u'user', u'%d' % int(t*5)]]
-
-(a)pytest.mark.parametrize("time_to_info,params,predicate"cate", [
-(info_statgrab_cpu_hpux, {}, lambda cr: ("user", 40.0) in cr.perfdata and
# TODO: This only represents the status quo - check whether this even makes sense.
- ("system", 20.0) in cr.perfdata and
# Note that systime and total are being ignored entirely by the check.
- ("wait", 20.0) in cr.perfdata
- ),
+info_statgrab_cpu_hpux = [
+ [u'idle', u'300'],
+ [u'iowait', u'300'],
+ [u'kernel', u'300'],
+ [u'nice', u'300'],
+ [u'swap', u'0'],
+ [u'systime', u'300'],
+ [u'total', u'1800'],
+ [u'user', u'300'],
+]
+
+
+# If mock_state is a tuple, it is returned upon
+# every call to `get_item_state`. Let's say
+# The check ran 23 seconds ago, and all values
+# were zero:
+mock_state_tuple = (23., 0)
+
+
+# If mock_state is a dictionary, the values will
+# be returned according to their key,
+# as you would expect.
+mock_state_dict = {
+ 'cpu.util.1' : (3, 200), # user
+ 'cpu.util.2' : (1, 320), # nice
+ 'cpu.util.3' : (4, 100),# system
+ 'cpu.util.4' : (1, -123),# idle
+ 'cpu.util.5' : (5, 1000),# iowait
+ 'cpu.util.6' : (9, 3),# irq
+ 'cpu.util.7' : (2, 33),# softirq
+ 'cpu.util.8' : (6, 122),# steal
+ 'cpu.util.9' : (5, 300),# guest
+ 'cpu.util.10': (3, 300),# guest_nice
+}
+
+
+# If mock_state is a function, it must accept two
+# arguments, just like dict.get:
+def mock_state_function(key, _default):
+ counter = int(key.split('.')[-1])
+ return (23, (counter < 6) * 300)
+
+
+# TODO: These only represent the status quo - check whether they even make sense.
+# Note that systime and total are being ignored entirely by the check.
+expected_result_1 = CheckResult([
+ BasicCheckResult(0, "user: 40.0%, system: 20.0%",
+ [('user', 40.0, None, None, None, None),
+ ('system', 20.0, None, None, None, None),
+ ('wait', 20.0, None, None, None, None)]),
+ BasicCheckResult(0, "wait: 20.0%", None),
+ BasicCheckResult(0, "steal: 0.0%",
+ [('steal', 0.0, None, None, None, None)]),
+ BasicCheckResult(0, "total: 80.0%", None)
+])
+
+
+expected_result_2 = CheckResult([
+ BasicCheckResult(0, "user: -51.6%, system: -105.8%",
+ [('user', -51.61290322580645, None, None, None, None),
+ ('system', -105.80645161290323, None, None, None, None),
+ ('wait', 451.61290322580646, None, None, None, None)]),
+ BasicCheckResult(0, "wait: 451.6%", None),
+ BasicCheckResult(0, "steal: 78.7%",
+ [('steal', 78.70967741935483, None, None, None, None)]),
+ BasicCheckResult(0, "total: 372.9%", None)
+])
+
+
+(a)pytest.mark.parametrize("info,mockstate,expected_result"sult", [
+(info_statgrab_cpu_hpux, mock_state_tuple, expected_result_1),
+(info_statgrab_cpu_hpux, mock_state_dict, expected_result_2),
])
-def test_statgrab_cpu_check(check_manager, time_to_info, params, predicate):
+def test_statgrab_cpu_check(check_manager, info, mockstate, expected_result):
+
check = check_manager.get_check("statgrab_cpu")
- # NOTE: We do not mock the time module here, because the check does not depend on
any
- # absolute rates. The mocking that was included in this test previously had no
- # effect, which went unnoticed because it doesn't matter in this case.
- try:
- list(check.run_check(None, params, time_to_info(0)))
- except MKCounterWrapped:
- pass
- result = checktestlib.CheckResult(check.run_check(None, params, time_to_info(60)))
- assert predicate(result)
+
+ # set up mocking of `get_item_state`
+ with MockItemState(mockstate):
+ result = CheckResult(check.run_check(None, {}, info))
+ assertCheckResultsEqual(result, expected_result)
+
+
+(a)pytest.mark.parametrize("info,mockstate"tate", [
+(info_statgrab_cpu_hpux, mock_state_function),
+])
+def test_statgrab_cpu_check_error(check_manager, info, mockstate):
+
+ check = check_manager.get_check("statgrab_cpu")
+
+ with MockItemState(mockstate):
+ # the mock values are designed to raise an exception.
+ # to make sure it is raised, use this:
+ with assertMKCounterWrapped('Too short time difference since last
check'):
+ CheckResult(check.run_check(None, {}, info))
+ # # You could omit the error message it you don't care about it:
+ # with assertMKCounterWrapped()
+ # CheckResult(check.run_check(None, {}, info))
+
+
+
+