Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop _virtual from hard coding the 'virtual' key. #55534

Merged
merged 3 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')