|
| 1 | +# |
| 2 | +# Copyright 2012-2022 Ghent University |
| 3 | +# |
| 4 | +# This file is part of vsc-utils, |
| 5 | +# originally created by the HPC team of Ghent University (http://ugent.be/hpc/en), |
| 6 | +# with support of Ghent University (http://ugent.be/hpc), |
| 7 | +# the Flemish Supercomputer Centre (VSC) (https://www.vscentrum.be), |
| 8 | +# the Flemish Research Foundation (FWO) (http://www.fwo.be/en) |
| 9 | +# and the Department of Economy, Science and Innovation (EWI) (http://www.ewi-vlaanderen.be/en). |
| 10 | +# |
| 11 | +# https://github.com/hpcugent/vsc-utils |
| 12 | +# |
| 13 | +# vsc-utils is free software: you can redistribute it and/or modify |
| 14 | +# it under the terms of the GNU Library General Public License as |
| 15 | +# published by the Free Software Foundation, either version 2 of |
| 16 | +# the License, or (at your option) any later version. |
| 17 | +# |
| 18 | +# vsc-utils is distributed in the hope that it will be useful, |
| 19 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | +# GNU Library General Public License for more details. |
| 22 | +# |
| 23 | +# You should have received a copy of the GNU Library General Public License |
| 24 | +# along with vsc-utils. If not, see <http://www.gnu.org/licenses/>. |
| 25 | +# |
| 26 | +""" |
| 27 | +Tests for the vsc.utils.zabbix module. |
| 28 | +
|
| 29 | +@author: Andy Georges (Ghent University) |
| 30 | +@author: Samuel Moors (Vrije Universiteit Brussel) |
| 31 | +""" |
| 32 | +import json |
| 33 | +import os |
| 34 | +import tempfile |
| 35 | +import time |
| 36 | +import sys |
| 37 | +import random |
| 38 | +import string |
| 39 | +from pwd import getpwuid |
| 40 | + |
| 41 | +from vsc.install.testing import TestCase |
| 42 | + |
| 43 | +from vsc.utils.zabbix import ZabbixReporter, SimpleZabbix |
| 44 | +from vsc.utils.nagios import NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN |
| 45 | +from vsc.utils.py2vs3 import StringIO |
| 46 | + |
| 47 | + |
| 48 | +class TestZabbix(TestCase): |
| 49 | + """Test for the zabbix reporter class.""" |
| 50 | + |
| 51 | + def setUp(self): |
| 52 | + user = getpwuid(os.getuid()) |
| 53 | + self.nagios_user = user.pw_name |
| 54 | + super(TestZabbix, self).setUp() |
| 55 | + |
| 56 | + def test_cache(self): |
| 57 | + """Test the caching mechanism in the reporter.""" |
| 58 | + length = random.randint(1, 30) |
| 59 | + exit_code = random.randint(0, 3) |
| 60 | + threshold = random.randint(0, 10) |
| 61 | + |
| 62 | + message = ''.join(random.choice(string.printable) for x in range(length)) |
| 63 | + message = message.rstrip() |
| 64 | + message = json.dumps([message]) |
| 65 | + |
| 66 | + (handle, filename) = tempfile.mkstemp() |
| 67 | + os.unlink(filename) |
| 68 | + os.close(handle) |
| 69 | + reporter = ZabbixReporter('test_cache', filename, threshold, self.nagios_user) |
| 70 | + |
| 71 | + nagios_exit = [NAGIOS_EXIT_OK, NAGIOS_EXIT_WARNING, NAGIOS_EXIT_CRITICAL, NAGIOS_EXIT_UNKNOWN][exit_code] |
| 72 | + |
| 73 | + reporter.cache(nagios_exit, message) |
| 74 | + |
| 75 | + (handle, output_filename) = tempfile.mkstemp() |
| 76 | + os.close(handle) |
| 77 | + |
| 78 | + try: |
| 79 | + old_stdout = sys.stdout |
| 80 | + buffer = StringIO() |
| 81 | + sys.stdout = buffer |
| 82 | + reporter_test = ZabbixReporter('test_cache', filename, threshold, self.nagios_user) |
| 83 | + reporter_test.report_and_exit() |
| 84 | + except SystemExit as err: |
| 85 | + line = buffer.getvalue().rstrip() |
| 86 | + sys.stdout = old_stdout |
| 87 | + buffer.close() |
| 88 | + self.assertTrue(err.code == nagios_exit[0]) |
| 89 | + line = json.loads(line) |
| 90 | + self.assertTrue(line[1] == nagios_exit[1]) |
| 91 | + self.assertTrue(line[2][0] == json.loads(message)[0]) |
| 92 | + |
| 93 | + os.unlink(filename) |
0 commit comments