mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Improve code readability.
This commit is contained in:
parent
bbd159f8a0
commit
badf339b01
@ -3,6 +3,7 @@ import os
|
||||
import sys
|
||||
import shutil
|
||||
import tempfile
|
||||
import textwrap
|
||||
from cStringIO import StringIO
|
||||
|
||||
# Import Salt Testing libs
|
||||
@ -44,7 +45,7 @@ class PyDSLRendererTestCase(TestCase):
|
||||
)
|
||||
|
||||
def test_state_declarations(self):
|
||||
result = self.render_sls('''
|
||||
result = self.render_sls(textwrap.dedent('''
|
||||
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='/')
|
||||
@ -52,7 +53,7 @@ state('X').cmd('run', 'echo hello world', cwd='/')
|
||||
a_cmd = state('A').cmd
|
||||
a_cmd.run(shell='/bin/bash')
|
||||
state('A').service.running(name='apache')
|
||||
''')
|
||||
'''))
|
||||
self.assertTrue('A' in result and 'X' in result)
|
||||
A_cmd = result['A']['cmd']
|
||||
self.assertEqual(A_cmd[0], 'run')
|
||||
@ -85,7 +86,7 @@ state('A').service.running(name='apache')
|
||||
self.assertEqual(s[2]['source'], 'salt://path/to/file')
|
||||
|
||||
def test_requisite_declarations(self):
|
||||
result = self.render_sls('''
|
||||
result = self.render_sls(textwrap.dedent('''
|
||||
state('X').cmd.run('echo hello')
|
||||
state('A').cmd.run('mkdir tmp', cwd='/var')
|
||||
state('B').cmd.run('ls -la', cwd='/var/tmp') \
|
||||
@ -97,7 +98,7 @@ state('G').service.watch_in(state('A').cmd)
|
||||
|
||||
state('H').cmd.require_in(cmd='echo hello')
|
||||
state('H').cmd.run('echo world')
|
||||
''')
|
||||
'''))
|
||||
self.assertTrue(len(result), 6)
|
||||
self.assertTrue(set("X A B G H".split()).issubset(set(result.keys())))
|
||||
b = result['B']['cmd']
|
||||
@ -113,7 +114,7 @@ state('H').cmd.run('echo world')
|
||||
)
|
||||
|
||||
def test_include_extend(self):
|
||||
result = self.render_sls('''
|
||||
result = self.render_sls(textwrap.dedent('''
|
||||
include(
|
||||
'some.sls.file',
|
||||
'another.sls.file',
|
||||
@ -128,7 +129,7 @@ extend(
|
||||
state('Y').file('managed', name='a_file.txt'),
|
||||
state('Z').service.watch(file='A')
|
||||
)
|
||||
''')
|
||||
'''))
|
||||
self.assertEqual(len(result), 4)
|
||||
self.assertEqual(
|
||||
result['include'],
|
||||
@ -148,7 +149,8 @@ extend(
|
||||
self.assertEqual(extend['A']['cmd'][0], 'run')
|
||||
|
||||
def test_cmd_call(self):
|
||||
result = self.HIGHSTATE.state.call_template_str('''#!pydsl
|
||||
result = self.HIGHSTATE.state.call_template_str(textwrap.dedent('''\
|
||||
#!pydsl
|
||||
state('A').cmd.run('echo this is state A', cwd='/')
|
||||
|
||||
some_var = 12345
|
||||
@ -160,7 +162,7 @@ state('C').cmd.call(do_something, 1, 2, 3, x=1, y=2) \
|
||||
|
||||
state('G').cmd.wait('echo this is state G', cwd='/') \
|
||||
.watch(state('C').cmd)
|
||||
''')
|
||||
'''))
|
||||
ret = (result[k] for k in result.keys() if 'do_something' in k).next()
|
||||
changes = ret['changes']
|
||||
self.assertEqual(
|
||||
@ -173,19 +175,19 @@ state('G').cmd.wait('echo this is state G', cwd='/') \
|
||||
|
||||
def test_multiple_state_func_in_state_mod(self):
|
||||
with self.assertRaisesRegexp(PyDslError, 'Multiple state functions'):
|
||||
self.render_sls('''
|
||||
self.render_sls(textwrap.dedent('''
|
||||
state('A').cmd.run('echo hoho')
|
||||
state('A').cmd.wait('echo hehe')
|
||||
''')
|
||||
'''))
|
||||
|
||||
def test_no_state_func_in_state_mod(self):
|
||||
with self.assertRaisesRegexp(PyDslError, 'No state function specified'):
|
||||
self.render_sls('''
|
||||
self.render_sls(textwrap.dedent('''
|
||||
state('B').cmd.require(cmd='hoho')
|
||||
''')
|
||||
'''))
|
||||
|
||||
def test_load_highstate(self):
|
||||
result = self.render_sls('''
|
||||
result = self.render_sls(textwrap.dedent('''
|
||||
import yaml
|
||||
__pydsl__.load_highstate(yaml.load("""
|
||||
A:
|
||||
@ -204,7 +206,7 @@ B:
|
||||
"""))
|
||||
|
||||
state('A').cmd.run(name='echo hello world')
|
||||
''')
|
||||
'''))
|
||||
self.assertEqual(len(result), 3)
|
||||
self.assertEqual(result['A']['cmd'][0], 'run')
|
||||
self.assertEqual(result['A']['cmd'][1]['name'], 'echo hello')
|
||||
@ -219,7 +221,7 @@ state('A').cmd.run(name='echo hello world')
|
||||
self.assertEqual(result['B']['service'][2]['watch'][0]['cmd'], 'A')
|
||||
|
||||
def test_ordered_states(self):
|
||||
result = self.render_sls('''
|
||||
result = self.render_sls(textwrap.dedent('''
|
||||
__pydsl__.set(ordered=True)
|
||||
A = state('A')
|
||||
state('B').cmd.run('echo bbbb')
|
||||
@ -227,7 +229,7 @@ A.cmd.run('echo aaa')
|
||||
state('B').cmd.run(cwd='/')
|
||||
state('C').cmd.run('echo ccc')
|
||||
state('B').file.managed(source='/a/b/c')
|
||||
''')
|
||||
'''))
|
||||
self.assertEqual(len(result['B']['cmd']), 3)
|
||||
self.assertEqual(result['A']['cmd'][1]['require'][0]['cmd'], 'B')
|
||||
self.assertEqual(result['C']['cmd'][1]['require'][0]['cmd'], 'A')
|
||||
@ -243,7 +245,7 @@ state('B').file.managed(source='/a/b/c')
|
||||
)
|
||||
output = os.path.join(dirpath, 'output')
|
||||
try:
|
||||
write_to(os.path.join(dirpath, 'xxx.sls'),
|
||||
write_to(os.path.join(dirpath, 'xxx.sls'), textwrap.dedent(
|
||||
'''#!stateconf -os yaml . jinja
|
||||
.X:
|
||||
cmd.run:
|
||||
@ -257,18 +259,18 @@ state('B').file.managed(source='/a/b/c')
|
||||
cmd.run:
|
||||
- name: echo Z >> {2}
|
||||
- cwd: /
|
||||
'''.format(output, output, output))
|
||||
write_to(os.path.join(dirpath, 'yyy.sls'),
|
||||
'''#!pydsl|stateconf -ps
|
||||
'''.format(output, output, output)))
|
||||
write_to(os.path.join(dirpath, 'yyy.sls'), textwrap.dedent('''\
|
||||
#!pydsl|stateconf -ps
|
||||
|
||||
__pydsl__.set(ordered=True)
|
||||
state('.D').cmd.run('echo D >> {0}', cwd='/')
|
||||
state('.E').cmd.run('echo E >> {1}', cwd='/')
|
||||
state('.F').cmd.run('echo F >> {2}', cwd='/')
|
||||
'''.format(output, output, output))
|
||||
'''.format(output, output, output)))
|
||||
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'),
|
||||
'''#!pydsl|stateconf -ps
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
|
||||
#!pydsl|stateconf -ps
|
||||
|
||||
include('xxx', 'yyy')
|
||||
|
||||
@ -283,7 +285,7 @@ __pydsl__.set(ordered=True)
|
||||
state('.A').cmd.run('echo A >> {0}', cwd='/')
|
||||
state('.B').cmd.run('echo B >> {1}', cwd='/')
|
||||
state('.C').cmd.run('echo C >> {2}', cwd='/')
|
||||
'''.format(output, output, output))
|
||||
'''.format(output, output, output)))
|
||||
|
||||
state_highstate({'base': ['aaa']}, dirpath)
|
||||
with open(output, 'r') as f:
|
||||
@ -302,8 +304,8 @@ state('.C').cmd.run('echo C >> {2}', cwd='/')
|
||||
)
|
||||
output = os.path.join(dirpath, 'output')
|
||||
try:
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'),
|
||||
'''#!pydsl|stateconf -ps
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
|
||||
#!pydsl|stateconf -ps
|
||||
|
||||
include('xxx')
|
||||
yyy = include('yyy')
|
||||
@ -319,10 +321,10 @@ __pydsl__.set(ordered=True)
|
||||
yyy.hello('red', 1)
|
||||
yyy.hello('green', 2)
|
||||
yyy.hello('blue', 3)
|
||||
'''.format(output))
|
||||
'''.format(output)))
|
||||
|
||||
write_to(os.path.join(dirpath, 'xxx.sls'),
|
||||
'''#!stateconf -os yaml . jinja
|
||||
write_to(os.path.join(dirpath, 'xxx.sls'), textwrap.dedent('''\
|
||||
#!stateconf -os yaml . jinja
|
||||
|
||||
include:
|
||||
- yyy
|
||||
@ -350,10 +352,10 @@ extend:
|
||||
- name: echo X3 >> {3}
|
||||
- cwd: /
|
||||
|
||||
'''.format(output, output, output, output))
|
||||
'''.format(output, output, output, output)))
|
||||
|
||||
write_to(os.path.join(dirpath, 'yyy.sls'),
|
||||
'''#!pydsl|stateconf -ps
|
||||
write_to(os.path.join(dirpath, 'yyy.sls'), textwrap.dedent('''\
|
||||
#!pydsl|stateconf -ps
|
||||
|
||||
include('xxx')
|
||||
__pydsl__.set(ordered=True)
|
||||
@ -364,10 +366,10 @@ state('.Y3').cmd.run('echo Y3 >> {2}', cwd='/')
|
||||
|
||||
def hello(color, number):
|
||||
state(color).cmd.run('echo hello '+color+' '+str(number)+' >> {3}', cwd='/')
|
||||
'''.format(output, output, output, output))
|
||||
'''.format(output, output, output, output)))
|
||||
|
||||
state_highstate({'base': ['aaa']}, dirpath)
|
||||
expected = '''
|
||||
expected = textwrap.dedent('''\
|
||||
X1
|
||||
X2
|
||||
X3
|
||||
@ -377,7 +379,7 @@ Y3
|
||||
hello red 1
|
||||
hello green 2
|
||||
hello blue 3
|
||||
'''.lstrip()
|
||||
''')
|
||||
|
||||
with open(output, 'r') as f:
|
||||
self.assertEqual(sorted(f.read()), sorted(expected))
|
||||
@ -396,8 +398,8 @@ hello blue 3
|
||||
)
|
||||
)
|
||||
try:
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'),
|
||||
'''#!pydsl
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
|
||||
#!pydsl
|
||||
|
||||
__pydsl__.set(ordered=True)
|
||||
A = state('A')
|
||||
@ -410,7 +412,7 @@ state().cmd.run('echo hoho >> {2}/yyy.txt', cwd='/')
|
||||
|
||||
A.file.managed('{3}/xxx.txt', source='salt://zzz.txt')
|
||||
A()
|
||||
'''.format(dirpath, dirpath, dirpath, dirpath))
|
||||
'''.format(dirpath, dirpath, dirpath, dirpath)))
|
||||
state_highstate({'base': ['aaa']}, dirpath)
|
||||
with open(os.path.join(dirpath, 'yyy.txt'), 'r') as f:
|
||||
|
||||
@ -430,24 +432,24 @@ A()
|
||||
)
|
||||
output = os.path.join(dirpath, 'output')
|
||||
try:
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'),
|
||||
'''#!pydsl
|
||||
write_to(os.path.join(dirpath, 'aaa.sls'), textwrap.dedent('''\
|
||||
#!pydsl
|
||||
__salt__['state.sls']('bbb')
|
||||
state().cmd.run('echo bbbbbb', cwd='/')
|
||||
''')
|
||||
write_to(os.path.join(dirpath, 'bbb.sls'),
|
||||
'''))
|
||||
write_to(os.path.join(dirpath, 'bbb.sls'), textwrap.dedent(
|
||||
'''
|
||||
# {{ salt['state.sls']('ccc')
|
||||
test:
|
||||
cmd.run:
|
||||
- name: echo bbbbbbb
|
||||
- cwd: /
|
||||
''')
|
||||
write_to(os.path.join(dirpath, 'ccc.sls'),
|
||||
'''))
|
||||
write_to(os.path.join(dirpath, 'ccc.sls'), textwrap.dedent(
|
||||
'''
|
||||
#!pydsl
|
||||
state().cmd.run('echo ccccc', cwd='/')
|
||||
''')
|
||||
'''))
|
||||
state_highstate({'base': ['aaa']}, dirpath)
|
||||
finally:
|
||||
shutil.rmtree(dirpath, ignore_errors=True)
|
||||
|
Loading…
Reference in New Issue
Block a user