def test_check(self): ''' Test if it check for the existence of a rule in the table and chain ''' self.assertEqual( iptables.check(table='filter', chain=None, rule=None, family='ipv4'), 'Error: Chain needs to be specified') self.assertEqual( iptables.check(table='filter', chain='INPUT', rule=None, family='ipv4'), 'Error: Rule needs to be specified') mock_rule = 'm state --state RELATED,ESTABLISHED -j ACCEPT' mock_chain = 'INPUT' mock_uuid = 31337 mock_cmd = MagicMock( return_value='-A {0}\n-A {1}'.format(mock_chain, hex(mock_uuid))) with patch.object(uuid, 'getnode', MagicMock(return_value=mock_uuid)): with patch.dict(iptables.__salt__, {'cmd.run': mock_cmd}): self.assertTrue( iptables.check(table='filter', chain=mock_chain, rule=mock_rule, family='ipv4')) mock = MagicMock(return_value='') with patch.object(salt_cmd, 'run', mock): mock_cmd = MagicMock(return_value='') with patch.dict(iptables.__salt__, {'cmd.run': mock_cmd}): self.assertFalse( iptables.check(table='filter', chain='INPUT', rule=mock_rule, family='ipv4')) mock_uuid = MagicMock(return_value=1234) with patch.object(uuid, 'getnode', mock_uuid): mock_cmd = MagicMock(return_value='-A 0x4d2') with patch.dict(iptables.__salt__, {'cmd.run': mock_cmd}): self.assertTrue( iptables.check(table='filter', chain='0x4d2', rule=mock_rule, family='ipv4'))
def test_check(self): ''' Test if it check for the existence of a rule in the table and chain ''' self.assertEqual(iptables.check(table='filter', chain=None, rule=None, family='ipv4'), 'Error: Chain needs to be specified') self.assertEqual(iptables.check(table='filter', chain='INPUT', rule=None, family='ipv4'), 'Error: Rule needs to be specified') mock_rule = 'm state --state RELATED,ESTABLISHED -j ACCEPT' mock_chain = 'INPUT' mock_uuid = 31337 mock_cmd = MagicMock(return_value='-A {0}\n-A {1}'.format(mock_chain, hex(mock_uuid))) mock_has = MagicMock(return_value=True) mock_not = MagicMock(return_value=False) with patch.object(iptables, '_has_option', mock_not): with patch.object(uuid, 'getnode', MagicMock(return_value=mock_uuid)): with patch.dict(iptables.__salt__, {'cmd.run': mock_cmd}): self.assertTrue(iptables.check(table='filter', chain=mock_chain, rule=mock_rule, family='ipv4')) mock_cmd = MagicMock(return_value='') with patch.object(iptables, '_has_option', mock_not): with patch.object(uuid, 'getnode', MagicMock(return_value=mock_uuid)): with patch.dict(iptables.__salt__, {'cmd.run': MagicMock(return_value='')}): self.assertFalse(iptables.check(table='filter', chain=mock_chain, rule=mock_rule, family='ipv4')) with patch.object(iptables, '_has_option', mock_has): with patch.dict(iptables.__salt__, {'cmd.run': mock_cmd}): self.assertTrue(iptables.check(table='filter', chain='INPUT', rule=mock_rule, family='ipv4')) mock_cmd = MagicMock(return_value='-A 0x4d2') mock_uuid = MagicMock(return_value=1234) with patch.object(iptables, '_has_option', mock_has): with patch.object(uuid, 'getnode', mock_uuid): with patch.dict(iptables.__salt__, {'cmd.run': mock_cmd}): self.assertTrue(iptables.check(table='filter', chain='0x4d2', rule=mock_rule, family='ipv4'))
def test_check(self): """ Test if it check for the existence of a rule in the table and chain """ self.assertEqual( iptables.check(table="filter", chain=None, rule=None, family="ipv4"), "Error: Chain needs to be specified", ) self.assertEqual( iptables.check(table="filter", chain="INPUT", rule=None, family="ipv4"), "Error: Rule needs to be specified", ) mock_rule = "m state --state RELATED,ESTABLISHED -j ACCEPT" mock_chain = "INPUT" mock_uuid = 31337 mock_cmd = MagicMock( return_value="-A {}\n-A {}".format(mock_chain, hex(mock_uuid))) mock_has = MagicMock(return_value=True) mock_not = MagicMock(return_value=False) with patch.object(iptables, "_has_option", mock_not): with patch.object(uuid, "getnode", MagicMock(return_value=mock_uuid)): with patch.dict(iptables.__salt__, {"cmd.run": mock_cmd}): self.assertTrue( iptables.check( table="filter", chain=mock_chain, rule=mock_rule, family="ipv4", )) mock_cmd = MagicMock(return_value="") with patch.object(iptables, "_has_option", mock_not): with patch.object(uuid, "getnode", MagicMock(return_value=mock_uuid)): with patch.dict(iptables.__salt__, {"cmd.run": MagicMock(return_value="")}): self.assertFalse( iptables.check( table="filter", chain=mock_chain, rule=mock_rule, family="ipv4", )) with patch.object(iptables, "_has_option", mock_has): with patch.dict(iptables.__salt__, {"cmd.run": mock_cmd}): self.assertTrue( iptables.check(table="filter", chain="INPUT", rule=mock_rule, family="ipv4")) mock_cmd = MagicMock(return_value="-A 0x4d2") mock_uuid = MagicMock(return_value=1234) with patch.object(iptables, "_has_option", mock_has): with patch.object(uuid, "getnode", mock_uuid): with patch.dict(iptables.__salt__, {"cmd.run": mock_cmd}): self.assertTrue( iptables.check(table="filter", chain="0x4d2", rule=mock_rule, family="ipv4"))