Skip to content

Commit 012dc39

Browse files
authored
Add unit test infrastructure (sonic-net#139)
Add infrastructure for running unit tests
1 parent a659219 commit 012dc39

7 files changed

+87
-2
lines changed

.gitignore

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
*.pyc
22
*/__pycache__/
3+
tests/*.pyc
4+
tests/__pycache__/
5+
6+
# Distribution / packaging
37
build/
48
sonic_platform_common.egg-info/
5-
.cache
9+
.cache
10+
11+
# Unit test / coverage reports
12+
.coverage
13+
htmlcov/

pytest.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
addopts = --cov=sonic_platform_base --cov-report html

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[aliases]
2+
test=pytest

setup.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,14 @@
3434
'redis',
3535
'sonic-py-common'
3636
],
37-
setup_requires= [
37+
setup_requires = [
38+
'pytest-runner',
3839
'wheel'
3940
],
41+
tests_require = [
42+
'pytest',
43+
'pytest-cov',
44+
],
4045
classifiers=[
4146
'Development Status :: 3 - Alpha',
4247
'Environment :: Plugins',

tests/__init__.py

Whitespace-only changes.

tests/sfputilhelper_test.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import os
2+
import sys
3+
4+
import pytest
5+
try:
6+
import sonic_platform_base.sonic_sfp.sfputilhelper
7+
except Exception as e:
8+
print("Failed to load sonic_platform_base.sonic_sfp.sfputilhelper due to {}".format(repr(e)))
9+
10+
11+
@pytest.fixture(scope="class")
12+
def setup_class(request):
13+
# Configure the setup
14+
test_dir = os.path.dirname(os.path.realpath(__file__))
15+
request.cls.port_config = os.path.join(
16+
test_dir, 't0-sample-port-config.ini')
17+
18+
request.cls.port_config = sonic_platform_base.sonic_sfp.sfputilhelper.SfpUtilHelper()
19+
20+
21+
@pytest.mark.usefixtures("setup_class")
22+
class TestSfpUtilHelper(object):
23+
24+
platform_sfputil = None
25+
port_config = None
26+
27+
def test_read_port_mappings(self):
28+
29+
try:
30+
platform_sfputil.read_porttab_mappings(self.port_config, 0)
31+
except Exception as e:
32+
print("Failed to read port tab mappings to {}".format(repr(e)))
33+
34+
PORT_LIST = ["Ethernet0",
35+
"Ethernet4",
36+
"Ethernet8",
37+
"Ethernet12",
38+
"Ethernet16",
39+
"Ethernet20",
40+
"Ethernet24",
41+
"Ethernet28",
42+
"Ethernet32",
43+
"Ethernet36",
44+
"Ethernet40",
45+
"Ethernet44",
46+
"Ethernet48"]
47+
48+
if self.platform_sfputil is not None:
49+
logical_port_list = self.platform_sfputil.logical
50+
assert len(logical_port_name) == len(self.port_list)
51+
for logical_port_name in logical_port_list:
52+
assert logical_port_name in PORT_LIST
53+
else:
54+
print("platform_sfputil is None, cannot read Ports")

tests/t0-sample-port-config.ini

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# name lanes alias
2+
Ethernet0 29,30,31,32 fortyGigE0/0
3+
Ethernet4 25,26,27,28 fortyGigE0/4
4+
Ethernet8 37,38,39,40 fortyGigE0/8
5+
Ethernet12 33,34,35,36 fortyGigE0/12
6+
Ethernet16 41,42,43,44 fortyGigE0/16
7+
Ethernet20 45,46,47,48 fortyGigE0/20
8+
Ethernet24 5,6,7,8 fortyGigE0/24
9+
Ethernet28 1,2,3,4 fortyGigE0/28
10+
Ethernet32 9,10,11,12 fortyGigE0/32
11+
Ethernet36 13,14,15,16 fortyGigE0/36
12+
Ethernet40 21,22,23,24 fortyGigE0/40
13+
Ethernet44 17,18,19,20 fortyGigE0/44
14+
Ethernet48 49,50,51,52 fortyGigE0/48

0 commit comments

Comments
 (0)