1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00
Files
jc/tests/test_last.py

174 lines
7.3 KiB
Python
Raw Normal View History

2020-02-28 15:15:24 -08:00
import os
import sys
import time
2020-02-28 15:15:24 -08:00
import json
import unittest
import jc.parsers.last
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# Set the timezone on POSIX systems. Need to manually set for Windows tests
if not sys.platform.startswith('win32'):
os.environ['TZ'] = 'America/Los_Angeles'
time.tzset()
2020-02-28 15:15:24 -08:00
class MyTests(unittest.TestCase):
def setUp(self):
# input
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.centos_7_7_last = f.read()
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/last.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.ubuntu_18_4_last = f.read()
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/last.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.osx_10_14_6_last = f.read()
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lastb.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.centos_7_7_lastb = f.read()
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lastb.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.ubuntu_18_4_lastb = f.read()
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last-w.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.centos_7_7_last_w = f.read()
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/last-w.out'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.ubuntu_18_4_last_w = f.read()
2020-05-09 11:22:26 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/fedora32/last.out'), 'r', encoding='utf-8') as f:
self.fedora32_last = f.read()
2020-05-30 18:54:09 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/last.out'), 'r', encoding='utf-8') as f:
self.freebsd12_last = f.read()
2021-01-05 13:00:38 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/last-F.out'), 'r', encoding='utf-8') as f:
self.ubuntu_20_4_last_F = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last-crash.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_last_crash = f.read()
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last-wF.out'), 'r', encoding='utf-8') as f:
self.centos_7_7_last_wF = f.read()
2020-02-28 15:15:24 -08:00
# output
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.centos_7_7_last_json = json.loads(f.read())
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/last.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.ubuntu_18_4_last_json = json.loads(f.read())
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/osx-10.14.6/last.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.osx_10_14_6_last_json = json.loads(f.read())
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/lastb.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.centos_7_7_lastb_json = json.loads(f.read())
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/lastb.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.ubuntu_18_4_lastb_json = json.loads(f.read())
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last-w.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.centos_7_7_last_w_json = json.loads(f.read())
2020-03-04 19:40:32 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-18.04/last-w.json'), 'r', encoding='utf-8') as f:
2020-02-28 15:15:24 -08:00
self.ubuntu_18_4_last_w_json = json.loads(f.read())
2020-05-09 11:22:26 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/fedora32/last.json'), 'r', encoding='utf-8') as f:
self.fedora32_last_json = json.loads(f.read())
2020-05-30 18:54:09 -07:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/freebsd12/last.json'), 'r', encoding='utf-8') as f:
self.freebsd12_last_json = json.loads(f.read())
2021-01-05 13:00:38 -08:00
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/ubuntu-20.04/last-F.json'), 'r', encoding='utf-8') as f:
self.ubuntu_20_4_last_F_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last-crash.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_last_crash_json = json.loads(f.read())
with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/centos-7.7/last-wF.json'), 'r', encoding='utf-8') as f:
self.centos_7_7_last_wF_json = json.loads(f.read())
2020-06-10 17:54:06 -07:00
def test_last_nodata(self):
"""
Test plain 'last' with no data
"""
self.assertEqual(jc.parsers.last.parse('', quiet=True), [])
2020-02-28 15:15:24 -08:00
def test_last_centos_7_7(self):
"""
Test plain 'last' on Centos 7.7
"""
self.assertEqual(jc.parsers.last.parse(self.centos_7_7_last, quiet=True), self.centos_7_7_last_json)
def test_last_ubuntu_18_4(self):
"""
Test plain 'last' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.last.parse(self.ubuntu_18_4_last, quiet=True), self.ubuntu_18_4_last_json)
def test_last_osx_10_14_6(self):
"""
Test plain 'last' on OSX 10.14.6
"""
self.assertEqual(jc.parsers.last.parse(self.osx_10_14_6_last, quiet=True), self.osx_10_14_6_last_json)
def test_lastb_centos_7_7(self):
"""
Test plain 'lastb' on Centos 7.7
"""
self.assertEqual(jc.parsers.last.parse(self.centos_7_7_lastb, quiet=True), self.centos_7_7_lastb_json)
def test_lastb_ubuntu_18_4(self):
"""
Test plain 'lastb' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.last.parse(self.ubuntu_18_4_lastb, quiet=True), self.ubuntu_18_4_lastb_json)
def test_last_w_centos_7_7(self):
"""
Test 'last -w' on Centos 7.7
"""
self.assertEqual(jc.parsers.last.parse(self.centos_7_7_last_w, quiet=True), self.centos_7_7_last_w_json)
def test_last_w_ubuntu_18_4(self):
"""
Test 'last -w' on Ubuntu 18.4
"""
self.assertEqual(jc.parsers.last.parse(self.ubuntu_18_4_last_w, quiet=True), self.ubuntu_18_4_last_w_json)
2021-03-25 20:42:58 -07:00
def test_last_F_ubuntu_20_4(self):
"""
Test 'last -F' on Ubuntu 20.4
"""
self.assertEqual(jc.parsers.last.parse(self.ubuntu_20_4_last_F, quiet=True), self.ubuntu_20_4_last_F_json)
2021-01-05 13:00:38 -08:00
2020-05-09 11:22:26 -07:00
def test_last_fedora32(self):
"""
Test plain 'last' on Fedora32
"""
self.assertEqual(jc.parsers.last.parse(self.fedora32_last, quiet=True), self.fedora32_last_json)
2020-05-30 18:54:09 -07:00
def test_last_freebsd12(self):
"""
Test plain 'last' on FreeBSD12
"""
self.assertEqual(jc.parsers.last.parse(self.freebsd12_last, quiet=True), self.freebsd12_last_json)
def test_last_crash_centos_7_7(self):
"""
Test plain 'last' on Centos 7.7 with crash entries
"""
self.assertEqual(jc.parsers.last.parse(self.centos_7_7_last_crash, quiet=True), self.centos_7_7_last_crash_json)
2021-03-25 20:42:58 -07:00
def test_last_wF_centos_7_7(self):
"""
Test 'last -wF' on Centos 7.7
"""
self.assertEqual(jc.parsers.last.parse(self.centos_7_7_last_wF, quiet=True), self.centos_7_7_last_wF_json)
2020-02-28 15:15:24 -08:00
if __name__ == '__main__':
unittest.main()