Skip to content

Commit

Permalink
Update #73: Added deviceManager to singleton
Browse files Browse the repository at this point in the history
Added the deviceManager to the singleton and update the RAFTUnitTest
classes to allow it's usage. Also created a test to check it is working.
  • Loading branch information
TB-1993 committed Aug 7, 2024
1 parent 65b6ac4 commit 1aec56b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
9 changes: 8 additions & 1 deletion framework/core/raftUnittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
sys.path.append(path)

from framework.core.singleton import SINGLETON
from framework.core.logModule import logModule
from framework.core.utilities import utilities

class RAFTUnitTestCase(unittest.TestCase):
"""
Expand All @@ -67,6 +67,13 @@ def __init__(self, *args, **kwargs):
self.testName = self.__class__.__name__
self.qcId = kwargs.get('qcId', '')
self._singleton.setSummaryLog(self.testName, self.qcId)
self.log = self._singleton.testLog
self.devices = self._singleton.setupDevices(self.log, self.log.logPath)
self.dut = self.devices.getDevice("dut")
self.cpe = self._singleton.getCPEInfo()
if self.cpe is None:
self.log.warn("CPE device not set")
self.utils = utilities(self.log)
super().__init__(*args, **kwargs)

@property
Expand Down
29 changes: 28 additions & 1 deletion framework/core/singleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import os

from framework.core.decodeParams import decodeParams
from framework.core.deviceManager import deviceManager
from framework.core.rackController import rackController
from framework.core.configParser import configParser
from framework.core.logModule import logModule
Expand Down Expand Up @@ -75,18 +76,19 @@ def __new__(cls, log):

if cls._instance is False:
cls._instance = True
cls._verbosity = 1
cls.summaryLog = None
cls.logPath =None
cls.testLog = None
cls.testLogPath = None
cls.devices = None
cls.config = decodeParams(log)
cls._rackController = rackController(cls.config.rackConfig)
cls.deviceConfig = configParser(cls.config.deviceConfig)
cls.deviceConfig.decodeConfig(cls.config.rackConfig)
cls.rack = cls._get_rack()
cls.slotInfo = cls._getSlotInfo()
cls.logConfig = cls.config.rackConfig.get("local").get("log")
cls._verbosity = 1
return cls

@classmethod
Expand Down Expand Up @@ -234,5 +236,30 @@ def setSummaryLog(cls, testName:str, qcId:str=''):
logFilename = "test-{}.log".format(cls.summaryLog.summaryTestTotal)
cls.testLog.setFilename(cls.testLogPath, logFilename)

@classmethod
def setupDevices(cls, log: logModule, logPath:str):
"""Setup the deviceManager for the devices in the current slot under test.
Args:
log (logModule): Log Module for the current test.
logPath (str): Path of logging output for the current test.
Returns:
deviceManager : deviceManager object for all devices in the slot in use by the
current test.
"""
if cls.devices is None:
cls.devices = deviceManager(cls.slotInfo.config.get("devices"), log, logPath)
return cls.devices

@classmethod
def getCPEInfo(cls):
"""Get the device config information for the current CPE device.
Returns:
dict: Dictionary of information for the current CPE device.
"""
cpePlatform = cls.slotInfo.getPlatform()
return cls.deviceConfig.getCPEEntryViaPlatform(cpePlatform)

SINGLETON = Singleton(log=None)
49 changes: 49 additions & 0 deletions tests/singleton_tests/test_deviceManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

import os
import sys

try:
from yaml import load, SafeLoader as Loader
except:
from yaml import load, CLoader as Loader

# Add the directory containing the framework package to the Python path
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))
sys.path.append(path)

from framework.core.raftUnittest import RAFTUnitTestCase, RAFTUnitTestMain
from framework.core.commandModules.consoleInterface import consoleInterface

class TestDeviceManager(RAFTUnitTestCase):

def test_devicesList(self):
"""
Test the list of devices set up in the device manager is correct.
"""
device_list = list(self.devices.devices.keys())
expected_device_list_dicts = self._singleton.slotInfo.config.get('devices',[])
expected_device_list = []
for device_dict in expected_device_list_dicts:
expected_device_list += device_dict.keys()
self.assertEqual(expected_device_list, device_list,f'Expected to find dut in device list. Device list contains: [{", ".join(device_list)}]')

def test_DUTInfo(self):
"""
Test the information of the dut in the slot is correct.
"""
expectedName = 'dut'
self.assertEqual(self.dut.deviceName, expectedName, f'Expected device name to be {expectedName}. Got [{self.dut.deviceName}]')
expectedConfig = self._singleton.slotInfo.config.get('devices',[{'dut':{}}])[0]
self.assertEqual(expectedConfig, self.dut.rawConfig, 'DUT device config does not match expected config.')

def test_getSessionForDUT(self):
"""
Test the console session for the dut has been instantiated.
"""
session = self.dut.getConsoleSession()
self.assertIsInstance(session,consoleInterface)


if __name__ == '__main__':
RAFTUnitTestMain()

0 comments on commit 1aec56b

Please sign in to comment.