Skip to content

Commit 4a66b94

Browse files
frenzymadnesshroncok
authored andcommitted
Add new test suite for testing output of scripts
1 parent 2bb2e10 commit 4a66b94

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

tests/test_scripts.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import unittest
2+
import ethtool
3+
import sys
4+
from io import TextIOWrapper, BytesIO
5+
from imp import load_source
6+
7+
pifc = load_source('pifc', 'scripts/pifconfig')
8+
peth = load_source('peth', 'scripts/pethtool')
9+
10+
11+
def find_suitable_device():
12+
active_devices = ethtool.get_active_devices()
13+
for device in active_devices:
14+
device_flags = ethtool.get_flags(device)
15+
str_flags = pifc.flags2str(device_flags)
16+
try:
17+
wireless_protocol = ethtool.get_wireless_protocol(device)
18+
except Exception:
19+
wireless_protocol = None
20+
21+
if 'UP' in str_flags and 'RUNNING' in str_flags \
22+
and 'BROADCAST' in str_flags and not wireless_protocol:
23+
return device
24+
25+
return None
26+
27+
28+
loopback = 'lo'
29+
device = find_suitable_device()
30+
31+
32+
class ScriptsTests(unittest.TestCase):
33+
34+
def setUp(self):
35+
self._old_stdout = sys.stdout
36+
self._stdout = sys.stdout = TextIOWrapper(BytesIO(), sys.stdout.encoding)
37+
38+
def _output(self):
39+
self._stdout.seek(0)
40+
return self._stdout.read()
41+
42+
def tearDown(self):
43+
sys.stdout = self._old_stdout
44+
self._stdout.close()
45+
46+
def test_flags2str(self):
47+
self.assertEqual(pifc.flags2str(0), '')
48+
self.assertEqual(pifc.flags2str(73), 'UP LOOPBACK RUNNING')
49+
self.assertEqual(pifc.flags2str(4305), 'UP POINTOPOINT RUNNING NOARP MULTICAST')
50+
self.assertEqual(pifc.flags2str(4163), 'UP BROADCAST RUNNING MULTICAST')
51+
52+
# Tests for loopback
53+
54+
def test_driver_lo(self):
55+
self.assertIsNone(peth.show_driver(loopback))
56+
self.assertEqual(self._output(),
57+
'driver: not implemented\nbus-info: not available\n'
58+
)
59+
60+
def test_show_ring_lo(self):
61+
self.assertIsNone(peth.show_ring(loopback))
62+
self.assertEqual(self._output(),
63+
'Ring parameters for {}:\n NOT supported!\n'.format(loopback)
64+
)
65+
66+
def test_show_coalesce_lo(self):
67+
self.assertIsNone(peth.show_coalesce(loopback))
68+
self.assertEqual(self._output(),
69+
'Coalesce parameters for {}:\n NOT supported!\n'.format(loopback)
70+
)
71+
72+
def test_show_offload_lo(self):
73+
self.assertIsNone(peth.show_offload(loopback))
74+
self.assertEqual(self._output(),
75+
'''scatter-gather: on
76+
tcp segmentation offload: on
77+
udp fragmentation offload: on
78+
generic segmentation offload: on
79+
'''
80+
)
81+
82+
# Tests for another device
83+
if device:
84+
def test_driver_eth(self):
85+
self.assertIsNone(peth.show_driver(device))
86+
expected_lines_start = ['driver: ', 'bus-info: ']
87+
lines = self._output().split('\n')
88+
for expected_start, line in zip(expected_lines_start, lines):
89+
self.assertTrue(line.startswith(expected_start))
90+
91+
def test_show_ring_eth(self):
92+
self.assertIsNone(peth.show_ring(device))
93+
expected_lines_start = ['Ring parameters for ',
94+
'Pre-set maximums:', 'RX:', 'RX Mini:',
95+
'RX Jumbo:', 'TX', 'Current hardware settings:',
96+
'RX:', 'RX Mini:', 'RX Jumbo:', 'TX:']
97+
lines = self._output().split('\n')
98+
for expected_start, line in zip(expected_lines_start, lines):
99+
self.assertTrue(line.startswith(expected_start))
100+
101+
def test_show_coalesce_eth(self):
102+
self.assertIsNone(peth.show_coalesce(device))
103+
expected_lines_start = ['Coalesce parameters for',
104+
'Adaptive RX:',
105+
'stats-block-usecs:',
106+
'sample-interval:',
107+
'pkt-rate-low:',
108+
'pkt-rate-high:',
109+
'rx-usecs:',
110+
'rx-frames:',
111+
'rx-usecs-irq:',
112+
'rx-frames-irq:',
113+
'tx-usecs:',
114+
'tx-frames:',
115+
'tx-usecs-irq:',
116+
'tx-frames-irq:',
117+
'rx-usecs-low:',
118+
'rx-frame-low:',
119+
'tx-usecs-low:',
120+
'tx-frame-low:',
121+
'rx-usecs-high:',
122+
'rx-frame-high:',
123+
'tx-usecs-high:',
124+
'tx-frame-high:']
125+
126+
lines = self._output().split('\n')
127+
for expected_start, line in zip(expected_lines_start, lines):
128+
self.assertTrue(line.startswith(expected_start))
129+
130+
def test_show_offload_eth(self):
131+
self.assertIsNone(peth.show_offload(device))
132+
expected_lines_start = ['scatter-gather:',
133+
'tcp segmentation offload:',
134+
'udp fragmentation offload:',
135+
'generic segmentation offload:']
136+
lines = self._output().split('\n')
137+
for expected_start, line in zip(expected_lines_start, lines):
138+
self.assertTrue(line.startswith(expected_start))
139+
140+
141+
if __name__ == '__main__':
142+
unittest.main()

0 commit comments

Comments
 (0)