def test_delete(self): ''' Test to delete a rule to a chain ''' ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''} self.assertDictEqual(iptables.delete('salt', rules=[]), ret) mock = MagicMock(return_value=[]) with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(return_value='a') with patch.dict(iptables.__salt__, {'iptables.build_rule': mock}): mock = MagicMock(side_effect=[False, True, True, True]) with patch.dict(iptables.__salt__, {'iptables.check': mock}): ret.update({'comment': 'iptables rule for salt' ' already absent for ipv4 (a)', 'result': True}) self.assertDictEqual(iptables.delete('salt', table='', chain=''), ret) with patch.dict(iptables.__opts__, {'test': True}): ret.update({'result': None, 'comment': 'iptables rule for salt needs' ' to be deleted for ipv4 (a)'}) self.assertDictEqual(iptables.delete('salt', table='', chain=''), ret) with patch.dict(iptables.__opts__, {'test': False}): mock = MagicMock(side_effect=[False, True]) with patch.dict(iptables.__salt__, {'iptables.delete': mock}): ret.update({'result': True, 'changes': {'locale': 'salt'}, 'comment': 'Delete iptables rule' ' for salt a'}) self.assertDictEqual(iptables.delete('salt', table='', chain='', position=''), ret) ret.update({'result': False, 'changes': {}, 'comment': 'Failed to delete iptables' ' rule for salt.\nAttempted rule was a' }) self.assertDictEqual(iptables.delete('salt', table='', chain='', position=''), ret)
def test_delete(self): """ Test to delete a rule to a chain """ ret = {"name": "salt", "changes": {}, "result": None, "comment": ""} self.assertDictEqual(iptables.delete("salt", rules=[]), ret) mock = MagicMock(return_value=[]) with patch.object(iptables, "_STATE_INTERNAL_KEYWORDS", mock): mock = MagicMock(return_value="a") with patch.dict(iptables.__salt__, {"iptables.build_rule": mock}): mock = MagicMock(side_effect=[False, True, True, True]) with patch.dict(iptables.__salt__, {"iptables.check": mock}): ret.update({ "comment": "iptables rule for salt" " already absent for ipv4 (a)", "result": True, }) self.assertDictEqual( iptables.delete("salt", table="", chain=""), ret) with patch.dict(iptables.__opts__, {"test": True}): ret.update({ "result": None, "comment": "iptables rule for salt needs" " to be deleted for ipv4 (a)", }) self.assertDictEqual( iptables.delete("salt", table="", chain=""), ret) with patch.dict(iptables.__opts__, {"test": False}): mock = MagicMock(side_effect=[False, True]) with patch.dict(iptables.__salt__, {"iptables.delete": mock}): ret.update({ "result": True, "changes": { "locale": "salt" }, "comment": "Delete iptables rule" " for salt a", }) self.assertDictEqual( iptables.delete("salt", table="", chain="", position=""), ret, ) ret.update({ "result": False, "changes": {}, "comment": "Failed to delete iptables" " rule for salt.\nAttempted rule was a", }) self.assertDictEqual( iptables.delete("salt", table="", chain="", position=""), ret, )
def test_delete(self): """ Test to delete a rule to a chain """ ret = {"name": "salt", "changes": {}, "result": None, "comment": ""} self.assertDictEqual(iptables.delete("salt", rules=[]), ret) mock = MagicMock(return_value=[]) with patch.object(iptables, "_STATE_INTERNAL_KEYWORDS", mock): mock = MagicMock(return_value="a") with patch.dict(iptables.__salt__, {"iptables.build_rule": mock}): mock = MagicMock(side_effect=[False, True, True, True, True, False]) with patch.dict(iptables.__salt__, {"iptables.check": mock}): ret.update( { "comment": ( "iptables rule for salt already absent for ipv4 (a)" ), "result": True, } ) self.assertDictEqual( iptables.delete("salt", table="", chain=""), ret ) with patch.dict(iptables.__opts__, {"test": True}): ret.update( { "result": None, "comment": ( "iptables rule for salt needs" " to be deleted for ipv4 (a)" ), } ) self.assertDictEqual( iptables.delete("salt", table="", chain=""), ret ) with patch.dict(iptables.__opts__, {"test": False}): mock = MagicMock(side_effect=[False, True, False, False]) with patch.dict(iptables.__salt__, {"iptables.delete": mock}): ret.update( { "result": True, "changes": {"locale": "salt"}, "comment": "Delete iptables rule for salt a", } ) self.assertDictEqual( iptables.delete( "salt", table="", chain="", position="" ), ret, ) ret.update( { "result": False, "changes": {}, "comment": ( "Failed to delete iptables" " rule for salt.\nAttempted rule was a" ), } ) self.assertDictEqual( iptables.delete( "salt", table="", chain="", position="" ), ret, ) mock_save = MagicMock( side_effect=['Wrote 1 lines to "/tmp/iptables"', ""] ) with patch.dict( iptables.__salt__, {"iptables.save": mock_save} ): mock = MagicMock(side_effect=[True, False]) with patch.dict( iptables.__salt__, {"iptables.check": mock} ): mock = MagicMock(side_effect=[""]) with patch.dict( iptables.__salt__, {"iptables.get_rules": mock} ): ret.update( { "changes": {"locale": "salt"}, "result": True, "comment": "Deleted and saved iptables rule" ' salt for ipv4\na\nWrote 1 lines to "/tmp/iptables"', } ) self.assertDictEqual( iptables.delete( "salt", table="", chain="", save="/tmp/iptables", ), ret, ) ret.update( { "changes": {}, "result": True, "comment": "iptables rule for salt already absent for ipv4 (a)", } ) self.assertDictEqual( iptables.delete( "salt", table="", chain="", save="/tmp/iptables", ), ret, ) self.assertEqual( mock_save.mock_calls[0][2]["filename"], "/tmp/iptables", )