def test_append(self): ''' Test to append a rule to a chain ''' ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': '' } self.assertDictEqual(iptables.append('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=[True, False, False, False]) with patch.dict(iptables.__salt__, {'iptables.check': mock}): ret.update({'comment': 'iptables rule for salt' ' already set (a) for ipv4', 'result': True}) self.assertDictEqual(iptables.append('salt', table='', chain=''), ret) with patch.dict(iptables.__opts__, {'test': True}): ret.update({'result': None, 'comment': 'iptables rule for salt' ' needs to be set (a) for ipv4'}) self.assertDictEqual(iptables.append('salt', table='', chain=''), ret) with patch.dict(iptables.__opts__, {'test': False}): mock = MagicMock(side_effect=[True, False]) with patch.dict(iptables.__salt__, {'iptables.append': mock}): ret.update({'changes': {'locale': 'salt'}, 'result': True, 'comment': 'Set iptables rule' ' for salt to: a for ipv4'}) self.assertDictEqual(iptables.append('salt', table='', chain=''), ret) ret.update({'changes': {}, 'result': False, 'comment': 'Failed to set iptables' ' rule for salt.\nAttempted rule was' ' a for ipv4'}) self.assertDictEqual(iptables.append('salt', table='', chain=''), ret)
def test_append(self): """ Test to append a rule to a chain """ ret = {"name": "salt", "changes": {}, "result": None, "comment": ""} self.assertDictEqual(iptables.append("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=[True, False, False, False]) with patch.dict(iptables.__salt__, {"iptables.check": mock}): ret.update({ "comment": "iptables rule for salt" " already set (a) for ipv4", "result": True, }) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret) with patch.dict(iptables.__opts__, {"test": True}): ret.update({ "result": None, "comment": "iptables rule for salt" " needs to be set (a) for ipv4", }) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret) with patch.dict(iptables.__opts__, {"test": False}): mock = MagicMock(side_effect=[True, False]) with patch.dict(iptables.__salt__, {"iptables.append": mock}): ret.update({ "changes": { "locale": "salt" }, "result": True, "comment": "Set iptables rule" " for salt to: a for ipv4", }) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret) ret.update({ "changes": {}, "result": False, "comment": "Failed to set iptables" " rule for salt.\nAttempted rule was" " a for ipv4", }) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret)
def test_append(self): """ Test to append a rule to a chain """ ret = {"name": "salt", "changes": {}, "result": None, "comment": ""} self.assertDictEqual(iptables.append("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=[True, False, False, False, False, True]) with patch.dict(iptables.__salt__, {"iptables.check": mock}): ret.update( { "comment": ( "iptables rule for salt already set (a) for ipv4" ), "result": True, } ) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret ) with patch.dict(iptables.__opts__, {"test": True}): ret.update( { "result": None, "comment": ( "iptables rule for salt" " needs to be set (a) for ipv4" ), } ) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret ) with patch.dict(iptables.__opts__, {"test": False}): mock = MagicMock(side_effect=[True, False, True, True]) with patch.dict(iptables.__salt__, {"iptables.append": mock}): ret.update( { "changes": {"locale": "salt"}, "result": True, "comment": ( "Set iptables rule for salt to: a for ipv4" ), } ) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret ) ret.update( { "changes": {}, "result": False, "comment": ( "Failed to set iptables" " rule for salt.\nAttempted rule was" " a for ipv4" ), } ) self.assertDictEqual( iptables.append("salt", table="", chain=""), ret ) mock_save = MagicMock( side_effect=['Wrote 1 lines to "/tmp/iptables"', ""] ) with patch.dict( iptables.__salt__, {"iptables.save": mock_save} ): mock_get_saved_rules = MagicMock(side_effect=[""]) with patch.dict( iptables.__salt__, {"iptables.get_saved_rules": mock_get_saved_rules}, ): mock = MagicMock(side_effect=[""]) with patch.dict( iptables.__salt__, {"iptables.get_rules": mock} ): ret.update( { "changes": {"locale": "salt"}, "result": True, "comment": "Set and saved iptables rule" ' salt for ipv4\na\nWrote 1 lines to "/tmp/iptables"', } ) self.assertDictEqual( iptables.append( "salt", table="", chain="", save="/tmp/iptables", ), ret, ) ret.update( { "changes": {}, "result": True, "comment": "iptables rule for salt already set (a) for ipv4", } ) self.assertDictEqual( iptables.append( "salt", table="", chain="", save="/tmp/iptables", ), ret, ) self.assertEqual( mock_get_saved_rules.mock_calls[0][2][ "conf_file" ], "/tmp/iptables", ) self.assertEqual( mock_save.mock_calls[0][2]["filename"], "/tmp/iptables", )
def test_append(self): ''' Test to append a rule to a chain ''' ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''} self.assertDictEqual(iptables.append('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=[True, False, False, False, False, True]) with patch.dict(iptables.__salt__, {'iptables.check': mock}): ret.update({ 'comment': 'iptables rule for salt' ' already set (a) for ipv4', 'result': True }) self.assertDictEqual( iptables.append('salt', table='', chain=''), ret) with patch.dict(iptables.__opts__, {'test': True}): ret.update({ 'result': None, 'comment': 'iptables rule for salt' ' needs to be set (a) for ipv4' }) self.assertDictEqual( iptables.append('salt', table='', chain=''), ret) with patch.dict(iptables.__opts__, {'test': False}): mock = MagicMock(side_effect=[True, False, True, True]) with patch.dict(iptables.__salt__, {'iptables.append': mock}): ret.update({ 'changes': { 'locale': 'salt' }, 'result': True, 'comment': 'Set iptables rule' ' for salt to: a for ipv4' }) self.assertDictEqual( iptables.append('salt', table='', chain=''), ret) ret.update({ 'changes': {}, 'result': False, 'comment': 'Failed to set iptables' ' rule for salt.\nAttempted rule was' ' a for ipv4' }) self.assertDictEqual( iptables.append('salt', table='', chain=''), ret) mock_save = MagicMock(side_effect=[ 'Wrote 1 lines to "/tmp/iptables"', '' ]) with patch.dict(iptables.__salt__, {'iptables.save': mock_save}): mock_get_saved_rules = MagicMock( side_effect=['']) with patch.dict( iptables.__salt__, { 'iptables.get_saved_rules': mock_get_saved_rules }): mock = MagicMock(side_effect=['']) with patch.dict( iptables.__salt__, {'iptables.get_rules': mock}): ret.update({ 'changes': { 'locale': 'salt' }, 'result': True, 'comment': 'Set and saved iptables rule' ' salt for ipv4\na\nWrote 1 lines to "/tmp/iptables"' }) self.assertDictEqual( iptables.append( 'salt', table='', chain='', save='/tmp/iptables'), ret) ret.update({ 'changes': {}, 'result': True, 'comment': 'iptables rule for salt already set (a) for ipv4' }) self.assertDictEqual( iptables.append( 'salt', table='', chain='', save='/tmp/iptables'), ret) self.assertEqual( mock_get_saved_rules.mock_calls[0] [2]['conf_file'], '/tmp/iptables') self.assertEqual( mock_save.mock_calls[0][2] ['filename'], '/tmp/iptables')