mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Add a test module for pydsl and some bug fixes.
This commit is contained in:
parent
d5c2826e8e
commit
a8b8a6eb18
@ -30,7 +30,8 @@ class Sls(object):
|
||||
|
||||
def state(self, id=None):
|
||||
if not id:
|
||||
id = str(_uuid())
|
||||
id = '.'+str(_uuid())
|
||||
# adds a leading dot to make use of stateconf's namespace feature.
|
||||
try:
|
||||
return self.all_decls[id]
|
||||
except KeyError:
|
||||
@ -47,8 +48,10 @@ class Sls(object):
|
||||
self.state().stateconf.set(slsmod=slsmod)
|
||||
|
||||
highstate = Dict()
|
||||
highstate['include'] = self.includes[:]
|
||||
highstate['extend'] = extend = Dict()
|
||||
if self.includes:
|
||||
highstate['include'] = self.includes[:]
|
||||
if self.extends:
|
||||
highstate['extend'] = extend = Dict()
|
||||
for ext in self.extends:
|
||||
extend[ext._id] = ext
|
||||
for decl in self.decls:
|
||||
@ -94,7 +97,7 @@ class StateModule(object):
|
||||
return self._func
|
||||
else:
|
||||
return getattr(self._func, name)
|
||||
self._func = f = StateFunction(name)
|
||||
self._func = f = StateFunction(name, self._name)
|
||||
return f
|
||||
|
||||
def __call__(self, name, *args, **kws):
|
||||
@ -116,7 +119,8 @@ def _generate_requsite_method(t):
|
||||
|
||||
class StateFunction(object):
|
||||
|
||||
def __init__(self, name):
|
||||
def __init__(self, name, parent_mod):
|
||||
self.mod_name = parent_mod
|
||||
self.name = name
|
||||
self.args = []
|
||||
|
||||
@ -128,7 +132,7 @@ class StateFunction(object):
|
||||
args = list(args)
|
||||
if args:
|
||||
first = args[0]
|
||||
if self.name == 'call' and callable(first):
|
||||
if self.mod_name == 'cmd' and self.name == 'call' and callable(first):
|
||||
args[0] = first.__name__
|
||||
kws = dict(func=first, args=args[1:], kws=kws)
|
||||
del args[1:]
|
||||
|
62
tests/unit/pydsl_test.py
Normal file
62
tests/unit/pydsl_test.py
Normal file
@ -0,0 +1,62 @@
|
||||
# Import Python libs
|
||||
import sys
|
||||
from cStringIO import StringIO
|
||||
|
||||
# Import Salt libs
|
||||
from saltunittest import TestCase
|
||||
import salt.loader
|
||||
import salt.config
|
||||
from salt.state import State
|
||||
|
||||
REQUISITES = ['require', 'require_in', 'use', 'use_in', 'watch', 'watch_in']
|
||||
|
||||
OPTS = salt.config.master_config('whatever, just load the defaults!')
|
||||
# we should have used minion_config(), but that would try to resolve
|
||||
# the master hostname, and retry for 30 seconds! Lucily for our purpose,
|
||||
# master conf or minion conf, it doesn't matter.
|
||||
OPTS['id'] = 'whatever'
|
||||
OPTS['file_client'] = 'local'
|
||||
OPTS['file_roots'] = dict(base=['/'])
|
||||
|
||||
OPTS['grains'] = salt.loader.grains(OPTS)
|
||||
STATE = State(OPTS)
|
||||
|
||||
def render_sls(content, sls='', env='base', **kws):
|
||||
sys.modules['salt.loaded.int.render.pydsl'].__salt__ = STATE.functions
|
||||
return STATE.rend['pydsl'](
|
||||
StringIO(content), env=env, sls=sls,
|
||||
**kws)
|
||||
|
||||
|
||||
|
||||
class PyDSLRendererTestCase(TestCase):
|
||||
|
||||
def test_state_declarations(self):
|
||||
result = render_sls('''
|
||||
state('A').cmd.run('ls -la', cwd='/var/tmp')
|
||||
state().file.managed('myfile.txt', source='salt://path/to/file')
|
||||
state('X').cmd('run', 'echo hello world', cwd='/')
|
||||
''')
|
||||
self.assertTrue('A' in result and 'X' in result)
|
||||
A_cmd = result['A']['cmd']
|
||||
self.assertEqual(A_cmd[0], 'run')
|
||||
self.assertEqual(A_cmd[1]['name'], 'ls -la')
|
||||
self.assertEqual(A_cmd[2]['cwd'], '/var/tmp')
|
||||
|
||||
X_cmd = result['X']['cmd']
|
||||
self.assertEqual(X_cmd[0], 'run')
|
||||
self.assertEqual(X_cmd[1]['name'], 'echo hello world')
|
||||
self.assertEqual(X_cmd[2]['cwd'], '/')
|
||||
|
||||
del result['A']
|
||||
del result['X']
|
||||
self.assertEqual(len(result), 2)
|
||||
# 2 rather than 1 because pydsl adds an extra no-op state
|
||||
# declaration.
|
||||
|
||||
s = result.itervalues().next()['file']
|
||||
self.assertEqual(s[0], 'managed')
|
||||
self.assertEqual(s[1]['name'], 'myfile.txt')
|
||||
self.assertEqual(s[2]['source'], 'salt://path/to/file')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user