예제 #1
0
    def test_append(self):
        '''
            Test to append a rule to 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 (a) for ipv4'
                    })
                    self.assertDictEqual(
                        nftables.append('salt', table='', chain=''), ret)

                    with patch.dict(nftables.__opts__, {"test": True}):
                        ret.update({
                            'result':
                            None,
                            'comment':
                            'nftables rule for salt needs'
                            ' to be set (a) for ipv4'
                        })
                        self.assertDictEqual(
                            nftables.append('salt', table='', chain=''), ret)

                    with patch.dict(nftables.__opts__, {"test": False}):
                        mock = MagicMock(side_effect=[True, False])
                        with patch.dict(nftables.__salt__,
                                        {"nftables.append": mock}):
                            ret.update({
                                'changes': {
                                    'locale': 'salt'
                                },
                                'comment': 'Set nftables rule for salt'
                                ' to: a for ipv4',
                                'result': True
                            })
                            self.assertDictEqual(
                                nftables.append('salt', table='', chain=''),
                                ret)

                            ret.update({
                                'changes': {},
                                'comment':
                                'Failed to set nftables'
                                ' rule for salt.\nAttempted rule was'
                                ' a for ipv4',
                                'result':
                                False
                            })
                            self.assertDictEqual(
                                nftables.append('salt', table='', chain=''),
                                ret)
예제 #2
0
    def test_append(self):
        '''
            Test to append a rule to 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 (a) for ipv4'})
                    self.assertDictEqual(nftables.append('salt', table='',
                                                         chain=''), ret)

                    with patch.dict(nftables.__opts__, {"test": True}):
                        ret.update({'result': None,
                                    'comment': 'nftables rule for salt needs'
                                    ' to be set (a) for ipv4'})
                        self.assertDictEqual(nftables.append('salt', table='',
                                                             chain=''), ret)

                    with patch.dict(nftables.__opts__, {"test": False}):
                        mock = MagicMock(side_effect=[True, False])
                        with patch.dict(nftables.__salt__,
                                        {"nftables.append": mock}):
                            ret.update({'changes': {'locale': 'salt'},
                                        'comment': 'Set nftables rule for salt'
                                        ' to: a for ipv4',
                                        'result': True})
                            self.assertDictEqual(nftables.append('salt',
                                                                 table='',
                                                                 chain=''),
                                                 ret)

                            ret.update({'changes': {},
                                        'comment': 'Failed to set nftables'
                                        ' rule for salt.\nAttempted rule was'
                                        ' a for ipv4', 'result': False})
                            self.assertDictEqual(nftables.append('salt',
                                                                 table='',
                                                                 chain=''),
                                                 ret)
예제 #3
0
def test_append():
    """
    Test to append a rule to 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 (a) for ipv4"
                })
                assert nftables.append("salt", table="", chain="") == ret

                with patch.dict(nftables.__opts__, {"test": True}):
                    ret.update({
                        "result":
                        None,
                        "comment":
                        "nftables rule for salt needs to be set (a) for ipv4",
                    })
                    assert nftables.append("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.append": mock}):
                        ret.update({
                            "changes": {
                                "locale": "salt"
                            },
                            "comment":
                            "Set nftables rule for salt to: a for ipv4",
                            "result": True,
                        })
                        assert nftables.append("salt", table="",
                                               chain="") == ret

                        ret.update({
                            "changes": {},
                            "comment":
                            "Failed to set nftables rule for"
                            " salt.\nAttempted rule was a for ipv4.\n",
                            "result":
                            False,
                        })
                        assert nftables.append("salt", table="",
                                               chain="") == ret