From 76db2beeb2ea7e234c1d75e680092fe25a9bd24c Mon Sep 17 00:00:00 2001 From: Drew Freiberger Date: Thu, 22 Apr 2021 15:45:55 -0500 Subject: [PATCH] Add unit tests for openvswitch nrpe check helpers --- tests/contrib/charmsupport/test_nrpe.py | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/contrib/charmsupport/test_nrpe.py b/tests/contrib/charmsupport/test_nrpe.py index 334cf18a2..e068adaa5 100644 --- a/tests/contrib/charmsupport/test_nrpe.py +++ b/tests/contrib/charmsupport/test_nrpe.py @@ -494,3 +494,46 @@ def test_copy_nrpe_checks_nrpe_files_dir(self): self.patched['copy2'].assert_called_once_with( 'filea', '/usr/local/lib/nagios/plugins/filea') + + def test_enable_sudo_for_openvswitch_checks(self): + error_msg = 'test failure' + nrpe.enable_sudo_for_openvswitch_checks() + + self.patched['open'].side_effect = IOError(error_msg) + nrpe.enable_sudo_for_openvswitch_checks() + self.patched['open'].side_effect = None + + self.patched['chmod'].side_effect = OSError(error_msg) + nrpe.enable_sudo_for_openvswitch_checks() + self.patched['chmod'].side_effect = None + + self.patched['chown'].side_effect = OSError(error_msg) + nrpe.enable_sudo_for_openvswitch_checks() + self.patched['chown'].side_effect = None + + expected_path = '/etc/sudoers.d/99-check_openvswitch' + expected_success_log = ("Sudoers file for check_openvswitch " + "installed: {}".format(expected_path)) + expected_failure_log = ("Failed to setup sudoers file for " + "check_openvswitch: {}".format(error_msg)) + self.patched['open'].assert_has_calls( + [call(expected_path, 'w')], any_order=True) + self.patched['chmod'].assert_has_calls( + [call(expected_path, 0o100440)], any_order=True) + self.patched['chown'].assert_has_calls( + [call(expected_path, uid=0, gid=0)], any_order=True) + self.patched['log'].assert_has_calls( + [call(expected_success_log, nrpe.DEBUG), + call(expected_failure_log), + call(expected_failure_log), + call(expected_failure_log)]) + self.check_call_counts(log=4, open=4, chmod=3, chown=2) + + @patch.object(nrpe.NRPE, 'add_check') + def test_add_openvswitch_checks(self, mock_nrpe_add_check): + foo = nrpe.NRPE() + nrpe.add_openvswitch_checks(foo, 'testunit') + mock_nrpe_add_check.assert_called_once_with( + shortname='openvswitch', + description='Check Open vSwitch {testunit}', + check_cmd='check_openvswitch.py')