test_introspect.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.introspect as introspect
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class IntrospectTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.introspect
  16. """
  17. def setup_loader_modules(self):
  18. return {introspect: {}}
  19. # 'running_service_owners' function tests: 1
  20. def test_running_service_owners(self):
  21. """
  22. Test if it determine which packages own the currently running services.
  23. """
  24. err1 = (
  25. "The module for the package manager on this system does not"
  26. " support looking up which package(s) owns which file(s)"
  27. )
  28. err2 = (
  29. "The file module on this system does not "
  30. "support looking up open files on the system"
  31. )
  32. ret = {
  33. "Error": {
  34. "Unsupported File Module": "{0}".format(err2),
  35. "Unsupported Package Manager": "{0}".format(err1),
  36. }
  37. }
  38. self.assertDictEqual(introspect.running_service_owners(), ret)
  39. mock = MagicMock(return_value={})
  40. with patch.dict(
  41. introspect.__salt__,
  42. {"pkg.owner": mock, "file.open_files": mock, "service.execs": mock},
  43. ):
  44. self.assertDictEqual(introspect.running_service_owners(), {})
  45. # 'enabled_service_owners' function tests: 1
  46. def test_enabled_service_owners(self):
  47. """
  48. Test if it return which packages own each of the services
  49. that are currently enabled.
  50. """
  51. err1 = (
  52. "The module for the package manager on this system does not"
  53. " support looking up which package(s) owns which file(s)"
  54. )
  55. err2 = (
  56. "The module for the service manager on this system does not"
  57. " support showing descriptive service data"
  58. )
  59. ret = {
  60. "Error": {
  61. "Unsupported Service Manager": "{0}".format(err2),
  62. "Unsupported Package Manager": "{0}".format(err1),
  63. }
  64. }
  65. self.assertDictEqual(introspect.enabled_service_owners(), ret)
  66. mock = MagicMock(return_value={})
  67. with patch.dict(
  68. introspect.__salt__,
  69. {"pkg.owner": mock, "service.show": mock, "service.get_enabled": mock},
  70. ):
  71. self.assertDictEqual(introspect.enabled_service_owners(), {})
  72. # 'service_highstate' function tests: 1
  73. def test_service_highstate(self):
  74. """
  75. Test if it return running and enabled services in a highstate structure.
  76. """
  77. with patch(
  78. "salt.modules.introspect.running_service_owners", MagicMock(return_value={})
  79. ), patch(
  80. "salt.modules.introspect.enabled_service_owners", MagicMock(return_value={})
  81. ):
  82. self.assertDictEqual(introspect.service_highstate(), {})