test_core.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import skipIf
  12. if salt.utils.platform.is_windows():
  13. try:
  14. import salt.modules.reg
  15. except ImportError:
  16. pass
  17. def _freebsd_or_openbsd():
  18. return salt.utils.platform.is_freebsd() or salt.utils.platform.is_openbsd()
  19. @pytest.mark.windows_whitelisted
  20. class TestGrainsCore(ModuleCase):
  21. """
  22. Test the core grains grains
  23. """
  24. @skipIf(not _freebsd_or_openbsd(), "Only run on FreeBSD or OpenBSD")
  25. def test_freebsd_openbsd_mem_total(self):
  26. """
  27. test grains['mem_total']
  28. """
  29. physmem = self.run_function("sysctl.get", ["hw.physmem"])
  30. self.assertEqual(
  31. self.run_function("grains.items")["mem_total"], int(physmem) // 1048576
  32. )
  33. @skipIf(not salt.utils.platform.is_openbsd(), "Only run on OpenBSD")
  34. def test_openbsd_swap_total(self):
  35. """
  36. test grains['swap_total']
  37. """
  38. swapmem = self.run_function("cmd.run", ["swapctl -sk"]).split(" ")[1]
  39. self.assertEqual(
  40. self.run_function("grains.items")["swap_total"], int(swapmem) // 1048576
  41. )
  42. @pytest.mark.windows_whitelisted
  43. class TestGrainsReg(ModuleCase, LoaderModuleMockMixin):
  44. """
  45. Test the core windows grains
  46. """
  47. def setup_loader_modules(self):
  48. self.opts = opts = salt.config.DEFAULT_MINION_OPTS.copy()
  49. utils = salt.loader.utils(opts, whitelist=["reg"])
  50. return {salt.modules.reg: {"__opts__": opts, "__utils__": utils}}
  51. @skipIf(not salt.utils.platform.is_windows(), "Only run on Windows")
  52. @skipIf(True, "SLOWTEST skip")
  53. def test_win_cpu_model(self):
  54. """
  55. test grains['cpu_model']
  56. """
  57. cpu_model_text = salt.modules.reg.read_value(
  58. "HKEY_LOCAL_MACHINE",
  59. "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
  60. "ProcessorNameString",
  61. ).get("vdata")
  62. self.assertEqual(self.run_function("grains.items")["cpu_model"], cpu_model_text)