def test_insert(self): ''' Test to insert a rule into a chain ''' ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value=[]) with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(return_value={'result': True, 'comment': '', 'rule': 'a'}) with patch.dict(nftables.__salt__, {'nftables.build_rule': mock}): mock = MagicMock(side_effect=[{'result': True, 'comment': ''}, {'result': False, 'comment': ''}, {'result': False, 'comment': ''}, {'result': False, 'comment': ''}]) with patch.dict(nftables.__salt__, {'nftables.check': mock}): ret.update({'comment': 'nftables rule for salt already' ' set for ipv4 (a)'}) self.assertDictEqual(nftables.insert('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': True}): ret.update({'result': None, 'comment': 'nftables rule for salt' ' needs to be set for ipv4 (a)'}) self.assertDictEqual(nftables.insert('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {'test': False}): mock = MagicMock(side_effect=[{'result': True, 'comment': ''}, {'result': False, 'comment': ''}]) with patch.dict(nftables.__salt__, {'nftables.insert': mock}): ret.update({'changes': {'locale': 'salt'}, 'comment': 'Set nftables rule for' ' salt to: a for ipv4', 'result': True}) self.assertDictEqual(nftables.insert('salt', table='', chain='', position=''), ret) ret.update({'changes': {}, 'comment': 'Failed to set nftables' ' rule for salt.\nAttempted rule was' ' a', 'result': False}) self.assertDictEqual(nftables.insert('salt', table='', chain='', position=''), ret)
def test_insert(self): ''' Test to insert a rule into a chain ''' ret = {'name': 'salt', 'changes': {}, 'result': True, 'comment': ''} mock = MagicMock(return_value=[]) with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock): mock = MagicMock(return_value='a') with patch.dict(nftables.__salt__, {"nftables.build_rule": mock}): mock = MagicMock(side_effect=[True, False, False, False]) with patch.dict(nftables.__salt__, {"nftables.check": mock}): ret.update({'comment': 'nftables rule for salt already' ' set for ipv4 (a)'}) self.assertDictEqual(nftables.insert('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {"test": True}): ret.update({'result': None, 'comment': 'nftables rule for salt' ' needs to be set for ipv4 (a)'}) self.assertDictEqual(nftables.insert('salt', table='', chain=''), ret) with patch.dict(nftables.__opts__, {"test": False}): mock = MagicMock(side_effect=[True, False]) with patch.dict(nftables.__salt__, {"nftables.insert": mock}): ret.update({'changes': {'locale': 'salt'}, 'comment': 'Set nftables rule for' ' salt to: a for ipv4', 'result': True}) self.assertDictEqual(nftables.insert('salt', table='', chain='', position=''), ret) ret.update({'changes': {}, 'comment': 'Failed to set nftables' ' rule for salt.\nAttempted rule was' ' a', 'result': False}) self.assertDictEqual(nftables.insert('salt', table='', chain='', position=''), ret)
def test_insert(): """ Test to insert a rule into a chain """ ret = {"name": "salt", "changes": {}, "result": True, "comment": ""} mock = MagicMock(return_value=[]) with patch.object(nftables, "_STATE_INTERNAL_KEYWORDS", mock): mock = MagicMock(return_value={ "result": True, "comment": "", "rule": "a" }) with patch.dict(nftables.__salt__, {"nftables.build_rule": mock}): mock = MagicMock(side_effect=[ { "result": True, "comment": "" }, { "result": False, "comment": "" }, { "result": False, "comment": "" }, { "result": False, "comment": "" }, ]) with patch.dict(nftables.__salt__, {"nftables.check": mock}): ret.update({ "comment": "nftables rule for salt already" " set for ipv4 (a)" }) assert nftables.insert("salt", table="", chain="") == ret with patch.dict(nftables.__opts__, {"test": True}): ret.update({ "result": None, "comment": "nftables rule for salt needs to be set for ipv4 (a)", }) assert nftables.insert("salt", table="", chain="") == ret with patch.dict(nftables.__opts__, {"test": False}): mock = MagicMock(side_effect=[ { "result": True, "comment": "" }, { "result": False, "comment": "" }, ]) with patch.dict(nftables.__salt__, {"nftables.insert": mock}): ret.update({ "changes": { "locale": "salt" }, "comment": "Set nftables rule for salt to: a for ipv4", "result": True, }) assert (nftables.insert("salt", table="", chain="", position="") == ret) ret.update({ "changes": {}, "comment": "Failed to set nftables rule for" " salt.\nAttempted rule was a", "result": False, }) assert (nftables.insert("salt", table="", chain="", position="") == ret)