Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import salt.states.logrotate as logrotate
import salt.utils.platform
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
Mock,
patch,
NO_MOCK,
NO_MOCK_REASON
)
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(salt.utils.platform.is_windows(), 'Only works on POSIX-like systems')
class LogrotateTestMakeFile(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.states.logrotate
'''
def setup_loader_modules(self):
return {logrotate: {}}
def test_make_file(self):
with_block_support = Mock(__enter__=Mock(), __exit__=Mock())
with patch.dict(logrotate.__opts__, {'test': True}, clear=True):
with patch.object(logrotate.os.path, 'isfile', return_value=False) as isfile:
with patch.object(logrotate.salt.utils.files, 'fopen', return_value=with_block_support) as fopen:
with patch.dict(logrotate.__salt__, {'logrotate.get': Mock(return_value=None)}, clear=True):
ret = logrotate.set_('logrotate-wtmp-rotate',
'/var/log/wtmp',
'rotate',
def test_setup_kubeconfig_data_overwrite(self):
'''
Test that provided `kubernetes.kubeconfig` configuration is overwritten
by provided kubeconfig_data in the command
:return:
'''
with mock_kubernetes_library() as mock_kubernetes_lib:
with patch.dict(kubernetes.__salt__, {'config.option': Mock(side_effect=self.settings)}):
mock_kubernetes_lib.config.load_kube_config = Mock()
config = kubernetes._setup_conn(kubeconfig_data='MTIzNDU2Nzg5MAo=', context='newcontext')
check_path = os.path.join('/tmp', 'salt-kubeconfig-')
if salt.utils.platform.is_windows():
check_path = os.path.join(os.environ.get('TMP'), 'salt-kubeconfig-')
elif salt.utils.platform.is_darwin():
check_path = os.path.join(os.environ.get('TMPDIR', '/tmp'), 'salt-kubeconfig-')
self.assertTrue(config['kubeconfig'].lower().startswith(check_path.lower()))
self.assertTrue(os.path.exists(config['kubeconfig']))
with salt.utils.files.fopen(config['kubeconfig'], 'r') as kcfg:
self.assertEqual('1234567890\n', kcfg.read())
kubernetes._cleanup(**config)
ret = envstate.setenv('notimportant', {'foo': 'bar'})
self.assertEqual(ret['changes'], {'foo': 'bar'})
with patch.dict(envstate.__salt__, {'reg.read_value': MagicMock()}):
ret = envstate.setenv(
'notimportant', {'test': False, 'foo': 'baz'}, false_unsets=True)
self.assertEqual(ret['changes'], {'test': None, 'foo': 'baz'})
if salt.utils.platform.is_windows():
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'FOO': 'baz'})
else:
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'foo': 'baz'})
with patch.dict(envstate.__salt__, {'reg.read_value': MagicMock()}):
ret = envstate.setenv('notimportant', {'test': False, 'foo': 'bax'})
self.assertEqual(ret['changes'], {'test': '', 'foo': 'bax'})
if salt.utils.platform.is_windows():
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'FOO': 'bax', 'TEST': ''})
else:
self.assertEqual(envstate.os.environ, {'INITIAL': 'initial', 'foo': 'bax', 'test': ''})
@skipIf(salt.utils.platform.is_darwin() and six.PY2, 'This test hangs on OS X on Py2')
def test_salt_contains_function(self):
'''
Test if we are able to check if a function exists inside the "salt"
wrapper (AliasLoader) which is available on Jinja templates.
'''
ret = self.run_function('state.sls', ['jinja_salt_contains_function'])
for state_ret in ret.values():
self.assertTrue(state_ret['result'])
import salt.utils.win_dacl as win_dacl
import salt.utils.win_reg as win_reg
try:
import pywintypes
import win32security
HAS_WIN32 = True
except ImportError:
HAS_WIN32 = False
FAKE_KEY = 'SOFTWARE\\{0}'.format(generate_random_name('SaltTesting-'))
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(not HAS_WIN32, 'Requires pywin32')
@skipIf(not salt.utils.platform.is_windows(), 'System is not Windows')
class WinDaclTestCase(TestCase):
'''
Test cases for salt.utils.win_dacl in the registry
'''
def test_get_sid_string(self):
'''
Validate getting a pysid object from a name
'''
sid_obj = win_dacl.get_sid('Administrators')
self.assertTrue(
isinstance(sid_obj, pywintypes.SIDType))
self.assertEqual(
win32security.LookupAccountSid(None, sid_obj)[0],
'Administrators')
def test_get_sid_sid_string(self):
@skipIf(salt.utils.platform.is_windows(), 'No verify_env Windows')
def test_verify_env(self):
root_dir = tempfile.mkdtemp(dir=TMP)
var_dir = os.path.join(root_dir, 'var', 'log', 'salt')
key_dir = os.path.join(root_dir, 'key_dir')
verify_env([var_dir], getpass.getuser(), root_dir=root_dir)
self.assertTrue(os.path.exists(var_dir))
dir_stat = os.stat(var_dir)
self.assertEqual(dir_stat.st_uid, os.getuid())
self.assertEqual(dir_stat.st_mode & stat.S_IRWXU, stat.S_IRWXU)
self.assertEqual(dir_stat.st_mode & stat.S_IRWXG, 40)
self.assertEqual(dir_stat.st_mode & stat.S_IRWXO, 5)
opts.update(kwargs)
if 'color' not in opts:
def is_pipe():
'''
Check if sys.stdout is a pipe or not
'''
try:
fileno = sys.stdout.fileno()
except (AttributeError, io.UnsupportedOperation):
fileno = -1 # sys.stdout is StringIO or fake
return not os.isatty(fileno)
if opts.get('force_color', False):
opts['color'] = True
elif opts.get('no_color', False) or is_pipe() or salt.utils.platform.is_windows():
opts['color'] = False
else:
opts['color'] = True
else:
if opts.get('force_color', False):
opts['color'] = True
elif opts.get('no_color', False) or salt.utils.platform.is_windows():
opts['color'] = False
else:
pass
outputters = salt.loader.outputters(opts)
if out not in outputters:
# Since the grains outputter was removed we don't need to fire this
# error when old minions are asking for it
if out != 'grains':
def __virtual__():
if salt.utils.platform.is_darwin():
return __virtualname__
return (False, 'state.profile only available on macOS.')
def __virtual__():
if not salt.utils.platform.is_windows():
return False, 'This module only works on windows'
win_version = __grains__['osfullname']
if '2012' not in win_version and '2016' not in win_version:
return False, 'This module only works with Server 2012 (Win8) or higher'
return __virtualname__
def __virtual__():
'''This module only works using proxy minions.'''
if not HAS_LIBS:
return (
False,
'The following dependencies are required to use the jss modules: '
'python-jss'
)
if salt.utils.platform.is_proxy():
return __virtualname__
return (False, 'The jamf_proxy execution module failed to load: '
'only available on proxy minions.')