1
0

test_environment.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # coding=utf-8
  2. '''
  3. Test case for utils/__init__.py
  4. '''
  5. from __future__ import unicode_literals, print_function, absolute_import
  6. from tests.support.unit import TestCase, skipIf
  7. import salt.utils.environment
  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': {
  37. 'modules': {
  38. 'system': {
  39. '_': expectation,
  40. },
  41. },
  42. },
  43. },
  44. '__file__': '/daemons/loose/modules/system.py',
  45. }
  46. assert salt.utils.environment.get_module_environment(_globals) == expectation
  47. def test_get_module_environment_pillars(self):
  48. '''
  49. Test for salt.utils.get_module_environment
  50. Test if __pillar__ is visible.
  51. :return:
  52. '''
  53. expectation = {'message': 'The CPU has shifted, and become decentralized.'}
  54. _globals = {
  55. '__opts__': {
  56. 'system-environment': {
  57. 'electric': {
  58. 'interference': {
  59. '_': expectation,
  60. },
  61. },
  62. },
  63. },
  64. '__file__': '/piezo/electric/interference.py',
  65. }
  66. assert salt.utils.environment.get_module_environment(_globals) == expectation
  67. def test_get_module_environment_pillar_override(self):
  68. '''
  69. Test for salt.utils.get_module_environment
  70. Test if __pillar__ is overriding __opts__.
  71. :return:
  72. '''
  73. expectation = {'msg': 'The CPU has shifted, and become decentralized.'}
  74. _globals = {
  75. '__opts__': {
  76. 'system-environment': {
  77. 'electric': {
  78. 'interference': {
  79. '_': {'msg': 'Trololo!'},
  80. },
  81. },
  82. },
  83. },
  84. '__pillar__': {
  85. 'system-environment': {
  86. 'electric': {
  87. 'interference': {
  88. '_': expectation,
  89. },
  90. },
  91. },
  92. },
  93. '__file__': '/piezo/electric/interference.py',
  94. }
  95. assert salt.utils.environment.get_module_environment(_globals) == expectation
  96. def test_get_module_environment_sname_found(self):
  97. '''
  98. Test for salt.utils.get_module_environment
  99. Section name and module name are found.
  100. :return:
  101. '''
  102. expectation = {'msg': 'All operators are on strike due to broken coffee machine!'}
  103. _globals = {
  104. '__opts__': {
  105. 'system-environment': {
  106. 'jumping': {
  107. 'interference': {
  108. '_': expectation,
  109. },
  110. },
  111. },
  112. },
  113. '__file__': '/route/flapping/at_the_nap.py',
  114. }
  115. assert salt.utils.environment.get_module_environment(_globals) == {}
  116. _globals['__file__'] = '/route/jumping/interference.py'
  117. assert salt.utils.environment.get_module_environment(_globals) == expectation
  118. def test_get_module_environment_mname_found(self):
  119. '''
  120. Test for salt.utils.get_module_environment
  121. Module name is found.
  122. :return:
  123. '''
  124. expectation = {'msg': 'All operators are on strike due to broken coffee machine!'}
  125. _globals = {
  126. '__pillar__': {
  127. 'system-environment': {
  128. 'jumping': {
  129. 'nonsense': {
  130. '_': expectation,
  131. },
  132. },
  133. },
  134. },
  135. '__file__': '/route/jumping/interference.py',
  136. }
  137. assert salt.utils.environment.get_module_environment(_globals) == {}
  138. _globals['__pillar__']['system-environment']['jumping']['interference'] = {}
  139. _globals['__pillar__']['system-environment']['jumping']['interference']['_'] = expectation
  140. assert salt.utils.environment.get_module_environment(_globals) == expectation
  141. def test_get_module_environment_vname_found(self):
  142. '''
  143. Test for salt.utils.get_module_environment
  144. Virtual name is found.
  145. :return:
  146. '''
  147. expectation = {'msg': 'All operators are on strike due to broken coffee machine!'}
  148. _globals = {
  149. '__virtualname__': 'nonsense',
  150. '__pillar__': {
  151. 'system-environment': {
  152. 'jumping': {
  153. 'nonsense': {
  154. '_': expectation,
  155. },
  156. },
  157. },
  158. },
  159. '__file__': '/route/jumping/translation.py',
  160. }
  161. assert salt.utils.environment.get_module_environment(_globals) == expectation
  162. def test_get_module_environment_vname_overridden(self):
  163. '''
  164. Test for salt.utils.get_module_environment
  165. Virtual namespace overridden.
  166. :return:
  167. '''
  168. expectation = {'msg': 'New management.'}
  169. _globals = {
  170. '__virtualname__': 'nonsense',
  171. '__pillar__': {
  172. 'system-environment': {
  173. 'funny': {
  174. 'translation': {
  175. '_': expectation,
  176. },
  177. 'nonsense': {
  178. '_': {'msg': 'This is wrong'},
  179. },
  180. },
  181. },
  182. },
  183. '__file__': '/lost/in/funny/translation.py',
  184. }
  185. assert salt.utils.environment.get_module_environment(_globals) == expectation