Skip to content

Commit

Permalink
[xcvrd] add unit test infrastructure and unit tests for xcvrd (#133)
Browse files Browse the repository at this point in the history
Summary:
This PR provides the necessary infrastructure to add pytest support and integration in sonic-xcvrd submodule.
This PR also adds unit tests for xcvrd daemon.
What is the motivation for this PR?
To add the pytest unittest support in sonic-platform-daemon, sonix-xcvrd daemon as well as add some unit tests
Signed-off-by: vaibhav-dahiya <vdahiya@microsoft.com>
  • Loading branch information
vdahiya12 authored Dec 15, 2020
1 parent 8555077 commit b0be7ca
Show file tree
Hide file tree
Showing 8 changed files with 532 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sonic-xcvrd/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --cov=xcvrd --cov-report html
2 changes: 2 additions & 0 deletions sonic-xcvrd/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test=pytest
4 changes: 4 additions & 0 deletions sonic-xcvrd/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
setup_requires=[
'wheel'
],
tests_require=[
'pytest',
'pytest-cov',
],
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: No Input/Output (Daemon)',
Expand Down
Empty file added sonic-xcvrd/tests/__init__.py
Empty file.
262 changes: 262 additions & 0 deletions sonic-xcvrd/tests/mock_platform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
class MockDevice:
def __init__(self):
self.name = None
self.presence = True
self.model = 'FAN Model'
self.serial = 'Fan Serial'

def get_name(self):
return self.name

def get_presence(self):
return self.presence

def get_model(self):
return self.model

def get_serial(self):
return self.serial

def get_position_in_parent(self):
return 1

def is_replaceable(self):
return True

def get_status(self):
return True


class MockFan(MockDevice):
STATUS_LED_COLOR_RED = 'red'
STATUS_LED_COLOR_GREEN = 'green'

def __init__(self):
MockDevice.__init__(self)
self.speed = 20
self.speed_tolerance = 20
self.target_speed = 20
self.status = True
self.direction = 'intake'
self.led_status = 'red'

def get_speed(self):
return self.speed

def get_speed_tolerance(self):
return self.speed_tolerance

def get_target_speed(self):
return self.target_speed

def get_status(self):
return self.status

def get_direction(self):
return self.direction

def get_status_led(self):
return self.led_status

def set_status_led(self, value):
self.led_status = value

def make_under_speed(self):
self.speed = 1
self.target_speed = 2
self.speed_tolerance = 0

def make_over_speed(self):
self.speed = 2
self.target_speed = 1
self.speed_tolerance = 0

def make_normal_speed(self):
self.speed = 1
self.target_speed = 1
self.speed_tolerance = 0


class MockErrorFan(MockFan):
def get_speed(self):
raise Exception('Fail to get speed')


class MockPsu(MockDevice):
def __init__(self):
MockDevice.__init__(self)
self.fan_list = []

def get_all_fans(self):
return self.fan_list


class MockFanDrawer(MockDevice):
def __init__(self, index):
MockDevice.__init__(self)
self.name = 'FanDrawer {}'.format(index)
self.fan_list = []
self.led_status = 'red'

def get_name(self):
return self.name

def get_all_fans(self):
return self.fan_list

def get_status_led(self):
return self.led_status

def set_status_led(self, value):
self.led_status = value


class MockThermal(MockDevice):
def __init__(self, index=None):
MockDevice.__init__(self)
self.name = None
self.name = 'Thermal {}'.format(index) if index != None else None
self.temperature = 2
self.minimum_temperature = 1
self.maximum_temperature = 5
self.high_threshold = 3
self.low_threshold = 1
self.high_critical_threshold = 4
self.low_critical_threshold = 0

def get_name(self):
return self.name

def get_temperature(self):
return self.temperature

def get_minimum_recorded(self):
return self.minimum_temperature

def get_maximum_recorded(self):
return self.maximum_temperature

def get_high_threshold(self):
return self.high_threshold

def get_low_threshold(self):
return self.low_threshold

def get_high_critical_threshold(self):
return self.high_critical_threshold

def get_low_critical_threshold(self):
return self.low_critical_threshold

def make_over_temper(self):
self.high_threshold = 2
self.temperature = 3
self.low_threshold = 1

def make_under_temper(self):
self.high_threshold = 3
self.temperature = 1
self.low_threshold = 2

def make_normal_temper(self):
self.high_threshold = 3
self.temperature = 2
self.low_threshold = 1


class MockErrorThermal(MockThermal):
def get_temperature(self):
raise Exception('Fail to get temperature')


class MockChassis:
def __init__(self):
self.fan_list = []
self.psu_list = []
self.thermal_list = []
self.fan_drawer_list = []
self.sfp_list = []
self.is_chassis_system = False

def get_all_fans(self):
return self.fan_list

def get_all_psus(self):
return self.psu_list

def get_all_thermals(self):
return self.thermal_list

def get_all_fan_drawers(self):
return self.fan_drawer_list

def get_all_sfps(self):
return self.sfp_list

def get_num_thermals(self):
return len(self.thermal_list)

def make_absence_fan(self):
fan = MockFan()
fan.presence = False
fan_drawer = MockFanDrawer(len(self.fan_drawer_list))
fan_drawer.fan_list.append(fan)
self.fan_list.append(fan)
self.fan_drawer_list.append(fan_drawer)

def make_fault_fan(self):
fan = MockFan()
fan.status = False
fan_drawer = MockFanDrawer(len(self.fan_drawer_list))
fan_drawer.fan_list.append(fan)
self.fan_list.append(fan)
self.fan_drawer_list.append(fan_drawer)

def make_under_speed_fan(self):
fan = MockFan()
fan.make_under_speed()
fan_drawer = MockFanDrawer(len(self.fan_drawer_list))
fan_drawer.fan_list.append(fan)
self.fan_list.append(fan)
self.fan_drawer_list.append(fan_drawer)

def make_over_speed_fan(self):
fan = MockFan()
fan.make_over_speed()
fan_drawer = MockFanDrawer(len(self.fan_drawer_list))
fan_drawer.fan_list.append(fan)
self.fan_list.append(fan)
self.fan_drawer_list.append(fan_drawer)

def make_error_fan(self):
fan = MockErrorFan()
fan_drawer = MockFanDrawer(len(self.fan_drawer_list))
fan_drawer.fan_list.append(fan)
self.fan_list.append(fan)
self.fan_drawer_list.append(fan_drawer)

def make_over_temper_thermal(self):
thermal = MockThermal()
thermal.make_over_temper()
self.thermal_list.append(thermal)

def make_under_temper_thermal(self):
thermal = MockThermal()
thermal.make_under_temper()
self.thermal_list.append(thermal)

def make_error_thermal(self):
thermal = MockErrorThermal()
self.thermal_list.append(thermal)

def is_modular_chassis(self):
return self.is_chassis_system

def set_modular_chassis(self, is_true):
self.is_chassis_system = is_true

def set_my_slot(self, my_slot):
self.my_slot = my_slot

def get_my_slot(self):
return self.my_slot
31 changes: 31 additions & 0 deletions sonic-xcvrd/tests/mock_swsscommon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
STATE_DB = ''
CHASSIS_STATE_DB = ''


class Table:
def __init__(self, db, table_name):
self.table_name = table_name
self.mock_dict = {}

def _del(self, key):
if key in self.mock_dict:
del self.mock_dict[key]
pass

def set(self, key, fvs):
self.mock_dict[key] = fvs
pass

def get(self, key):
if key in self.mock_dict:
return self.mock_dict[key]
return None

def get_size(self):
return (len(self.mock_dict))


class FieldValuePairs:
def __init__(self, fvs):
self.fv_dict = dict(fvs)
pass
14 changes: 14 additions & 0 deletions sonic-xcvrd/tests/t0-sample-port-config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# name lanes alias
Ethernet0 29,30,31,32 fortyGigE0/0
Ethernet4 25,26,27,28 fortyGigE0/4
Ethernet8 37,38,39,40 fortyGigE0/8
Ethernet12 33,34,35,36 fortyGigE0/12
Ethernet16 41,42,43,44 fortyGigE0/16
Ethernet20 45,46,47,48 fortyGigE0/20
Ethernet24 5,6,7,8 fortyGigE0/24
Ethernet28 1,2,3,4 fortyGigE0/28
Ethernet32 9,10,11,12 fortyGigE0/32
Ethernet36 13,14,15,16 fortyGigE0/36
Ethernet40 21,22,23,24 fortyGigE0/40
Ethernet44 17,18,19,20 fortyGigE0/44
Ethernet48 49,50,51,52 fortyGigE0/48
Loading

0 comments on commit b0be7ca

Please sign in to comment.