diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2022-01-16 00:04:42 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2022-01-16 00:04:42 +0100 |
commit | e094d9fa1f777b20967cdc3bca24b001e7b130f8 (patch) | |
tree | b5873f9cf95b1266263a530d689e2d2972b0aa37 | |
parent | 54f79caab48d80b2303e938b647e359e28014e3f (diff) | |
download | psutil-e094d9fa1f777b20967cdc3bca24b001e7b130f8.tar.gz |
basic unit tests
Signed-off-by: Giampaolo Rodola <g.rodola@gmail.com>
-rw-r--r-- | psutil/__init__.py | 7 | ||||
-rw-r--r-- | psutil/_virt.py | 46 | ||||
-rw-r--r-- | psutil/tests/__init__.py | 3 | ||||
-rwxr-xr-x | psutil/tests/test_contracts.py | 9 | ||||
-rwxr-xr-x | psutil/tests/test_system.py | 6 |
5 files changed, 50 insertions, 21 deletions
diff --git a/psutil/__init__.py b/psutil/__init__.py index 2423fa49..cdebd155 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -2365,12 +2365,13 @@ def users(): if LINUX or WINDOWS: def virtualization(): - """Tries to guess if we're running in a virtual environment. + """Try different techniques to guess if we're running in a + virtual environment (either a container or a virtual machine). If so, it returns the virtualization technology being used as a string, else it returns an empty string. """ - from psutil._virt import virtualization - return virtualization() + from psutil._virt import detect + return detect() __all__.append("virtualization") diff --git a/psutil/_virt.py b/psutil/_virt.py index ee2a8204..9ba58c26 100644 --- a/psutil/_virt.py +++ b/psutil/_virt.py @@ -302,7 +302,7 @@ if LINUX: elif b"IBM/S390" in value: return VIRTUALIZATION_IBM_SYSTEMZ - def virtualization(): + def get_functions(): # There is a distinction between containers and VMs. # A "container" is typically "shared kernel virtualization", e.g. LXC. # A "vm" is "full hardware virtualization", e.g. VirtualBox. @@ -335,21 +335,7 @@ if LINUX: # vms others vmothers.ask_proc_cpuinfo, ] - retval = None - for func in funcs: - func_name = "%s.%s" % ( - func.__self__.__class__.__name__, func.__name__) - debug("trying method %r" % func_name) - try: - retval = func() - if retval: - break - except (IOError, OSError) as err: - debug(err) - except (AccessDenied, NoSuchProcess) as err: - debug(err) - - return retval or "" + return funcs # ===================================================================== # --- Windows @@ -358,10 +344,36 @@ if LINUX: elif WINDOWS: from . import _psutil_windows as cext - def virtualization(): + def ask_cpuid(): vendor = cext.__cpuid() if vendor is not None: if vendor in CPUID_VENDOR_TABLE: return CPUID_VENDOR_TABLE[vendor] else: return VIRTUALIZATION_VM_OTHER + + def get_functions(): + return [ + ask_cpuid, + ] + +# --- + + +def detect(): + funcs = get_functions() + retval = None + for func in funcs: + func_name = "%s.%s" % ( + func.__self__.__class__.__name__, func.__name__) + debug("trying method %r" % func_name) + try: + retval = func() + if retval: + break + except (IOError, OSError) as err: + debug(err) + except (AccessDenied, NoSuchProcess) as err: + debug(err) + + return retval or "" diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 21bb3e61..a8b80d49 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -87,7 +87,7 @@ __all__ = [ "HAS_CPU_AFFINITY", "HAS_CPU_FREQ", "HAS_ENVIRON", "HAS_PROC_IO_COUNTERS", "HAS_IONICE", "HAS_MEMORY_MAPS", "HAS_PROC_CPU_NUM", "HAS_RLIMIT", "HAS_SENSORS_BATTERY", "HAS_BATTERY", "HAS_SENSORS_FANS", - "HAS_SENSORS_TEMPERATURES", "HAS_MEMORY_FULL_INFO", + "HAS_SENSORS_TEMPERATURES", "HAS_MEMORY_FULL_INFO", "HAS_VIRTUALIZATION", # subprocesses 'pyrun', 'terminate', 'reap_children', 'spawn_testproc', 'spawn_zombie', 'spawn_children_pair', @@ -192,6 +192,7 @@ except Exception: HAS_SENSORS_FANS = hasattr(psutil, "sensors_fans") HAS_SENSORS_TEMPERATURES = hasattr(psutil, "sensors_temperatures") HAS_THREADS = hasattr(psutil.Process, "threads") +HAS_VIRTUALIZATION = hasattr(psutil, "virtualization") SKIP_SYSCONS = (MACOS or AIX) and os.getuid() != 0 # --- misc diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py index 7401cc15..9c4e6c9b 100755 --- a/psutil/tests/test_contracts.py +++ b/psutil/tests/test_contracts.py @@ -40,6 +40,7 @@ from psutil.tests import HAS_CPU_FREQ from psutil.tests import HAS_NET_IO_COUNTERS from psutil.tests import HAS_SENSORS_FANS from psutil.tests import HAS_SENSORS_TEMPERATURES +from psutil.tests import HAS_VIRTUALIZATION from psutil.tests import PYPY from psutil.tests import SKIP_SYSCONS from psutil.tests import VALID_PROC_STATUSES @@ -147,6 +148,9 @@ class TestAvailSystemAPIs(PsutilTestCase): self.assertEqual(hasattr(psutil, "sensors_battery"), LINUX or WINDOWS or FREEBSD or MACOS) + def test_virtualization(self): + self.assertEqual(hasattr(psutil, "virtualization"), LINUX or WINDOWS) + class TestAvailProcessAPIs(PsutilTestCase): @@ -327,6 +331,11 @@ class TestSystemAPITypes(PsutilTestCase): self.assertIsInstance(user.host, (str, type(None))) self.assertIsInstance(user.pid, (int, type(None))) + @unittest.skipIf(not HAS_VIRTUALIZATION, "not supported") + def test_virtualization(self): + ret = psutil.virtualization() + self.assertIsInstance(ret, str) + class TestProcessWaitType(PsutilTestCase): diff --git a/psutil/tests/test_system.py b/psutil/tests/test_system.py index ed328d01..620153fa 100755 --- a/psutil/tests/test_system.py +++ b/psutil/tests/test_system.py @@ -42,6 +42,7 @@ from psutil.tests import HAS_NET_IO_COUNTERS from psutil.tests import HAS_SENSORS_BATTERY from psutil.tests import HAS_SENSORS_FANS from psutil.tests import HAS_SENSORS_TEMPERATURES +from psutil.tests import HAS_VIRTUALIZATION from psutil.tests import IS_64BIT from psutil.tests import PYPY from psutil.tests import UNICODE_SUFFIX @@ -217,6 +218,11 @@ class TestMiscAPIs(PsutilTestCase): else: psutil.Process(user.pid) + @unittest.skipIf(not HAS_VIRTUALIZATION, "not suported") + def test_virtualization(self): + ret = psutil.virtualization() + self.assertIsInstance(ret, str) + def test_test(self): # test for psutil.test() function stdout = sys.stdout |