2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-05-14 17:18:36 +00:00
|
|
|
'''
|
|
|
|
Tests for the file state
|
|
|
|
'''
|
2013-06-27 12:32:54 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2015-04-07 02:31:13 +00:00
|
|
|
import os
|
|
|
|
import textwrap
|
2012-06-30 22:48:28 +00:00
|
|
|
import tempfile
|
2012-05-14 17:18:36 +00:00
|
|
|
|
2013-06-27 12:32:54 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2013-06-24 19:06:49 +00:00
|
|
|
# Import salt libs
|
2013-06-27 12:32:54 +00:00
|
|
|
import integration
|
2015-04-07 02:31:13 +00:00
|
|
|
import salt.utils
|
|
|
|
|
|
|
|
|
|
|
|
STATE_DIR = os.path.join(integration.FILES, 'file', 'base')
|
2013-06-24 19:06:49 +00:00
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-11-21 12:43:53 +00:00
|
|
|
class CMDTest(integration.ModuleCase,
|
|
|
|
integration.SaltReturnAssertsMixIn):
|
2012-05-14 17:18:36 +00:00
|
|
|
'''
|
|
|
|
Validate the cmd state
|
|
|
|
'''
|
2015-04-07 02:31:13 +00:00
|
|
|
def test_run_simple(self):
|
2012-05-14 17:18:36 +00:00
|
|
|
'''
|
|
|
|
cmd.run
|
|
|
|
'''
|
2012-06-30 22:48:28 +00:00
|
|
|
ret = self.run_state('cmd.run', name='ls', cwd=tempfile.gettempdir())
|
2012-11-21 12:43:53 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-14 17:18:36 +00:00
|
|
|
|
2015-04-07 02:31:13 +00:00
|
|
|
def test_test_run_simple(self):
|
2012-05-14 17:18:36 +00:00
|
|
|
'''
|
|
|
|
cmd.run test interface
|
|
|
|
'''
|
2012-06-30 22:48:28 +00:00
|
|
|
ret = self.run_state('cmd.run', name='ls',
|
|
|
|
cwd=tempfile.gettempdir(), test=True)
|
2012-11-21 12:43:53 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-07-20 06:21:01 +00:00
|
|
|
|
2016-05-09 17:06:44 +00:00
|
|
|
|
|
|
|
class CMDRunRedirectTest(integration.ModuleCase,
|
|
|
|
integration.SaltReturnAssertsMixIn):
|
|
|
|
'''
|
|
|
|
Validate the cmd state of run_redirect
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
self.state_name = 'run_redirect'
|
|
|
|
state_filename = self.state_name + '.sls'
|
|
|
|
self.state_file = os.path.join(STATE_DIR, state_filename)
|
|
|
|
self.test_file = tempfile.mkstemp()[1]
|
|
|
|
super(CMDRunRedirectTest, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.remove(self.state_file)
|
|
|
|
os.remove(self.test_file)
|
|
|
|
super(CMDRunRedirectTest, self).tearDown()
|
2015-04-07 02:31:13 +00:00
|
|
|
|
|
|
|
def test_run_unless(self):
|
|
|
|
'''
|
|
|
|
test cmd.run unless
|
|
|
|
'''
|
|
|
|
state_key = 'cmd_|-/var/log/messages_|-/var/log/messages_|-run'
|
2016-05-09 17:06:44 +00:00
|
|
|
salt.utils.fopen(self.state_file, 'w').write(textwrap.dedent('''\
|
|
|
|
/var/log/messages:
|
|
|
|
cmd.run:
|
|
|
|
- unless: echo cheese > {0}
|
|
|
|
'''.format(self.test_file)))
|
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [self.state_name])
|
|
|
|
self.assertTrue(ret[state_key]['result'])
|
2015-04-07 02:31:13 +00:00
|
|
|
|
|
|
|
def test_run_creates_exists(self):
|
|
|
|
'''
|
|
|
|
test cmd.run creates already there
|
|
|
|
'''
|
2016-05-09 17:06:44 +00:00
|
|
|
state_key = 'cmd_|-touch {0}_|-touch {0}_|-run'.format(self.test_file)
|
|
|
|
salt.utils.fopen(self.state_file, 'w').write(textwrap.dedent('''\
|
|
|
|
touch {0}:
|
|
|
|
cmd.run:
|
|
|
|
- creates: {0}
|
|
|
|
'''.format(self.test_file)))
|
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [self.state_name])
|
|
|
|
self.assertTrue(ret[state_key]['result'])
|
|
|
|
self.assertEqual(len(ret[state_key]['changes']), 0)
|
2015-04-07 02:31:13 +00:00
|
|
|
|
|
|
|
def test_run_creates_new(self):
|
|
|
|
'''
|
|
|
|
test cmd.run creates not there
|
|
|
|
'''
|
2016-05-09 17:06:44 +00:00
|
|
|
os.remove(self.test_file)
|
|
|
|
state_key = 'cmd_|-touch {0}_|-touch {0}_|-run'.format(self.test_file)
|
|
|
|
salt.utils.fopen(self.state_file, 'w').write(textwrap.dedent('''\
|
|
|
|
touch {0}:
|
|
|
|
cmd.run:
|
|
|
|
- creates: {0}
|
|
|
|
'''.format(self.test_file)))
|
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [self.state_name])
|
|
|
|
self.assertTrue(ret[state_key]['result'])
|
|
|
|
self.assertEqual(len(ret[state_key]['changes']), 4)
|
|
|
|
|
|
|
|
def test_run_redirect(self):
|
|
|
|
'''
|
|
|
|
test cmd.run with shell redirect
|
|
|
|
'''
|
|
|
|
state_key = 'cmd_|-date > {0}_|-date > {0}_|-run'.format(self.test_file)
|
|
|
|
salt.utils.fopen(self.state_file, 'w').write(textwrap.dedent('''\
|
|
|
|
date > {0}:
|
|
|
|
cmd.run
|
|
|
|
'''.format(self.test_file)))
|
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [self.state_name])
|
|
|
|
self.assertTrue(ret[state_key]['result'])
|
|
|
|
|
|
|
|
|
|
|
|
class CMDRunWatchTest(integration.ModuleCase,
|
|
|
|
integration.SaltReturnAssertsMixIn):
|
|
|
|
'''
|
|
|
|
Validate the cmd state of run_watch
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
self.state_name = 'run_watch'
|
|
|
|
state_filename = self.state_name + '.sls'
|
|
|
|
self.state_file = os.path.join(STATE_DIR, state_filename)
|
|
|
|
super(CMDRunWatchTest, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.remove(self.state_file)
|
|
|
|
super(CMDRunWatchTest, self).tearDown()
|
2015-04-07 02:31:13 +00:00
|
|
|
|
|
|
|
def test_run_watch(self):
|
|
|
|
'''
|
|
|
|
test cmd.run watch
|
|
|
|
'''
|
2016-03-03 02:59:57 +00:00
|
|
|
saltines_key = 'cmd_|-saltines_|-echo_|-run'
|
2015-04-07 02:31:13 +00:00
|
|
|
biscuits_key = 'cmd_|-biscuits_|-echo hello_|-wait'
|
|
|
|
|
2016-05-09 17:06:44 +00:00
|
|
|
salt.utils.fopen(self.state_file, 'w').write(textwrap.dedent('''\
|
|
|
|
saltines:
|
|
|
|
cmd.run:
|
|
|
|
- name: echo
|
|
|
|
- cwd: /
|
|
|
|
- stateful: True
|
|
|
|
|
|
|
|
biscuits:
|
|
|
|
cmd.wait:
|
|
|
|
- name: echo hello
|
|
|
|
- cwd: /
|
|
|
|
- watch:
|
|
|
|
- cmd: saltines
|
|
|
|
'''))
|
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [self.state_name])
|
|
|
|
self.assertTrue(ret[saltines_key]['result'])
|
|
|
|
self.assertTrue(ret[biscuits_key]['result'])
|
2015-04-07 02:31:13 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
2016-05-09 17:06:44 +00:00
|
|
|
run_tests([CMDTest, CMDRunRedirectTest, CMDRunWatchTest])
|