test_core.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Test the core grains
  4. '''
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing libs
  8. from tests.support.case import ModuleCase
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import skipIf
  11. # Import Salt libs
  12. import salt.loader
  13. import salt.utils.platform
  14. if salt.utils.platform.is_windows():
  15. try:
  16. import salt.modules.reg
  17. except ImportError:
  18. pass
  19. def _freebsd_or_openbsd():
  20. return salt.utils.platform.is_freebsd() or salt.utils.platform.is_openbsd()
  21. class TestGrainsCore(ModuleCase):
  22. '''
  23. Test the core grains grains
  24. '''
  25. @skipIf(not _freebsd_or_openbsd(), 'Only run on FreeBSD or OpenBSD')
  26. def test_freebsd_openbsd_mem_total(self):
  27. '''
  28. test grains['mem_total']
  29. '''
  30. physmem = self.run_function('sysctl.get', ['hw.physmem'])
  31. self.assertEqual(
  32. self.run_function('grains.items')['mem_total'],
  33. int(physmem) / 1048576
  34. )
  35. @skipIf(not salt.utils.platform.is_openbsd(), 'Only run on OpenBSD')
  36. def test_openbsd_swap_total(self):
  37. '''
  38. test grains['swap_total']
  39. '''
  40. swapmem = self.run_function('cmd.run', ['swapctl -sk']).split(' ')[1]
  41. self.assertEqual(
  42. self.run_function('grains.items')['swap_total'],
  43. int(swapmem) / 1048576
  44. )
  45. class TestGrainsReg(ModuleCase, LoaderModuleMockMixin):
  46. '''
  47. Test the core windows grains
  48. '''
  49. def setup_loader_modules(self):
  50. self.opts = opts = salt.config.DEFAULT_MINION_OPTS
  51. utils = salt.loader.utils(opts, whitelist=['reg'])
  52. return {
  53. salt.modules.reg: {
  54. '__opts__': opts,
  55. '__utils__': utils,
  56. }
  57. }
  58. @skipIf(not salt.utils.platform.is_windows(), 'Only run on Windows')
  59. def test_win_cpu_model(self):
  60. '''
  61. test grains['cpu_model']
  62. '''
  63. cpu_model_text = salt.modules.reg.read_value(
  64. 'HKEY_LOCAL_MACHINE',
  65. 'HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0',
  66. 'ProcessorNameString').get('vdata')
  67. self.assertEqual(
  68. self.run_function('grains.items')['cpu_model'],
  69. cpu_model_text
  70. )