Ejemplo n.º 1
0
    def test_flush(self):
        '''
            Test to flush current nftables state
        '''
        ret = {'name': 'salt',
               'changes': {},
               'result': None,
               'comment': ''}
        mock = MagicMock(return_value=[])
        with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock):
            mock = MagicMock(side_effect=[{'result': False, 'comment': ''},
                                          {'result': True, 'comment': ''},
                                          {'result': True, 'comment': ''},
                                          {'result': True, 'comment': ''}])
            with patch.dict(nftables.__salt__, {'nftables.check_table': mock}):
                ret.update({'comment': 'Failed to flush table  in family'
                            ' ipv4, table does not exist.',
                            'result': False})
                self.assertDictEqual(nftables.flush('salt',
                                                    table='', chain=''),
                                     ret)

                mock = MagicMock(side_effect=[{'result': False, 'comment': ''},
                                              {'result': True, 'comment': ''},
                                              {'result': True, 'comment': ''}])
                with patch.dict(nftables.__salt__,
                                {'nftables.check_chain': mock}):
                    ret.update({'comment': 'Failed to flush chain  in table'
                                '  in family ipv4, chain does not exist.'})
                    self.assertDictEqual(nftables.flush('salt', table='',
                                                        chain=''), ret)

                    mock = MagicMock(side_effect=[{'result': True,
                                                   'comment': ''},
                                                  {'result': False,
                                                   'comment': ''}])
                    with patch.dict(nftables.__salt__,
                                    {'nftables.flush': mock}):
                        ret.update({'changes': {'locale': 'salt'},
                                    'comment': 'Flush nftables rules in  table'
                                    '  chain ipv4 family',
                                    'result': True})
                        self.assertDictEqual(nftables.flush('salt', table='',
                                                            chain=''), ret)

                        ret.update({'changes': {},
                                    'comment': 'Failed to flush'
                                    ' nftables rules',
                                    'result': False})
                        self.assertDictEqual(nftables.flush('salt', table='',
                                                            chain=''), ret)
Ejemplo n.º 2
0
    def test_flush(self):
        '''
            Test to flush current nftables state
        '''
        ret = {'name': 'salt',
               'changes': {},
               'result': None,
               'comment': ''}
        mock = MagicMock(return_value=[])
        with patch.object(nftables, '_STATE_INTERNAL_KEYWORDS', mock):
            mock = MagicMock(side_effect=[False, True, True, True])
            with patch.dict(nftables.__salt__, {'nftables.check_table': mock}):
                ret.update({'comment': 'Failed to flush table  in family'
                            ' ipv4, table does not exist.',
                            'result': False})
                self.assertDictEqual(nftables.flush('salt',
                                                    table='', chain=''),
                                     ret)

                mock = MagicMock(side_effect=[False, True, True])
                with patch.dict(nftables.__salt__,
                                {'nftables.check_chain': mock}):
                    ret.update({'comment': 'Failed to flush chain  in table'
                                '  in family ipv4, chain does not exist.'})
                    self.assertDictEqual(nftables.flush('salt', table='',
                                                        chain=''), ret)

                    mock = MagicMock(side_effect=[True, False])
                    with patch.dict(nftables.__salt__,
                                    {'nftables.flush': mock}):
                        ret.update({'changes': {'locale': 'salt'},
                                    'comment': 'Flush nftables rules in  table'
                                    '  chain ipv4 family',
                                    'result': True})
                        self.assertDictEqual(nftables.flush('salt', table='',
                                                            chain=''), ret)

                        ret.update({'changes': {},
                                    'comment': 'Failed to flush'
                                    ' nftables rules',
                                    'result': False})
                        self.assertDictEqual(nftables.flush('salt', table='',
                                                            chain=''), ret)
Ejemplo n.º 3
0
def test_flush():
    """
    Test to flush current nftables state
    """
    ret = {"name": "salt", "changes": {}, "result": None, "comment": ""}
    mock = MagicMock(return_value=[])
    with patch.object(nftables, "_STATE_INTERNAL_KEYWORDS", mock):
        mock = MagicMock(side_effect=[
            {
                "result": False,
                "comment": ""
            },
            {
                "result": True,
                "comment": ""
            },
            {
                "result": True,
                "comment": ""
            },
            {
                "result": True,
                "comment": ""
            },
        ])
        with patch.dict(nftables.__salt__, {"nftables.check_table": mock}):
            with patch.dict(nftables.__opts__, {"test": False}):
                ret.update({
                    "comment":
                    "Failed to flush table  in family ipv4, table does not exist.",
                    "result": False,
                })
                assert (nftables.flush("salt",
                                       table="",
                                       chain="",
                                       ignore_absence=False) == ret)

                mock = MagicMock(side_effect=[
                    {
                        "result": False,
                        "comment": ""
                    },
                    {
                        "result": True,
                        "comment": ""
                    },
                    {
                        "result": True,
                        "comment": ""
                    },
                ])
                with patch.dict(nftables.__salt__,
                                {"nftables.check_chain": mock}):
                    ret.update({
                        "comment":
                        "Failed to flush chain  in table  in family ipv4, chain does not exist."
                    })
                    assert (nftables.flush("salt",
                                           table="",
                                           chain="",
                                           ignore_absence=False) == ret)

                    mock = MagicMock(side_effect=[
                        {
                            "result": True,
                            "comment": ""
                        },
                        {
                            "result": False,
                            "comment": ""
                        },
                    ])
                    with patch.dict(nftables.__salt__,
                                    {"nftables.flush": mock}):
                        ret.update({
                            "changes": {
                                "locale": "salt"
                            },
                            "comment":
                            "Flush nftables rules in  table  chain ipv4 family",
                            "result": True,
                        })
                        assert nftables.flush("salt", table="",
                                              chain="") == ret

                        ret.update({
                            "changes": {},
                            "comment": "Failed to flush nftables rules",
                            "result": False,
                        })
                        assert nftables.flush("salt", table="",
                                              chain="") == ret