示例#1
0
    def test_match_time(self):
        with patch('salt.utils.files.fopen', mock_open(read_data=raw)):
            with patch('time.time', MagicMock(return_value=1506121200)):
                with patch('struct.unpack', MagicMock(return_value=pack)):
                    config = [{
                        'users': {
                            'garet': {
                                'time': {
                                    'end': '5pm',
                                    'start': '3pm'
                                }
                            }
                        }
                    }]

                    ret = btmp.validate(config)

                    self.assertEqual(ret, (True, 'Valid beacon configuration'))

                    _expected = [{
                        'addr': 1505937373,
                        'exit_status': 0,
                        'inittab': '',
                        'hostname': '::1',
                        'PID': 29774,
                        'session': 0,
                        'user': '******',
                        'time': 0,
                        'line': 'ssh:notty',
                        'type': 6
                    }]
                    ret = btmp.beacon(config)
                    self.assertEqual(ret, _expected)
示例#2
0
def test_invalid_groups():
    config = [{"groups": ["docker"]}]

    ret = btmp.validate(config)

    assert ret == (False,
                   "Group configuration for btmp beacon must be a dictionary.")
示例#3
0
def test_invalid_users():
    config = [{"users": ["gareth"]}]

    ret = btmp.validate(config)

    assert ret == (False,
                   "User configuration for btmp beacon must be a dictionary.")
示例#4
0
def test_match():
    with patch("salt.utils.files.fopen", mock_open(read_data=raw)):
        with patch("struct.unpack", MagicMock(return_value=pack)):
            config = [{"users": {"garet": {}}}]

            ret = btmp.validate(config)

            assert ret == (True, "Valid beacon configuration")

            _expected = [
                {
                    "addr": 1505937373,
                    "exit_status": 0,
                    "inittab": "",
                    "hostname": "::1",
                    "PID": 29774,
                    "session": 0,
                    "user": "******",
                    "time": 0,
                    "line": "ssh:notty",
                    "type": 6,
                }
            ]
            ret = btmp.beacon(config)
            assert ret == _expected
示例#5
0
    def test_invalid_groups(self):
        config = [{"groups": ["docker"]}]

        ret = btmp.validate(config)

        self.assertEqual(
            ret, (False,
                  "Group configuration for btmp beacon must be a dictionary."))
示例#6
0
    def test_invalid_users(self):
        config = [{'users': ['gareth']}]

        ret = btmp.validate(config)

        self.assertEqual(
            ret, (False,
                  'User configuration for btmp beacon must be a dictionary.'))
示例#7
0
    def test_invalid_groups(self):
        config = [{'groups': ['docker']}]

        ret = btmp.validate(config)

        self.assertEqual(
            ret, (False,
                  'Group configuration for btmp beacon must be a dictionary.'))
示例#8
0
    def test_invalid_users(self):
        config = [{"users": ["gareth"]}]

        ret = btmp.validate(config)

        self.assertEqual(
            ret, (False,
                  "User configuration for btmp beacon must be a dictionary."))
示例#9
0
def test_groups_invalid_time_range():
    config = [{"groups": {"docker": {"time_range": {"start": "3pm"}}}}]

    ret = btmp.validate(config)

    assert ret == (
        False,
        "The time_range parameter for btmp beacon must contain start & end options.",
    )
示例#10
0
    def test_groups_invalid_time_range(self):
        config = [{'groups': {'docker': {'time_range': {'start': '3pm'}}}}]

        ret = btmp.validate(config)

        self.assertEqual(ret, (
            False,
            'The time_range parameter for btmp beacon must contain start & end options.'
        ))
示例#11
0
    def test_users_invalid_time_range(self):
        config = [{"users": {"gareth": {"time_range": {"start": "3pm"}}}}]

        ret = btmp.validate(config)

        self.assertEqual(
            ret,
            (
                False,
                "The time_range parameter for btmp beacon must contain start & end options.",
            ),
        )
示例#12
0
    def test_match_group(self):

        for groupadd in (
                "salt.modules.aix_group",
                "salt.modules.mac_group",
                "salt.modules.pw_group",
                "salt.modules.solaris_group",
                "salt.modules.win_groupadd",
        ):
            mock_group_info = {
                "passwd": "x",
                "gid": 100,
                "name": "users",
                "members": ["garet"],
            }

            with patch("salt.utils.files.fopen", mock_open(read_data=raw)):
                with patch("time.time", MagicMock(return_value=1506121200)):
                    with patch("struct.unpack", MagicMock(return_value=pack)):
                        with patch(
                                "{}.info".format(groupadd),
                                new=MagicMock(return_value=mock_group_info),
                        ):
                            config = [{
                                "group": {
                                    "users": {
                                        "time_range": {
                                            "end": "5pm",
                                            "start": "3pm"
                                        }
                                    }
                                }
                            }]

                            ret = btmp.validate(config)

                            self.assertEqual(
                                ret, (True, "Valid beacon configuration"))

                            _expected = [{
                                "addr": 1505937373,
                                "exit_status": 0,
                                "inittab": "",
                                "hostname": "::1",
                                "PID": 29774,
                                "session": 0,
                                "user": "******",
                                "time": 0,
                                "line": "ssh:notty",
                                "type": 6,
                            }]
                            ret = btmp.beacon(config)
                            self.assertEqual(ret, _expected)
示例#13
0
    def test_no_match(self):
        config = [{'users': {'gareth': {'time_range': {'end': '09-22-2017 5pm',
                                                       'start': '09-22-2017 3pm'}}}}
                  ]

        ret = btmp.validate(config)

        self.assertEqual(ret, (True, 'Valid beacon configuration'))

        with patch('salt.utils.files.fopen', mock_open()) as m_open:
            ret = btmp.beacon(config)
            m_open.assert_called_with(btmp.BTMP, 'rb')
            self.assertEqual(ret, [])
示例#14
0
    def test_match_group(self):

        for groupadd in ('salt.modules.aix_group', 'salt.modules.mac_group',
                         'salt.modules.pw_group', 'salt.modules.solaris_group',
                         'salt.modules.win_groupadd'):
            mock_group_info = {
                'passwd': 'x',
                'gid': 100,
                'name': 'users',
                'members': ['garet']
            }

            with patch('salt.utils.files.fopen', mock_open(read_data=raw)):
                with patch('time.time', MagicMock(return_value=1506121200)):
                    with patch('struct.unpack', MagicMock(return_value=pack)):
                        with patch(
                                '{0}.info'.format(groupadd),
                                new=MagicMock(return_value=mock_group_info)):
                            config = [{
                                'group': {
                                    'users': {
                                        'time_range': {
                                            'end': '5pm',
                                            'start': '3pm'
                                        }
                                    }
                                }
                            }]

                            ret = btmp.validate(config)

                            self.assertEqual(
                                ret, (True, 'Valid beacon configuration'))

                            _expected = [{
                                'addr': 1505937373,
                                'exit_status': 0,
                                'inittab': '',
                                'hostname': '::1',
                                'PID': 29774,
                                'session': 0,
                                'user': '******',
                                'time': 0,
                                'line': 'ssh:notty',
                                'type': 6
                            }]
                            ret = btmp.beacon(config)
                            self.assertEqual(ret, _expected)
示例#15
0
    def test_no_match(self):
        config = [{
            'users': {
                'gareth': {
                    'time': {
                        'end': '5pm',
                        'start': '3pm'
                    }
                }
            }
        }]

        ret = btmp.validate(config)

        self.assertEqual(ret, (True, 'Valid beacon configuration'))

        ret = btmp.beacon(config)
        self.assertEqual(ret, [])
示例#16
0
def test_no_match():
    config = [{
        "users": {
            "gareth": {
                "time_range": {
                    "end": "09-22-2017 5pm",
                    "start": "09-22-2017 3pm",
                }
            }
        }
    }]

    ret = btmp.validate(config)

    assert ret == (True, "Valid beacon configuration")

    with patch("salt.utils.files.fopen", mock_open(b"")) as m_open:
        ret = btmp.beacon(config)
        call_args = next(iter(m_open.filehandles.values()))[0].call.args
        assert call_args == (btmp.BTMP, "rb"), call_args
        assert ret == [], ret
示例#17
0
    def test_no_match(self):
        config = [{
            'users': {
                'gareth': {
                    'time': {
                        'end': '5pm',
                        'start': '3pm'
                    }
                }
            }
        }]

        ret = btmp.validate(config)

        self.assertEqual(ret, (True, 'Valid beacon configuration'))

        with patch('salt.utils.files.fopen', mock_open(b'')) as m_open:
            ret = btmp.beacon(config)
            call_args = next(six.itervalues(m_open.filehandles))[0].call.args
            assert call_args == (btmp.BTMP, 'rb'), call_args
            assert ret == [], ret
示例#18
0
def test_match_time():
    with patch("salt.utils.files.fopen", mock_open(read_data=raw)):
        mock_now = datetime.datetime(2017, 9, 22, 16, 0, 0, 0)
        with patch("datetime.datetime", MagicMock()), patch(
            "datetime.datetime.now", MagicMock(return_value=mock_now)
        ):
            with patch("struct.unpack", MagicMock(return_value=pack)):
                config = [
                    {
                        "users": {
                            "garet": {
                                "time_range": {
                                    "end": "09-22-2017 5pm",
                                    "start": "09-22-2017 3pm",
                                }
                            }
                        }
                    }
                ]

                ret = btmp.validate(config)

                assert ret == (True, "Valid beacon configuration")

                _expected = [
                    {
                        "addr": 1505937373,
                        "exit_status": 0,
                        "inittab": "",
                        "hostname": "::1",
                        "PID": 29774,
                        "session": 0,
                        "user": "******",
                        "time": 0,
                        "line": "ssh:notty",
                        "type": 6,
                    }
                ]
                ret = btmp.beacon(config)
                assert ret == _expected
示例#19
0
def test_empty_config():
    config = [{}]

    ret = btmp.validate(config)

    assert ret == (True, "Valid beacon configuration")
示例#20
0
def test_non_list_config():
    config = {}
    ret = btmp.validate(config)

    assert ret == (False, "Configuration for btmp beacon must be a list.")
示例#21
0
    def test_empty_config(self):
        config = [{}]

        ret = btmp.validate(config)

        self.assertEqual(ret, (True, "Valid beacon configuration"))
示例#22
0
    def test_non_list_config(self):
        config = {}
        ret = btmp.validate(config)

        self.assertEqual(
            ret, (False, "Configuration for btmp beacon must be a list."))