Esempio n. 1
0
    def test_present(self):
        '''
        Test to ensure a Linux ACL is present
        '''
        name = '/root'
        acl_type = 'users'
        acl_name = 'damian'
        perms = 'rwx'

        ret = {'name': name,
               'result': None,
               'comment': '',
               'changes': {}}

        mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name:
                                                          {'octal': 'A'}}]}},
                                      {name: {acl_type: [{}]}},
                                      {name: {acl_type: ''}}])
        with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
            with patch.dict(linux_acl.__opts__, {'test': True}):
                comt = ('Permissions have been updated')
                ret.update({'comment': comt})
                self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                       perms), ret)

                comt = ('Permissions will be applied')
                ret.update({'comment': comt})
                self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                       perms), ret)

            comt = ('ACL Type does not exist')
            ret.update({'comment': comt, 'result': False})
            self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                   perms), ret)
Esempio n. 2
0
    def test_present(self):
        '''
        Test to ensure a Linux ACL is present
        '''
        name = '/root'
        acl_type = 'users'
        acl_name = 'damian'
        perms = 'rwx'

        ret = {'name': name,
               'result': None,
               'comment': '',
               'changes': {}}

        mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name:
                                                          {'octal': 'A'}}]}},
                                      {name: {acl_type: [{}]}},
                                      {name: {acl_type: ''}}])
        with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
            with patch.dict(linux_acl.__opts__, {'test': True}):
                comt = ('Permissions have been updated')
                ret.update({'comment': comt})
                self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                       perms), ret)

                comt = ('Permissions will be applied')
                ret.update({'comment': comt})
                self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                       perms), ret)

            comt = ('ACL Type does not exist')
            ret.update({'comment': comt, 'result': False})
            self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                   perms), ret)
Esempio n. 3
0
    def test_present(self):
        '''
        Test to ensure a Linux ACL is present
        '''
        self.maxDiff = None
        name = '/root'
        acl_type = 'users'
        acl_name = 'damian'
        perms = 'rwx'

        mock = MagicMock(side_effect=[{
            name: {
                acl_type: [{
                    acl_name: {
                        'octal': 'A'
                    }
                }]
            }
        }, {
            name: {
                acl_type: [{
                    acl_name: {
                        'octal': 'A'
                    }
                }]
            }
        }, {
            name: {
                acl_type: [{
                    acl_name: {
                        'octal': 'A'
                    }
                }]
            }
        }, {
            name: {
                acl_type: [{}]
            }
        }, {
            name: {
                acl_type: [{}]
            }
        }, {
            name: {
                acl_type: [{}]
            }
        }, {
            name: {
                acl_type: ''
            }
        }])
        mock_modfacl = MagicMock(return_value=True)

        with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}):
            # Update - test=True
            with patch.dict(linux_acl.__opts__, {'test': True}):
                comt = ('Updated permissions will be applied for {0}: A -> {1}'
                        ''.format(acl_name, perms))
                ret = {
                    'name': name,
                    'comment': comt,
                    'changes': {},
                    'pchanges': {
                        'new': {
                            'acl_name': acl_name,
                            'acl_type': acl_type,
                            'perms': perms
                        },
                        'old': {
                            'acl_name': acl_name,
                            'acl_type': acl_type,
                            'perms': 'A'
                        }
                    },
                    'result': None
                }

                self.assertDictEqual(
                    linux_acl.present(name, acl_type, acl_name, perms), ret)
            # Update - test=False
            with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
                with patch.dict(linux_acl.__opts__, {'test': False}):
                    comt = ('Updated permissions for {0}'.format(acl_name))
                    ret = {
                        'name': name,
                        'comment': comt,
                        'changes': {
                            'new': {
                                'acl_name': acl_name,
                                'acl_type': acl_type,
                                'perms': perms
                            },
                            'old': {
                                'acl_name': acl_name,
                                'acl_type': acl_type,
                                'perms': 'A'
                            }
                        },
                        'pchanges': {},
                        'result': True
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms),
                        ret)
            # Update - modfacl error
            with patch.dict(
                    linux_acl.__salt__,
                {
                    'acl.modfacl':
                    MagicMock(side_effect=CommandExecutionError('Custom err'))
                }):
                with patch.dict(linux_acl.__opts__, {'test': False}):
                    comt = ('Error updating permissions for {0}: Custom err'
                            ''.format(acl_name))
                    ret = {
                        'name': name,
                        'comment': comt,
                        'changes': {},
                        'pchanges': {},
                        'result': False
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms),
                        ret)
            # New - test=True
            with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
                with patch.dict(linux_acl.__opts__, {'test': True}):
                    comt = ('New permissions will be applied '
                            'for {0}: {1}'.format(acl_name, perms))
                    ret = {
                        'name': name,
                        'comment': comt,
                        'changes': {},
                        'pchanges': {
                            'new': {
                                'acl_name': acl_name,
                                'acl_type': acl_type,
                                'perms': perms
                            }
                        },
                        'result': None
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms),
                        ret)
            # New - test=False
            with patch.dict(linux_acl.__salt__, {'acl.modfacl': mock_modfacl}):
                with patch.dict(linux_acl.__opts__, {'test': False}):
                    comt = ('Applied new permissions for {0}'.format(acl_name))
                    ret = {
                        'name': name,
                        'comment': comt,
                        'changes': {
                            'new': {
                                'acl_name': acl_name,
                                'acl_type': acl_type,
                                'perms': perms
                            }
                        },
                        'pchanges': {},
                        'result': True
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms),
                        ret)
            # New - modfacl error
            with patch.dict(
                    linux_acl.__salt__,
                {
                    'acl.modfacl':
                    MagicMock(side_effect=CommandExecutionError('Custom err'))
                }):
                with patch.dict(linux_acl.__opts__, {'test': False}):
                    comt = ('Error updating permissions for {0}: Custom err'
                            ''.format(acl_name))
                    ret = {
                        'name': name,
                        'comment': comt,
                        'changes': {},
                        'pchanges': {},
                        'result': False
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms),
                        ret)
            # No acl type
            comt = ('ACL Type does not exist')
            ret = {
                'name': name,
                'comment': comt,
                'result': False,
                'changes': {},
                'pchanges': {}
            }
            self.assertDictEqual(
                linux_acl.present(name, acl_type, acl_name, perms), ret)
Esempio n. 4
0
    def test_present(self):
        """
        Test to ensure a Linux ACL is present
        """
        self.maxDiff = None
        name = "/root"
        acl_type = "users"
        acl_name = "damian"
        perms = "rwx"

        mock = MagicMock(
            side_effect=[
                {name: {acl_type: [{acl_name: {"octal": 5}}]}},
                {name: {acl_type: [{acl_name: {"octal": 5}}]}},
                {name: {acl_type: [{acl_name: {"octal": 5}}]}},
                {name: {acl_type: [{}]}},
                {name: {acl_type: [{}]}},
                {name: {acl_type: [{}]}},
                {
                    name: {acl_type: [{acl_name: {"octal": 7}}]},
                    name + "/foo": {acl_type: [{acl_name: {"octal": 5}}]},
                },
                {
                    name: {acl_type: [{acl_name: {"octal": 7}}]},
                    name + "/foo": {acl_type: [{acl_name: {"octal": 7}}]},
                },
                {name: {acl_type: ""}},
            ]
        )
        mock_modfacl = MagicMock(return_value=True)

        with patch.dict(linux_acl.__salt__, {"acl.getfacl": mock}):
            # Update - test=True
            with patch.dict(linux_acl.__opts__, {"test": True}):
                comt = (
                    "Updated permissions will be applied for {0}: r-x -> {1}"
                    "".format(acl_name, perms)
                )
                ret = {
                    "name": name,
                    "comment": comt,
                    "changes": {
                        "new": {
                            "acl_name": acl_name,
                            "acl_type": acl_type,
                            "perms": perms,
                        },
                        "old": {
                            "acl_name": acl_name,
                            "acl_type": acl_type,
                            "perms": "r-x",
                        },
                    },
                    "result": None,
                }

                self.assertDictEqual(
                    linux_acl.present(name, acl_type, acl_name, perms), ret
                )
            # Update - test=False
            with patch.dict(linux_acl.__salt__, {"acl.modfacl": mock_modfacl}):
                with patch.dict(linux_acl.__opts__, {"test": False}):
                    comt = "Updated permissions for {0}".format(acl_name)
                    ret = {
                        "name": name,
                        "comment": comt,
                        "changes": {
                            "new": {
                                "acl_name": acl_name,
                                "acl_type": acl_type,
                                "perms": perms,
                            },
                            "old": {
                                "acl_name": acl_name,
                                "acl_type": acl_type,
                                "perms": "r-x",
                            },
                        },
                        "result": True,
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms), ret
                    )
            # Update - modfacl error
            with patch.dict(
                linux_acl.__salt__,
                {
                    "acl.modfacl": MagicMock(
                        side_effect=CommandExecutionError("Custom err")
                    )
                },
            ):
                with patch.dict(linux_acl.__opts__, {"test": False}):
                    comt = "Error updating permissions for {0}: Custom err" "".format(
                        acl_name
                    )
                    ret = {
                        "name": name,
                        "comment": comt,
                        "changes": {},
                        "result": False,
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms), ret
                    )
            # New - test=True
            with patch.dict(linux_acl.__salt__, {"acl.modfacl": mock_modfacl}):
                with patch.dict(linux_acl.__opts__, {"test": True}):
                    comt = "New permissions will be applied " "for {0}: {1}".format(
                        acl_name, perms
                    )
                    ret = {
                        "name": name,
                        "comment": comt,
                        "changes": {
                            "new": {
                                "acl_name": acl_name,
                                "acl_type": acl_type,
                                "perms": perms,
                            }
                        },
                        "result": None,
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms), ret
                    )
            # New - test=False
            with patch.dict(linux_acl.__salt__, {"acl.modfacl": mock_modfacl}):
                with patch.dict(linux_acl.__opts__, {"test": False}):
                    comt = "Applied new permissions for {0}".format(acl_name)
                    ret = {
                        "name": name,
                        "comment": comt,
                        "changes": {
                            "new": {
                                "acl_name": acl_name,
                                "acl_type": acl_type,
                                "perms": perms,
                            }
                        },
                        "result": True,
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms), ret
                    )
            # New - modfacl error
            with patch.dict(
                linux_acl.__salt__,
                {
                    "acl.modfacl": MagicMock(
                        side_effect=CommandExecutionError("Custom err")
                    )
                },
            ):
                with patch.dict(linux_acl.__opts__, {"test": False}):
                    comt = "Error updating permissions for {0}: Custom err" "".format(
                        acl_name
                    )
                    ret = {
                        "name": name,
                        "comment": comt,
                        "changes": {},
                        "result": False,
                    }
                    self.assertDictEqual(
                        linux_acl.present(name, acl_type, acl_name, perms), ret
                    )

            # New - recurse true
            with patch.dict(linux_acl.__salt__, {"acl.getfacl": mock}):
                # Update - test=True
                with patch.dict(linux_acl.__opts__, {'test': True}):
                    comt = ('Updated permissions will be applied for {0}: rwx -> {1}'
                            ''.format(acl_name, perms))
                    ret = {'name': name,
                           'comment': comt,
                           'changes': {'new': {'acl_name': acl_name,
                                               'acl_type': acl_type,
                                               'perms': perms},
                                       'old': {'acl_name': acl_name,
                                               'acl_type': acl_type,
                                               'perms': 'rwx'}},
                           'result': None}

                    self.assertDictEqual(linux_acl.present(name, acl_type, acl_name,
                                                           perms, recurse=False), ret)

            # New - recurse true - nothing to do
            with patch.dict(linux_acl.__salt__, {"acl.getfacl": mock}):
                # Update - test=True
                with patch.dict(linux_acl.__opts__, {"test": True}):
                    comt = "Permissions are in the desired state"
                    ret = {"name": name, "comment": comt, "changes": {}, "result": True}

                    self.assertDictEqual(
                        linux_acl.present(
                            name, acl_type, acl_name, perms, recurse=True
                        ),
                        ret,
                    )

            # No acl type
            comt = "ACL Type does not exist"
            ret = {"name": name, "comment": comt, "result": False, "changes": {}}
            self.assertDictEqual(
                linux_acl.present(name, acl_type, acl_name, perms), ret
            )