Permalink
Browse files

Fix a case when a single shorthand state is declared without arguments.

Also, now automatically adds the name argument, if one is not provided,
for states declared under a dot-prefixed state id.
  • Loading branch information...
kjkuan committed Nov 19, 2012
1 parent af93e5f commit 190f4bd8282dfe3b62c9492f4d774d55ca1e21be
Showing with 81 additions and 5 deletions.
  1. +40 −2 salt/renderers/stateconf.py
  2. +41 −3 tests/unit/stateconf_test.py
@@ -70,9 +70,22 @@
files.
For example, in the `salt://some/file.sls`, a state id such as ``.sls_params``
- will be turned into ``some.file::sls_params``.
+ will be turned into ``some.file::sls_params``. Example::
- Moreover, the leading dot trick can be used with extending state ids as well,
+ .vim:
+ package.installed
+
+ Above will be translated into::
+
+ some.file::vim:
+ package.installed:
+ - name: vim
+
+ Notice how that if a state under a dot-prefixed state id has no 'name'
+ argument then one will be added automatically by using the state id with
+ the leading dot stripped off.
+
+ The leading dot trick can be used with extending state ids as well,
so you can include relatively and extend relatively. For example, when
extending a state in `salt://some/other_file.sls`, eg,::
@@ -294,6 +307,7 @@ def process_sls_data(data, context=None, extract=False):
# wrong during the preprocessing.
data = copy.deepcopy(high)
try:
+ rewrite_single_shorthand_state_decl(data)
rewrite_sls_includes_excludes(data, sls)
if not extract and IMPLICIT_REQUIRE:
@@ -371,6 +385,22 @@ def has_names_decls(data):
for _ in nvlist(args, ['names']):
return sid
+def rewrite_single_shorthand_state_decl(data):
+ '''
+ Rewrite all state declarations that look like this::
+
+ state_id_decl:
+ state.func
+
+ into::
+
+ state_id_decl:
+ state.func: []
+ '''
+ for sid, states in data.items():
+ if isinstance(states, basestring):
+ data[sid] = {states: []}
+
def _parent_sls(sls):
i = sls.rfind('.')
@@ -477,6 +507,14 @@ def rename_state_ids(data, sls, is_extend=False):
'Can\'t rename state id({0}) into {1} because the later '
'already exists!'.format(sid, newsid)
)
+ # add a '- name: sid' to those states without '- name'.
+ for args in data[sid].itervalues():
+ for arg in args:
+ if isinstance(arg, dict) and iter(arg).next() == 'name':
+ break
+ else: # then no '- name: ...' is defined in the state args
+ # add the sid without the leading dot as the name.
+ args.insert(0, dict(name=sid[1:]))
data[newsid] = data[sid]
del data[sid]
@@ -67,6 +67,40 @@ def test_sls_dir(self):
'echo sls_dir=path/to')
+ def test_states_declared_with_shorthand_no_args(self):
+ result = render_sls('''
+test:
+ cmd.run:
+ - name: echo testing
+ - cwd: /
+test1:
+ pkg.installed
+test2:
+ user.present
+''' )
+ self.assertTrue(len(result), 3)
+ for args in (result['test1']['pkg.installed'],
+ result['test2']['user.present'] ):
+ self.assertTrue(isinstance(args, list))
+ self.assertEqual(len(args), 0)
+ self.assertEqual(result['test']['cmd.run'][0]['name'], 'echo testing')
+
+
+ def test_adding_state_name_arg_for_dot_state_id(self):
+ result = render_sls('''
+.test:
+ pkg.installed:
+ - cwd: /
+.test2:
+ pkg.installed:
+ - name: vim
+''', sls='test')
+ self.assertEqual(
+ result['test::test']['pkg.installed'][0]['name'], 'test')
+ self.assertEqual(
+ result['test::test2']['pkg.installed'][0]['name'], 'vim')
+
+
def test_state_prefix(self):
result = render_sls('''
.test:
@@ -153,7 +187,9 @@ def test_goal_state_generation(self):
''', sls='test.goalstate', argline='yaml . jinja')
self.assertTrue(len(result), len('ABCDE')+1)
- reqs = result['test.goalstate::goal']['stateconf.set'][0]['require']
+ reqs = result['test.goalstate::goal']['stateconf.set'][1]['require']
+ # note: arg 0 is the name arg.
+
self.assertEqual(set([i.itervalues().next() for i in reqs]),
set('ABCDE'))
@@ -207,7 +243,9 @@ def test_implicit_require_with_goal_state(self):
self.assertEqual(G_req[2]['cmd'], 'F')
goal_args = result['test::goal']['stateconf.set']
- self.assertEqual(len(goal_args), 1)
+ # Note: arg 0 is the auto-added name arg.
+
+ self.assertEqual(len(goal_args), 2)
self.assertEqual(
- [i.itervalues().next() for i in goal_args[0]['require']],
+ [i.itervalues().next() for i in goal_args[1]['require']],
list('ABCDEFG'))

0 comments on commit 190f4bd

Please sign in to comment.