Skip to content

Instantly share code, notes, and snippets.

@richm
Created August 23, 2021 16:51
Show Gist options
  • Save richm/7458f61df337ada8d9f2382595ae5cc4 to your computer and use it in GitHub Desktop.
Save richm/7458f61df337ada8d9f2382595ae5cc4 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# Copyright: (c) 2020, Rich Megginson <rmeggins@redhat.com>
# SPDX-License-Identifier: GPL-2.0-or-later
#
""" Unit tests for kernel_settings module """
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
try:
from unittest.mock import MagicMock, Mock, patch
except ImportError:
from mock import MagicMock, Mock, patch
import firewall_lib
class MockException(Exception): pass
class MockAnsibleModule(object):
def __init__(self, **kwargs):
self.params = {}
self.call_params = {}
if not kwargs:
return
if "name" in kwargs:
self.name = kwargs["name"]
return
def __call__(self, **kwargs):
for kk, vv in kwargs["argument_spec"].items():
self.call_params[kk] = vv.get("default")
if kk not in self.params:
self.params[kk] = self.call_params[kk]
self.supports_check_mode = kwargs["supports_check_mode"]
return self
def set_params(self, params):
self.params = params
def fail_json(self, msg):
self.fail_msg = msg
raise MockException()
class FirewallLibParsers(unittest.TestCase):
"""test param to profile conversion and vice versa"""
# def assertRegex(self, text, expected_regex, msg=None):
# """Fail the test unless the text matches the regular expression."""
# assert re.search(expected_regex, text)
# def setUp(self):
# self.test_root_dir = tempfile.mkdtemp(suffix=".lsr")
# os.environ["TEST_ROOT_DIR"] = self.test_root_dir
# self.test_cleanup = kernel_settings.setup_for_testing()
# self.tuned_config = tuned.utils.global_config.GlobalConfig()
# self.logger = Mock()
# def tearDown(self):
# self.test_cleanup()
# shutil.rmtree(self.test_root_dir)
# del os.environ["TEST_ROOT_DIR"]
def test_parse_port(self):
"""Test the code that parses port values."""
module = Mock()
item = "a/b"
rc = firewall_lib.parse_port(module, item)
self.assertEqual(("a", "b"), rc)
def test_parse_forward_port(self):
"""Test the code that parses port values."""
module = Mock()
module.fail_json = Mock(side_effect=MockException())
item = "aaa"
with self.assertRaises(MockException):
rc = firewall_lib.parse_forward_port(module, item)
module.fail_json.assert_called_with(msg="improper forward_port format: aaa")
item = "a/b;;"
rc = firewall_lib.parse_forward_port(module, item)
self.assertEqual(("a", "b", None, None), rc)
class FirewallLibMain(unittest.TestCase):
"""Test main function."""
@patch('firewall_lib.AnsibleModule')
def test_main_error_no_firewall_backend(self, module_class):
module_class.return_value.fail_json = Mock(side_effect=MockException())
with self.assertRaises(MockException):
firewall_lib.main()
module_class.return_value.fail_json.assert_called_with(msg="No firewall backend could be imported.")
@patch('firewall_lib.AnsibleModule', new_callable=MockAnsibleModule)
@patch('firewall_lib.HAS_FIREWALLD', True)
def test_main_error_no_params(self, am_class):
with self.assertRaises(MockException):
firewall_lib.main()
self.assertEqual(
am_class.fail_msg,
"One of service, port, source_port, forward_port, masquerade, rich_rule, source, "
"interface, icmp_block, icmp_block_inversion, target or zone needs to be set"
)
@patch('firewall_lib.AnsibleModule', new_callable=MockAnsibleModule)
@patch('firewall_lib.HAS_FIREWALLD', True)
def test_main_error_timeout_icmp_block_inversion(self, am_class):
am_class.set_params({"icmp_block_inversion": True, "timeout": 1})
with self.assertRaises(MockException):
firewall_lib.main()
self.assertEqual(
am_class.fail_msg,
"timeout can not be used with icmp_block_inverson only"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment