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)
def test_invalid_groups(): config = [{"groups": ["docker"]}] ret = btmp.validate(config) assert ret == (False, "Group configuration for btmp beacon must be a dictionary.")
def test_invalid_users(): config = [{"users": ["gareth"]}] ret = btmp.validate(config) assert ret == (False, "User configuration for btmp beacon must be a dictionary.")
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
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."))
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.'))
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.'))
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."))
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.", )
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.' ))
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.", ), )
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)
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, [])
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)
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, [])
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
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
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
def test_empty_config(): config = [{}] ret = btmp.validate(config) assert ret == (True, "Valid beacon configuration")
def test_non_list_config(): config = {} ret = btmp.validate(config) assert ret == (False, "Configuration for btmp beacon must be a list.")
def test_empty_config(self): config = [{}] ret = btmp.validate(config) self.assertEqual(ret, (True, "Valid beacon configuration"))
def test_non_list_config(self): config = {} ret = btmp.validate(config) self.assertEqual( ret, (False, "Configuration for btmp beacon must be a list."))