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..ac05f7211507 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={'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')