salt/tests/unit/utils/test_minions.py

369 lines
14 KiB
Python
Raw Normal View History

2016-07-29 22:31:50 +00:00
# -*- coding: utf-8 -*-
# Import python libs
from __future__ import absolute_import
# Import Salt Libs
Use explicit unicode strings + break up salt.utils This PR is part of what will be an ongoing effort to use explicit unicode strings in Salt. Because Python 3 does not suport Python 2's raw unicode string syntax (i.e. `ur'\d+'`), we must use `salt.utils.locales.sdecode()` to ensure that the raw string is unicode. However, because of how `salt/utils/__init__.py` has evolved into the hulking monstrosity it is today, this means importing a large module in places where it is not needed, which could negatively impact performance. For this reason, this PR also breaks out some of the functions from `salt/utils/__init__.py` into new/existing modules under `salt/utils/`. The long term goal will be that the modules within this directory do not depend on importing `salt.utils`. A summary of the changes in this PR is as follows: * Moves the following functions from `salt.utils` to new locations (including a deprecation warning if invoked from `salt.utils`): `to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`, `dequote`, `is_hex`, `is_bin_str`, `rand_string`, `contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`, `which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`, `is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`, `is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`, `is_openbsd`, `is_aix` * Moves the functions already deprecated by @rallytime to the bottom of `salt/utils/__init__.py` for better organization, so we can keep the deprecated ones separate from the ones yet to be deprecated as we continue to break up `salt.utils` * Updates `salt/*.py` and all files under `salt/client/` to use explicit unicode string literals. * Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils import foo` becomes `import salt.utils.foo as foo`). * Renames the `test.rand_str` function to `test.random_hash` to more accurately reflect what it does * Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`) such that it returns a string matching the passed size. Previously this function would get `size` bytes from `os.urandom()`, base64-encode it, and return the result, which would in most cases not be equal to the passed size.
2017-07-25 01:47:15 +00:00
import salt.utils.minions as minions
2016-07-29 22:31:50 +00:00
# Import Salt Testing Libs
from tests.support.unit import TestCase
from tests.support.mock import (
patch,
MagicMock,
)
2016-07-29 22:31:50 +00:00
NODEGROUPS = {
'group1': 'L@host1,host2,host3',
'group2': ['G@foo:bar', 'or', 'web1*'],
'group3': ['N@group1', 'or', 'N@group2'],
'group4': ['host4', 'host5', 'host6'],
}
EXPECTED = {
'group1': ['L@host1,host2,host3'],
'group2': ['G@foo:bar', 'or', 'web1*'],
'group3': ['(', '(', 'L@host1,host2,host3', ')', 'or', '(', 'G@foo:bar', 'or', 'web1*', ')', ')'],
'group4': ['L@host4,host5,host6'],
}
class MinionsTestCase(TestCase):
'''
TestCase for salt.utils.minions module functions
'''
def test_nodegroup_comp(self):
'''
Test a simple string nodegroup
'''
for nodegroup in NODEGROUPS:
expected = EXPECTED[nodegroup]
ret = minions.nodegroup_comp(nodegroup, NODEGROUPS)
self.assertEqual(ret, expected)
class CkMinionsTestCase(TestCase):
'''
TestCase for salt.utils.minions.CkMinions class
'''
def setUp(self):
self.ckminions = minions.CkMinions({})
def test_spec_check(self):
# Test spec-only rule
auth_list = ['@runner']
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'wheel')
self.assertFalse(ret)
ret = self.ckminions.spec_check(auth_list, 'testarg', {}, 'runner')
mock_ret = {'error': {'name': 'SaltInvocationError',
'message': 'A command invocation error occurred: Check syntax.'}}
self.assertDictEqual(mock_ret, ret)
# Test spec in plural form
auth_list = ['@runners']
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'wheel')
self.assertFalse(ret)
# Test spec with module.function restriction
auth_list = [{'@runner': 'test.arg'}]
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'wheel')
self.assertFalse(ret)
ret = self.ckminions.spec_check(auth_list, 'tes.arg', {}, 'runner')
self.assertFalse(ret)
ret = self.ckminions.spec_check(auth_list, 'test.ar', {}, 'runner')
self.assertFalse(ret)
# Test function name is a regex
auth_list = [{'@runner': 'test.arg.*some'}]
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertFalse(ret)
ret = self.ckminions.spec_check(auth_list, 'test.argsome', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'test.arg_aaa_some', {}, 'runner')
self.assertTrue(ret)
# Test a list of funcs
auth_list = [{'@runner': ['test.arg', 'jobs.active']}]
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'test.active', {}, 'runner')
self.assertFalse(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.arg', {}, 'runner')
self.assertFalse(ret)
# Test args-kwargs rules
auth_list = [{
'@runner': {
'test.arg': {
'args': ['1', '2'],
'kwargs': {
'aaa': 'bbb',
'ccc': 'ddd'
}
}
}
}]
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = {
'arg': ['1', '2', '3'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd', 'zzz': 'zzz'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddc'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1', '3'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = {
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1', '2'],
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
# Test kwargs only
auth_list = [{
'@runner': {
'test.arg': {
'kwargs': {
'aaa': 'bbb',
'ccc': 'ddd'
}
}
}
}]
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
# Test args only
auth_list = [{
'@runner': {
'test.arg': {
'args': ['1', '2']
}
}
}]
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertFalse(ret)
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
# Test list of args
auth_list = [{'@runner': [{'test.arg': [{'args': ['1', '2'],
'kwargs': {'aaa': 'bbb',
'ccc': 'ddd'
}
},
{'args': ['2', '3'],
'kwargs': {'aaa': 'aaa',
'ccc': 'ccc'
}
}]
}]
}]
args = {
'arg': ['1', '2'],
'kwarg': {'aaa': 'bbb', 'ccc': 'ddd'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = {
'arg': ['2', '3'],
'kwarg': {'aaa': 'aaa', 'ccc': 'ccc'}
}
ret = self.ckminions.spec_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
# Test @module form
auth_list = ['@jobs']
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'wheel')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'test.arg', {}, 'runner')
self.assertFalse(ret)
ret = self.ckminions.spec_check(auth_list, 'job.arg', {}, 'runner')
self.assertFalse(ret)
# Test @module: function
auth_list = [{'@jobs': 'active'}]
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'wheel')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.active_jobs', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.activ', {}, 'runner')
self.assertFalse(ret)
# Test @module: [functions]
auth_list = [{'@jobs': ['active', 'li']}]
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.list_jobs', {}, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.last_run', {}, 'runner')
self.assertFalse(ret)
# Test @module: function with args
auth_list = [{'@jobs': {'active': {'args': ['1', '2'],
'kwargs': {'a': 'b', 'c': 'd'}}}}]
args = {'arg': ['1', '2'],
'kwarg': {'a': 'b', 'c': 'd'}}
ret = self.ckminions.spec_check(auth_list, 'jobs.active', args, 'runner')
self.assertTrue(ret)
ret = self.ckminions.spec_check(auth_list, 'jobs.active', {}, 'runner')
self.assertFalse(ret)
@patch('salt.utils.minions.CkMinions._pki_minions', MagicMock(return_value=['alpha', 'beta', 'gamma']))
def test_auth_check(self):
# Test function-only rule
auth_list = ['test.ping']
ret = self.ckminions.auth_check(auth_list, 'test.ping', None, 'alpha')
self.assertTrue(ret)
ret = self.ckminions.auth_check(auth_list, 'test.arg', None, 'alpha')
self.assertFalse(ret)
# Test minion and function
auth_list = [{'alpha': 'test.ping'}]
ret = self.ckminions.auth_check(auth_list, 'test.ping', None, 'alpha')
self.assertTrue(ret)
ret = self.ckminions.auth_check(auth_list, 'test.arg', None, 'alpha')
self.assertFalse(ret)
ret = self.ckminions.auth_check(auth_list, 'test.ping', None, 'beta')
self.assertFalse(ret)
# Test function list
auth_list = [{'*': ['test.*', 'saltutil.cmd']}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', None, 'alpha')
self.assertTrue(ret)
ret = self.ckminions.auth_check(auth_list, 'test.ping', None, 'beta')
self.assertTrue(ret)
ret = self.ckminions.auth_check(auth_list, 'saltutil.cmd', None, 'gamma')
self.assertTrue(ret)
ret = self.ckminions.auth_check(auth_list, 'saltutil.running', None, 'gamma')
self.assertFalse(ret)
# Test an args and kwargs rule
auth_list = [{
'alpha': {
'test.arg': {
'args': ['1', '2'],
'kwargs': {
'aaa': 'bbb',
'ccc': 'ddd'
}
}
}
}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', None, 'runner')
self.assertFalse(ret)
ret = self.ckminions.auth_check(auth_list, 'test.arg', [], 'runner')
self.assertFalse(ret)
args = ['1', '2', {'aaa': 'bbb', 'ccc': 'ddd', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = ['1', '2', '3', {'aaa': 'bbb', 'ccc': 'ddd', 'eee': 'fff', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = ['1', {'aaa': 'bbb', 'ccc': 'ddd', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = ['1', '2', {'aaa': 'bbb', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = ['1', '3', {'aaa': 'bbb', 'ccc': 'ddd', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
args = ['1', '2', {'aaa': 'bbb', 'ccc': 'fff', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertFalse(ret)
# Test kwargs only rule
auth_list = [{
'alpha': {
'test.arg': {
'kwargs': {
'aaa': 'bbb',
'ccc': 'ddd'
}
}
}
}]
args = ['1', '2', {'aaa': 'bbb', 'ccc': 'ddd', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = [{'aaa': 'bbb', 'ccc': 'ddd', 'eee': 'fff', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
# Test args only rule
auth_list = [{
'alpha': {
'test.arg': {
'args': ['1', '2'],
}
}
}]
args = ['1', '2', {'aaa': 'bbb', 'ccc': 'ddd', '__kwarg__': True}]
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)
args = ['1', '2']
ret = self.ckminions.auth_check(auth_list, 'test.arg', args, 'runner')
self.assertTrue(ret)