2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-05-23 14:31:58 +00:00
|
|
|
# Import python libs
|
2013-10-15 21:09:37 +00:00
|
|
|
import os
|
2012-10-06 00:29:24 +00:00
|
|
|
import yaml
|
2013-10-15 21:09:37 +00:00
|
|
|
import shutil
|
2014-07-17 14:32:07 +00:00
|
|
|
import time
|
2012-05-23 14:31:58 +00:00
|
|
|
|
2013-06-27 12:29:48 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2012-05-23 14:31:58 +00:00
|
|
|
# Import salt libs
|
2013-06-27 12:29:48 +00:00
|
|
|
import integration
|
2014-11-26 18:33:22 +00:00
|
|
|
import salt.utils
|
2012-05-23 14:31:58 +00:00
|
|
|
|
|
|
|
|
2012-08-04 21:08:06 +00:00
|
|
|
class MatchTest(integration.ShellCase, integration.ShellCaseCommonTestsMixIn):
|
2012-05-23 14:31:58 +00:00
|
|
|
'''
|
|
|
|
Test salt matchers
|
|
|
|
'''
|
2012-08-04 21:08:06 +00:00
|
|
|
_call_binary_ = 'salt'
|
|
|
|
|
2012-05-23 14:31:58 +00:00
|
|
|
def test_list(self):
|
|
|
|
'''
|
|
|
|
test salt -L matcher
|
|
|
|
'''
|
|
|
|
data = self.run_salt('-L minion test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('-L minion,sub_minion test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
|
2013-01-29 17:20:02 +00:00
|
|
|
def test_compound(self):
|
|
|
|
'''
|
|
|
|
test salt compound matcher
|
|
|
|
'''
|
|
|
|
data = self.run_salt('-C "min* and G@test_grain:cheese" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
2014-07-17 14:32:07 +00:00
|
|
|
time.sleep(2)
|
2013-01-29 17:20:02 +00:00
|
|
|
data = self.run_salt('-C "min* and not G@test_grain:foo" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
2014-07-17 14:32:07 +00:00
|
|
|
time.sleep(2)
|
2013-01-29 17:20:02 +00:00
|
|
|
data = self.run_salt('-C "min* not G@test_grain:foo" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
2014-07-17 14:32:07 +00:00
|
|
|
time.sleep(2)
|
2013-02-08 00:48:21 +00:00
|
|
|
match = 'P@test_grain:^cheese$ and * and G@test_grain:cheese'
|
|
|
|
data = self.run_salt('-t 1 -C \'{0}\' test.ping'.format(match))
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
2014-07-17 14:32:07 +00:00
|
|
|
time.sleep(2)
|
2013-02-08 00:48:21 +00:00
|
|
|
match = 'L@sub_minion and E@.*'
|
|
|
|
data = self.run_salt('-t 1 -C "{0}" test.ping'.format(match))
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
|
|
|
|
2012-05-23 14:31:58 +00:00
|
|
|
def test_glob(self):
|
|
|
|
'''
|
|
|
|
test salt glob matcher
|
|
|
|
'''
|
|
|
|
data = self.run_salt('minion test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('"*" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
|
|
|
|
def test_regex(self):
|
|
|
|
'''
|
|
|
|
test salt regex matcher
|
|
|
|
'''
|
|
|
|
data = self.run_salt('-E "^minion$" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('-E ".*" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
2012-05-23 14:46:12 +00:00
|
|
|
|
|
|
|
def test_grain(self):
|
|
|
|
'''
|
|
|
|
test salt grain matcher
|
|
|
|
'''
|
2013-01-26 06:13:51 +00:00
|
|
|
# First-level grain (string value)
|
2012-05-23 14:46:12 +00:00
|
|
|
data = self.run_salt('-t 1 -G "test_grain:cheese" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('-G "test_grain:spam" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
2013-01-26 06:13:51 +00:00
|
|
|
# First-level grain (list member)
|
|
|
|
data = self.run_salt('-t 1 -G "planets:earth" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('-G "planets:saturn" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
2013-03-13 01:02:15 +00:00
|
|
|
data = self.run_salt('-G "planets:pluto" test.ping')
|
2014-07-15 19:43:46 +00:00
|
|
|
expect = None
|
|
|
|
if self.master_opts['transport'] == 'zeromq':
|
|
|
|
expect = (
|
|
|
|
'No minions matched the target. '
|
|
|
|
'No command was sent, no jid was '
|
|
|
|
'assigned.'
|
|
|
|
)
|
|
|
|
elif self.master_opts['transport'] == 'raet':
|
|
|
|
expect = ''
|
2013-03-13 01:02:15 +00:00
|
|
|
self.assertEqual(
|
|
|
|
''.join(data),
|
2014-07-15 19:43:46 +00:00
|
|
|
expect
|
2013-03-13 01:02:15 +00:00
|
|
|
)
|
2013-01-26 06:13:51 +00:00
|
|
|
# Nested grain (string value)
|
|
|
|
data = self.run_salt('-t 1 -G "level1:level2:foo" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('-G "level1:level2:bar" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
|
|
|
# Nested grain (list member)
|
|
|
|
data = self.run_salt('-t 1 -G "companions:one:ian" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('-G "companions:two:jamie" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
2012-05-27 23:41:49 +00:00
|
|
|
|
2012-05-23 15:39:10 +00:00
|
|
|
def test_regrain(self):
|
|
|
|
'''
|
|
|
|
test salt grain matcher
|
|
|
|
'''
|
2012-06-30 20:10:34 +00:00
|
|
|
data = self.run_salt(
|
|
|
|
'-t 1 --grain-pcre "test_grain:^cheese$" test.ping'
|
|
|
|
)
|
2012-05-23 15:39:10 +00:00
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertNotIn('sub_minion', data)
|
|
|
|
data = self.run_salt('--grain-pcre "test_grain:.*am$" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
2012-05-23 14:31:58 +00:00
|
|
|
|
2012-05-23 16:18:54 +00:00
|
|
|
def test_pillar(self):
|
|
|
|
'''
|
|
|
|
test pillar matcher
|
|
|
|
'''
|
2013-01-26 06:13:51 +00:00
|
|
|
# First-level pillar (string value)
|
2012-05-23 16:18:54 +00:00
|
|
|
data = self.run_salt('-I "monty:python" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
2013-01-26 06:13:51 +00:00
|
|
|
# First-level pillar (string value, only in sub_minion)
|
2012-05-23 16:18:54 +00:00
|
|
|
data = self.run_salt('-I "sub:sub_minion" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
self.assertNotIn('minion', data.replace('sub_minion', 'stub'))
|
2013-01-26 06:13:51 +00:00
|
|
|
# First-level pillar (list member)
|
2012-12-12 23:37:21 +00:00
|
|
|
data = self.run_salt('-I "knights:Bedevere" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
2013-01-26 06:13:51 +00:00
|
|
|
# Nested pillar (string value)
|
|
|
|
data = self.run_salt('-I "level1:level2:foo" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
# Nested pillar (list member)
|
|
|
|
data = self.run_salt('-I "companions:three:sarah jane" test.ping')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
2012-05-23 16:18:54 +00:00
|
|
|
|
2013-01-26 06:13:51 +00:00
|
|
|
def test_ipcidr(self):
|
2013-01-14 13:04:23 +00:00
|
|
|
subnets_data = self.run_salt('--out yaml \'*\' network.subnets')
|
|
|
|
yaml_data = yaml.load('\n'.join(subnets_data))
|
|
|
|
|
|
|
|
# We're just after the first defined subnet from 'minion'
|
|
|
|
subnet = yaml_data['minion'][0]
|
2012-10-06 00:29:24 +00:00
|
|
|
|
|
|
|
data = self.run_salt('-S {0} test.ping'.format(subnet))
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
self.assertIn('sub_minion', data)
|
|
|
|
|
2012-05-27 23:41:49 +00:00
|
|
|
def test_static(self):
|
|
|
|
'''
|
|
|
|
test salt static call
|
|
|
|
'''
|
|
|
|
data = self.run_salt('minion test.ping --static')
|
|
|
|
data = '\n'.join(data)
|
|
|
|
self.assertIn('minion', data)
|
|
|
|
|
2012-09-07 18:21:07 +00:00
|
|
|
def test_salt_documentation(self):
|
|
|
|
'''
|
|
|
|
Test to see if we're supporting --doc
|
|
|
|
'''
|
2015-02-17 22:20:43 +00:00
|
|
|
data = self.run_salt('-d "*" user')
|
|
|
|
self.assertIn("'user.add:'", data)
|
2012-09-07 18:21:07 +00:00
|
|
|
|
2012-11-09 11:23:30 +00:00
|
|
|
def test_salt_documentation_arguments_not_assumed(self):
|
|
|
|
'''
|
|
|
|
Test to see if we're not auto-adding '*' and 'sys.doc' to the call
|
|
|
|
'''
|
2014-07-15 20:50:32 +00:00
|
|
|
data = self.run_salt('-d -t 20')
|
2015-02-17 22:20:43 +00:00
|
|
|
self.assertIn("'user.add:'", data)
|
|
|
|
data = self.run_salt('"*" -d -t 20')
|
|
|
|
self.assertIn("'user.add:'", data)
|
|
|
|
data = self.run_salt('"*" -d user -t 20')
|
|
|
|
self.assertIn("'user.add:'", data)
|
|
|
|
data = self.run_salt('"*" sys.doc -d user -t 20')
|
|
|
|
self.assertIn("'user.add:'", data)
|
|
|
|
data = self.run_salt('"*" sys.doc user -t 20')
|
|
|
|
self.assertIn("'user.add:'", data)
|
2012-11-09 11:32:36 +00:00
|
|
|
|
2014-04-24 14:42:47 +00:00
|
|
|
def test_salt_documentation_too_many_arguments(self):
|
|
|
|
'''
|
|
|
|
Test to see if passing additional arguments shows an error
|
|
|
|
'''
|
|
|
|
data = self.run_salt('-d minion salt ldap.search "filter=ou=People"', catch_stderr=True)
|
|
|
|
self.assertIn('You can only get documentation for one method at one time', '\n'.join(data[1]))
|
|
|
|
|
2013-10-15 21:09:37 +00:00
|
|
|
def test_issue_7754(self):
|
|
|
|
old_cwd = os.getcwd()
|
|
|
|
config_dir = os.path.join(integration.TMP, 'issue-7754')
|
|
|
|
if not os.path.isdir(config_dir):
|
|
|
|
os.makedirs(config_dir)
|
|
|
|
|
|
|
|
os.chdir(config_dir)
|
|
|
|
|
|
|
|
config_file_name = 'master'
|
2014-11-26 18:33:22 +00:00
|
|
|
with salt.utils.fopen(self.get_config_file_path(config_file_name), 'r') as fhr:
|
|
|
|
config = yaml.load(
|
|
|
|
fhr.read()
|
|
|
|
)
|
|
|
|
config['log_file'] = 'file:///dev/log/LOG_LOCAL3'
|
|
|
|
with salt.utils.fopen(os.path.join(config_dir, config_file_name), 'w') as fhw:
|
|
|
|
fhw.write(
|
|
|
|
yaml.dump(config, default_flow_style=False)
|
|
|
|
)
|
2013-10-15 23:03:12 +00:00
|
|
|
ret = self.run_script(
|
2013-10-15 21:09:37 +00:00
|
|
|
self._call_binary_,
|
|
|
|
'--config-dir {0} minion test.ping'.format(
|
|
|
|
config_dir
|
|
|
|
),
|
2014-01-19 07:47:15 +00:00
|
|
|
timeout=15,
|
|
|
|
catch_stderr=True,
|
|
|
|
with_retcode=True
|
2013-10-15 21:09:37 +00:00
|
|
|
)
|
|
|
|
try:
|
2014-01-19 07:47:15 +00:00
|
|
|
self.assertIn('minion', '\n'.join(ret[0]))
|
2013-10-15 21:09:37 +00:00
|
|
|
self.assertFalse(os.path.isdir(os.path.join(config_dir, 'file:')))
|
2014-01-19 07:47:15 +00:00
|
|
|
except AssertionError:
|
|
|
|
# We now fail when we're unable to properly set the syslog logger
|
|
|
|
self.assertIn(
|
|
|
|
'Failed to setup the Syslog logging handler', '\n'.join(ret[1])
|
|
|
|
)
|
|
|
|
self.assertEqual(ret[2], 2)
|
2013-10-15 21:09:37 +00:00
|
|
|
finally:
|
|
|
|
os.chdir(old_cwd)
|
|
|
|
if os.path.isdir(config_dir):
|
|
|
|
shutil.rmtree(config_dir)
|
|
|
|
|
2012-11-09 11:23:30 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(MatchTest)
|