2012-05-05 14:09:23 +00:00
|
|
|
# Import python libs
|
2012-08-25 23:38:39 +00:00
|
|
|
import os
|
2012-09-28 15:04:38 +00:00
|
|
|
import shutil
|
2012-03-29 23:18:32 +00:00
|
|
|
import integration
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-03-29 23:18:32 +00:00
|
|
|
|
|
|
|
class StateModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validate the test module
|
|
|
|
'''
|
2012-09-01 06:49:34 +00:00
|
|
|
|
|
|
|
maxDiff = None
|
|
|
|
|
2012-03-29 23:18:32 +00:00
|
|
|
def test_show_highstate(self):
|
|
|
|
'''
|
|
|
|
state.show_highstate
|
|
|
|
'''
|
|
|
|
high = self.run_function('state.show_highstate')
|
|
|
|
self.assertTrue(isinstance(high, dict))
|
|
|
|
self.assertTrue('/testfile' in high)
|
|
|
|
self.assertEqual(high['/testfile']['__env__'], 'base')
|
|
|
|
|
|
|
|
def test_show_lowstate(self):
|
|
|
|
'''
|
|
|
|
state.show_lowstate
|
|
|
|
'''
|
|
|
|
low = self.run_function('state.show_lowstate')
|
|
|
|
self.assertTrue(isinstance(low, list))
|
|
|
|
self.assertTrue(isinstance(low[0], dict))
|
|
|
|
|
2012-05-14 03:31:46 +00:00
|
|
|
def test_catch_recurse(self):
|
|
|
|
'''
|
|
|
|
state.show_sls used to catch a recursive ref
|
|
|
|
'''
|
|
|
|
err = self.run_function('state.sls', mods='recurse_fail')
|
2012-05-14 05:16:45 +00:00
|
|
|
self.assertIn('recursive', err[0])
|
2012-05-14 03:31:46 +00:00
|
|
|
|
2012-06-16 04:41:36 +00:00
|
|
|
def test_no_recurse(self):
|
|
|
|
'''
|
2012-06-19 16:29:14 +00:00
|
|
|
verify that a sls structure is NOT a recursive ref
|
2012-06-16 04:41:36 +00:00
|
|
|
'''
|
|
|
|
sls = self.run_function('state.show_sls', mods='recurse_ok')
|
|
|
|
self.assertIn('snmpd', sls)
|
|
|
|
|
2012-06-19 16:29:14 +00:00
|
|
|
def test_no_recurse_two(self):
|
|
|
|
'''
|
|
|
|
verify that a sls structure is NOT a recursive ref
|
|
|
|
'''
|
|
|
|
sls = self.run_function('state.show_sls', mods='recurse_ok_two')
|
|
|
|
self.assertIn('/etc/nagios/nrpe.cfg', sls)
|
|
|
|
|
2012-09-01 06:49:34 +00:00
|
|
|
def test_issue_1896_file_append_source(self):
|
|
|
|
'''
|
|
|
|
Verify that we can append a file's contents
|
|
|
|
'''
|
|
|
|
self.run_function('state.sls', mods='testappend')
|
|
|
|
self.run_function('state.sls', mods='testappend.step-1')
|
|
|
|
self.run_function('state.sls', mods='testappend.step-2')
|
2012-09-28 23:40:30 +00:00
|
|
|
self.assertMultiLineEqual('''\
|
2012-09-01 06:49:34 +00:00
|
|
|
# set variable identifying the chroot you work in (used in the prompt below)
|
|
|
|
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
|
|
|
debian_chroot=$(cat /etc/debian_chroot)
|
|
|
|
fi
|
2012-09-03 15:40:52 +00:00
|
|
|
|
2012-09-01 06:49:34 +00:00
|
|
|
# enable bash completion in interactive shells
|
|
|
|
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
|
|
|
|
. /etc/bash_completion
|
|
|
|
fi
|
|
|
|
|
2012-09-28 23:40:30 +00:00
|
|
|
''', open('/tmp/salttest/test.append', 'r').read())
|
2012-09-01 06:49:34 +00:00
|
|
|
|
|
|
|
# Re-append switching order
|
|
|
|
self.run_function('state.sls', mods='testappend.step-2')
|
|
|
|
self.run_function('state.sls', mods='testappend.step-1')
|
2012-09-28 23:40:30 +00:00
|
|
|
self.assertMultiLineEqual('''\
|
2012-09-01 06:49:34 +00:00
|
|
|
# set variable identifying the chroot you work in (used in the prompt below)
|
|
|
|
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
|
|
|
debian_chroot=$(cat /etc/debian_chroot)
|
|
|
|
fi
|
2012-09-03 15:40:52 +00:00
|
|
|
|
2012-09-01 06:49:34 +00:00
|
|
|
# enable bash completion in interactive shells
|
|
|
|
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
|
|
|
|
. /etc/bash_completion
|
|
|
|
fi
|
|
|
|
|
2012-09-28 23:40:30 +00:00
|
|
|
''', open('/tmp/salttest/test.append', 'r').read())
|
2012-09-01 06:49:34 +00:00
|
|
|
|
2012-08-25 13:44:07 +00:00
|
|
|
def test_issue_1876_syntax_error(self):
|
|
|
|
'''
|
|
|
|
verify that we catch the following syntax error::
|
|
|
|
|
|
|
|
/tmp/salttest/issue-1876:
|
|
|
|
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://testfile
|
|
|
|
|
|
|
|
file.append:
|
|
|
|
- text: foo
|
|
|
|
|
|
|
|
'''
|
|
|
|
sls = self.run_function('state.sls', mods='issue-1876')
|
|
|
|
self.assertIn(
|
|
|
|
'Name "/tmp/salttest/issue-1876" in sls "issue-1876" contains '
|
|
|
|
'multiple state decs of the same type', sls
|
|
|
|
)
|
|
|
|
|
2012-08-25 23:38:39 +00:00
|
|
|
def test_issue_1879_too_simple_contains_check(self):
|
2012-09-28 23:40:30 +00:00
|
|
|
contents = '''\
|
2012-08-25 23:38:39 +00:00
|
|
|
# set variable identifying the chroot you work in (used in the prompt below)
|
|
|
|
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
|
|
|
|
debian_chroot=$(cat /etc/debian_chroot)
|
|
|
|
fi
|
2012-08-28 09:03:11 +00:00
|
|
|
|
2012-08-25 23:38:39 +00:00
|
|
|
# enable bash completion in interactive shells
|
|
|
|
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
|
|
|
|
. /etc/bash_completion
|
2012-08-28 09:03:11 +00:00
|
|
|
fi
|
|
|
|
|
2012-09-28 23:40:30 +00:00
|
|
|
'''
|
2012-08-28 09:03:11 +00:00
|
|
|
# Create the file
|
|
|
|
self.run_function('state.sls', mods='issue-1879')
|
|
|
|
# The first append
|
|
|
|
self.run_function('state.sls', mods='issue-1879.step-1')
|
|
|
|
# The seccond append
|
|
|
|
self.run_function('state.sls', mods='issue-1879.step-2')
|
|
|
|
# Does it match?
|
|
|
|
try:
|
|
|
|
self.assertMultiLineEqual(
|
2012-09-28 23:40:30 +00:00
|
|
|
contents, open('/tmp/salttest/issue-1879', 'r').read()
|
2012-08-28 09:03:11 +00:00
|
|
|
)
|
|
|
|
# Make sure we don't re-append existing text
|
|
|
|
self.run_function('state.sls', mods='issue-1879.step-1')
|
|
|
|
self.run_function('state.sls', mods='issue-1879.step-2')
|
|
|
|
self.assertMultiLineEqual(
|
2012-09-28 23:40:30 +00:00
|
|
|
contents, open('/tmp/salttest/issue-1879', 'r').read()
|
2012-08-28 09:03:11 +00:00
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
shutil.copy('/tmp/salttest/issue-1879', '/tmp/salttest/issue-1879.bak')
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
os.unlink('/tmp/salttest/issue-1879')
|
2012-08-25 23:38:39 +00:00
|
|
|
|
2012-09-28 00:17:37 +00:00
|
|
|
def test_include(self):
|
|
|
|
fnames = ('/tmp/include-test', '/tmp/to-include-test')
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.sls', mods='include-test')
|
|
|
|
for part in ret.itervalues():
|
|
|
|
self.assertTrue(part['result'])
|
|
|
|
for fname in fnames:
|
|
|
|
self.assertTrue(os.path.isfile(fname))
|
|
|
|
self.assertFalse(os.path.isfile('/tmp/exclude-test'))
|
|
|
|
finally:
|
|
|
|
for fname in list(fnames) + ['/tmp/exclude-test']:
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
os.remove(fname)
|
|
|
|
|
|
|
|
def test_exclude(self):
|
|
|
|
fnames = ('/tmp/include-test', '/tmp/exclude-test')
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.sls', mods='exclude-test')
|
|
|
|
for part in ret.itervalues():
|
|
|
|
self.assertTrue(part['result'])
|
|
|
|
for fname in fnames:
|
|
|
|
self.assertTrue(os.path.isfile(fname))
|
|
|
|
self.assertFalse(os.path.isfile('/tmp/to-include-test'))
|
|
|
|
finally:
|
|
|
|
for fname in list(fnames) + ['/tmp/to-include-test']:
|
|
|
|
if os.path.isfile(fname):
|
|
|
|
os.remove(fname)
|
|
|
|
|
2012-09-28 15:04:38 +00:00
|
|
|
def test_issue_2068_template_str(self):
|
|
|
|
venv_dir = '/tmp/issue-2068-template-str'
|
2012-09-28 00:17:37 +00:00
|
|
|
|
2012-09-28 15:04:38 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='issue-2068-template-str-no-dot'
|
|
|
|
)
|
|
|
|
self.assertTrue(isinstance(ret, dict))
|
|
|
|
self.assertNotEqual(ret, {})
|
|
|
|
for part in ret.itervalues():
|
|
|
|
self.assertTrue(part['result'])
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
|
|
|
# Let's load the template from the filesystem. If running this state
|
|
|
|
# with state.sls works, so should using state.template_str
|
|
|
|
template_path = os.path.join(
|
|
|
|
os.path.dirname(os.path.dirname(__file__)),
|
|
|
|
'files', 'file', 'base', 'issue-2068-template-str-no-dot.sls'
|
|
|
|
)
|
|
|
|
|
|
|
|
template = open(template_path, 'r').read()
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.template_str', [template])
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(ret, dict)), ret
|
|
|
|
self.assertNotEqual(ret, {})
|
|
|
|
|
|
|
|
for key in ret.iterkeys():
|
|
|
|
self.assertTrue(ret[key]['result'])
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(os.path.join(venv_dir, 'bin', 'pep8'))
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
2012-09-28 16:34:55 +00:00
|
|
|
# Now using state.template
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.template', [template_path])
|
|
|
|
self.assertTrue(isinstance(ret, dict))
|
|
|
|
self.assertNotEqual(ret, {})
|
|
|
|
for part in ret.itervalues():
|
|
|
|
self.assertTrue(part['result'])
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
2012-09-28 15:04:38 +00:00
|
|
|
# Now the problematic #2068 including dot's
|
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls', mods='issue-2068-template-str'
|
|
|
|
)
|
|
|
|
self.assertTrue(isinstance(ret, dict))
|
|
|
|
self.assertNotEqual(ret, {})
|
|
|
|
for part in ret.itervalues():
|
|
|
|
self.assertTrue(part['result'])
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
|
|
|
# Let's load the template from the filesystem. If running this state
|
|
|
|
# with state.sls works, so should using state.template_str
|
|
|
|
template_path = os.path.join(
|
|
|
|
os.path.dirname(os.path.dirname(__file__)),
|
|
|
|
'files', 'file', 'base', 'issue-2068-template-str.sls'
|
|
|
|
)
|
|
|
|
|
|
|
|
template = open(template_path, 'r').read()
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.template_str', [template])
|
|
|
|
|
|
|
|
self.assertTrue(isinstance(ret, dict)), ret
|
|
|
|
self.assertNotEqual(ret, {})
|
|
|
|
|
|
|
|
for key in ret.iterkeys():
|
|
|
|
self.assertTrue(ret[key]['result'])
|
|
|
|
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.isfile(os.path.join(venv_dir, 'bin', 'pep8'))
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
2012-08-25 23:38:39 +00:00
|
|
|
|
2012-09-28 16:34:55 +00:00
|
|
|
# Now using state.template
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.template', [template_path])
|
|
|
|
self.assertTrue(isinstance(ret, dict))
|
|
|
|
self.assertNotEqual(ret, {})
|
|
|
|
for part in ret.itervalues():
|
|
|
|
self.assertTrue(part['result'])
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(venv_dir):
|
|
|
|
shutil.rmtree(venv_dir)
|
|
|
|
|
2012-09-28 16:56:13 +00:00
|
|
|
def test_template_invalid_items(self):
|
|
|
|
TEMPLATE = '''\
|
|
|
|
{0}:
|
|
|
|
- issue-2068-template-str
|
|
|
|
|
|
|
|
/tmp/test-template-invalid-items:
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://testfile
|
|
|
|
'''
|
|
|
|
for item in ('include', 'exclude', 'extends'):
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', [TEMPLATE.format(item)]
|
|
|
|
)
|
|
|
|
self.assertTrue(isinstance(ret, list))
|
|
|
|
self.assertNotEqual(ret, [])
|
|
|
|
self.assertEqual(
|
|
|
|
['The \'{0}\' declaration found on \'<template-str>\' is '
|
|
|
|
'invalid when rendering single templates'.format(item)],
|
|
|
|
ret
|
|
|
|
)
|
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(StateModuleTest)
|