Example #1
0
def test_set_policy():
    """
    Test to sets the default policy for iptables firewall tables
    """
    ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}

    mock = MagicMock(return_value=[])
    with patch.object(iptables, "_STATE_INTERNAL_KEYWORDS", mock):
        mock = MagicMock(return_value="stack")
        with patch.dict(iptables.__salt__, {"iptables.get_policy": mock}):
            ret.update({
                "comment": ("iptables default policy for chain"
                            "  on table  for ipv4 already set to stack")
            })
            assert (iptables.set_policy("salt",
                                        table="",
                                        chain="",
                                        policy="stack") == ret)

            with patch.dict(iptables.__opts__, {"test": True}):
                ret.update({
                    "comment": ("iptables default policy for chain"
                                "  on table  for ipv4 needs to be set"
                                " to sal"),
                    "result":
                    None,
                })
                assert (iptables.set_policy("salt",
                                            table="",
                                            chain="",
                                            policy="sal") == ret)

            with patch.dict(iptables.__opts__, {"test": False}):
                mock = MagicMock(side_effect=[False, True])
                with patch.dict(iptables.__salt__,
                                {"iptables.set_policy": mock}):
                    ret.update({
                        "changes": {
                            "locale": "salt"
                        },
                        "comment":
                        "Set default policy for  to sal family ipv4",
                        "result": True,
                    })
                    assert (iptables.set_policy("salt",
                                                table="",
                                                chain="",
                                                policy="sal") == ret)

                    ret.update({
                        "comment": "Failed to set iptables default policy",
                        "result": False,
                        "changes": {},
                    })
                    assert (iptables.set_policy("salt",
                                                table="",
                                                chain="",
                                                policy="sal") == ret)
Example #2
0
    def test_set_policy(self):
        '''
            Test to sets the default policy for iptables firewall tables
        '''
        ret = {'name': 'salt',
               'changes': {},
               'result': True,
               'comment': ''}

        mock = MagicMock(return_value=[])
        with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
            mock = MagicMock(return_value='stack')
            with patch.dict(iptables.__salt__, {'iptables.get_policy': mock}):
                ret.update({'comment': 'iptables default policy for chain'
                            '  on table  for ipv4 already set to stack'})
                self.assertDictEqual(iptables.set_policy('salt',
                                                         table='',
                                                         chain='',
                                                         policy='stack'), ret)

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

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

                        ret.update({'comment': 'Failed to set iptables'
                                    ' default policy',
                                    'result': False,
                                    'changes': {}})
                        self.assertDictEqual(iptables.set_policy('salt',
                                                                 table='',
                                                                 chain='',
                                                                 policy='sal'),
                                             ret)
Example #3
0
    def test_set_policy(self):
        '''
            Test to sets the default policy for iptables firewall tables
        '''
        ret = {'name': 'salt',
               'changes': {},
               'result': True,
               'comment': ''}

        mock = MagicMock(return_value=[])
        with patch.object(iptables, '_STATE_INTERNAL_KEYWORDS', mock):
            mock = MagicMock(return_value='stack')
            with patch.dict(iptables.__salt__, {'iptables.get_policy': mock}):
                ret.update({'comment': 'iptables default policy for chain'
                            '  on table  for ipv4 already set to stack'})
                self.assertDictEqual(iptables.set_policy('salt',
                                                         table='',
                                                         chain='',
                                                         policy='stack'), ret)

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

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

                        ret.update({'comment': 'Failed to set iptables'
                                    ' default policy',
                                    'result': False,
                                    'changes': {}})
                        self.assertDictEqual(iptables.set_policy('salt',
                                                                 table='',
                                                                 chain='',
                                                                 policy='sal'),
                                             ret)