123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- # coding=utf-8
- '''
- Test case for utils/__init__.py
- '''
- from __future__ import unicode_literals, print_function, absolute_import
- from tests.support.unit import TestCase, skipIf
- import salt.utils.environment
- try:
- import pytest
- except ImportError:
- pytest = None
- @skipIf(pytest is None, 'PyTest is missing')
- class UtilsTestCase(TestCase):
- '''
- Test case for utils/__init__.py
- '''
- def test_get_module_environment_empty(self):
- '''
- Test for salt.utils.get_module_environment
- Test if empty globals returns to an empty environment
- with the correct type.
- :return:
- '''
- out = salt.utils.environment.get_module_environment({})
- assert out == {}
- assert isinstance(out, dict)
- def test_get_module_environment_opts(self):
- '''
- Test for salt.utils.get_module_environment
- Test if __opts__ are visible.
- :return:
- '''
- expectation = {'message': 'Melting hard drives'}
- _globals = {
- '__opts__': {
- 'system-environment': {
- 'modules': {
- 'system': {
- '_': expectation,
- },
- },
- },
- },
- '__file__': '/daemons/loose/modules/system.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == expectation
- def test_get_module_environment_pillars(self):
- '''
- Test for salt.utils.get_module_environment
- Test if __pillar__ is visible.
- :return:
- '''
- expectation = {'message': 'The CPU has shifted, and become decentralized.'}
- _globals = {
- '__opts__': {
- 'system-environment': {
- 'electric': {
- 'interference': {
- '_': expectation,
- },
- },
- },
- },
- '__file__': '/piezo/electric/interference.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == expectation
- def test_get_module_environment_pillar_override(self):
- '''
- Test for salt.utils.get_module_environment
- Test if __pillar__ is overriding __opts__.
- :return:
- '''
- expectation = {'msg': 'The CPU has shifted, and become decentralized.'}
- _globals = {
- '__opts__': {
- 'system-environment': {
- 'electric': {
- 'interference': {
- '_': {'msg': 'Trololo!'},
- },
- },
- },
- },
- '__pillar__': {
- 'system-environment': {
- 'electric': {
- 'interference': {
- '_': expectation,
- },
- },
- },
- },
- '__file__': '/piezo/electric/interference.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == expectation
- def test_get_module_environment_sname_found(self):
- '''
- Test for salt.utils.get_module_environment
- Section name and module name are found.
- :return:
- '''
- expectation = {'msg': 'All operators are on strike due to broken coffee machine!'}
- _globals = {
- '__opts__': {
- 'system-environment': {
- 'jumping': {
- 'interference': {
- '_': expectation,
- },
- },
- },
- },
- '__file__': '/route/flapping/at_the_nap.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == {}
- _globals['__file__'] = '/route/jumping/interference.py'
- assert salt.utils.environment.get_module_environment(_globals) == expectation
- def test_get_module_environment_mname_found(self):
- '''
- Test for salt.utils.get_module_environment
- Module name is found.
- :return:
- '''
- expectation = {'msg': 'All operators are on strike due to broken coffee machine!'}
- _globals = {
- '__pillar__': {
- 'system-environment': {
- 'jumping': {
- 'nonsense': {
- '_': expectation,
- },
- },
- },
- },
- '__file__': '/route/jumping/interference.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == {}
- _globals['__pillar__']['system-environment']['jumping']['interference'] = {}
- _globals['__pillar__']['system-environment']['jumping']['interference']['_'] = expectation
- assert salt.utils.environment.get_module_environment(_globals) == expectation
- def test_get_module_environment_vname_found(self):
- '''
- Test for salt.utils.get_module_environment
- Virtual name is found.
- :return:
- '''
- expectation = {'msg': 'All operators are on strike due to broken coffee machine!'}
- _globals = {
- '__virtualname__': 'nonsense',
- '__pillar__': {
- 'system-environment': {
- 'jumping': {
- 'nonsense': {
- '_': expectation,
- },
- },
- },
- },
- '__file__': '/route/jumping/translation.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == expectation
- def test_get_module_environment_vname_overridden(self):
- '''
- Test for salt.utils.get_module_environment
- Virtual namespace overridden.
- :return:
- '''
- expectation = {'msg': 'New management.'}
- _globals = {
- '__virtualname__': 'nonsense',
- '__pillar__': {
- 'system-environment': {
- 'funny': {
- 'translation': {
- '_': expectation,
- },
- 'nonsense': {
- '_': {'msg': 'This is wrong'},
- },
- },
- },
- },
- '__file__': '/lost/in/funny/translation.py',
- }
- assert salt.utils.environment.get_module_environment(_globals) == expectation
|