Skip to content

Commit

Permalink
Added more UTs, fixed issue surafced by said UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
assrinivasan committed May 21, 2024
1 parent b13ad3f commit 5b4f5d1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
13 changes: 9 additions & 4 deletions sonic_platform_base/sonic_storage/storage_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
try:
import os
import sys
import json
import psutil
except ImportError as e:
raise ImportError (str(e) + "- required module not found")
Expand All @@ -22,7 +21,7 @@ def __init__(self, diskdev):
Args:
Block device path for which we need to get information
"""
self.DISKSTATS_FILE = "/proc/diskstats"

self.storage_disk = os.path.basename(diskdev)
self.fsstats_reads = 0
self.fsstats_writes = 0
Expand All @@ -38,7 +37,10 @@ def get_fs_io_reads(self):
N/A
"""

self.fsstats_reads = int(psutil.disk_io_counters(perdisk=True, nowrap=True)[self.storage_disk].read_count)
try:
self.fsstats_reads = int(psutil.disk_io_counters(perdisk=True, nowrap=True)[self.storage_disk].read_count)
except Exception as ex:
pass

return self.fsstats_reads

Expand All @@ -53,6 +55,9 @@ def get_fs_io_writes(self):
N/A
"""

self.fsstats_writes = psutil.disk_io_counters(perdisk=True, nowrap=True)[self.storage_disk].write_count
try:
self.fsstats_writes = psutil.disk_io_counters(perdisk=True, nowrap=True)[self.storage_disk].write_count
except Exception as ex:
pass

return self.fsstats_writes
2 changes: 0 additions & 2 deletions tests/test_emmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from sonic_platform_base.sonic_storage.emmc import EmmcUtil

sys.modules['psutil'] = mock.MagicMock()

mocked_files = {
'/sys/block/emmctest/device/enhanced_area_offset': '0',
'/sys/block/emmctest/device/life_time': '0x02 0x02',
Expand Down
53 changes: 53 additions & 0 deletions tests/test_storage_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import sys
import types
from mock import MagicMock, patch
#import psutil

from sonic_platform_base.sonic_storage.storage_common import StorageCommon

class DiskIOCounters():
def __init__(self, perdisk=True, nowrap=True):

self.read_count = 18038
self.write_count = 95836

class TestStorageCommon:

@patch('psutil.disk_io_counters', MagicMock(return_value={'sda': DiskIOCounters()}))
def test_get_reads_writes(self):

common_object = StorageCommon('/dev/sda')

reads = common_object.get_fs_io_reads()
writes = common_object.get_fs_io_writes()

assert (reads == 18038)
assert (writes == 95836)


def test_init(self):
common_object = StorageCommon('/dev/sda')

assert common_object.storage_disk == 'sda'
assert common_object.fsstats_reads == 0
assert common_object.fsstats_writes == 0



def test_get_reads_writes_bad_disk(self):
common_object = StorageCommon('/dev/glorp')

with patch('psutil.disk_io_counters', MagicMock()) as mock_psutil_output:

class DiskIOCounters():
def __init__(self, perdisk=True, nowrap=True):
pass

mock_psutil_output.return_value = {'glorp': DiskIOCounters()}
reads = common_object.get_fs_io_reads()
writes = common_object.get_fs_io_writes()

assert (reads == 0)
assert (writes == 0)

46 changes: 46 additions & 0 deletions tests/test_storage_devices.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
from unittest import mock
from pytest import raises

from sonic_platform_base.sonic_storage.storage_devices import StorageDevices

log_identifier = "test_storage_devices"

mocked_basepath = ['loop1', 'mmcblk0', 'loop6', 'mmcblk0boot0', 'loop4', 'loop2', 'loop0', 'loop7', 'mmcblk0boot1', 'sda', 'loop5', 'loop3']
mocked_devices = { 'mmcblk0' : None, 'sda' : None}


class TestStorageDevices:

@mock.patch('os.listdir', mock.MagicMock(return_value=mocked_basepath))
def test_get_storage_devices_none(self):

def mock_factory(self, dummy_key):
return None

with mock.patch.object(StorageDevices, '_storage_device_object_factory', new=mock_factory):
storage = StorageDevices(log_identifier)
assert storage.devices == mocked_devices


@mock.patch('os.listdir', mock.MagicMock(return_value=mocked_basepath))
@mock.patch('sonic_platform_base.sonic_storage.emmc')
@mock.patch('sonic_platform_base.sonic_storage.ssd')
def test_get_storage_devices_mockobjs(self, mock_emmc, mock_ssd):

mock_emmc = mock.MagicMock()
mock_ssd = mock.MagicMock()

def mock_factory(self, dummy_key):
if dummy_key == "mmcblk0": return mock_emmc
else: return mock_ssd

with mock.patch.object(StorageDevices, '_storage_device_object_factory', new=mock_factory):
storage = StorageDevices(log_identifier)

assert (list(storage.devices.keys()) == ['mmcblk0', 'sda'])
assert isinstance(storage.devices['mmcblk0'], mock.MagicMock)
assert isinstance(storage.devices['sda'], mock.MagicMock)
assert (storage.devices['mmcblk0'] == mock_emmc)
assert (storage.devices['sda'] == mock_ssd)

0 comments on commit 5b4f5d1

Please sign in to comment.