Exemplo n.º 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': {'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)
Exemplo n.º 2
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
Exemplo n.º 3
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
Exemplo n.º 4
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, [])
Exemplo n.º 5
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, [])
Exemplo n.º 6
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
Exemplo n.º 7
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)
Exemplo n.º 8
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
Exemplo n.º 9
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