diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rw-r--r-- | tests/test_check_remote_host.py | 67 | ||||
-rw-r--r-- | tests/test_sadf.py | 190 |
3 files changed, 259 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 65843f4..e06a2ba 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -19,6 +19,8 @@ # test_modules = \ + test_check_remote_host.py \ + test_sadf.py \ $(NULL) dist_glusternagiosaddonstests_DATA = \ diff --git a/tests/test_check_remote_host.py b/tests/test_check_remote_host.py new file mode 100644 index 0000000..c5c602d --- /dev/null +++ b/tests/test_check_remote_host.py @@ -0,0 +1,67 @@ +# +# Copyright 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# + +import mock + +from testrunner import PluginsTestCase as TestCaseBase +from plugins.check_remote_host import * + + +class TestHello(TestCaseBase): + # Method to test the execCmd() method + @mock.patch('check_remote_host.subprocess.Popen') + def testExecCmd(self, mock_popen): + reference = subprocess.Popen('any command', close_fds=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out = "sample output" + err = "" + reference.communicate.return_value = (out, err) + self.assertTrue(reference.communicate, "communicate called") + + # Method to test the getPingStatus() method + @mock.patch('check_remote_host.execCmd') + def testGetPingStatus(self, mock_execCmd): + rc = 0 + out = "sample output" + err = "" + mock_execCmd.return_value = (rc, out, err) + getPingStatus('dummy host') + mock_execCmd.assert_called_with([ + '/usr/lib64/nagios/plugins/check_ping', '-H', 'dummy', 'host', + '-w', '3000.0,80%', '-c', '5000.0,100%']) + self.assertRaises(OSError, execCmd, + ['/usr/lib64/nagios/plugins/check_ping', '-H', + 'dummy', 'host', '-w', '3000.0,80%', '-c', + '5000.0,100%']) + + # Method to test the checkLiveStatus() method + @mock.patch('check_remote_host.socket.socket') + def testCheckLiveStatus(self, mock_socket): + reference = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + self.assertTrue(mock_socket, "called") + reference.recv.return_value = "0\n" + checkLiveStatus("dummy host", "dummy srvc") + reference.connect.assert_called_with('/var/spool/nagios/cmd/live') + reference.send.assert_called_with("GET services\nColumns: state\n" + "Filter: description = dummy srvc\n" + "Filter: host_address = " + "dummy host\n") + self.assertEquals(0, checkLiveStatus("dummy host", "dummy srvc")) diff --git a/tests/test_sadf.py b/tests/test_sadf.py new file mode 100644 index 0000000..ced037e --- /dev/null +++ b/tests/test_sadf.py @@ -0,0 +1,190 @@ +# +# Copyright 2014 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# +# Refer to the README and COPYING files for full details of the license +# + +import xml.etree.cElementTree as etree + +from testrunner import PluginsTestCase as TestCaseBase +from plugins import sadf + + +class sadfTests(TestCaseBase): + + def _etree_to_dict_arg_test(self): + out = """<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE sysstat PUBLIC "DTD v2.15 sysstat //EN" +"http://pagesperso-orange.fr/sebastien.godard/sysstat-2.15.dtd"> +<sysstat> +<sysdata-version>2.15</sysdata-version> +<host nodename="dhcp-0-171.blr.redhat.com"> +<sysname>Linux</sysname> +<release>3.11.3-201.fc19.x86_64</release> +<machine>x86_64</machine> +<number-of-cpus>4</number-of-cpus> +<file-date>2014-03-07</file-date> +<statistics> +<timestamp date="2014-03-07" time="05:00:01" utc="1" interval="59"> +<memory per="second" unit="kB"> +<memfree>6821428</memfree> +<memused>1049448</memused> +<memused-percent>13.33</memused-percent> +<buffers>49416</buffers> +<cached>536932</cached> +<commit>2127484</commit> +<commit-percent>7.38</commit-percent> +<active>361428</active> +<inactive>487048</inactive> +<dirty>1256</dirty> +</memory> +</timestamp> +</statistics> +<restarts> +<boot date="2014-03-07" time="04:58:08" utc="1"/> +</restarts> +</host> +</sysstat> +""" + tree = etree.fromstring(out) + expected_dict = \ + {'sysstat': {'host': + {'sysname': 'Linux', + 'statistics': {'timestamp': + {'date': '2014-03-07', + 'utc': '1', 'interval': '59', + 'time': '05:00:01', + 'memory': + {'memused-percent': '13.33', + 'cached': '536932', + 'unit': 'kB', + 'per': 'second', + 'memfree': '6821428', + 'inactive': '487048', + 'commit-percent': '7.38', + 'active': '361428', + 'commit': '2127484', + 'memused': '1049448', + 'buffers': '49416', + 'dirty': '1256'}}}, + 'nodename': 'dhcp-0-171.blr.redhat.com', + 'file-date': '2014-03-07', + 'number-of-cpus': '4', + 'restarts': {'boot': + {'date': '2014-03-07', 'utc': '1', + 'time': '04:58:08'}}, + 'machine': 'x86_64', + 'release': '3.11.3-201.fc19.x86_64'}, + 'sysdata-version': '2.15'}} + + actual_dict = sadf.etree_to_dict(tree) + self.assertEquals(actual_dict, expected_dict) + + def _etree_to_dict_string_test(self): + out = """<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE sysstat PUBLIC "DTD v2.15 sysstat //EN" +"http://pagesperso-orange.fr/sebastien.godard/sysstat-2.15.dtd"> +<sysstat> +<sysdata-version>2.15</sysdata-version> +<host nodename="dhcp-0-171.blr.redhat.com"> +<sysname>Linux</sysname> +<release>3.11.3-201.fc19.x86_64</release> +<machine>x86_64</machine> +<number-of-cpus>4</number-of-cpus> +<file-date>2014-03-07</file-date> +<statistics> +<timestamp date="2014-03-07" time="05:00:01" utc="1" interval="59"> +<memory per="second" unit="kB"> +Test string +<memfree>6821428</memfree> +<memused>1049448</memused> +<memused-percent>13.33</memused-percent> +<buffers>49416</buffers> +<cached>536932</cached> +<commit>2127484</commit> +<commit-percent>7.38</commit-percent> +<active>361428</active> +<inactive>487048</inactive> +<dirty>1256</dirty> +</memory> +</timestamp> +</statistics> +<restarts> +<boot date="2014-03-07" time="04:58:08" utc="1"/> +</restarts> +</host> +</sysstat> +""" + tree = etree.fromstring(out) + expected_dict = \ + {'sysstat': {'host': + {'sysname': 'Linux', + 'statistics': {'timestamp': + {'date': '2014-03-07', + 'utc': '1', 'interval': '59', + 'time': '05:00:01', 'memory': + {'#text': 'Test string', + 'memused-percent': '13.33', + 'cached': '536932', 'unit': 'kB', + 'per': 'second', + 'memfree': '6821428', + 'inactive': '487048', + 'commit-percent': '7.38', + 'active': '361428', + 'commit': '2127484', + 'memused': '1049448', + 'buffers': '49416', + 'dirty': '1256'}}}, + 'nodename': 'dhcp-0-171.blr.redhat.com', + 'file-date': '2014-03-07', 'number-of-cpus': '4', + 'restarts': {'boot': {'date': '2014-03-07', + 'utc': '1', + 'time': '04:58:08'}}, + 'machine': 'x86_64', + 'release': '3.11.3-201.fc19.x86_64'}, + 'sysdata-version': '2.15'}} + actual_dict = sadf.etree_to_dict(tree) + #print actual_dict + #exit(0) + self.assertEquals(actual_dict, expected_dict) + + def _etree_to_dict_empty_test(self): + out = """<?xml version="1.0" encoding="UTF-8"?> +<sysstat> +<buffers></buffers> +<cached></cached> +<commit>2127484</commit> +<commit-percent>7.38</commit-percent> +<active>361428</active> +<inactive>487048</inactive> +</sysstat> +""" + tree = etree.fromstring(out) + expected_dict = \ + {'sysstat': {'cached': None, + 'inactive': '487048', + 'commit-percent': '7.38', + 'active': '361428', + 'commit': '2127484', + 'buffers': None}} + actual_dict = sadf.etree_to_dict(tree) + self.assertEquals(actual_dict, expected_dict) + + def test_etree_to_dict_test(self): + self._etree_to_dict_arg_test() + self._etree_to_dict_string_test() + self._etree_to_dict_empty_test() |