test_environment.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # coding=utf-8
  2. """
  3. Test case for utils/__init__.py
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import salt.utils.environment
  7. from tests.support.unit import TestCase, skipIf
  8. try:
  9. import pytest
  10. except ImportError:
  11. pytest = None
  12. @skipIf(pytest is None, "PyTest is missing")
  13. class UtilsTestCase(TestCase):
  14. """
  15. Test case for utils/__init__.py
  16. """
  17. def test_get_module_environment_empty(self):
  18. """
  19. Test for salt.utils.get_module_environment
  20. Test if empty globals returns to an empty environment
  21. with the correct type.
  22. :return:
  23. """
  24. out = salt.utils.environment.get_module_environment({})
  25. assert out == {}
  26. assert isinstance(out, dict)
  27. def test_get_module_environment_opts(self):
  28. """
  29. Test for salt.utils.get_module_environment
  30. Test if __opts__ are visible.
  31. :return:
  32. """
  33. expectation = {"message": "Melting hard drives"}
  34. _globals = {
  35. "__opts__": {
  36. "system-environment": {"modules": {"system": {"_": expectation}}},
  37. },
  38. "__file__": "/daemons/loose/modules/system.py",
  39. }
  40. assert salt.utils.environment.get_module_environment(_globals) == expectation
  41. def test_get_module_environment_pillars(self):
  42. """
  43. Test for salt.utils.get_module_environment
  44. Test if __pillar__ is visible.
  45. :return:
  46. """
  47. expectation = {"message": "The CPU has shifted, and become decentralized."}
  48. _globals = {
  49. "__opts__": {
  50. "system-environment": {
  51. "electric": {"interference": {"_": expectation}},
  52. },
  53. },
  54. "__file__": "/piezo/electric/interference.py",
  55. }
  56. assert salt.utils.environment.get_module_environment(_globals) == expectation
  57. def test_get_module_environment_pillar_override(self):
  58. """
  59. Test for salt.utils.get_module_environment
  60. Test if __pillar__ is overriding __opts__.
  61. :return:
  62. """
  63. expectation = {"msg": "The CPU has shifted, and become decentralized."}
  64. _globals = {
  65. "__opts__": {
  66. "system-environment": {
  67. "electric": {"interference": {"_": {"msg": "Trololo!"}}},
  68. },
  69. },
  70. "__pillar__": {
  71. "system-environment": {
  72. "electric": {"interference": {"_": expectation}},
  73. },
  74. },
  75. "__file__": "/piezo/electric/interference.py",
  76. }
  77. assert salt.utils.environment.get_module_environment(_globals) == expectation
  78. def test_get_module_environment_sname_found(self):
  79. """
  80. Test for salt.utils.get_module_environment
  81. Section name and module name are found.
  82. :return:
  83. """
  84. expectation = {
  85. "msg": "All operators are on strike due to broken coffee machine!"
  86. }
  87. _globals = {
  88. "__opts__": {
  89. "system-environment": {
  90. "jumping": {"interference": {"_": expectation}},
  91. },
  92. },
  93. "__file__": "/route/flapping/at_the_nap.py",
  94. }
  95. assert salt.utils.environment.get_module_environment(_globals) == {}
  96. _globals["__file__"] = "/route/jumping/interference.py"
  97. assert salt.utils.environment.get_module_environment(_globals) == expectation
  98. def test_get_module_environment_mname_found(self):
  99. """
  100. Test for salt.utils.get_module_environment
  101. Module name is found.
  102. :return:
  103. """
  104. expectation = {
  105. "msg": "All operators are on strike due to broken coffee machine!"
  106. }
  107. _globals = {
  108. "__pillar__": {
  109. "system-environment": {"jumping": {"nonsense": {"_": expectation}}},
  110. },
  111. "__file__": "/route/jumping/interference.py",
  112. }
  113. assert salt.utils.environment.get_module_environment(_globals) == {}
  114. _globals["__pillar__"]["system-environment"]["jumping"]["interference"] = {}
  115. _globals["__pillar__"]["system-environment"]["jumping"]["interference"][
  116. "_"
  117. ] = expectation
  118. assert salt.utils.environment.get_module_environment(_globals) == expectation
  119. def test_get_module_environment_vname_found(self):
  120. """
  121. Test for salt.utils.get_module_environment
  122. Virtual name is found.
  123. :return:
  124. """
  125. expectation = {
  126. "msg": "All operators are on strike due to broken coffee machine!"
  127. }
  128. _globals = {
  129. "__virtualname__": "nonsense",
  130. "__pillar__": {
  131. "system-environment": {"jumping": {"nonsense": {"_": expectation}}},
  132. },
  133. "__file__": "/route/jumping/translation.py",
  134. }
  135. assert salt.utils.environment.get_module_environment(_globals) == expectation
  136. def test_get_module_environment_vname_overridden(self):
  137. """
  138. Test for salt.utils.get_module_environment
  139. Virtual namespace overridden.
  140. :return:
  141. """
  142. expectation = {"msg": "New management."}
  143. _globals = {
  144. "__virtualname__": "nonsense",
  145. "__pillar__": {
  146. "system-environment": {
  147. "funny": {
  148. "translation": {"_": expectation},
  149. "nonsense": {"_": {"msg": "This is wrong"}},
  150. },
  151. },
  152. },
  153. "__file__": "/lost/in/funny/translation.py",
  154. }
  155. assert salt.utils.environment.get_module_environment(_globals) == expectation