Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
[D] adding MagicMock for psutil.cpu_freq() to be testable on VM #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyxsys committed Jan 8, 2022
1 parent 867da7f commit b15ddd4
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions daemon/test/test_report_module.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import datetime
import sys
import sys, os
import psutil
import unittest
from unittest.case import expectedFailure
from unittest.mock import MagicMock
from psutil._common import scpufreq

# Include src directory for imports
sys.path.append('../')
Expand All @@ -17,8 +18,21 @@ class TestSystemReportClass(unittest.TestCase):
}
}

@classmethod
def setUpClass(cls):
#Change return value for psutil.cpu_freq() incase of linux VM
#See https://github.com/giampaolo/psutil/pull/1493
cpufreqExistsAndHasContents = len(os.listdir('/sys/devices/system/cpu/cpufreq/')) == 0 if os.path.isdir('/sys/devices/system/cpu/cpufreq/') else False
cpu0ExistsAndHasCpuFreq = os.path.exists('/sys/devices/system/cpu/cpu0/cpufreq') if os.path.isdir('/sys/devices/system/cpu0') else False

if psutil.LINUX & (not cpufreqExistsAndHasContents) & (not cpu0ExistsAndHasCpuFreq):
psutil.cpu_freq = MagicMock(return_value=scpufreq(current=1, min=1, max=1))

def setUp(self):
self.test_report=SysReport()




def tearDown(self):
del self.test_report
Expand Down

1 comment on commit b15ddd4

@Pyxsys
Copy link
Owner Author

@Pyxsys Pyxsys commented on b15ddd4 Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psutil function cpu_freq() does not fetch info for linux VM as defined in giampaolo/psutil#1493.
MagicMock added due simulate non-VM environment for our tests.

Please sign in to comment.