From 205beb07aca233af08cc62edb93edfa2cd010301 Mon Sep 17 00:00:00 2001 From: cmcmarrow Date: Thu, 5 Dec 2019 11:55:13 -0700 Subject: [PATCH 1/2] Stop _virtual from hardcoding the 'virtual' key. --- salt/grains/core.py | 3 ++- tests/unit/grains/test_core.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/salt/grains/core.py b/salt/grains/core.py index 34547b46b4c7..d850b77d09e7 100644 --- a/salt/grains/core.py +++ b/salt/grains/core.py @@ -680,7 +680,8 @@ def _virtual(osdata): # Provides: # virtual # virtual_subtype - grains = {'virtual': 'physical'} + + grains = {'virtual': osdata.get('virtual', 'physical')} # Skip the below loop on platforms which have none of the desired cmds # This is a temporary measure until we can write proper virtual hardware diff --git a/tests/unit/grains/test_core.py b/tests/unit/grains/test_core.py index 2d3fac62d6a0..726ae1464dfb 100644 --- a/tests/unit/grains/test_core.py +++ b/tests/unit/grains/test_core.py @@ -9,6 +9,7 @@ import os import socket import textwrap +import platform # Import Salt Testing Libs try: @@ -33,6 +34,8 @@ import salt.utils.network import salt.utils.platform import salt.utils.path +import salt.modules.cmdmod +import salt.modules.smbios import salt.grains.core as core # Import 3rd-party libs @@ -1263,3 +1266,49 @@ def test_cwd_is_cwd(self): finally: # change back to original directory os.chdir(cwd) + + def test_virtual_set_virtual_grain(self): + osdata = {} + + (osdata['kernel'], osdata['nodename'], + osdata['kernelrelease'], osdata['kernelversion'], osdata['cpuarch'], _) = platform.uname() + + with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run, + 'cmd.run_all': salt.modules.cmdmod.run_all, + 'cmd.retcode': salt.modules.cmdmod.retcode, + 'smbios.get': salt.modules.smbios.get}): + + virtual_grains = core._virtual(osdata) + + self.assertIn('virtual', virtual_grains) + + def test_virtual_has_virtual_grain(self): + osdata = {'virtual': 'something'} + + (osdata['kernel'], osdata['nodename'], + osdata['kernelrelease'], osdata['kernelversion'], osdata['cpuarch'], _) = platform.uname() + + with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run, + 'cmd.run_all': salt.modules.cmdmod.run_all, + 'cmd.retcode': salt.modules.cmdmod.retcode, + 'smbios.get': salt.modules.smbios.get}): + + virtual_grains = core._virtual(osdata) + + self.assertIn('virtual', virtual_grains) + self.assertNotEqual(virtual_grains['virtual'], 'physical') + + @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows') + def test_osdata_virtual_key_win(self): + with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run, + 'cmd.run_all': salt.modules.cmdmod.run_all, + 'cmd.retcode': salt.modules.cmdmod.retcode, + 'smbios.get': salt.modules.smbios.get}): + with patch.object(core, + '_windows_virtual', + return_value=MagicMock(return_value={'virtual': 'something'})) as _windows_virtual: + _windows_virtual.assert_called_once() + osdata_grains = core.os_data() + + self.assertIn('virtual', osdata_grains) + self.assertNotEqual(osdata_grains['virtual'], 'physical') From be2fcc1a971652fa1ed7001b8298ad7b632657ba Mon Sep 17 00:00:00 2001 From: Charles McMarrow <48689152+cmcmarrow@users.noreply.github.com> Date: Fri, 6 Dec 2019 11:50:44 -0700 Subject: [PATCH 2/2] fix test_osdata_virtual_key_win --- tests/unit/grains/test_core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/grains/test_core.py b/tests/unit/grains/test_core.py index 726ae1464dfb..ac05f7211507 100644 --- a/tests/unit/grains/test_core.py +++ b/tests/unit/grains/test_core.py @@ -1306,9 +1306,9 @@ def test_osdata_virtual_key_win(self): 'smbios.get': salt.modules.smbios.get}): with patch.object(core, '_windows_virtual', - return_value=MagicMock(return_value={'virtual': 'something'})) as _windows_virtual: - _windows_virtual.assert_called_once() + return_value={'virtual': 'something'}) as _windows_virtual: osdata_grains = core.os_data() + _windows_virtual.assert_called_once() self.assertIn('virtual', osdata_grains) self.assertNotEqual(osdata_grains['virtual'], 'physical')