2014-11-19 22:56:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
2015-02-06 15:40:08 +00:00
|
|
|
import os
|
2014-11-21 19:05:13 +00:00
|
|
|
|
2014-11-19 22:56:09 +00:00
|
|
|
# Import Salt Testing libs
|
2017-02-19 15:35:30 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2018-03-27 21:10:24 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.mock import (
|
2015-08-31 11:47:29 +00:00
|
|
|
MagicMock,
|
2015-09-01 11:56:43 +00:00
|
|
|
patch
|
2015-08-31 11:47:29 +00:00
|
|
|
)
|
2014-11-19 22:56:09 +00:00
|
|
|
# Import salt libs
|
|
|
|
import salt.states.environ as envstate
|
|
|
|
import salt.modules.environ as envmodule
|
2018-03-29 03:54:35 +00:00
|
|
|
import salt.modules.reg
|
|
|
|
import salt.utils
|
2014-11-19 22:56:09 +00:00
|
|
|
|
2015-02-06 15:40:08 +00:00
|
|
|
|
2017-02-19 15:35:30 +00:00
|
|
|
class TestEnvironState(TestCase, LoaderModuleMockMixin):
|
2015-02-06 17:46:31 +00:00
|
|
|
|
2017-03-22 12:12:36 +00:00
|
|
|
def setup_loader_modules(self):
|
|
|
|
loader_globals = {
|
2017-02-19 15:35:30 +00:00
|
|
|
'__env__': 'base',
|
|
|
|
'__opts__': {'test': False},
|
2018-03-26 19:00:39 +00:00
|
|
|
'__salt__': {
|
|
|
|
'environ.setenv': envmodule.setenv,
|
2018-03-29 03:54:35 +00:00
|
|
|
'reg.read_value': salt.modules.reg.read_value,
|
2018-03-26 19:00:39 +00:00
|
|
|
}
|
2017-02-19 15:35:30 +00:00
|
|
|
}
|
2017-03-22 12:12:36 +00:00
|
|
|
return {envstate: loader_globals, envmodule: loader_globals}
|
2014-11-19 22:56:09 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-03-22 18:25:22 +00:00
|
|
|
patcher = patch.dict(os.environ, {'INITIAL': 'initial'}, clear=True)
|
2018-03-22 03:00:18 +00:00
|
|
|
patcher.start()
|
2018-03-22 20:47:41 +00:00
|
|
|
|
2018-03-22 03:00:18 +00:00
|
|
|
def reset_environ(patcher):
|
|
|
|
patcher.stop()
|
2018-03-22 20:47:41 +00:00
|
|
|
|
2018-03-22 03:00:18 +00:00
|
|
|
self.addCleanup(reset_environ, patcher)
|
2014-11-19 22:56:09 +00:00
|
|
|
|
|
|
|
def test_setenv(self):
|
|
|
|
'''test that a subsequent calls of setenv changes nothing'''
|
|
|
|
|
|
|
|
ret = envstate.setenv('test', 'value')
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value'})
|
|
|
|
|
|
|
|
ret = envstate.setenv('test', 'other')
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'other'})
|
|
|
|
|
|
|
|
# once again with the same value
|
|
|
|
ret = envstate.setenv('test', 'other')
|
|
|
|
self.assertEqual(ret['changes'], {})
|
|
|
|
|
2018-03-29 03:54:35 +00:00
|
|
|
@skipIf(not salt.utils.is_windows(), 'Windows only')
|
2015-08-31 11:47:29 +00:00
|
|
|
def test_setenv_permanent(self):
|
2018-03-27 18:38:20 +00:00
|
|
|
'''test that we can set perminent environment variables (requires pywin32)'''
|
|
|
|
with patch.dict(envmodule.__salt__, {'reg.set_value': MagicMock(), 'reg.delete_value': MagicMock()}):
|
2015-08-31 11:47:29 +00:00
|
|
|
ret = envstate.setenv('test', 'value', permanent=True)
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value'})
|
|
|
|
envmodule.__salt__['reg.set_value'].assert_called_with("HKCU", "Environment", 'test', 'value')
|
|
|
|
|
|
|
|
ret = envstate.setenv('test', False, false_unsets=True, permanent=True)
|
|
|
|
self.assertEqual(ret['changes'], {'test': None})
|
|
|
|
envmodule.__salt__['reg.delete_value'].assert_called_with("HKCU", "Environment", 'test')
|
|
|
|
|
2014-11-19 22:56:09 +00:00
|
|
|
def test_setenv_dict(self):
|
|
|
|
'''test that setenv can be invoked with dict'''
|
|
|
|
ret = envstate.setenv('notimportant', {'test': 'value'})
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value'})
|
|
|
|
|
|
|
|
def test_setenv_int(self):
|
|
|
|
'''test that setenv can not be invoked with int
|
|
|
|
(actually it's anything other than strings and dict)'''
|
|
|
|
ret = envstate.setenv('test', 1)
|
|
|
|
self.assertEqual(ret['result'], False)
|
|
|
|
|
|
|
|
def test_setenv_unset(self):
|
|
|
|
'''test that ``false_unsets`` option removes variable from environment'''
|
|
|
|
ret = envstate.setenv('test', 'value')
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value'})
|
|
|
|
|
|
|
|
ret = envstate.setenv('notimportant', {'test': False}, false_unsets=True)
|
|
|
|
self.assertEqual(ret['changes'], {'test': None})
|
|
|
|
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial'})
|
|
|
|
|
|
|
|
def test_setenv_clearall(self):
|
|
|
|
'''test that ``clear_all`` option sets other values to '' '''
|
|
|
|
ret = envstate.setenv('test', 'value', clear_all=True)
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value', 'INITIAL': ''})
|
2018-03-29 03:54:35 +00:00
|
|
|
if salt.utils.is_windows():
|
2018-03-22 18:25:22 +00:00
|
|
|
self.assertEqual(envstate.os.environ, {'TEST': 'value', 'INITIAL': ''})
|
|
|
|
else:
|
|
|
|
self.assertEqual(envstate.os.environ, {'test': 'value', 'INITIAL': ''})
|
2014-11-19 22:56:09 +00:00
|
|
|
|
|
|
|
def test_setenv_clearall_with_unset(self):
|
|
|
|
'''test that ``clear_all`` option combined with ``false_unsets``
|
|
|
|
unsets other values from environment'''
|
|
|
|
ret = envstate.setenv('test', 'value', false_unsets=True, clear_all=True)
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value', 'INITIAL': None})
|
2018-03-29 03:54:35 +00:00
|
|
|
if salt.utils.is_windows():
|
2018-03-22 18:25:22 +00:00
|
|
|
self.assertEqual(envstate.os.environ, {'TEST': 'value'})
|
|
|
|
else:
|
|
|
|
self.assertEqual(envstate.os.environ, {'test': 'value'})
|
2014-11-19 22:56:09 +00:00
|
|
|
|
|
|
|
def test_setenv_unset_multi(self):
|
|
|
|
'''test basically same things that above tests but with multiple values passed'''
|
|
|
|
ret = envstate.setenv('notimportant', {'foo': 'bar'})
|
|
|
|
self.assertEqual(ret['changes'], {'foo': 'bar'})
|
|
|
|
|
2017-07-11 19:23:11 +00:00
|
|
|
with patch.dict(envstate.__salt__, {'reg.read_value': MagicMock()}):
|
|
|
|
ret = envstate.setenv(
|
|
|
|
'notimportant', {'test': False, 'foo': 'baz'}, false_unsets=True)
|
2014-11-19 22:56:09 +00:00
|
|
|
self.assertEqual(ret['changes'], {'test': None, 'foo': 'baz'})
|
2018-03-29 03:54:35 +00:00
|
|
|
if salt.utils.is_windows():
|
2018-03-22 18:25:22 +00:00
|
|
|
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'FOO': 'baz'})
|
|
|
|
else:
|
|
|
|
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'foo': 'baz'})
|
2014-11-19 22:56:09 +00:00
|
|
|
|
2017-07-11 19:23:11 +00:00
|
|
|
with patch.dict(envstate.__salt__, {'reg.read_value': MagicMock()}):
|
|
|
|
ret = envstate.setenv('notimportant', {'test': False, 'foo': 'bax'})
|
2014-11-19 22:56:09 +00:00
|
|
|
self.assertEqual(ret['changes'], {'test': '', 'foo': 'bax'})
|
2018-03-29 03:54:35 +00:00
|
|
|
if salt.utils.is_windows():
|
2018-03-22 18:25:22 +00:00
|
|
|
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'FOO': 'bax', 'TEST': ''})
|
|
|
|
else:
|
|
|
|
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'foo': 'bax', 'test': ''})
|
2014-11-19 22:56:09 +00:00
|
|
|
|
|
|
|
def test_setenv_test_mode(self):
|
|
|
|
'''test that imitating action returns good values'''
|
2017-03-31 17:42:25 +00:00
|
|
|
with patch.dict(envstate.__opts__, {'test': True}):
|
|
|
|
ret = envstate.setenv('test', 'value')
|
|
|
|
self.assertEqual(ret['changes'], {'test': 'value'})
|
|
|
|
ret = envstate.setenv('INITIAL', 'initial')
|
|
|
|
self.assertEqual(ret['changes'], {})
|