-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_lab0.py
executable file
·70 lines (55 loc) · 2.19 KB
/
test_lab0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pathlib
import re
import subprocess
import unittest
class TestLab0(unittest.TestCase):
PATH = pathlib.Path('/proc/count')
def _make():
result = subprocess.run(['make'], capture_output=True, text=True)
return result
def _insmod():
result = subprocess.run(['sudo', 'insmod', 'proc_count.ko'],
capture_output=True, text=True)
return result
def _rmmod():
result = subprocess.run(['sudo', 'rmmod', 'proc_count'],
capture_output=True, text=True)
return result
def _make_clean():
result = subprocess.run(['make', 'clean'],
capture_output=True, text=True)
return result
@classmethod
def setUpClass(cls):
cls.make = cls._make().returncode == 0
cls.insmod = cls._insmod().returncode == 0
@classmethod
def tearDownClass(cls):
cls._rmmod()
cls._make_clean()
def test_exists(self):
self.assertTrue(self.make, msg='make failed')
self.assertTrue(self.insmod, msg='insmod failed')
self.assertTrue(self.PATH.exists(),
msg=f'{self.PATH} does not exist with module')
TestLab0._rmmod()
self.assertFalse(self.PATH.exists(),
msg=f'{self.PATH} exists without module')
TestLab0._insmod()
def test_format(self):
self.assertTrue(self.make, msg='make failed')
self.assertTrue(self.insmod, msg='insmod failed')
with open(self.PATH, 'r') as f:
m = re.match(r'^\d+\n$', f.read(), flags=re.ASCII)
self.assertIsNotNone(
m,
msg='there should only be digits followed by a newline'
)
def test_count(self):
self.assertTrue(self.make, msg='make failed')
self.assertTrue(self.insmod, msg='insmod failed')
result = subprocess.run('ps aux | wc -l',
capture_output=True, shell=True, text=True)
with open(self.PATH, 'r') as f:
self.assertEqual(int(result.stdout), int(f.read()) + 4,
msg='number of processes did not match')