예제 #1
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": {"gareth": {}}}]

            ret = wtmp.validate(config)

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

            _expected = [
                {
                    "PID": 6216,
                    "action": "login",
                    "line": "pts/14",
                    "session": 0,
                    "time": 0,
                    "exit_status": 0,
                    "inittab": "s/14",
                    "type": 7,
                    "addr": 1506101523,
                    "hostname": "::1",
                    "user": "******",
                }
            ]

            ret = wtmp.beacon(config)
            log.debug("wtmp beacon: %s", ret)
            assert ret == _expected
예제 #2
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': {'gareth': {'time': {'end': '5pm',
                                                             'start': '3pm'}}}}
                              ]

                    ret = wtmp.validate(config)

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

                    _expected = [{'PID': 6216,
                                  'line': 'pts/14',
                                  'session': 0,
                                  'time': 0,
                                  'exit_status': 0,
                                  'inittab': 's/14',
                                  'type': 7,
                                  'addr': 1506101523,
                                  'hostname': '::1',
                                  'user': '******'}]

                    ret = wtmp.beacon(config)
                    self.assertEqual(ret, _expected)
예제 #3
0
def test_invalid_users():
    config = [{"users": ["gareth"]}]

    ret = wtmp.validate(config)

    assert ret == (False,
                   "User configuration for wtmp beacon must be a dictionary.")
예제 #4
0
def test_invalid_groups():
    config = [{"groups": ["docker"]}]

    ret = wtmp.validate(config)

    assert ret == (False,
                   "Group configuration for wtmp beacon must be a dictionary.")
예제 #5
0
    def test_invalid_groups(self):
        config = [{'groups': ['docker']}]

        ret = wtmp.validate(config)

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

        ret = wtmp.validate(config)

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

        ret = wtmp.validate(config)

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

        ret = wtmp.validate(config)

        self.assertEqual(
            ret, (False,
                  "User configuration for wtmp beacon must be a dictionary."))
예제 #9
0
def test_match_group():

    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": ["gareth"],
        }

        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": {
                                            "end": "09-22-2017 5pm",
                                            "start": "09-22-2017 3pm",
                                        }
                                    }
                                }
                            }
                        ]

                        ret = wtmp.validate(config)

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

                        _expected = [
                            {
                                "PID": 6216,
                                "action": "login",
                                "line": "pts/14",
                                "session": 0,
                                "time": 0,
                                "exit_status": 0,
                                "inittab": "s/14",
                                "type": 7,
                                "addr": 1506101523,
                                "hostname": "::1",
                                "user": "******",
                            }
                        ]

                        ret = wtmp.beacon(config)
                        assert ret == _expected
예제 #10
0
def test_groups_invalid_time_range():
    config = [{"groups": {"docker": {"time_range": {"start": "3pm"}}}}]

    ret = wtmp.validate(config)

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

        ret = wtmp.validate(config)

        self.assertEqual(ret, (
            False,
            'The time_range parameter for wtmp beacon must contain start & end options.'
        ))
예제 #12
0
    def test_no_match(self):
        config = [{'users': {'gareth': {'time': {'end': '5pm',
                                                 'start': '3pm'}}}}
                  ]

        ret = wtmp.validate(config)

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

        ret = wtmp.beacon(config)
        self.assertEqual(ret, [])
예제 #13
0
    def test_users_invalid_time_range(self):
        config = [{"users": {"gareth": {"time_range": {"start": "3pm"}}}}]

        ret = wtmp.validate(config)

        self.assertEqual(
            ret,
            (
                False,
                "The time_range parameter for wtmp beacon must contain start & end options.",
            ),
        )
예제 #14
0
    def test_no_match(self):
        config = [{'users': {'gareth': {'time': {'end': '5pm',
                                                 'start': '3pm'}}}}
                  ]

        ret = wtmp.validate(config)

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

        with patch('salt.utils.files.fopen', mock_open()) as m_open:
            ret = wtmp.beacon(config)
            m_open.assert_called_with(wtmp.WTMP, 'rb')
            self.assertEqual(ret, [])
예제 #15
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': ['gareth']
            }

            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': {
                                            'end': '09-22-2017 5pm',
                                            'start': '09-22-2017 3pm'
                                        }
                                    }
                                }
                            }]

                            ret = wtmp.validate(config)

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

                            _expected = [{
                                'PID': 6216,
                                'action': 'login',
                                'line': 'pts/14',
                                'session': 0,
                                'time': 0,
                                'exit_status': 0,
                                'inittab': 's/14',
                                'type': 7,
                                'addr': 1506101523,
                                'hostname': '::1',
                                'user': '******'
                            }]

                            ret = wtmp.beacon(config)
                            self.assertEqual(ret, _expected)
예제 #16
0
파일: test_wtmp.py 프로젝트: elisapa/salt-1
    def test_no_match(self):
        config = [{'users': {'gareth': {'time_range': {'end': '09-22-2017 5pm',
                                                       'start': '09-22-2017 3pm'}}}}
                  ]

        ret = wtmp.validate(config)

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

        with patch('salt.utils.files.fopen', mock_open(b'')) as m_open:
            ret = wtmp.beacon(config)
            call_args = next(six.itervalues(m_open.filehandles))[0].call.args
            assert call_args == (wtmp.WTMP, 'rb'), call_args
            assert ret == [], ret
예제 #17
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": {
                            "gareth": {
                                "time": {
                                    "end": "09-22-2017 5pm",
                                    "start": "09-22-2017 3pm",
                                }
                            }
                        }
                    }
                ]

                ret = wtmp.validate(config)

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

                _expected = [
                    {
                        "PID": 6216,
                        "action": "login",
                        "line": "pts/14",
                        "session": 0,
                        "time": 0,
                        "exit_status": 0,
                        "inittab": "s/14",
                        "type": 7,
                        "addr": 1506101523,
                        "hostname": "::1",
                        "user": "******",
                    }
                ]

                ret = wtmp.beacon(config)
                assert ret == _expected
예제 #18
0
    def test_no_match(self):
        config = [{
            "users": {
                "gareth": {
                    "time_range": {
                        "end": "09-22-2017 5pm",
                        "start": "09-22-2017 3pm",
                    }
                }
            }
        }]

        ret = wtmp.validate(config)

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

        with patch("salt.utils.files.fopen", mock_open(b"")) as m_open:
            ret = wtmp.beacon(config)
            call_args = next(six.itervalues(m_open.filehandles))[0].call.args
            assert call_args == (wtmp.WTMP, "rb"), call_args
            assert ret == [], ret
예제 #19
0
def test_empty_config():
    config = [{}]

    ret = wtmp.validate(config)

    assert ret == (True, "Valid beacon configuration")
예제 #20
0
def test_non_list_config():
    config = {}
    ret = wtmp.validate(config)

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

        ret = wtmp.validate(config)

        self.assertEqual(ret, (True, 'Valid beacon configuration'))
예제 #22
0
    def test_non_list_config(self):
        config = {}
        ret = wtmp.validate(config)

        self.assertEqual(ret, (False, 'Configuration for wtmp beacon must'
                                      ' be a list.'))