Example #1
0
    def test_insert(self):
        '''
            Test to insert a rule into a chain
        '''
        ret = {'name': 'salt',
               'changes': {},
               'result': None,
               'comment': ''}

        self.assertDictEqual(iptables.insert('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 for ipv4 (a)',
                                'result': True})
                    self.assertDictEqual(iptables.insert('salt',
                                                         table='', chain=''),
                                         ret)

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

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

                            ret.update({'changes': {},
                                        'result': False,
                                        'comment': 'Failed to set iptables'
                                        ' rule for salt.\nAttempted rule was a'
                                        })
                            self.assertDictEqual(iptables.insert('salt',
                                                                 table='',
                                                                 chain='',
                                                                 position=''),
                                                 ret)
Example #2
0
    def test_insert(self):
        '''
            Test to insert a rule into a chain
        '''
        ret = {'name': 'salt',
               'changes': {},
               'result': None,
               'comment': ''}

        self.assertDictEqual(iptables.insert('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 for ipv4 (a)',
                                'result': True})
                    self.assertDictEqual(iptables.insert('salt',
                                                         table='', chain=''),
                                         ret)

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

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

                            ret.update({'changes': {},
                                        'result': False,
                                        'comment': 'Failed to set iptables'
                                        ' rule for salt.\nAttempted rule was a'
                                        })
                            self.assertDictEqual(iptables.insert('salt',
                                                                 table='',
                                                                 chain='',
                                                                 position=''),
                                                 ret)
Example #3
0
    def test_insert(self):
        """
            Test to insert a rule into a chain
        """
        ret = {"name": "salt", "changes": {}, "result": None, "comment": ""}

        self.assertDictEqual(iptables.insert("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 for ipv4 (a)",
                        "result": True,
                    })
                    self.assertDictEqual(
                        iptables.insert("salt", table="", chain=""), ret)

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

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

                            ret.update({
                                "changes": {},
                                "result":
                                False,
                                "comment":
                                "Failed to set iptables"
                                " rule for salt.\nAttempted rule was a",
                            })
                            self.assertDictEqual(
                                iptables.insert("salt",
                                                table="",
                                                chain="",
                                                position=""),
                                ret,
                            )
    def test_insert(self):
        """
        Test to insert a rule into a chain
        """
        ret = {"name": "salt", "changes": {}, "result": None, "comment": ""}

        self.assertDictEqual(iptables.insert("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 for ipv4 (a)"
                            ),
                            "result": True,
                        }
                    )
                    self.assertDictEqual(
                        iptables.insert("salt", table="", chain=""), ret
                    )

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

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

                            ret.update(
                                {
                                    "changes": {},
                                    "result": False,
                                    "comment": (
                                        "Failed to set iptables"
                                        " rule for salt.\nAttempted rule was a"
                                    ),
                                }
                            )
                            self.assertDictEqual(
                                iptables.insert(
                                    "salt", table="", chain="", position=""
                                ),
                                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.insert(
                                                "salt",
                                                table="",
                                                chain="",
                                                position="",
                                                save="/tmp/iptables",
                                            ),
                                            ret,
                                        )
                                        ret.update(
                                            {
                                                "changes": {},
                                                "result": True,
                                                "comment": "iptables rule for salt already set for ipv4 (a)",
                                            }
                                        )
                                        self.assertDictEqual(
                                            iptables.insert(
                                                "salt",
                                                table="",
                                                chain="",
                                                position="",
                                                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",
                                        )
Example #5
0
    def test_insert(self):
        '''
            Test to insert a rule into a chain
        '''
        ret = {'name': 'salt', 'changes': {}, 'result': None, 'comment': ''}

        self.assertDictEqual(iptables.insert('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 for ipv4 (a)',
                        'result': True
                    })
                    self.assertDictEqual(
                        iptables.insert('salt', table='', chain=''), ret)

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

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

                            ret.update({
                                'changes': {},
                                'result':
                                False,
                                'comment':
                                'Failed to set iptables'
                                ' rule for salt.\nAttempted rule was a'
                            })
                            self.assertDictEqual(
                                iptables.insert('salt',
                                                table='',
                                                chain='',
                                                position=''), 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.insert(
                                                'salt',
                                                table='',
                                                chain='',
                                                position='',
                                                save='/tmp/iptables'), ret)
                                        ret.update({
                                            'changes': {},
                                            'result':
                                            True,
                                            'comment':
                                            'iptables rule for salt already set for ipv4 (a)'
                                        })
                                        self.assertDictEqual(
                                            iptables.insert(
                                                'salt',
                                                table='',
                                                chain='',
                                                position='',
                                                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')