"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tests/unit/modules/test_iptables.py" between
salt-3002.1.tar.gz and salt-3002.2.tar.gz

About: SaltStack is a systems management software for data center automation, cloud orchestration, server provisioning, configuration management and more. Community version.

test_iptables.py  (salt-3002.1):test_iptables.py  (salt-3002.2)
# -*- coding: utf-8 -*-
""" """
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com> :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
""" """
# Import Python Libs import textwrap
from __future__ import absolute_import, print_function, unicode_literals
import uuid import uuid
# Import Salt Libs
import salt.modules.iptables as iptables import salt.modules.iptables as iptables
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch from tests.support.mock import MagicMock, mock_open, patch
from tests.support.unit import TestCase from tests.support.unit import TestCase
class IptablesTestCase(TestCase, LoaderModuleMockMixin): class IptablesTestCase(TestCase, LoaderModuleMockMixin):
""" """
Test cases for salt.modules.iptables Test cases for salt.modules.iptables
""" """
def setup_loader_modules(self): def setup_loader_modules(self):
return {iptables: {}} return {iptables: {}}
skipping to change at line 251 skipping to change at line 245
"INPUT", "INPUT",
command="I", command="I",
position="3", position="3",
full="True", full="True",
match="state", match="state",
jump="ACCEPT", jump="ACCEPT",
), ),
ret, ret,
) )
# 'get_saved_rules' function tests: 1 # 'get_saved_rules' function tests: 2
def test_get_saved_rules(self): def test_get_saved_rules(self):
""" """
Test if it return a data structure of the rules in the conf file Test if it return a data structure of the rules in the conf file
""" """
mock = MagicMock(return_value=False) mock = MagicMock(return_value=False)
with patch.object(iptables, "_parse_conf", mock): with patch.object(iptables, "_parse_conf", mock):
self.assertFalse(iptables.get_saved_rules()) self.assertFalse(iptables.get_saved_rules())
mock.assert_called_with(conf_file=None, family="ipv4") mock.assert_called_with(conf_file=None, family="ipv4")
def test_get_saved_rules_nilinuxrt(self):
"""
Test get rules on NILinuxRT system
"""
data = {
"/etc/natinst/share/iptables.conf": textwrap.dedent(
"""\
# Generated by iptables-save v1.6.2 on Thu Oct 8 12:13:44 2020
*filter
:INPUT ACCEPT [2958:584773]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [92:23648]
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
COMMIT
# Completed on Thu Oct 8 12:13:44 2020
"""
)
}
expected_input_rules = [
{
"protocol": ["tcp"],
"jump": ["ACCEPT"],
"match": ["tcp"],
"destination_port": ["80"],
}
]
file_mock = mock_open(read_data=data)
with patch.dict(
iptables.__grains__, {"os_family": "NILinuxRT", "os": "NILinuxRT"}
):
with patch.object(iptables.salt.utils.files, "fopen", file_mock):
rules = iptables.get_saved_rules()
self.assertEqual(
expected_input_rules, rules["filter"]["INPUT"]["rules"]
)
# 'get_rules' function tests: 1 # 'get_rules' function tests: 1
def test_get_rules(self): def test_get_rules(self):
""" """
Test if it return a data structure of the current, in-memory rules Test if it return a data structure of the current, in-memory rules
""" """
mock = MagicMock(return_value=False) mock = MagicMock(return_value=False)
with patch.object(iptables, "_parse_conf", mock): with patch.object(iptables, "_parse_conf", mock):
self.assertFalse(iptables.get_rules()) self.assertFalse(iptables.get_rules())
mock.assert_called_with(in_mem=True, family="ipv4") mock.assert_called_with(in_mem=True, family="ipv4")
skipping to change at line 406 skipping to change at line 436
self.assertEqual( self.assertEqual(
iptables.check(table="filter", chain="INPUT", rule=None, family="ipv 4"), iptables.check(table="filter", chain="INPUT", rule=None, family="ipv 4"),
"Error: Rule needs to be specified", "Error: Rule needs to be specified",
) )
mock_rule = "m state --state RELATED,ESTABLISHED -j ACCEPT" mock_rule = "m state --state RELATED,ESTABLISHED -j ACCEPT"
mock_chain = "INPUT" mock_chain = "INPUT"
mock_uuid = 31337 mock_uuid = 31337
mock_cmd = MagicMock( mock_cmd = MagicMock(
return_value="-A {0}\n-A {1}".format(mock_chain, hex(mock_uuid)) return_value="-A {}\n-A {}".format(mock_chain, hex(mock_uuid))
) )
mock_has = MagicMock(return_value=True) mock_has = MagicMock(return_value=True)
mock_not = MagicMock(return_value=False) mock_not = MagicMock(return_value=False)
with patch.object(iptables, "_has_option", mock_not): with patch.object(iptables, "_has_option", mock_not):
with patch.object(uuid, "getnode", MagicMock(return_value=mock_uuid) ): with patch.object(uuid, "getnode", MagicMock(return_value=mock_uuid) ):
with patch.dict(iptables.__salt__, {"cmd.run": mock_cmd}): with patch.dict(iptables.__salt__, {"cmd.run": mock_cmd}):
self.assertTrue( self.assertTrue(
iptables.check( iptables.check(
table="filter", table="filter",
 End of changes. 8 change blocks. 
10 lines changed or deleted 40 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)