test_core.py 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Erik Johnson <erik@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. import os
  9. import socket
  10. import textwrap
  11. import platform
  12. # Import Salt Testing Libs
  13. try:
  14. import pytest
  15. except ImportError as import_error:
  16. pytest = None
  17. from tests.support.mixins import LoaderModuleMockMixin
  18. from tests.support.unit import TestCase, skipIf
  19. from tests.support.mock import (
  20. Mock,
  21. MagicMock,
  22. patch,
  23. mock_open,
  24. )
  25. # Import Salt Libs
  26. import salt.utils.dns
  27. import salt.utils.files
  28. import salt.utils.network
  29. import salt.utils.platform
  30. import salt.utils.path
  31. import salt.modules.cmdmod
  32. import salt.modules.smbios
  33. import salt.grains.core as core
  34. # Import 3rd-party libs
  35. from salt.ext import six
  36. from salt._compat import ipaddress
  37. log = logging.getLogger(__name__)
  38. # Globals
  39. IPv4Address = ipaddress.IPv4Address
  40. IPv6Address = ipaddress.IPv6Address
  41. IP4_LOCAL = '127.0.0.1'
  42. IP4_ADD1 = '10.0.0.1'
  43. IP4_ADD2 = '10.0.0.2'
  44. IP6_LOCAL = '::1'
  45. IP6_ADD1 = '2001:4860:4860::8844'
  46. IP6_ADD2 = '2001:4860:4860::8888'
  47. IP6_ADD_SCOPE = 'fe80::6238:e0ff:fe06:3f6b%enp2s0'
  48. OS_RELEASE_DIR = os.path.join(os.path.dirname(__file__), "os-releases")
  49. SOLARIS_DIR = os.path.join(os.path.dirname(__file__), 'solaris')
  50. @skipIf(not pytest, False)
  51. class CoreGrainsTestCase(TestCase, LoaderModuleMockMixin):
  52. '''
  53. Test cases for core grains
  54. '''
  55. def setup_loader_modules(self):
  56. return {core: {}}
  57. @patch("os.path.isfile")
  58. def test_parse_etc_os_release(self, path_isfile_mock):
  59. path_isfile_mock.side_effect = lambda x: x == "/usr/lib/os-release"
  60. with salt.utils.files.fopen(os.path.join(OS_RELEASE_DIR, "ubuntu-17.10")) as os_release_file:
  61. os_release_content = os_release_file.read()
  62. with patch("salt.utils.files.fopen", mock_open(read_data=os_release_content)):
  63. os_release = core._parse_os_release(
  64. '/etc/os-release',
  65. '/usr/lib/os-release')
  66. self.assertEqual(os_release, {
  67. "NAME": "Ubuntu",
  68. "VERSION": "17.10 (Artful Aardvark)",
  69. "ID": "ubuntu",
  70. "ID_LIKE": "debian",
  71. "PRETTY_NAME": "Ubuntu 17.10",
  72. "VERSION_ID": "17.10",
  73. "HOME_URL": "https://www.ubuntu.com/",
  74. "SUPPORT_URL": "https://help.ubuntu.com/",
  75. "BUG_REPORT_URL": "https://bugs.launchpad.net/ubuntu/",
  76. "PRIVACY_POLICY_URL": "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
  77. "VERSION_CODENAME": "artful",
  78. "UBUNTU_CODENAME": "artful",
  79. })
  80. def test_parse_cpe_name_wfn(self):
  81. '''
  82. Parse correct CPE_NAME data WFN formatted
  83. :return:
  84. '''
  85. for cpe, cpe_ret in [('cpe:/o:opensuse:leap:15.0',
  86. {'phase': None, 'version': '15.0', 'product': 'leap',
  87. 'vendor': 'opensuse', 'part': 'operating system'}),
  88. ('cpe:/o:vendor:product:42:beta',
  89. {'phase': 'beta', 'version': '42', 'product': 'product',
  90. 'vendor': 'vendor', 'part': 'operating system'})]:
  91. ret = core._parse_cpe_name(cpe)
  92. for key in cpe_ret:
  93. assert key in ret
  94. assert cpe_ret[key] == ret[key]
  95. def test_parse_cpe_name_v23(self):
  96. '''
  97. Parse correct CPE_NAME data v2.3 formatted
  98. :return:
  99. '''
  100. for cpe, cpe_ret in [('cpe:2.3:o:microsoft:windows_xp:5.1.601:beta:*:*:*:*:*:*',
  101. {'phase': 'beta', 'version': '5.1.601', 'product': 'windows_xp',
  102. 'vendor': 'microsoft', 'part': 'operating system'}),
  103. ('cpe:2.3:h:corellian:millenium_falcon:1.0:*:*:*:*:*:*:*',
  104. {'phase': None, 'version': '1.0', 'product': 'millenium_falcon',
  105. 'vendor': 'corellian', 'part': 'hardware'}),
  106. ('cpe:2.3:*:dark_empire:light_saber:3.0:beta:*:*:*:*:*:*',
  107. {'phase': 'beta', 'version': '3.0', 'product': 'light_saber',
  108. 'vendor': 'dark_empire', 'part': None})]:
  109. ret = core._parse_cpe_name(cpe)
  110. for key in cpe_ret:
  111. assert key in ret
  112. assert cpe_ret[key] == ret[key]
  113. def test_parse_cpe_name_broken(self):
  114. '''
  115. Parse broken CPE_NAME data
  116. :return:
  117. '''
  118. for cpe in ['cpe:broken', 'cpe:broken:in:all:ways:*:*:*:*',
  119. 'cpe:x:still:broken:123', 'who:/knows:what:is:here']:
  120. assert core._parse_cpe_name(cpe) == {}
  121. def test_missing_os_release(self):
  122. with patch('salt.utils.files.fopen', mock_open(read_data={})):
  123. os_release = core._parse_os_release('/etc/os-release', '/usr/lib/os-release')
  124. self.assertEqual(os_release, {})
  125. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  126. def test__windows_platform_data(self):
  127. grains = core._windows_platform_data()
  128. keys = ['biosversion',
  129. 'osrelease',
  130. 'kernelrelease',
  131. 'motherboard',
  132. 'serialnumber',
  133. 'timezone',
  134. 'manufacturer',
  135. 'kernelversion',
  136. 'osservicepack',
  137. 'virtual',
  138. 'productname',
  139. 'osfullname',
  140. 'osmanufacturer',
  141. 'osversion',
  142. 'windowsdomain']
  143. for key in keys:
  144. self.assertIn(key, grains)
  145. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  146. def test_gnu_slash_linux_in_os_name(self):
  147. '''
  148. Test to return a list of all enabled services
  149. '''
  150. _path_exists_map = {
  151. '/proc/1/cmdline': False
  152. }
  153. _path_isfile_map = {}
  154. _cmd_run_map = {
  155. 'dpkg --print-architecture': 'amd64',
  156. }
  157. path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
  158. path_isfile_mock = MagicMock(
  159. side_effect=lambda x: _path_isfile_map.get(x, False)
  160. )
  161. cmd_run_mock = MagicMock(
  162. side_effect=lambda x: _cmd_run_map[x]
  163. )
  164. empty_mock = MagicMock(return_value={})
  165. orig_import = __import__
  166. if six.PY2:
  167. built_in = '__builtin__'
  168. else:
  169. built_in = 'builtins'
  170. def _import_mock(name, *args):
  171. if name == 'lsb_release':
  172. raise ImportError('No module named lsb_release')
  173. return orig_import(name, *args)
  174. # - Skip the first if statement
  175. # - Skip the selinux/systemd stuff (not pertinent)
  176. # - Skip the init grain compilation (not pertinent)
  177. # - Ensure that lsb_release fails to import
  178. # - Skip all the /etc/*-release stuff (not pertinent)
  179. # - Mock linux_distribution to give us the OS name that we want
  180. # - Make a bunch of functions return empty dicts, we don't care about
  181. # these grains for the purposes of this test.
  182. # - Mock the osarch
  183. distro_mock = MagicMock(return_value=('Debian GNU/Linux', '8.3', ''))
  184. with patch.object(salt.utils.platform, 'is_proxy',
  185. MagicMock(return_value=False)), \
  186. patch.object(core, '_linux_bin_exists',
  187. MagicMock(return_value=False)), \
  188. patch.object(os.path, 'exists', path_exists_mock), \
  189. patch('{0}.__import__'.format(built_in), side_effect=_import_mock), \
  190. patch.object(os.path, 'isfile', path_isfile_mock), \
  191. patch.object(core, '_parse_lsb_release', empty_mock), \
  192. patch.object(core, '_parse_os_release', empty_mock), \
  193. patch.object(core, '_parse_lsb_release', empty_mock), \
  194. patch.object(core, 'linux_distribution', distro_mock), \
  195. patch.object(core, '_linux_cpudata', empty_mock), \
  196. patch.object(core, '_linux_gpu_data', empty_mock), \
  197. patch.object(core, '_memdata', empty_mock), \
  198. patch.object(core, '_hw_data', empty_mock), \
  199. patch.object(core, '_virtual', empty_mock), \
  200. patch.object(core, '_ps', empty_mock), \
  201. patch.dict(core.__salt__, {'cmd.run': cmd_run_mock}):
  202. os_grains = core.os_data()
  203. self.assertEqual(os_grains.get('os_family'), 'Debian')
  204. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  205. def test_suse_os_from_cpe_data(self):
  206. '''
  207. Test if 'os' grain is parsed from CPE_NAME of /etc/os-release
  208. '''
  209. _path_exists_map = {
  210. '/proc/1/cmdline': False
  211. }
  212. _os_release_map = {
  213. 'NAME': 'SLES',
  214. 'VERSION': '12-SP1',
  215. 'VERSION_ID': '12.1',
  216. 'PRETTY_NAME': 'SUSE Linux Enterprise Server 12 SP1',
  217. 'ID': 'sles',
  218. 'ANSI_COLOR': '0;32',
  219. 'CPE_NAME': 'cpe:/o:suse:sles:12:sp1'
  220. }
  221. path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
  222. empty_mock = MagicMock(return_value={})
  223. osarch_mock = MagicMock(return_value="amd64")
  224. os_release_mock = MagicMock(return_value=_os_release_map)
  225. orig_import = __import__
  226. if six.PY2:
  227. built_in = '__builtin__'
  228. else:
  229. built_in = 'builtins'
  230. def _import_mock(name, *args):
  231. if name == 'lsb_release':
  232. raise ImportError('No module named lsb_release')
  233. return orig_import(name, *args)
  234. distro_mock = MagicMock(
  235. return_value=('SUSE Linux Enterprise Server ', '12', 'x86_64')
  236. )
  237. # - Skip the first if statement
  238. # - Skip the selinux/systemd stuff (not pertinent)
  239. # - Skip the init grain compilation (not pertinent)
  240. # - Ensure that lsb_release fails to import
  241. # - Skip all the /etc/*-release stuff (not pertinent)
  242. # - Mock linux_distribution to give us the OS name that we want
  243. # - Mock the osarch
  244. with patch.object(salt.utils.platform, 'is_proxy',
  245. MagicMock(return_value=False)), \
  246. patch.object(core, '_linux_bin_exists',
  247. MagicMock(return_value=False)), \
  248. patch.object(os.path, 'exists', path_exists_mock), \
  249. patch('{0}.__import__'.format(built_in),
  250. side_effect=_import_mock), \
  251. patch.object(os.path, 'isfile', MagicMock(return_value=False)), \
  252. patch.object(core, '_parse_os_release', os_release_mock), \
  253. patch.object(core, '_parse_lsb_release', empty_mock), \
  254. patch.object(core, 'linux_distribution', distro_mock), \
  255. patch.object(core, '_linux_gpu_data', empty_mock), \
  256. patch.object(core, '_hw_data', empty_mock), \
  257. patch.object(core, '_linux_cpudata', empty_mock), \
  258. patch.object(core, '_virtual', empty_mock), \
  259. patch.dict(core.__salt__, {'cmd.run': osarch_mock}):
  260. os_grains = core.os_data()
  261. self.assertEqual(os_grains.get('os_family'), 'Suse')
  262. self.assertEqual(os_grains.get('os'), 'SUSE')
  263. def _run_os_grains_tests(self, os_release_filename, os_release_map, expectation):
  264. path_isfile_mock = MagicMock(side_effect=lambda x: x in os_release_map.get('files', []))
  265. empty_mock = MagicMock(return_value={})
  266. osarch_mock = MagicMock(return_value="amd64")
  267. if os_release_filename:
  268. os_release_data = core._parse_os_release(
  269. os.path.join(OS_RELEASE_DIR, os_release_filename)
  270. )
  271. else:
  272. os_release_data = os_release_map.get('os_release_file', {})
  273. os_release_mock = MagicMock(return_value=os_release_data)
  274. orig_import = __import__
  275. if six.PY2:
  276. built_in = '__builtin__'
  277. else:
  278. built_in = 'builtins'
  279. def _import_mock(name, *args):
  280. if name == 'lsb_release':
  281. raise ImportError('No module named lsb_release')
  282. return orig_import(name, *args)
  283. suse_release_file = os_release_map.get('suse_release_file')
  284. file_contents = {'/proc/1/cmdline': ''}
  285. if suse_release_file:
  286. file_contents['/etc/SuSE-release'] = suse_release_file
  287. # - Skip the first if statement
  288. # - Skip the selinux/systemd stuff (not pertinent)
  289. # - Skip the init grain compilation (not pertinent)
  290. # - Ensure that lsb_release fails to import
  291. # - Skip all the /etc/*-release stuff (not pertinent)
  292. # - Mock linux_distribution to give us the OS name that we want
  293. # - Mock the osarch
  294. distro_mock = MagicMock(return_value=os_release_map['linux_distribution'])
  295. with patch.object(salt.utils.platform, 'is_proxy', MagicMock(return_value=False)), \
  296. patch.object(core, '_linux_bin_exists', MagicMock(return_value=False)), \
  297. patch.object(os.path, 'exists', path_isfile_mock), \
  298. patch('{0}.__import__'.format(built_in), side_effect=_import_mock), \
  299. patch.object(os.path, 'isfile', path_isfile_mock), \
  300. patch.object(core, '_parse_os_release', os_release_mock), \
  301. patch.object(core, '_parse_lsb_release', empty_mock), \
  302. patch('salt.utils.files.fopen', mock_open(read_data=file_contents)), \
  303. patch.object(core, 'linux_distribution', distro_mock), \
  304. patch.object(core, '_linux_gpu_data', empty_mock), \
  305. patch.object(core, '_linux_cpudata', empty_mock), \
  306. patch.object(core, '_virtual', empty_mock), \
  307. patch.dict(core.__salt__, {'cmd.run': osarch_mock}):
  308. os_grains = core.os_data()
  309. grains = {k: v for k, v in os_grains.items()
  310. if k in set(["os", "os_family", "osfullname", "oscodename", "osfinger",
  311. "osrelease", "osrelease_info", "osmajorrelease"])}
  312. self.assertEqual(grains, expectation)
  313. def _run_suse_os_grains_tests(self, os_release_map, expectation):
  314. os_release_map['linux_distribution'] = ('SUSE test', 'version', 'arch')
  315. expectation['os'] = 'SUSE'
  316. expectation['os_family'] = 'Suse'
  317. self._run_os_grains_tests(None, os_release_map, expectation)
  318. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  319. def test_suse_os_grains_sles11sp3(self):
  320. '''
  321. Test if OS grains are parsed correctly in SLES 11 SP3
  322. '''
  323. _os_release_map = {
  324. 'suse_release_file': textwrap.dedent('''
  325. SUSE Linux Enterprise Server 11 (x86_64)
  326. VERSION = 11
  327. PATCHLEVEL = 3
  328. '''),
  329. 'files': ["/etc/SuSE-release"],
  330. }
  331. expectation = {
  332. 'oscodename': 'SUSE Linux Enterprise Server 11 SP3',
  333. 'osfullname': "SLES",
  334. 'osrelease': '11.3',
  335. 'osrelease_info': (11, 3),
  336. 'osmajorrelease': 11,
  337. 'osfinger': 'SLES-11',
  338. }
  339. self._run_suse_os_grains_tests(_os_release_map, expectation)
  340. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  341. def test_suse_os_grains_sles11sp4(self):
  342. '''
  343. Test if OS grains are parsed correctly in SLES 11 SP4
  344. '''
  345. _os_release_map = {
  346. 'os_release_file': {
  347. 'NAME': 'SLES',
  348. 'VERSION': '11.4',
  349. 'VERSION_ID': '11.4',
  350. 'PRETTY_NAME': 'SUSE Linux Enterprise Server 11 SP4',
  351. 'ID': 'sles',
  352. 'ANSI_COLOR': '0;32',
  353. 'CPE_NAME': 'cpe:/o:suse:sles:11:4'
  354. },
  355. }
  356. expectation = {
  357. 'oscodename': 'SUSE Linux Enterprise Server 11 SP4',
  358. 'osfullname': "SLES",
  359. 'osrelease': '11.4',
  360. 'osrelease_info': (11, 4),
  361. 'osmajorrelease': 11,
  362. 'osfinger': 'SLES-11',
  363. }
  364. self._run_suse_os_grains_tests(_os_release_map, expectation)
  365. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  366. def test_suse_os_grains_sles12(self):
  367. '''
  368. Test if OS grains are parsed correctly in SLES 12
  369. '''
  370. _os_release_map = {
  371. 'os_release_file': {
  372. 'NAME': 'SLES',
  373. 'VERSION': '12',
  374. 'VERSION_ID': '12',
  375. 'PRETTY_NAME': 'SUSE Linux Enterprise Server 12',
  376. 'ID': 'sles',
  377. 'ANSI_COLOR': '0;32',
  378. 'CPE_NAME': 'cpe:/o:suse:sles:12'
  379. },
  380. }
  381. expectation = {
  382. 'oscodename': 'SUSE Linux Enterprise Server 12',
  383. 'osfullname': "SLES",
  384. 'osrelease': '12',
  385. 'osrelease_info': (12,),
  386. 'osmajorrelease': 12,
  387. 'osfinger': 'SLES-12',
  388. }
  389. self._run_suse_os_grains_tests(_os_release_map, expectation)
  390. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  391. def test_suse_os_grains_sles12sp1(self):
  392. '''
  393. Test if OS grains are parsed correctly in SLES 12 SP1
  394. '''
  395. _os_release_map = {
  396. 'os_release_file': {
  397. 'NAME': 'SLES',
  398. 'VERSION': '12-SP1',
  399. 'VERSION_ID': '12.1',
  400. 'PRETTY_NAME': 'SUSE Linux Enterprise Server 12 SP1',
  401. 'ID': 'sles',
  402. 'ANSI_COLOR': '0;32',
  403. 'CPE_NAME': 'cpe:/o:suse:sles:12:sp1'
  404. },
  405. }
  406. expectation = {
  407. 'oscodename': 'SUSE Linux Enterprise Server 12 SP1',
  408. 'osfullname': "SLES",
  409. 'osrelease': '12.1',
  410. 'osrelease_info': (12, 1),
  411. 'osmajorrelease': 12,
  412. 'osfinger': 'SLES-12',
  413. }
  414. self._run_suse_os_grains_tests(_os_release_map, expectation)
  415. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  416. def test_suse_os_grains_opensuse_leap_42_1(self):
  417. '''
  418. Test if OS grains are parsed correctly in openSUSE Leap 42.1
  419. '''
  420. _os_release_map = {
  421. 'os_release_file': {
  422. 'NAME': 'openSUSE Leap',
  423. 'VERSION': '42.1',
  424. 'VERSION_ID': '42.1',
  425. 'PRETTY_NAME': 'openSUSE Leap 42.1 (x86_64)',
  426. 'ID': 'opensuse',
  427. 'ANSI_COLOR': '0;32',
  428. 'CPE_NAME': 'cpe:/o:opensuse:opensuse:42.1'
  429. },
  430. }
  431. expectation = {
  432. 'oscodename': 'openSUSE Leap 42.1 (x86_64)',
  433. 'osfullname': "Leap",
  434. 'osrelease': '42.1',
  435. 'osrelease_info': (42, 1),
  436. 'osmajorrelease': 42,
  437. 'osfinger': 'Leap-42',
  438. }
  439. self._run_suse_os_grains_tests(_os_release_map, expectation)
  440. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  441. def test_suse_os_grains_tumbleweed(self):
  442. '''
  443. Test if OS grains are parsed correctly in openSUSE Tumbleweed
  444. '''
  445. _os_release_map = {
  446. 'os_release_file': {
  447. 'NAME': 'openSUSE',
  448. 'VERSION': 'Tumbleweed',
  449. 'VERSION_ID': '20160504',
  450. 'PRETTY_NAME': 'openSUSE Tumbleweed (20160504) (x86_64)',
  451. 'ID': 'opensuse',
  452. 'ANSI_COLOR': '0;32',
  453. 'CPE_NAME': 'cpe:/o:opensuse:opensuse:20160504'
  454. },
  455. }
  456. expectation = {
  457. 'oscodename': 'openSUSE Tumbleweed (20160504) (x86_64)',
  458. 'osfullname': "Tumbleweed",
  459. 'osrelease': '20160504',
  460. 'osrelease_info': (20160504,),
  461. 'osmajorrelease': 20160504,
  462. 'osfinger': 'Tumbleweed-20160504',
  463. }
  464. self._run_suse_os_grains_tests(_os_release_map, expectation)
  465. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  466. def test_debian_7_os_grains(self):
  467. '''
  468. Test if OS grains are parsed correctly in Debian 7 "wheezy"
  469. '''
  470. _os_release_map = {
  471. 'linux_distribution': ('debian', '7.11', ''),
  472. }
  473. expectation = {
  474. 'os': 'Debian',
  475. 'os_family': 'Debian',
  476. 'oscodename': 'wheezy',
  477. 'osfullname': 'Debian GNU/Linux',
  478. 'osrelease': '7',
  479. 'osrelease_info': (7,),
  480. 'osmajorrelease': 7,
  481. 'osfinger': 'Debian-7',
  482. }
  483. self._run_os_grains_tests("debian-7", _os_release_map, expectation)
  484. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  485. def test_debian_8_os_grains(self):
  486. '''
  487. Test if OS grains are parsed correctly in Debian 8 "jessie"
  488. '''
  489. _os_release_map = {
  490. 'linux_distribution': ('debian', '8.10', ''),
  491. }
  492. expectation = {
  493. 'os': 'Debian',
  494. 'os_family': 'Debian',
  495. 'oscodename': 'jessie',
  496. 'osfullname': 'Debian GNU/Linux',
  497. 'osrelease': '8',
  498. 'osrelease_info': (8,),
  499. 'osmajorrelease': 8,
  500. 'osfinger': 'Debian-8',
  501. }
  502. self._run_os_grains_tests("debian-8", _os_release_map, expectation)
  503. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  504. def test_debian_9_os_grains(self):
  505. '''
  506. Test if OS grains are parsed correctly in Debian 9 "stretch"
  507. '''
  508. _os_release_map = {
  509. 'linux_distribution': ('debian', '9.3', ''),
  510. }
  511. expectation = {
  512. 'os': 'Debian',
  513. 'os_family': 'Debian',
  514. 'oscodename': 'stretch',
  515. 'osfullname': 'Debian GNU/Linux',
  516. 'osrelease': '9',
  517. 'osrelease_info': (9,),
  518. 'osmajorrelease': 9,
  519. 'osfinger': 'Debian-9',
  520. }
  521. self._run_os_grains_tests("debian-9", _os_release_map, expectation)
  522. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  523. def test_ubuntu_xenial_os_grains(self):
  524. '''
  525. Test if OS grains are parsed correctly in Ubuntu 16.04 "Xenial Xerus"
  526. '''
  527. _os_release_map = {
  528. 'linux_distribution': ('Ubuntu', '16.04', 'xenial'),
  529. }
  530. expectation = {
  531. 'os': 'Ubuntu',
  532. 'os_family': 'Debian',
  533. 'oscodename': 'xenial',
  534. 'osfullname': 'Ubuntu',
  535. 'osrelease': '16.04',
  536. 'osrelease_info': (16, 4),
  537. 'osmajorrelease': 16,
  538. 'osfinger': 'Ubuntu-16.04',
  539. }
  540. self._run_os_grains_tests("ubuntu-16.04", _os_release_map, expectation)
  541. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  542. def test_ubuntu_artful_os_grains(self):
  543. '''
  544. Test if OS grains are parsed correctly in Ubuntu 17.10 "Artful Aardvark"
  545. '''
  546. _os_release_map = {
  547. 'linux_distribution': ('Ubuntu', '17.10', 'artful'),
  548. }
  549. expectation = {
  550. 'os': 'Ubuntu',
  551. 'os_family': 'Debian',
  552. 'oscodename': 'artful',
  553. 'osfullname': 'Ubuntu',
  554. 'osrelease': '17.10',
  555. 'osrelease_info': (17, 10),
  556. 'osmajorrelease': 17,
  557. 'osfinger': 'Ubuntu-17.10',
  558. }
  559. self._run_os_grains_tests("ubuntu-17.10", _os_release_map, expectation)
  560. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  561. def test_windows_platform_data(self):
  562. '''
  563. Test the _windows_platform_data function
  564. '''
  565. grains = ['biosversion', 'kernelrelease', 'kernelversion',
  566. 'manufacturer', 'motherboard', 'osfullname', 'osmanufacturer',
  567. 'osrelease', 'osservicepack', 'osversion', 'productname',
  568. 'serialnumber', 'timezone', 'virtual', 'windowsdomain',
  569. 'windowsdomaintype']
  570. returned_grains = core._windows_platform_data()
  571. for grain in grains:
  572. self.assertIn(grain, returned_grains)
  573. valid_types = ['Unknown', 'Unjoined', 'Workgroup', 'Domain']
  574. self.assertIn(returned_grains['windowsdomaintype'], valid_types)
  575. valid_releases = ['Vista', '7', '8', '8.1', '10', '2008Server',
  576. '2008ServerR2', '2012Server', '2012ServerR2',
  577. '2016Server', '2019Server']
  578. self.assertIn(returned_grains['osrelease'], valid_releases)
  579. def test__windows_os_release_grain(self):
  580. versions = {
  581. 'Windows 10 Home': '10',
  582. 'Windows 10 Pro': '10',
  583. 'Windows 10 Pro for Workstations': '10',
  584. 'Windows 10 Pro Education': '10',
  585. 'Windows 10 Enterprise': '10',
  586. 'Windows 10 Enterprise LTSB': '10',
  587. 'Windows 10 Education': '10',
  588. 'Windows 10 IoT Core': '10',
  589. 'Windows 10 IoT Enterprise': '10',
  590. 'Windows 10 S': '10',
  591. 'Windows 8.1': '8.1',
  592. 'Windows 8.1 Pro': '8.1',
  593. 'Windows 8.1 Enterprise': '8.1',
  594. 'Windows 8.1 OEM': '8.1',
  595. 'Windows 8.1 with Bing': '8.1',
  596. 'Windows 8': '8',
  597. 'Windows 8 Pro': '8',
  598. 'Windows 8 Enterprise': '8',
  599. 'Windows 8 OEM': '8',
  600. 'Windows 7 Starter': '7',
  601. 'Windows 7 Home Basic': '7',
  602. 'Windows 7 Home Premium': '7',
  603. 'Windows 7 Professional': '7',
  604. 'Windows 7 Enterprise': '7',
  605. 'Windows 7 Ultimate': '7',
  606. 'Windows Thin PC': 'Thin',
  607. 'Windows Vista Starter': 'Vista',
  608. 'Windows Vista Home Basic': 'Vista',
  609. 'Windows Vista Home Premium': 'Vista',
  610. 'Windows Vista Business': 'Vista',
  611. 'Windows Vista Enterprise': 'Vista',
  612. 'Windows Vista Ultimate': 'Vista',
  613. 'Windows Server 2019 Essentials': '2019Server',
  614. 'Windows Server 2019 Standard': '2019Server',
  615. 'Windows Server 2019 Datacenter': '2019Server',
  616. 'Windows Server 2016 Essentials': '2016Server',
  617. 'Windows Server 2016 Standard': '2016Server',
  618. 'Windows Server 2016 Datacenter': '2016Server',
  619. 'Windows Server 2012 R2 Foundation': '2012ServerR2',
  620. 'Windows Server 2012 R2 Essentials': '2012ServerR2',
  621. 'Windows Server 2012 R2 Standard': '2012ServerR2',
  622. 'Windows Server 2012 R2 Datacenter': '2012ServerR2',
  623. 'Windows Server 2012 Foundation': '2012Server',
  624. 'Windows Server 2012 Essentials': '2012Server',
  625. 'Windows Server 2012 Standard': '2012Server',
  626. 'Windows Server 2012 Datacenter': '2012Server',
  627. 'Windows MultiPoint Server 2012': '2012Server',
  628. 'Windows Small Business Server 2011': '2011Server',
  629. 'Windows MultiPoint Server 2011': '2011Server',
  630. 'Windows Home Server 2011': '2011Server',
  631. 'Windows MultiPoint Server 2010': '2010Server',
  632. 'Windows Server 2008 R2 Foundation': '2008ServerR2',
  633. 'Windows Server 2008 R2 Standard': '2008ServerR2',
  634. 'Windows Server 2008 R2 Enterprise': '2008ServerR2',
  635. 'Windows Server 2008 R2 Datacenter': '2008ServerR2',
  636. 'Windows Server 2008 R2 for Itanium-based Systems': '2008ServerR2',
  637. 'Windows Web Server 2008 R2': '2008ServerR2',
  638. 'Windows Storage Server 2008 R2': '2008ServerR2',
  639. 'Windows HPC Server 2008 R2': '2008ServerR2',
  640. 'Windows Server 2008 Standard': '2008Server',
  641. 'Windows Server 2008 Enterprise': '2008Server',
  642. 'Windows Server 2008 Datacenter': '2008Server',
  643. 'Windows Server 2008 for Itanium-based Systems': '2008Server',
  644. 'Windows Server Foundation 2008': '2008Server',
  645. 'Windows Essential Business Server 2008': '2008Server',
  646. 'Windows HPC Server 2008': '2008Server',
  647. 'Windows Small Business Server 2008': '2008Server',
  648. 'Windows Storage Server 2008': '2008Server',
  649. 'Windows Web Server 2008': '2008Server'
  650. }
  651. for caption in versions:
  652. version = core._windows_os_release_grain(caption, 1)
  653. self.assertEqual(
  654. version,
  655. versions[caption],
  656. 'version: {0}\n'
  657. 'found: {1}\n'
  658. 'caption: {2}'.format(version, versions[caption], caption)
  659. )
  660. embedded_versions = {
  661. 'Windows Embedded 8.1 Industry Pro': '8.1',
  662. 'Windows Embedded 8 Industry Pro': '8',
  663. 'Windows POSReady 7': '7',
  664. 'Windows Embedded Standard 7': '7',
  665. 'Windows Embedded POSReady 2009': '2009',
  666. 'Windows Embedded Standard 2009': '2009',
  667. 'Windows XP Embedded': 'XP',
  668. }
  669. for caption in embedded_versions:
  670. version = core._windows_os_release_grain(caption, 1)
  671. self.assertEqual(
  672. version,
  673. embedded_versions[caption],
  674. '{0} != {1}\n'
  675. 'version: {0}\n'
  676. 'found: {1}\n'
  677. 'caption: {2}'.format(version, embedded_versions[caption], caption)
  678. )
  679. # Special Cases
  680. # Windows Embedded Standard is Windows 7
  681. caption = 'Windows Embedded Standard'
  682. with patch('platform.release', MagicMock(return_value='7')):
  683. version = core._windows_os_release_grain(caption, 1)
  684. self.assertEqual(version, '7')
  685. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  686. def test_linux_memdata(self):
  687. '''
  688. Test memdata on Linux systems
  689. '''
  690. _proc_meminfo = textwrap.dedent('''\
  691. MemTotal: 16277028 kB
  692. SwapTotal: 4789244 kB''')
  693. with patch('salt.utils.files.fopen', mock_open(read_data=_proc_meminfo)):
  694. memdata = core._linux_memdata()
  695. self.assertEqual(memdata.get('mem_total'), 15895)
  696. self.assertEqual(memdata.get('swap_total'), 4676)
  697. @skipIf(salt.utils.platform.is_windows(), 'System is Windows')
  698. def test_bsd_memdata(self):
  699. '''
  700. Test to memdata on *BSD systems
  701. '''
  702. _path_exists_map = {}
  703. _path_isfile_map = {}
  704. _cmd_run_map = {
  705. 'freebsd-version -u': '10.3-RELEASE',
  706. '/sbin/sysctl -n hw.physmem': '2121781248',
  707. '/sbin/sysctl -n vm.swap_total': '419430400'
  708. }
  709. path_exists_mock = MagicMock(side_effect=lambda x: _path_exists_map[x])
  710. path_isfile_mock = MagicMock(
  711. side_effect=lambda x: _path_isfile_map.get(x, False)
  712. )
  713. cmd_run_mock = MagicMock(
  714. side_effect=lambda x: _cmd_run_map[x]
  715. )
  716. empty_mock = MagicMock(return_value={})
  717. mock_freebsd_uname = ('FreeBSD',
  718. 'freebsd10.3-hostname-8148',
  719. '10.3-RELEASE',
  720. 'FreeBSD 10.3-RELEASE #0 r297264: Fri Mar 25 02:10:02 UTC 2016 root@releng1.nyi.freebsd.org:/usr/obj/usr/src/sys/GENERIC',
  721. 'amd64',
  722. 'amd64')
  723. with patch('platform.uname',
  724. MagicMock(return_value=mock_freebsd_uname)):
  725. with patch.object(salt.utils.platform, 'is_linux',
  726. MagicMock(return_value=False)):
  727. with patch.object(salt.utils.platform, 'is_freebsd',
  728. MagicMock(return_value=True)):
  729. # Skip the first if statement
  730. with patch.object(salt.utils.platform, 'is_proxy',
  731. MagicMock(return_value=False)):
  732. # Skip the init grain compilation (not pertinent)
  733. with patch.object(os.path, 'exists', path_exists_mock):
  734. with patch('salt.utils.path.which') as mock:
  735. mock.return_value = '/sbin/sysctl'
  736. # Make a bunch of functions return empty dicts,
  737. # we don't care about these grains for the
  738. # purposes of this test.
  739. with patch.object(
  740. core,
  741. '_bsd_cpudata',
  742. empty_mock):
  743. with patch.object(
  744. core,
  745. '_hw_data',
  746. empty_mock):
  747. with patch.object(
  748. core,
  749. '_virtual',
  750. empty_mock):
  751. with patch.object(
  752. core,
  753. '_ps',
  754. empty_mock):
  755. # Mock the osarch
  756. with patch.dict(
  757. core.__salt__,
  758. {'cmd.run': cmd_run_mock}):
  759. os_grains = core.os_data()
  760. self.assertEqual(os_grains.get('mem_total'), 2023)
  761. self.assertEqual(os_grains.get('swap_total'), 400)
  762. @skipIf(salt.utils.platform.is_windows(), 'System is Windows')
  763. def test_docker_virtual(self):
  764. '''
  765. Test if virtual grains are parsed correctly in Docker.
  766. '''
  767. with patch.object(os.path, 'isdir', MagicMock(return_value=False)):
  768. with patch.object(os.path,
  769. 'isfile',
  770. MagicMock(side_effect=lambda x: True if x == '/proc/1/cgroup' else False)):
  771. for cgroup_substr in (':/system.slice/docker', ':/docker/',
  772. ':/docker-ce/'):
  773. cgroup_data = \
  774. '10:memory{0}a_long_sha256sum'.format(cgroup_substr)
  775. log.debug(
  776. 'Testing Docker cgroup substring \'%s\'', cgroup_substr)
  777. with patch('salt.utils.files.fopen', mock_open(read_data=cgroup_data)):
  778. with patch.dict(core.__salt__, {'cmd.run_all': MagicMock()}):
  779. grains = core._virtual({'kernel': 'Linux'})
  780. self.assertEqual(
  781. grains.get('virtual_subtype'),
  782. 'Docker'
  783. )
  784. self.assertEqual(
  785. grains.get('virtual'),
  786. 'container',
  787. )
  788. @skipIf(salt.utils.platform.is_windows(), 'System is Windows')
  789. def test_lxc_virtual(self):
  790. '''
  791. Test if virtual grains are parsed correctly in LXC.
  792. '''
  793. with patch.object(os.path, 'isdir', MagicMock(return_value=False)):
  794. with patch.object(os.path,
  795. 'isfile',
  796. MagicMock(side_effect=lambda x: True if x == '/proc/1/cgroup' else False)):
  797. cgroup_data = '10:memory:/lxc/a_long_sha256sum'
  798. with patch('salt.utils.files.fopen', mock_open(read_data=cgroup_data)):
  799. with patch.dict(core.__salt__, {'cmd.run_all': MagicMock()}):
  800. grains = core._virtual({'kernel': 'Linux'})
  801. self.assertEqual(
  802. grains.get('virtual_subtype'),
  803. 'LXC'
  804. )
  805. self.assertEqual(
  806. grains.get('virtual'),
  807. 'container',
  808. )
  809. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  810. def test_xen_virtual(self):
  811. '''
  812. Test if OS grains are parsed correctly in Ubuntu Xenial Xerus
  813. '''
  814. with patch.multiple(os.path, isdir=MagicMock(side_effect=lambda x: x == '/sys/bus/xen'),
  815. isfile=MagicMock(side_effect=lambda x:
  816. x == '/sys/bus/xen/drivers/xenconsole')):
  817. with patch.dict(core.__salt__, {'cmd.run': MagicMock(return_value='')}):
  818. log.debug('Testing Xen')
  819. self.assertEqual(
  820. core._virtual({'kernel': 'Linux'}).get('virtual_subtype'),
  821. 'Xen PV DomU'
  822. )
  823. def test_if_virtual_subtype_exists_virtual_should_fallback_to_virtual(self):
  824. def mockstat(path):
  825. if path == '/':
  826. return 'fnord'
  827. elif path == '/proc/1/root/.':
  828. return 'roscivs'
  829. return None
  830. with patch.dict(
  831. core.__salt__,
  832. {
  833. 'cmd.run': MagicMock(return_value=''),
  834. 'cmd.run_all': MagicMock(return_value={'retcode': 0, 'stdout': ''}),
  835. }
  836. ):
  837. with patch.multiple(
  838. os.path,
  839. isfile=MagicMock(return_value=False),
  840. isdir=MagicMock(side_effect=lambda x: x == '/proc'),
  841. ):
  842. with patch.multiple(
  843. os,
  844. stat=MagicMock(side_effect=mockstat),
  845. ):
  846. grains = core._virtual({'kernel': 'Linux'})
  847. assert grains.get('virtual_subtype') is not None
  848. assert grains.get('virtual') == 'virtual'
  849. def _check_ipaddress(self, value, ip_v):
  850. '''
  851. check if ip address in a list is valid
  852. '''
  853. for val in value:
  854. assert isinstance(val, six.string_types)
  855. ip_method = 'is_ipv{0}'.format(ip_v)
  856. self.assertTrue(getattr(salt.utils.network, ip_method)(val))
  857. def _check_empty(self, key, value, empty):
  858. '''
  859. if empty is False and value does not exist assert error
  860. if empty is True and value exists assert error
  861. '''
  862. if not empty and not value:
  863. raise Exception("{0} is empty, expecting a value".format(key))
  864. elif empty and value:
  865. raise Exception("{0} is suppose to be empty. value: {1} \
  866. exists".format(key, value))
  867. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  868. def test_fqdn_return(self):
  869. '''
  870. test ip4 and ip6 return values
  871. '''
  872. net_ip4_mock = [IP4_LOCAL, IP4_ADD1, IP4_ADD2]
  873. net_ip6_mock = [IP6_LOCAL, IP6_ADD1, IP6_ADD2]
  874. self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
  875. ip4_empty=False, ip6_empty=False)
  876. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  877. def test_fqdn6_empty(self):
  878. '''
  879. test when ip6 is empty
  880. '''
  881. net_ip4_mock = [IP4_LOCAL, IP4_ADD1, IP4_ADD2]
  882. net_ip6_mock = []
  883. self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
  884. ip4_empty=False)
  885. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  886. def test_fqdn4_empty(self):
  887. '''
  888. test when ip4 is empty
  889. '''
  890. net_ip4_mock = []
  891. net_ip6_mock = [IP6_LOCAL, IP6_ADD1, IP6_ADD2]
  892. self._run_fqdn_tests(net_ip4_mock, net_ip6_mock,
  893. ip6_empty=False)
  894. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  895. def test_fqdn_all_empty(self):
  896. '''
  897. test when both ip4 and ip6 are empty
  898. '''
  899. net_ip4_mock = []
  900. net_ip6_mock = []
  901. self._run_fqdn_tests(net_ip4_mock, net_ip6_mock)
  902. def _run_fqdn_tests(self, net_ip4_mock, net_ip6_mock,
  903. ip6_empty=True, ip4_empty=True):
  904. def _check_type(key, value, ip4_empty, ip6_empty):
  905. '''
  906. check type and other checks
  907. '''
  908. assert isinstance(value, list)
  909. if '4' in key:
  910. self._check_empty(key, value, ip4_empty)
  911. self._check_ipaddress(value, ip_v='4')
  912. elif '6' in key:
  913. self._check_empty(key, value, ip6_empty)
  914. self._check_ipaddress(value, ip_v='6')
  915. ip4_mock = [(2, 1, 6, '', (IP4_ADD1, 0)),
  916. (2, 3, 0, '', (IP4_ADD2, 0))]
  917. ip6_mock = [(10, 1, 6, '', (IP6_ADD1, 0, 0, 0)),
  918. (10, 3, 0, '', (IP6_ADD2, 0, 0, 0))]
  919. with patch.dict(core.__opts__, {'ipv6': False}):
  920. with patch.object(salt.utils.network, 'ip_addrs',
  921. MagicMock(return_value=net_ip4_mock)):
  922. with patch.object(salt.utils.network, 'ip_addrs6',
  923. MagicMock(return_value=net_ip6_mock)):
  924. with patch.object(core.socket, 'getaddrinfo', side_effect=[ip4_mock, ip6_mock]):
  925. get_fqdn = core.ip_fqdn()
  926. ret_keys = ['fqdn_ip4', 'fqdn_ip6', 'ipv4', 'ipv6']
  927. for key in ret_keys:
  928. value = get_fqdn[key]
  929. _check_type(key, value, ip4_empty, ip6_empty)
  930. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  931. @patch.object(salt.utils.platform, 'is_windows', MagicMock(return_value=False))
  932. @patch('salt.grains.core.__opts__', {'ipv6': False})
  933. def test_dns_return(self):
  934. '''
  935. test the return for a dns grain. test for issue:
  936. https://github.com/saltstack/salt/issues/41230
  937. '''
  938. resolv_mock = {'domain': '', 'sortlist': [], 'nameservers':
  939. [ipaddress.IPv4Address(IP4_ADD1),
  940. ipaddress.IPv6Address(IP6_ADD1),
  941. IP6_ADD_SCOPE], 'ip4_nameservers':
  942. [ipaddress.IPv4Address(IP4_ADD1)],
  943. 'search': ['test.saltstack.com'], 'ip6_nameservers':
  944. [ipaddress.IPv6Address(IP6_ADD1),
  945. IP6_ADD_SCOPE], 'options': []}
  946. ret = {'dns': {'domain': '', 'sortlist': [], 'nameservers':
  947. [IP4_ADD1, IP6_ADD1,
  948. IP6_ADD_SCOPE], 'ip4_nameservers':
  949. [IP4_ADD1], 'search': ['test.saltstack.com'],
  950. 'ip6_nameservers': [IP6_ADD1, IP6_ADD_SCOPE],
  951. 'options': []}}
  952. with patch.object(salt.utils.dns, 'parse_resolv', MagicMock(return_value=resolv_mock)):
  953. assert core.dns() == ret
  954. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  955. @patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4', '5.6.7.8']))
  956. @patch('salt.utils.network.ip_addrs6',
  957. MagicMock(return_value=['fe80::a8b2:93ff:fe00:0', 'fe80::a8b2:93ff:dead:beef']))
  958. @patch('salt.utils.network.socket.getfqdn', MagicMock(side_effect=lambda v: v)) # Just pass-through
  959. def test_fqdns_return(self):
  960. '''
  961. test the return for a dns grain. test for issue:
  962. https://github.com/saltstack/salt/issues/41230
  963. '''
  964. reverse_resolv_mock = [('foo.bar.baz', [], ['1.2.3.4']),
  965. ('rinzler.evil-corp.com', [], ['5.6.7.8']),
  966. ('foo.bar.baz', [], ['fe80::a8b2:93ff:fe00:0']),
  967. ('bluesniff.foo.bar', [], ['fe80::a8b2:93ff:dead:beef'])]
  968. ret = {'fqdns': ['bluesniff.foo.bar', 'foo.bar.baz', 'rinzler.evil-corp.com']}
  969. with patch.object(socket, 'gethostbyaddr', side_effect=reverse_resolv_mock):
  970. fqdns = core.fqdns()
  971. self.assertIn('fqdns', fqdns)
  972. self.assertEqual(len(fqdns['fqdns']), len(ret['fqdns']))
  973. self.assertEqual(set(fqdns['fqdns']), set(ret['fqdns']))
  974. @skipIf(not salt.utils.platform.is_linux(), 'System is not Linux')
  975. @patch('salt.utils.network.ip_addrs', MagicMock(return_value=['1.2.3.4']))
  976. @patch('salt.utils.network.ip_addrs6', MagicMock(return_value=[]))
  977. def test_fqdns_socket_error(self):
  978. '''
  979. test the behavior on non-critical socket errors of the dns grain
  980. '''
  981. def _gen_gethostbyaddr(errno):
  982. def _gethostbyaddr(_):
  983. herror = socket.herror()
  984. herror.errno = errno
  985. raise herror
  986. return _gethostbyaddr
  987. for errno in (0, core.HOST_NOT_FOUND, core.NO_DATA):
  988. mock_log = MagicMock()
  989. with patch.object(socket, 'gethostbyaddr',
  990. side_effect=_gen_gethostbyaddr(errno)):
  991. with patch('salt.grains.core.log', mock_log):
  992. self.assertEqual(core.fqdns(), {'fqdns': []})
  993. mock_log.debug.assert_called_once()
  994. mock_log.error.assert_not_called()
  995. mock_log = MagicMock()
  996. with patch.object(socket, 'gethostbyaddr',
  997. side_effect=_gen_gethostbyaddr(-1)):
  998. with patch('salt.grains.core.log', mock_log):
  999. self.assertEqual(core.fqdns(), {'fqdns': []})
  1000. mock_log.debug.assert_not_called()
  1001. mock_log.error.assert_called_once()
  1002. def test_core_virtual(self):
  1003. '''
  1004. test virtual grain with cmd virt-what
  1005. '''
  1006. virt = 'kvm'
  1007. with patch.object(salt.utils.platform, 'is_windows',
  1008. MagicMock(return_value=False)):
  1009. with patch.object(salt.utils.path, 'which',
  1010. MagicMock(return_value=True)):
  1011. with patch.dict(core.__salt__, {'cmd.run_all':
  1012. MagicMock(return_value={'pid': 78,
  1013. 'retcode': 0,
  1014. 'stderr': '',
  1015. 'stdout': virt})}):
  1016. osdata = {'kernel': 'test', }
  1017. ret = core._virtual(osdata)
  1018. self.assertEqual(ret['virtual'], virt)
  1019. def test_solaris_sparc_s7zone(self):
  1020. '''
  1021. verify productname grain for s7 zone
  1022. '''
  1023. expectation = {
  1024. 'productname': 'SPARC S7-2',
  1025. 'product': 'SPARC S7-2',
  1026. }
  1027. with salt.utils.files.fopen(os.path.join(SOLARIS_DIR, 'prtconf.s7-zone')) as sparc_return_data:
  1028. this_sparc_return_data = '\n'.join(sparc_return_data.readlines())
  1029. this_sparc_return_data += '\n'
  1030. self._check_solaris_sparc_productname_grains(this_sparc_return_data, expectation)
  1031. def test_solaris_sparc_s7(self):
  1032. '''
  1033. verify productname grain for s7
  1034. '''
  1035. expectation = {
  1036. 'productname': 'SPARC S7-2',
  1037. 'product': 'SPARC S7-2',
  1038. }
  1039. with salt.utils.files.fopen(os.path.join(SOLARIS_DIR, 'prtdiag.s7')) as sparc_return_data:
  1040. this_sparc_return_data = '\n'.join(sparc_return_data.readlines())
  1041. this_sparc_return_data += '\n'
  1042. self._check_solaris_sparc_productname_grains(this_sparc_return_data, expectation)
  1043. def test_solaris_sparc_t5220(self):
  1044. '''
  1045. verify productname grain for t5220
  1046. '''
  1047. expectation = {
  1048. 'productname': 'SPARC Enterprise T5220',
  1049. 'product': 'SPARC Enterprise T5220',
  1050. }
  1051. with salt.utils.files.fopen(os.path.join(SOLARIS_DIR, 'prtdiag.t5220')) as sparc_return_data:
  1052. this_sparc_return_data = '\n'.join(sparc_return_data.readlines())
  1053. this_sparc_return_data += '\n'
  1054. self._check_solaris_sparc_productname_grains(this_sparc_return_data, expectation)
  1055. def test_solaris_sparc_t5220zone(self):
  1056. '''
  1057. verify productname grain for t5220 zone
  1058. '''
  1059. expectation = {
  1060. 'productname': 'SPARC Enterprise T5220',
  1061. 'product': 'SPARC Enterprise T5220',
  1062. }
  1063. with salt.utils.files.fopen(os.path.join(SOLARIS_DIR, 'prtconf.t5220-zone')) as sparc_return_data:
  1064. this_sparc_return_data = '\n'.join(sparc_return_data.readlines())
  1065. this_sparc_return_data += '\n'
  1066. self._check_solaris_sparc_productname_grains(this_sparc_return_data, expectation)
  1067. def _check_solaris_sparc_productname_grains(self, prtdata, expectation):
  1068. '''
  1069. verify product grains on solaris sparc
  1070. '''
  1071. import platform
  1072. path_isfile_mock = MagicMock(side_effect=lambda x: x in ['/etc/release'])
  1073. with salt.utils.files.fopen(os.path.join(OS_RELEASE_DIR, "solaris-11.3")) as os_release_file:
  1074. os_release_content = os_release_file.readlines()
  1075. uname_mock = MagicMock(return_value=(
  1076. 'SunOS', 'testsystem', '5.11', '11.3', 'sunv4', 'sparc'
  1077. ))
  1078. with patch.object(platform, 'uname', uname_mock), \
  1079. patch.object(salt.utils.platform, 'is_proxy',
  1080. MagicMock(return_value=False)), \
  1081. patch.object(salt.utils.platform, 'is_linux',
  1082. MagicMock(return_value=False)), \
  1083. patch.object(salt.utils.platform, 'is_windows',
  1084. MagicMock(return_value=False)), \
  1085. patch.object(salt.utils.platform, 'is_smartos',
  1086. MagicMock(return_value=False)), \
  1087. patch.object(salt.utils.path, 'which_bin',
  1088. MagicMock(return_value=None)), \
  1089. patch.object(os.path, 'isfile', path_isfile_mock), \
  1090. patch('salt.utils.files.fopen',
  1091. mock_open(read_data=os_release_content)) as os_release_file, \
  1092. patch.object(core, '_sunos_cpudata',
  1093. MagicMock(return_value={
  1094. 'cpuarch': 'sparcv9',
  1095. 'num_cpus': '1',
  1096. 'cpu_model': 'MOCK_CPU_MODEL',
  1097. 'cpu_flags': []})), \
  1098. patch.object(core, '_memdata',
  1099. MagicMock(return_value={'mem_total': 16384})), \
  1100. patch.object(core, '_virtual',
  1101. MagicMock(return_value={})), \
  1102. patch.object(core, '_ps', MagicMock(return_value={})), \
  1103. patch.object(salt.utils.path, 'which',
  1104. MagicMock(return_value=True)), \
  1105. patch.dict(core.__salt__,
  1106. {'cmd.run': MagicMock(return_value=prtdata)}):
  1107. os_grains = core.os_data()
  1108. grains = {k: v for k, v in os_grains.items()
  1109. if k in set(['product', 'productname'])}
  1110. self.assertEqual(grains, expectation)
  1111. @patch('os.path.isfile')
  1112. @patch('os.path.isdir')
  1113. def test_core_virtual_unicode(self, mock_file, mock_dir):
  1114. '''
  1115. test virtual grain with unicode character in product_name file
  1116. '''
  1117. def path_side_effect(path):
  1118. if path == '/sys/devices/virtual/dmi/id/product_name':
  1119. return True
  1120. return False
  1121. virt = 'kvm'
  1122. mock_file.side_effect = path_side_effect
  1123. mock_dir.side_effect = path_side_effect
  1124. with patch.object(salt.utils.platform, 'is_windows',
  1125. MagicMock(return_value=False)):
  1126. with patch.object(salt.utils.path, 'which',
  1127. MagicMock(return_value=True)):
  1128. with patch.dict(core.__salt__, {'cmd.run_all':
  1129. MagicMock(return_value={'pid': 78,
  1130. 'retcode': 0,
  1131. 'stderr': '',
  1132. 'stdout': virt})}):
  1133. with patch('salt.utils.files.fopen',
  1134. mock_open(read_data='嗨')):
  1135. osdata = {'kernel': 'Linux', }
  1136. ret = core._virtual(osdata)
  1137. self.assertEqual(ret['virtual'], virt)
  1138. @patch('salt.utils.path.which', MagicMock(return_value='/usr/sbin/sysctl'))
  1139. def test_osx_memdata_with_comma(self):
  1140. '''
  1141. test osx memdata method when comma returns
  1142. '''
  1143. def _cmd_side_effect(cmd):
  1144. if 'hw.memsize' in cmd:
  1145. return '4294967296'
  1146. elif 'vm.swapusage' in cmd:
  1147. return 'total = 1024,00M used = 160,75M free = 863,25M (encrypted)'
  1148. with patch.dict(core.__salt__, {'cmd.run': MagicMock(side_effect=_cmd_side_effect)}):
  1149. ret = core._osx_memdata()
  1150. assert ret['swap_total'] == 1024
  1151. assert ret['mem_total'] == 4096
  1152. @patch('salt.utils.path.which', MagicMock(return_value='/usr/sbin/sysctl'))
  1153. def test_osx_memdata(self):
  1154. '''
  1155. test osx memdata
  1156. '''
  1157. def _cmd_side_effect(cmd):
  1158. if 'hw.memsize' in cmd:
  1159. return '4294967296'
  1160. elif 'vm.swapusage' in cmd:
  1161. return 'total = 0.00M used = 0.00M free = 0.00M (encrypted)'
  1162. with patch.dict(core.__salt__, {'cmd.run': MagicMock(side_effect=_cmd_side_effect)}):
  1163. ret = core._osx_memdata()
  1164. assert ret['swap_total'] == 0
  1165. assert ret['mem_total'] == 4096
  1166. @skipIf(not core._DATEUTIL_TZ, 'Missing dateutil.tz')
  1167. def test_locale_info_tzname(self):
  1168. # mock datetime.now().tzname()
  1169. # cant just mock now because it is read only
  1170. tzname = Mock(return_value='MDT_FAKE')
  1171. now_ret_object = Mock(tzname=tzname)
  1172. now = Mock(return_value=now_ret_object)
  1173. datetime = Mock(now=now)
  1174. with patch.object(core, 'datetime', datetime=datetime) as datetime_module:
  1175. with patch.object(core.dateutil.tz, 'tzlocal', return_value=object) as tzlocal:
  1176. with patch.object(salt.utils.platform, 'is_proxy', return_value=False) as is_proxy:
  1177. ret = core.locale_info()
  1178. tzname.assert_called_once_with()
  1179. self.assertEqual(len(now_ret_object.method_calls), 1)
  1180. now.assert_called_once_with(object)
  1181. self.assertEqual(len(datetime.method_calls), 1)
  1182. self.assertEqual(len(datetime_module.method_calls), 1)
  1183. tzlocal.assert_called_once_with()
  1184. is_proxy.assert_called_once_with()
  1185. self.assertEqual(ret['locale_info']['timezone'], 'MDT_FAKE')
  1186. @skipIf(not core._DATEUTIL_TZ, 'Missing dateutil.tz')
  1187. def test_locale_info_unicode_error_tzname(self):
  1188. # UnicodeDecodeError most have the default string encoding
  1189. unicode_error = UnicodeDecodeError(str('fake'), b'\x00\x00', 1, 2, str('fake'))
  1190. # mock datetime.now().tzname()
  1191. # cant just mock now because it is read only
  1192. tzname = Mock(return_value='MDT_FAKE')
  1193. now_ret_object = Mock(tzname=tzname)
  1194. now = Mock(return_value=now_ret_object)
  1195. datetime = Mock(now=now)
  1196. # mock tzname[0].decode()
  1197. decode = Mock(return_value='CST_FAKE')
  1198. tzname2 = (Mock(decode=decode,),)
  1199. with patch.object(core, 'datetime', datetime=datetime) as datetime_module:
  1200. with patch.object(core.dateutil.tz, 'tzlocal', side_effect=unicode_error) as tzlocal:
  1201. with patch.object(salt.utils.platform, 'is_proxy', return_value=False) as is_proxy:
  1202. with patch.object(core.salt.utils.platform, 'is_windows', return_value=True) as is_windows:
  1203. with patch.object(core, 'time', tzname=tzname2):
  1204. ret = core.locale_info()
  1205. tzname.assert_not_called()
  1206. self.assertEqual(len(now_ret_object.method_calls), 0)
  1207. now.assert_not_called()
  1208. self.assertEqual(len(datetime.method_calls), 0)
  1209. decode.assert_called_once_with('mbcs')
  1210. self.assertEqual(len(tzname2[0].method_calls), 1)
  1211. self.assertEqual(len(datetime_module.method_calls), 0)
  1212. tzlocal.assert_called_once_with()
  1213. is_proxy.assert_called_once_with()
  1214. is_windows.assert_called_once_with()
  1215. self.assertEqual(ret['locale_info']['timezone'], 'CST_FAKE')
  1216. @skipIf(core._DATEUTIL_TZ, 'Not Missing dateutil.tz')
  1217. def test_locale_info_no_tz_tzname(self):
  1218. with patch.object(salt.utils.platform, 'is_proxy', return_value=False) as is_proxy:
  1219. with patch.object(core.salt.utils.platform, 'is_windows', return_value=True) as is_windows:
  1220. ret = core.locale_info()
  1221. is_proxy.assert_called_once_with()
  1222. is_windows.assert_not_called()
  1223. self.assertEqual(ret['locale_info']['timezone'], 'unknown')
  1224. def test_cwd_exists(self):
  1225. cwd_grain = core.cwd()
  1226. self.assertIsInstance(cwd_grain, dict)
  1227. self.assertTrue('cwd' in cwd_grain)
  1228. self.assertEqual(cwd_grain['cwd'], os.getcwd())
  1229. def test_cwd_is_cwd(self):
  1230. cwd = os.getcwd()
  1231. try:
  1232. # change directory
  1233. new_dir = os.path.split(cwd)[0]
  1234. os.chdir(new_dir)
  1235. cwd_grain = core.cwd()
  1236. self.assertEqual(cwd_grain['cwd'], new_dir)
  1237. finally:
  1238. # change back to original directory
  1239. os.chdir(cwd)
  1240. def test_virtual_set_virtual_grain(self):
  1241. osdata = {}
  1242. (osdata['kernel'], osdata['nodename'],
  1243. osdata['kernelrelease'], osdata['kernelversion'], osdata['cpuarch'], _) = platform.uname()
  1244. with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run,
  1245. 'cmd.run_all': salt.modules.cmdmod.run_all,
  1246. 'cmd.retcode': salt.modules.cmdmod.retcode,
  1247. 'smbios.get': salt.modules.smbios.get}):
  1248. virtual_grains = core._virtual(osdata)
  1249. self.assertIn('virtual', virtual_grains)
  1250. def test_virtual_has_virtual_grain(self):
  1251. osdata = {'virtual': 'something'}
  1252. (osdata['kernel'], osdata['nodename'],
  1253. osdata['kernelrelease'], osdata['kernelversion'], osdata['cpuarch'], _) = platform.uname()
  1254. with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run,
  1255. 'cmd.run_all': salt.modules.cmdmod.run_all,
  1256. 'cmd.retcode': salt.modules.cmdmod.retcode,
  1257. 'smbios.get': salt.modules.smbios.get}):
  1258. virtual_grains = core._virtual(osdata)
  1259. self.assertIn('virtual', virtual_grains)
  1260. self.assertNotEqual(virtual_grains['virtual'], 'physical')
  1261. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  1262. def test_windows_virtual_set_virtual_grain(self):
  1263. osdata = {}
  1264. (osdata['kernel'], osdata['nodename'],
  1265. osdata['kernelrelease'], osdata['kernelversion'], osdata['cpuarch'], _) = platform.uname()
  1266. with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run,
  1267. 'cmd.run_all': salt.modules.cmdmod.run_all,
  1268. 'cmd.retcode': salt.modules.cmdmod.retcode,
  1269. 'smbios.get': salt.modules.smbios.get}):
  1270. virtual_grains = core._windows_virtual(osdata)
  1271. self.assertIn('virtual', virtual_grains)
  1272. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  1273. def test_windows_virtual_has_virtual_grain(self):
  1274. osdata = {'virtual': 'something'}
  1275. (osdata['kernel'], osdata['nodename'],
  1276. osdata['kernelrelease'], osdata['kernelversion'], osdata['cpuarch'], _) = platform.uname()
  1277. with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run,
  1278. 'cmd.run_all': salt.modules.cmdmod.run_all,
  1279. 'cmd.retcode': salt.modules.cmdmod.retcode,
  1280. 'smbios.get': salt.modules.smbios.get}):
  1281. virtual_grains = core._windows_virtual(osdata)
  1282. self.assertIn('virtual', virtual_grains)
  1283. self.assertNotEqual(virtual_grains['virtual'], 'physical')
  1284. @skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
  1285. def test_osdata_virtual_key_win(self):
  1286. with patch.dict(core.__salt__, {'cmd.run': salt.modules.cmdmod.run,
  1287. 'cmd.run_all': salt.modules.cmdmod.run_all,
  1288. 'cmd.retcode': salt.modules.cmdmod.retcode,
  1289. 'smbios.get': salt.modules.smbios.get}):
  1290. _windows_platform_data_ret = core.os_data()
  1291. _windows_platform_data_ret['virtual'] = 'something'
  1292. with patch.object(core,
  1293. '_windows_platform_data',
  1294. return_value=_windows_platform_data_ret) as _windows_platform_data:
  1295. osdata_grains = core.os_data()
  1296. _windows_platform_data.assert_called_once()
  1297. self.assertIn('virtual', osdata_grains)
  1298. self.assertNotEqual(osdata_grains['virtual'], 'physical')