def test_insert(self): ''' Test if it insert a rule into the specified table & chain, at the specified position. ''' self.assertEqual(nftables.insert(), 'Error: Chain needs to be specified') self.assertEqual(nftables.insert(chain='input'), 'Error: Rule needs to be specified') _ru = 'input tcp dport 22 log accept' ret = 'Error: table filter in family ipv4 does not exist' mock = MagicMock(return_value='') with patch.dict(nftables.__salt__, {'cmd.run': mock}): self.assertEqual(nftables.insert(chain='input', rule=_ru), ret) ret = 'Error: chain input in table filter in family ipv4 does not exist' mock = MagicMock(return_value='table ip filter') with patch.dict(nftables.__salt__, {'cmd.run': mock}): self.assertEqual(nftables.insert(chain='input', rule=_ru), ret) r_val = 'table ip filter chain input {{ input tcp dport 22 log accept #' mock = MagicMock(return_value=r_val) with patch.dict(nftables.__salt__, {'cmd.run': mock}): self.assertTrue(nftables.insert(chain='input', rule=_ru))
def test_insert_rule(self): ''' Test if it insert a rule into the specified table & chain, at the specified position. ''' _ru = 'input tcp dport 22 log accept' mock = MagicMock(side_effect=['1', '']) with patch.dict(nftables.__salt__, {'cmd.run': mock}), \ patch('salt.modules.nftables.check', MagicMock(return_value={'result': False, 'comment': ''})), \ patch('salt.modules.nftables.check_chain', MagicMock(return_value={'result': True, 'comment': ''})), \ patch('salt.modules.nftables.check_table', MagicMock(return_value={'result': True, 'comment': ''})): _expected = { 'result': False, 'comment': 'Failed to add rule "{0}" chain input in table filter in family ipv4.' .format(_ru) } self.assertEqual(nftables.insert(chain='input', rule=_ru), _expected) _expected = { 'result': True, 'comment': 'Added rule "{0}" chain input in table filter in family ipv4.'. format(_ru) } self.assertEqual(nftables.insert(chain='input', rule=_ru), _expected)
def test_insert_rule(self): """ Test if it insert a rule into the specified table & chain, at the specified position. """ _ru = "input tcp dport 22 log accept" mock = MagicMock(side_effect=["1", ""]) with patch.dict(nftables.__salt__, {"cmd.run": mock}), patch( "salt.modules.nftables.check", MagicMock(return_value={"result": False, "comment": ""}), ), patch( "salt.modules.nftables.check_chain", MagicMock(return_value={"result": True, "comment": ""}), ), patch( "salt.modules.nftables.check_table", MagicMock(return_value={"result": True, "comment": ""}), ): _expected = { "result": False, "comment": 'Failed to add rule "{}" chain input in table filter in family ipv4.'.format( _ru ), } self.assertEqual(nftables.insert(chain="input", rule=_ru), _expected) _expected = { "result": True, "comment": 'Added rule "{}" chain input in table filter in family ipv4.'.format( _ru ), } self.assertEqual(nftables.insert(chain="input", rule=_ru), _expected)
def test_insert_rule(self): ''' Test if it insert a rule into the specified table & chain, at the specified position. ''' _ru = 'input tcp dport 22 log accept' mock = MagicMock(side_effect=['1', '']) with patch.dict(nftables.__salt__, {'cmd.run': mock}): self.assertFalse(nftables.insert(chain='input', rule=_ru)) self.assertTrue(nftables.insert(chain='input', rule=_ru))
def test_insert_rule(self): ''' Test if it insert a rule into the specified table & chain, at the specified position. ''' _ru = 'input tcp dport 22 log accept' mock = MagicMock(side_effect=['1', '']) with patch.dict(nftables.__salt__, {'cmd.run': mock}), \ patch('salt.modules.nftables.check', MagicMock(return_value=False)), \ patch('salt.modules.nftables.check_chain', MagicMock(return_value=True)), \ patch('salt.modules.nftables.check_table', MagicMock(return_value=True)): self.assertFalse(nftables.insert(chain='input', rule=_ru)) self.assertTrue(nftables.insert(chain='input', rule=_ru))
def test_insert(self): ''' Test if it insert a rule into the specified table & chain, at the specified position. ''' self.assertEqual(nftables.insert(), {'result': False, 'comment': 'Chain needs to be specified'}) self.assertEqual(nftables.insert(chain='input'), {'result': False, 'comment': 'Rule needs to be specified'}) _ru = 'input tcp dport 22 log accept' ret = {'result': False, 'comment': 'Table filter in family ipv4 does not exist'} mock = MagicMock(return_value='') with patch.dict(nftables.__salt__, {'cmd.run': mock}): self.assertEqual(nftables.insert(chain='input', rule=_ru), ret) ret = {'result': False, 'comment': 'Chain input in table filter in family ipv4 does not exist'} mock = MagicMock(return_value='table ip filter') with patch.dict(nftables.__salt__, {'cmd.run': mock}): self.assertEqual(nftables.insert(chain='input', rule=_ru), ret) r_val = 'table ip filter chain input {{ input tcp dport 22 log accept #' mock = MagicMock(return_value=r_val) with patch.dict(nftables.__salt__, {'cmd.run': mock}): res = nftables.insert(chain='input', rule=_ru) import logging log = logging.getLogger(__name__) log.debug('=== res %s ===', res) self.assertTrue(nftables.insert(chain='input', rule=_ru))
def test_insert(self): """ Test if it insert a rule into the specified table & chain, at the specified position. """ self.assertEqual( nftables.insert(), { "result": False, "comment": "Chain needs to be specified" }, ) self.assertEqual( nftables.insert(chain="input"), { "result": False, "comment": "Rule needs to be specified" }, ) _ru = "input tcp dport 22 log accept" ret = { "result": False, "comment": "Table filter in family ipv4 does not exist" } mock = MagicMock(return_value="") with patch.dict(nftables.__salt__, {"cmd.run": mock}): self.assertEqual(nftables.insert(chain="input", rule=_ru), ret) ret = { "result": False, "comment": "Chain input in table filter in family ipv4 does not exist", } mock = MagicMock(return_value="table ip filter") with patch.dict(nftables.__salt__, {"cmd.run": mock}): self.assertEqual(nftables.insert(chain="input", rule=_ru), ret) r_val = "table ip filter chain input {{ input tcp dport 22 log accept #" mock = MagicMock(return_value=r_val) with patch.dict(nftables.__salt__, {"cmd.run": mock}): res = nftables.insert(chain="input", rule=_ru) import logging log = logging.getLogger(__name__) log.debug("=== res %s ===", res) self.assertTrue(nftables.insert(chain="input", rule=_ru))