Skip to content

Commit

Permalink
Merge pull request #55534 from cmcmarrow/virtual_grain_fix
Browse files Browse the repository at this point in the history
Stop _virtual from hard coding the 'virtual' key.
  • Loading branch information
dwoz authored Dec 7, 2019
2 parents 825a3b0 + 9b28ddd commit 718e7e6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion salt/grains/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/grains/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import socket
import textwrap
import platform

# Import Salt Testing Libs
try:
Expand All @@ -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
Expand Down Expand Up @@ -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')

0 comments on commit 718e7e6

Please sign in to comment.