test_core.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. """
  3. Test the core grains
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import pytest
  7. import salt.loader
  8. import salt.utils.platform
  9. from tests.support.case import ModuleCase
  10. from tests.support.helpers import slowTest
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.unit import skipIf
  13. if salt.utils.platform.is_windows():
  14. try:
  15. import salt.modules.reg
  16. except ImportError:
  17. pass
  18. def _freebsd_or_openbsd():
  19. return salt.utils.platform.is_freebsd() or salt.utils.platform.is_openbsd()
  20. @pytest.mark.windows_whitelisted
  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"], int(physmem) // 1048576
  33. )
  34. @skipIf(not salt.utils.platform.is_openbsd(), "Only run on OpenBSD")
  35. def test_openbsd_swap_total(self):
  36. """
  37. test grains['swap_total']
  38. """
  39. swapmem = self.run_function("cmd.run", ["swapctl -sk"]).split(" ")[1]
  40. self.assertEqual(
  41. self.run_function("grains.items")["swap_total"], int(swapmem) // 1048576
  42. )
  43. @pytest.mark.windows_whitelisted
  44. class TestGrainsReg(ModuleCase, LoaderModuleMockMixin):
  45. """
  46. Test the core windows grains
  47. """
  48. def setup_loader_modules(self):
  49. self.opts = opts = salt.config.DEFAULT_MINION_OPTS.copy()
  50. utils = salt.loader.utils(opts, whitelist=["reg"])
  51. return {salt.modules.reg: {"__opts__": opts, "__utils__": utils}}
  52. @skipIf(not salt.utils.platform.is_windows(), "Only run on Windows")
  53. @slowTest
  54. def test_win_cpu_model(self):
  55. """
  56. test grains['cpu_model']
  57. """
  58. cpu_model_text = salt.modules.reg.read_value(
  59. "HKEY_LOCAL_MACHINE",
  60. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  61. "ProcessorNameString",
  62. ).get("vdata")
  63. self.assertEqual(self.run_function("grains.items")["cpu_model"], cpu_model_text)