2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-05-13 04:31:58 +00:00
|
|
|
'''
|
|
|
|
Tests for the file state
|
|
|
|
'''
|
2012-11-06 12:44:53 +00:00
|
|
|
|
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2013-11-24 12:15:20 +00:00
|
|
|
import glob
|
2014-04-25 03:27:59 +00:00
|
|
|
import grp
|
2014-07-14 22:40:03 +00:00
|
|
|
import os
|
|
|
|
import pwd
|
|
|
|
import shutil
|
|
|
|
import stat
|
|
|
|
import tempfile
|
2014-12-17 10:46:21 +00:00
|
|
|
import filecmp
|
2012-11-06 12:44:53 +00:00
|
|
|
|
2015-01-08 02:26:40 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
|
|
|
|
|
2013-06-27 12:38:01 +00:00
|
|
|
# Import Salt Testing libs
|
2014-04-25 19:38:14 +00:00
|
|
|
from salttesting import skipIf
|
|
|
|
from salttesting.helpers import (
|
|
|
|
destructiveTest,
|
|
|
|
ensure_in_syspath,
|
2014-04-25 22:58:41 +00:00
|
|
|
with_system_user_and_group
|
2014-04-25 19:38:14 +00:00
|
|
|
)
|
2013-06-27 12:38:01 +00:00
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
|
2012-11-06 12:44:53 +00:00
|
|
|
# Import salt libs
|
2013-06-27 12:38:01 +00:00
|
|
|
import integration
|
2012-11-18 18:57:10 +00:00
|
|
|
import salt.utils
|
2012-05-13 04:31:58 +00:00
|
|
|
|
2014-11-21 19:51:56 +00:00
|
|
|
# Import 3rd-party libs
|
|
|
|
import salt.ext.six as six
|
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-11-28 17:35:25 +00:00
|
|
|
class FileTest(integration.ModuleCase, integration.SaltReturnAssertsMixIn):
|
2012-05-13 04:31:58 +00:00
|
|
|
'''
|
|
|
|
Validate the file state
|
|
|
|
'''
|
2012-08-05 18:23:12 +00:00
|
|
|
|
2012-05-13 04:31:58 +00:00
|
|
|
def test_symlink(self):
|
|
|
|
'''
|
|
|
|
file.symlink
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'symlink')
|
|
|
|
tgt = os.path.join(integration.TMP, 'target')
|
|
|
|
ret = self.run_state('file.symlink', name=name, target=tgt)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 04:41:47 +00:00
|
|
|
|
|
|
|
def test_test_symlink(self):
|
|
|
|
'''
|
|
|
|
file.symlink test interface
|
|
|
|
'''
|
2013-03-12 05:44:31 +00:00
|
|
|
name = os.path.join(integration.TMP, 'symlink2')
|
2012-05-13 04:41:47 +00:00
|
|
|
tgt = os.path.join(integration.TMP, 'target')
|
|
|
|
ret = self.run_state('file.symlink', test=True, name=name, target=tgt)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 04:41:47 +00:00
|
|
|
|
2012-05-13 04:55:28 +00:00
|
|
|
def test_absent_file(self):
|
|
|
|
'''
|
|
|
|
file.absent
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'file_to_kill')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
2012-05-13 04:55:28 +00:00
|
|
|
fp_.write('killme')
|
|
|
|
ret = self.run_state('file.absent', name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 04:55:28 +00:00
|
|
|
self.assertFalse(os.path.isfile(name))
|
|
|
|
|
|
|
|
def test_absent_dir(self):
|
|
|
|
'''
|
|
|
|
file.absent
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'dir_to_kill')
|
2012-08-25 13:36:30 +00:00
|
|
|
if not os.path.isdir(name):
|
|
|
|
# left behind... Don't fail because of this!
|
|
|
|
os.makedirs(name)
|
2012-05-13 04:55:28 +00:00
|
|
|
ret = self.run_state('file.absent', name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 04:55:28 +00:00
|
|
|
self.assertFalse(os.path.isdir(name))
|
2012-05-13 14:18:05 +00:00
|
|
|
|
2012-05-13 04:55:28 +00:00
|
|
|
def test_absent_link(self):
|
|
|
|
'''
|
|
|
|
file.absent
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'link_to_kill')
|
Reverted back the `--key-logfile` deprecation.
On the parsers cleanup branch, I though about making the available options as equal as possible. Since the log_file setting for the master and the minion could be separately set, I made the logfile option for the key also log_file. Yet, since there's no key specific config file, it shares master, one could only specify the log_file settings from the cli, it could not be hardcoded on the config file, unless, we made a separate config file just for key.
So, in order to reduce required changes, and keep it all as backwards compatible as possible, the key log file options is now, once again, `--key-logfile` which will allow us to hardcode it in the masters config file as `key_logfile`.
This will also, hopefully make travis behave better too.
2012-08-08 00:08:38 +00:00
|
|
|
if not os.path.islink('{0}.tgt'.format(name)):
|
|
|
|
os.symlink(name, '{0}.tgt'.format(name))
|
2012-05-13 04:55:28 +00:00
|
|
|
ret = self.run_state('file.absent', name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.islink(name))
|
|
|
|
finally:
|
|
|
|
if os.path.islink('{0}.tgt'.format(name)):
|
|
|
|
os.unlink('{0}.tgt'.format(name))
|
2012-05-13 04:58:26 +00:00
|
|
|
|
2012-05-13 05:19:39 +00:00
|
|
|
def test_test_absent(self):
|
2012-05-13 04:58:26 +00:00
|
|
|
'''
|
|
|
|
file.absent test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'file_to_kill')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
2012-05-13 04:58:26 +00:00
|
|
|
fp_.write('killme')
|
|
|
|
ret = self.run_state('file.absent', test=True, name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(name))
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 05:19:39 +00:00
|
|
|
|
|
|
|
def test_managed(self):
|
|
|
|
'''
|
|
|
|
file.managed
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'grail_scene33')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.managed', name=name, source='salt://grail/scene33'
|
|
|
|
)
|
2012-05-13 05:19:39 +00:00
|
|
|
src = os.path.join(
|
2012-08-05 18:23:12 +00:00
|
|
|
integration.FILES, 'file', 'base', 'grail', 'scene33'
|
|
|
|
)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src, 'r') as fp_:
|
2012-05-13 05:19:39 +00:00
|
|
|
master_data = fp_.read()
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
2012-05-13 05:19:39 +00:00
|
|
|
minion_data = fp_.read()
|
|
|
|
self.assertEqual(master_data, minion_data)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 05:19:39 +00:00
|
|
|
|
2014-03-17 20:25:17 +00:00
|
|
|
def test_managed_file_mode(self):
|
|
|
|
'''
|
|
|
|
file.managed, correct file permissions
|
|
|
|
'''
|
|
|
|
desired_mode = 504 # 0770 octal
|
|
|
|
name = os.path.join(integration.TMP, 'grail_scene33')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, mode='0770', source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
|
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(name).st_mode
|
|
|
|
)
|
|
|
|
self.assertEqual(oct(desired_mode), oct(resulting_mode))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
def test_managed_file_mode_file_exists_replace(self):
|
|
|
|
'''
|
|
|
|
file.managed, existing file with replace=True, change permissions
|
|
|
|
'''
|
|
|
|
initial_mode = 504 # 0770 octal
|
|
|
|
desired_mode = 384 # 0600 octal
|
|
|
|
name = os.path.join(integration.TMP, 'grail_scene33')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, mode=oct(initial_mode), source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
|
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(name).st_mode
|
|
|
|
)
|
|
|
|
self.assertEqual(oct(initial_mode), oct(resulting_mode))
|
|
|
|
|
|
|
|
name = os.path.join(integration.TMP, 'grail_scene33')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, replace=True, mode=oct(desired_mode), source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(name).st_mode
|
|
|
|
)
|
|
|
|
self.assertEqual(oct(desired_mode), oct(resulting_mode))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
def test_managed_file_mode_file_exists_noreplace(self):
|
|
|
|
'''
|
|
|
|
file.managed, existing file with replace=False, change permissions
|
|
|
|
'''
|
|
|
|
initial_mode = 504 # 0770 octal
|
|
|
|
desired_mode = 384 # 0600 octal
|
|
|
|
name = os.path.join(integration.TMP, 'grail_scene33')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, replace=True, mode=oct(initial_mode), source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, replace=False, mode=oct(desired_mode), source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(name).st_mode
|
|
|
|
)
|
|
|
|
self.assertEqual(oct(desired_mode), oct(resulting_mode))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2014-06-10 00:56:24 +00:00
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2013-11-07 17:19:16 +00:00
|
|
|
def test_managed_dir_mode(self):
|
|
|
|
'''
|
2013-11-12 06:29:55 +00:00
|
|
|
Tests to ensure that file.managed creates directories with the
|
|
|
|
permissions requested with the dir_mode argument
|
2013-11-07 17:19:16 +00:00
|
|
|
'''
|
2013-11-12 06:29:55 +00:00
|
|
|
desired_mode = 511 # 0777 in octal
|
2013-11-07 17:19:16 +00:00
|
|
|
name = os.path.join(integration.TMP, 'a', 'managed_dir_mode_test_file')
|
2014-04-17 21:36:08 +00:00
|
|
|
desired_owner = 'nobody'
|
2013-11-07 17:19:16 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source='salt://grail/scene33',
|
|
|
|
mode=600,
|
|
|
|
makedirs=True,
|
2014-04-17 21:36:08 +00:00
|
|
|
user=desired_owner,
|
2013-11-07 17:19:16 +00:00
|
|
|
dir_mode=oct(desired_mode) # 0777
|
|
|
|
)
|
2013-11-12 06:29:55 +00:00
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(os.path.join(integration.TMP, 'a')).st_mode
|
|
|
|
)
|
2014-04-17 21:36:08 +00:00
|
|
|
resulting_owner = pwd.getpwuid(os.stat(os.path.join(integration.TMP, 'a')).st_uid).pw_name
|
2013-11-07 17:19:16 +00:00
|
|
|
self.assertEqual(oct(desired_mode), oct(resulting_mode))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-04-17 21:36:08 +00:00
|
|
|
self.assertEqual(desired_owner, resulting_owner)
|
2013-11-07 17:19:16 +00:00
|
|
|
|
2012-05-13 05:27:00 +00:00
|
|
|
def test_test_managed(self):
|
|
|
|
'''
|
|
|
|
file.managed test interface
|
|
|
|
'''
|
2013-03-23 20:41:27 +00:00
|
|
|
name = os.path.join(integration.TMP, 'grail_not_not_scene33')
|
2012-05-13 05:27:00 +00:00
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.managed', test=True, name=name, source='salt://grail/scene33'
|
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 05:27:00 +00:00
|
|
|
self.assertFalse(os.path.isfile(name))
|
2012-05-13 05:38:24 +00:00
|
|
|
|
2013-03-13 11:07:56 +00:00
|
|
|
def test_managed_show_diff_false(self):
|
|
|
|
'''
|
|
|
|
file.managed test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'grail_not_scene33')
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(name, 'wb') as fp_:
|
2013-03-13 11:07:56 +00:00
|
|
|
fp_.write('test_managed_show_diff_false\n')
|
|
|
|
|
|
|
|
ret = self.run_state(
|
2013-04-25 15:36:19 +00:00
|
|
|
'file.managed', name=name, source='salt://grail/scene33',
|
|
|
|
show_diff=False
|
2013-03-13 11:07:56 +00:00
|
|
|
)
|
|
|
|
|
2014-11-21 19:51:56 +00:00
|
|
|
changes = next(six.itervalues(ret))['changes']
|
2013-10-24 09:39:04 +00:00
|
|
|
self.assertEqual('<show_diff=False>', changes['diff'])
|
2013-03-13 11:07:56 +00:00
|
|
|
|
2012-05-13 05:38:24 +00:00
|
|
|
def test_directory(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'a_new_dir')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.directory', name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 05:38:24 +00:00
|
|
|
self.assertTrue(os.path.isdir(name))
|
|
|
|
|
2012-05-13 06:27:00 +00:00
|
|
|
def test_test_directory(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'a_not_dir')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.directory', test=True, name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 06:27:00 +00:00
|
|
|
self.assertFalse(os.path.isdir(name))
|
2012-05-13 06:54:28 +00:00
|
|
|
|
2013-06-11 00:22:19 +00:00
|
|
|
def test_directory_clean(self):
|
|
|
|
'''
|
|
|
|
file.directory with clean=True
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'directory_clean_dir')
|
|
|
|
if not os.path.isdir(name):
|
|
|
|
os.makedirs(name)
|
|
|
|
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
|
|
|
salt.utils.fopen(strayfile, 'w').close()
|
|
|
|
|
|
|
|
straydir = os.path.join(name, 'straydir')
|
|
|
|
if not os.path.isdir(straydir):
|
|
|
|
os.makedirs(straydir)
|
|
|
|
|
|
|
|
salt.utils.fopen(os.path.join(straydir, 'strayfile2'), 'w').close()
|
|
|
|
|
|
|
|
ret = self.run_state('file.directory', name=name, clean=True)
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.exists(strayfile))
|
|
|
|
self.assertFalse(os.path.exists(straydir))
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
|
|
|
|
|
|
|
def test_directory_clean_exclude(self):
|
|
|
|
'''
|
|
|
|
file.directory with clean=True and exclude_pat set
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'directory_clean_dir')
|
|
|
|
if not os.path.isdir(name):
|
|
|
|
os.makedirs(name)
|
|
|
|
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
|
|
|
salt.utils.fopen(strayfile, 'w').close()
|
|
|
|
|
|
|
|
straydir = os.path.join(name, 'straydir')
|
|
|
|
if not os.path.isdir(straydir):
|
|
|
|
os.makedirs(straydir)
|
|
|
|
|
|
|
|
strayfile2 = os.path.join(straydir, 'strayfile2')
|
|
|
|
salt.utils.fopen(strayfile2, 'w').close()
|
|
|
|
|
|
|
|
keepfile = os.path.join(straydir, 'keepfile')
|
|
|
|
salt.utils.fopen(keepfile, 'w').close()
|
|
|
|
|
|
|
|
ret = self.run_state('file.directory',
|
|
|
|
name=name,
|
|
|
|
clean=True,
|
|
|
|
exclude_pat='E@^straydir(|/keepfile)$')
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.exists(strayfile))
|
|
|
|
self.assertFalse(os.path.exists(strayfile2))
|
|
|
|
self.assertTrue(os.path.exists(keepfile))
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
|
|
|
|
|
|
|
def test_test_directory_clean_exclude(self):
|
|
|
|
'''
|
|
|
|
file.directory test with clean=True and exclude_pat set
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'directory_clean_dir')
|
|
|
|
if not os.path.isdir(name):
|
|
|
|
os.makedirs(name)
|
|
|
|
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
|
|
|
salt.utils.fopen(strayfile, 'w').close()
|
|
|
|
|
|
|
|
straydir = os.path.join(name, 'straydir')
|
|
|
|
if not os.path.isdir(straydir):
|
|
|
|
os.makedirs(straydir)
|
|
|
|
|
|
|
|
strayfile2 = os.path.join(straydir, 'strayfile2')
|
|
|
|
salt.utils.fopen(strayfile2, 'w').close()
|
|
|
|
|
|
|
|
keepfile = os.path.join(straydir, 'keepfile')
|
|
|
|
salt.utils.fopen(keepfile, 'w').close()
|
|
|
|
|
|
|
|
ret = self.run_state('file.directory',
|
|
|
|
test=True,
|
|
|
|
name=name,
|
|
|
|
clean=True,
|
|
|
|
exclude_pat='E@^straydir(|/keepfile)$')
|
|
|
|
|
2014-11-21 19:51:56 +00:00
|
|
|
comment = next(six.itervalues(ret))['comment']
|
2013-06-11 00:22:19 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
self.assertTrue(os.path.exists(strayfile))
|
|
|
|
self.assertTrue(os.path.exists(strayfile2))
|
|
|
|
self.assertTrue(os.path.exists(keepfile))
|
|
|
|
|
|
|
|
self.assertIn(strayfile, comment)
|
|
|
|
self.assertIn(strayfile2, comment)
|
|
|
|
self.assertNotIn(keepfile, comment)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
|
|
|
|
2012-05-13 06:54:28 +00:00
|
|
|
def test_recurse(self):
|
|
|
|
'''
|
|
|
|
file.recurse
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_dir')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.recurse', name=name, source='salt://grail')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '36', 'scene')))
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(name):
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
2012-05-13 06:54:28 +00:00
|
|
|
|
2013-11-02 18:31:14 +00:00
|
|
|
def test_recurse_specific_env(self):
|
|
|
|
'''
|
|
|
|
file.recurse passing __env__
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_dir_prod_env')
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
name=name,
|
|
|
|
source='salt://holy',
|
|
|
|
__env__='prod')
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '32', 'scene')))
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(name):
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
|
|
|
|
2013-11-12 06:29:55 +00:00
|
|
|
name = os.path.join(integration.TMP, 'recurse_dir_prod_env')
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
name=name,
|
|
|
|
source='salt://holy',
|
|
|
|
saltenv='prod')
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '32', 'scene')))
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(name):
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
|
|
|
|
2012-05-13 07:02:05 +00:00
|
|
|
def test_test_recurse(self):
|
|
|
|
'''
|
|
|
|
file.recurse test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_test_dir')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.recurse', test=True, name=name, source='salt://grail',
|
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 07:02:05 +00:00
|
|
|
self.assertFalse(os.path.isfile(os.path.join(name, '36', 'scene')))
|
2012-10-25 00:02:01 +00:00
|
|
|
self.assertFalse(os.path.exists(name))
|
|
|
|
|
2013-11-02 18:31:14 +00:00
|
|
|
def test_test_recurse_specific_env(self):
|
|
|
|
'''
|
|
|
|
file.recurse test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_test_dir_prod_env')
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
test=True,
|
|
|
|
name=name,
|
|
|
|
source='salt://holy',
|
|
|
|
__env__='prod'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
self.assertFalse(os.path.isfile(os.path.join(name, '32', 'scene')))
|
|
|
|
self.assertFalse(os.path.exists(name))
|
|
|
|
|
2013-11-12 06:29:55 +00:00
|
|
|
name = os.path.join(integration.TMP, 'recurse_test_dir_prod_env')
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
test=True,
|
|
|
|
name=name,
|
|
|
|
source='salt://holy',
|
|
|
|
saltenv='prod'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
self.assertFalse(os.path.isfile(os.path.join(name, '32', 'scene')))
|
|
|
|
self.assertFalse(os.path.exists(name))
|
|
|
|
|
2012-10-25 00:02:01 +00:00
|
|
|
def test_recurse_template(self):
|
|
|
|
'''
|
|
|
|
file.recurse with jinja template enabled
|
|
|
|
'''
|
|
|
|
_ts = 'TEMPLATE TEST STRING'
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_template_dir')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.recurse', name=name, source='salt://grail',
|
2012-11-06 06:26:08 +00:00
|
|
|
template='jinja', defaults={'spam': _ts})
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(os.path.join(name, 'scene33'), 'r') as fp_:
|
|
|
|
contents = fp_.read()
|
|
|
|
self.assertIn(_ts, contents)
|
2012-12-07 16:25:26 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
2012-10-25 00:02:01 +00:00
|
|
|
|
|
|
|
def test_recurse_clean(self):
|
|
|
|
'''
|
|
|
|
file.recurse with clean=True
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_clean_dir')
|
2012-12-07 16:25:26 +00:00
|
|
|
if not os.path.isdir(name):
|
|
|
|
os.makedirs(name)
|
2012-10-25 00:02:01 +00:00
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
2012-11-18 18:57:10 +00:00
|
|
|
salt.utils.fopen(strayfile, 'w').close()
|
2012-12-07 16:25:26 +00:00
|
|
|
|
2012-10-25 00:02:01 +00:00
|
|
|
# Corner cases: replacing file with a directory and vice versa
|
2012-11-18 18:57:10 +00:00
|
|
|
salt.utils.fopen(os.path.join(name, '36'), 'w').close()
|
2012-10-25 00:02:01 +00:00
|
|
|
os.makedirs(os.path.join(name, 'scene33'))
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.recurse', name=name, source='salt://grail', clean=True)
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.exists(strayfile))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '36', 'scene')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, 'scene33')))
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
2012-05-13 07:02:05 +00:00
|
|
|
|
2013-11-02 18:31:14 +00:00
|
|
|
def test_recurse_clean_specific_env(self):
|
|
|
|
'''
|
|
|
|
file.recurse with clean=True and __env__=prod
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_clean_dir_prod_env')
|
|
|
|
if not os.path.isdir(name):
|
|
|
|
os.makedirs(name)
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
|
|
|
salt.utils.fopen(strayfile, 'w').close()
|
|
|
|
|
|
|
|
# Corner cases: replacing file with a directory and vice versa
|
|
|
|
salt.utils.fopen(os.path.join(name, '32'), 'w').close()
|
|
|
|
os.makedirs(os.path.join(name, 'scene34'))
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
name=name,
|
|
|
|
source='salt://holy',
|
|
|
|
clean=True,
|
|
|
|
__env__='prod')
|
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.exists(strayfile))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '32', 'scene')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, 'scene34')))
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
|
|
|
|
2013-09-20 23:01:57 +00:00
|
|
|
def test_replace(self):
|
|
|
|
'''
|
|
|
|
file.replace
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'replace_test')
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('change_me')
|
|
|
|
|
|
|
|
ret = self.run_state('file.replace',
|
|
|
|
name=name, pattern='change', repl='salt', backup=False)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertIn('salt', fp_.read())
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
|
|
|
|
2014-12-17 10:46:21 +00:00
|
|
|
def test_replace_issue_18612(self):
|
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18612:
|
|
|
|
|
|
|
|
Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
|
|
|
|
an infinitely growing file as 'file.replace' didn't check beforehand
|
|
|
|
whether the changes had already been done to the file
|
|
|
|
|
|
|
|
# Case description:
|
|
|
|
|
|
|
|
The tested file contains one commented line
|
|
|
|
The commented line should be uncommented in the end, nothing else should change
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612'
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
with salt.utils.fopen(path_test, 'w+') as fp_test_:
|
|
|
|
fp_test_.write('# en_US.UTF-8')
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for x in range(0, 3):
|
|
|
|
ret.append(self.run_state('file.replace',
|
|
|
|
name=path_test, pattern='^# en_US.UTF-8$', repl='en_US.UTF-8', append_if_not_found=True))
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the number of lines didn't change, even after invoking 'file.replace' 3 times
|
|
|
|
with salt.utils.fopen(path_test, 'r') as fp_test_:
|
|
|
|
self.assertTrue((sum(1 for _ in fp_test_) == 1))
|
|
|
|
|
|
|
|
# ensure, the replacement succeeded
|
|
|
|
with salt.utils.fopen(path_test, 'r') as fp_test_:
|
|
|
|
self.assertTrue(fp_test_.read().startswith('en_US.UTF-8'))
|
|
|
|
|
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
|
|
|
def test_replace_issue_18612_prepend(self):
|
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18612:
|
|
|
|
|
|
|
|
Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
|
|
|
|
an infinitely growing file as 'file.replace' didn't check beforehand
|
|
|
|
whether the changes had already been done to the file
|
|
|
|
|
|
|
|
# Case description:
|
|
|
|
|
|
|
|
The tested multfile contains multiple lines not matching the pattern or replacement in any way
|
|
|
|
The replacement pattern should be prepended to the file
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612_prepend'
|
|
|
|
path_in = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.in'.format(test_name)
|
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.out'.format(test_name)
|
|
|
|
)
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
# create test file based on initial template
|
|
|
|
shutil.copyfile(path_in, path_test)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for x in range(0, 3):
|
|
|
|
ret.append(self.run_state('file.replace',
|
|
|
|
name=path_test, pattern='^# en_US.UTF-8$', repl='en_US.UTF-8', prepend_if_not_found=True))
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
|
|
|
|
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
|
|
|
|
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
|
|
|
def test_replace_issue_18612_append(self):
|
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18612:
|
|
|
|
|
|
|
|
Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
|
|
|
|
an infinitely growing file as 'file.replace' didn't check beforehand
|
|
|
|
whether the changes had already been done to the file
|
|
|
|
|
|
|
|
# Case description:
|
|
|
|
|
|
|
|
The tested multfile contains multiple lines not matching the pattern or replacement in any way
|
|
|
|
The replacement pattern should be appended to the file
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612_append'
|
|
|
|
path_in = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.in'.format(test_name)
|
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.out'.format(test_name)
|
|
|
|
)
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
# create test file based on initial template
|
|
|
|
shutil.copyfile(path_in, path_test)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for x in range(0, 3):
|
|
|
|
ret.append(self.run_state('file.replace',
|
|
|
|
name=path_test, pattern='^# en_US.UTF-8$', repl='en_US.UTF-8', append_if_not_found=True))
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
|
|
|
|
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
|
|
|
|
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
|
|
|
def test_replace_issue_18612_append_not_found_content(self):
|
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18612:
|
|
|
|
|
|
|
|
Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
|
|
|
|
an infinitely growing file as 'file.replace' didn't check beforehand
|
|
|
|
whether the changes had already been done to the file
|
|
|
|
|
|
|
|
# Case description:
|
|
|
|
|
|
|
|
The tested multfile contains multiple lines not matching the pattern or replacement in any way
|
|
|
|
The 'not_found_content' value should be appended to the file
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612_append_not_found_content'
|
|
|
|
path_in = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.in'.format(test_name)
|
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.out'.format(test_name)
|
|
|
|
)
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
# create test file based on initial template
|
|
|
|
shutil.copyfile(path_in, path_test)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for x in range(0, 3):
|
|
|
|
ret.append(
|
|
|
|
self.run_state('file.replace',
|
|
|
|
name=path_test,
|
|
|
|
pattern='^# en_US.UTF-8$',
|
|
|
|
repl='en_US.UTF-8',
|
|
|
|
append_if_not_found=True,
|
|
|
|
not_found_content='THIS LINE WASN\'T FOUND! SO WE\'RE APPENDING IT HERE!'
|
|
|
|
))
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
|
|
|
|
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
|
|
|
|
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
|
|
|
def test_replace_issue_18612_change_mid_line_with_comment(self):
|
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18612:
|
|
|
|
|
|
|
|
Using 'prepend_if_not_found' or 'append_if_not_found' resulted in
|
|
|
|
an infinitely growing file as 'file.replace' didn't check beforehand
|
|
|
|
whether the changes had already been done to the file
|
|
|
|
|
|
|
|
# Case description:
|
|
|
|
|
|
|
|
The tested file contains 5 key=value pairs
|
|
|
|
The commented key=value pair #foo=bar should be changed to foo=salt
|
|
|
|
The comment char (#) in front of foo=bar should be removed
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612_change_mid_line_with_comment'
|
|
|
|
path_in = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.in'.format(test_name)
|
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.out'.format(test_name)
|
|
|
|
)
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
# create test file based on initial template
|
|
|
|
shutil.copyfile(path_in, path_test)
|
|
|
|
|
|
|
|
ret = []
|
|
|
|
for x in range(0, 3):
|
|
|
|
ret.append(self.run_state('file.replace',
|
|
|
|
name=path_test, pattern='^#foo=bar$', repl='foo=salt', append_if_not_found=True))
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
|
|
|
|
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
|
|
|
|
|
|
|
# ensure, all 'file.replace' runs reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
2014-12-18 00:51:08 +00:00
|
|
|
def test_replace_issue_18841_no_changes(self):
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18841:
|
|
|
|
|
|
|
|
Using file.replace in a way which shouldn't modify the file at all
|
|
|
|
results in changed mtime of the original file and a backup file being created.
|
|
|
|
|
|
|
|
# Case description
|
|
|
|
|
2014-12-18 00:51:08 +00:00
|
|
|
The tested file contains multiple lines
|
|
|
|
The tested file contains a line already matching the replacement (no change needed)
|
2014-12-17 10:46:21 +00:00
|
|
|
The tested file's content shouldn't change at all
|
|
|
|
The tested file's mtime shouldn't change at all
|
2014-12-18 00:51:08 +00:00
|
|
|
No backup file should be created
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
2014-12-18 00:51:08 +00:00
|
|
|
test_name = 'test_replace_issue_18841_no_changes'
|
2014-12-17 10:46:21 +00:00
|
|
|
path_in = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.in'.format(test_name)
|
|
|
|
)
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
# create test file based on initial template
|
|
|
|
shutil.copyfile(path_in, path_test)
|
|
|
|
|
|
|
|
# get (m|a)time of file
|
|
|
|
fstats_orig = os.stat(path_test)
|
|
|
|
|
|
|
|
# define how far we predate the file
|
|
|
|
age = 5*24*60*60
|
|
|
|
|
|
|
|
# set (m|a)time of file 5 days into the past
|
|
|
|
os.utime(path_test, (fstats_orig.st_mtime-age, fstats_orig.st_atime-age))
|
|
|
|
|
|
|
|
ret = self.run_state('file.replace',
|
2014-12-18 00:51:08 +00:00
|
|
|
name=path_test,
|
|
|
|
pattern='^hello world$',
|
|
|
|
repl='goodbye world',
|
|
|
|
show_changes=True,
|
|
|
|
flags=['IGNORECASE'],
|
|
|
|
backup=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# get (m|a)time of file
|
|
|
|
fstats_post = os.stat(path_test)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the file content didn't change
|
|
|
|
self.assertTrue(filecmp.cmp(path_in, path_test))
|
|
|
|
|
|
|
|
# ensure no backup file was created
|
|
|
|
self.assertFalse(os.path.exists(path_test + '.bak'))
|
|
|
|
|
|
|
|
# ensure the file's mtime didn't change
|
|
|
|
self.assertTrue(fstats_post.st_mtime, fstats_orig.st_mtime-age)
|
|
|
|
|
|
|
|
# ensure, all 'file.replace' runs reported success
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
|
|
|
def test_replace_issue_18841_omit_backup(self):
|
|
|
|
'''
|
|
|
|
Test the (mis-)behaviour of file.replace as described in #18841:
|
|
|
|
|
|
|
|
Using file.replace in a way which shouldn't modify the file at all
|
|
|
|
results in changed mtime of the original file and a backup file being created.
|
|
|
|
|
|
|
|
# Case description
|
|
|
|
|
|
|
|
The tested file contains multiple lines
|
|
|
|
The tested file contains a line already matching the replacement (no change needed)
|
|
|
|
The tested file's content shouldn't change at all
|
|
|
|
The tested file's mtime shouldn't change at all
|
2015-01-16 21:35:47 +00:00
|
|
|
No backup file should be created, although backup=False isn't explicitly defined
|
2014-12-18 00:51:08 +00:00
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18841_omit_backup'
|
|
|
|
path_in = os.path.join(
|
|
|
|
integration.FILES, 'file.replace', '{0}.in'.format(test_name)
|
|
|
|
)
|
|
|
|
path_test = os.path.join(integration.TMP, test_name)
|
|
|
|
|
|
|
|
# create test file based on initial template
|
|
|
|
shutil.copyfile(path_in, path_test)
|
|
|
|
|
|
|
|
# get (m|a)time of file
|
|
|
|
fstats_orig = os.stat(path_test)
|
|
|
|
|
|
|
|
# define how far we predate the file
|
|
|
|
age = 5*24*60*60
|
|
|
|
|
|
|
|
# set (m|a)time of file 5 days into the past
|
|
|
|
os.utime(path_test, (fstats_orig.st_mtime-age, fstats_orig.st_atime-age))
|
|
|
|
|
|
|
|
ret = self.run_state('file.replace',
|
|
|
|
name=path_test,
|
|
|
|
pattern='^hello world$',
|
|
|
|
repl='goodbye world',
|
|
|
|
show_changes=True,
|
|
|
|
flags=['IGNORECASE']
|
|
|
|
)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
|
|
|
# get (m|a)time of file
|
|
|
|
fstats_post = os.stat(path_test)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# ensure, the file content didn't change
|
|
|
|
self.assertTrue(filecmp.cmp(path_in, path_test))
|
|
|
|
|
|
|
|
# ensure no backup file was created
|
|
|
|
self.assertFalse(os.path.exists(path_test + '.bak'))
|
|
|
|
|
|
|
|
# ensure the file's mtime didn't change
|
|
|
|
self.assertTrue(fstats_post.st_mtime, fstats_orig.st_mtime-age)
|
|
|
|
|
|
|
|
# ensure, all 'file.replace' runs reported success
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(path_test)
|
|
|
|
|
2012-05-13 07:35:01 +00:00
|
|
|
def test_comment(self):
|
|
|
|
'''
|
|
|
|
file.comment
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'comment_test')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
# write a line to file
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('comment_me')
|
|
|
|
# comment once
|
|
|
|
ret = self.run_state('file.comment', name=name, regex='^comment')
|
|
|
|
# result is positive
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
# line is commented
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertTrue(fp_.read().startswith('#comment'))
|
|
|
|
|
|
|
|
# comment twice
|
|
|
|
ret = self.run_state('file.comment', name=name, regex='^comment')
|
|
|
|
# result is still positive
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
# line is still commented
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertTrue(fp_.read().startswith('#comment'))
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 07:38:31 +00:00
|
|
|
|
|
|
|
def test_test_comment(self):
|
|
|
|
'''
|
|
|
|
file.comment test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'comment_test_test')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('comment_me')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.comment', test=True, name=name, regex='.*comment.*',
|
|
|
|
)
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('#comment', fp_.read())
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 07:54:20 +00:00
|
|
|
|
|
|
|
def test_uncomment(self):
|
|
|
|
'''
|
|
|
|
file.uncomment
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'uncomment_test')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#comment_me')
|
|
|
|
ret = self.run_state('file.uncomment', name=name, regex='^comment')
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('#comment', fp_.read())
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 07:54:20 +00:00
|
|
|
|
|
|
|
def test_test_uncomment(self):
|
|
|
|
'''
|
|
|
|
file.comment test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'uncomment_test_test')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#comment_me')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.uncomment', test=True, name=name, regex='^comment.*'
|
|
|
|
)
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertIn('#comment', fp_.read())
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 08:00:01 +00:00
|
|
|
|
|
|
|
def test_append(self):
|
|
|
|
'''
|
|
|
|
file.append
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'append_test')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#salty!')
|
|
|
|
ret = self.run_state('file.append', name=name, text='cheese')
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertIn('cheese', fp_.read())
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 08:00:01 +00:00
|
|
|
|
2012-05-13 08:02:20 +00:00
|
|
|
def test_test_append(self):
|
|
|
|
'''
|
|
|
|
file.append test interface
|
|
|
|
'''
|
2012-05-13 08:06:46 +00:00
|
|
|
name = os.path.join(integration.TMP, 'append_test_test')
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
with salt.utils.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#salty!')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', test=True, name=name, text='cheese'
|
|
|
|
)
|
|
|
|
with salt.utils.fopen(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('cheese', fp_.read())
|
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 08:06:46 +00:00
|
|
|
|
2012-08-28 11:05:50 +00:00
|
|
|
def test_append_issue_1864_makedirs(self):
|
|
|
|
'''
|
|
|
|
file.append but create directories if needed as an option
|
|
|
|
'''
|
|
|
|
fname = 'append_issue_1864_makedirs'
|
|
|
|
name = os.path.join(integration.TMP, fname)
|
2013-04-25 15:36:19 +00:00
|
|
|
try:
|
|
|
|
self.assertFalse(os.path.exists(name))
|
|
|
|
except AssertionError:
|
|
|
|
os.remove(name)
|
2012-08-28 11:05:50 +00:00
|
|
|
try:
|
|
|
|
# Non existing file get's touched
|
|
|
|
if os.path.isfile(name):
|
|
|
|
# left over
|
|
|
|
os.remove(name)
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', name=name, text='cheese', makedirs=True
|
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-08-28 11:05:50 +00:00
|
|
|
finally:
|
|
|
|
if os.path.isfile(name):
|
|
|
|
os.remove(name)
|
|
|
|
|
|
|
|
# Nested directory and file get's touched
|
|
|
|
name = os.path.join(integration.TMP, 'issue_1864', fname)
|
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', name=name, text='cheese', makedirs=True
|
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-08-28 11:05:50 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(
|
|
|
|
os.path.join(integration.TMP, 'issue_1864'),
|
|
|
|
ignore_errors=True
|
|
|
|
)
|
|
|
|
|
2012-05-13 08:06:46 +00:00
|
|
|
def test_touch(self):
|
|
|
|
'''
|
|
|
|
file.touch
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'touch_test')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.touch', name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertTrue(os.path.isfile(name))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(name)
|
2012-05-13 08:06:46 +00:00
|
|
|
|
2012-05-13 08:07:54 +00:00
|
|
|
def test_test_touch(self):
|
|
|
|
'''
|
|
|
|
file.touch test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'touch_test')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.touch', test=True, name=name)
|
2012-05-13 08:07:54 +00:00
|
|
|
self.assertFalse(os.path.isfile(name))
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-25 06:38:57 +00:00
|
|
|
|
2012-08-05 18:23:12 +00:00
|
|
|
def test_touch_directory(self):
|
|
|
|
'''
|
|
|
|
file.touch a directory
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'touch_test_dir')
|
|
|
|
try:
|
2012-08-25 13:21:38 +00:00
|
|
|
if not os.path.isdir(name):
|
|
|
|
# left behind... Don't fail because of this!
|
|
|
|
os.makedirs(name)
|
2012-08-05 18:23:12 +00:00
|
|
|
except OSError:
|
2013-06-27 12:38:01 +00:00
|
|
|
self.skipTest('Failed to create directory {0}'.format(name))
|
2012-08-05 18:23:12 +00:00
|
|
|
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
|
|
|
ret = self.run_state('file.touch', name=name)
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
|
|
|
finally:
|
|
|
|
os.removedirs(name)
|
2012-08-05 18:23:12 +00:00
|
|
|
|
2012-10-12 10:45:27 +00:00
|
|
|
def test_issue_2227_file_append(self):
|
|
|
|
'''
|
|
|
|
Text to append includes a percent symbol
|
|
|
|
'''
|
|
|
|
# let's make use of existing state to create a file with contents to
|
|
|
|
# test against
|
2012-11-06 12:44:53 +00:00
|
|
|
tmp_file_append = os.path.join(
|
|
|
|
integration.TMP, 'test.append'
|
|
|
|
)
|
2012-10-12 10:45:27 +00:00
|
|
|
if os.path.isfile(tmp_file_append):
|
|
|
|
os.remove(tmp_file_append)
|
|
|
|
self.run_function('state.sls', mods='testappend')
|
|
|
|
self.run_function('state.sls', mods='testappend.step1')
|
|
|
|
self.run_function('state.sls', mods='testappend.step2')
|
|
|
|
|
|
|
|
# Now our real test
|
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
2012-10-12 10:53:40 +00:00
|
|
|
'state.sls', mods='testappend.issue-2227'
|
2012-10-12 10:45:27 +00:00
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(tmp_file_append, 'r') as fp_:
|
|
|
|
contents_pre = fp_.read()
|
2012-10-12 10:45:27 +00:00
|
|
|
|
|
|
|
# It should not append text again
|
|
|
|
ret = self.run_function(
|
2012-10-12 10:53:40 +00:00
|
|
|
'state.sls', mods='testappend.issue-2227'
|
2012-10-12 10:45:27 +00:00
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-10-12 10:45:27 +00:00
|
|
|
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(tmp_file_append, 'r') as fp_:
|
|
|
|
contents_post = fp_.read()
|
2012-10-12 10:45:27 +00:00
|
|
|
|
2014-07-15 01:30:28 +00:00
|
|
|
self.assertEqual(contents_pre, contents_post)
|
2012-11-08 12:47:19 +00:00
|
|
|
except AssertionError:
|
|
|
|
shutil.copy(tmp_file_append, tmp_file_append + '.bak')
|
|
|
|
raise
|
2012-10-12 10:45:27 +00:00
|
|
|
finally:
|
|
|
|
if os.path.isfile(tmp_file_append):
|
|
|
|
os.remove(tmp_file_append)
|
|
|
|
|
2013-06-27 12:38:01 +00:00
|
|
|
def do_patch(self, patch_name='hello', src='Hello\n'):
|
2012-10-17 21:14:49 +00:00
|
|
|
if not self.run_function('cmd.has_exec', ['patch']):
|
|
|
|
self.skipTest('patch is not installed')
|
|
|
|
src_file = os.path.join(integration.TMP, 'src.txt')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src_file, 'w+') as fp:
|
2012-10-17 21:14:49 +00:00
|
|
|
fp.write(src)
|
2012-12-07 16:25:26 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.patch',
|
2012-10-17 21:14:49 +00:00
|
|
|
name=src_file,
|
2012-10-19 04:37:02 +00:00
|
|
|
source='salt://{0}.patch'.format(patch_name),
|
2012-10-17 21:14:49 +00:00
|
|
|
hash='md5=f0ef7081e1539ac00ef5b761b4fb01b3',
|
|
|
|
)
|
|
|
|
return src_file, ret
|
|
|
|
|
|
|
|
def test_patch(self):
|
|
|
|
src_file, ret = self.do_patch()
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src_file) as fp:
|
2012-10-17 21:14:49 +00:00
|
|
|
self.assertEqual(fp.read(), 'Hello world\n')
|
|
|
|
|
|
|
|
def test_patch_hash_mismatch(self):
|
|
|
|
src_file, ret = self.do_patch('hello_dolly')
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
2013-08-18 04:46:33 +00:00
|
|
|
'File {0} hash mismatch after patch was applied'.format(src_file),
|
|
|
|
ret
|
2012-12-07 16:25:26 +00:00
|
|
|
)
|
2012-10-17 21:14:49 +00:00
|
|
|
|
|
|
|
def test_patch_already_applied(self):
|
2012-12-07 16:25:26 +00:00
|
|
|
src_file, ret = self.do_patch(src='Hello world\n')
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-08-18 04:46:33 +00:00
|
|
|
self.assertInSaltComment('Patch is already applied', ret)
|
2012-10-17 21:14:49 +00:00
|
|
|
|
2012-11-08 12:47:19 +00:00
|
|
|
def test_issue_2401_file_comment(self):
|
|
|
|
# Get a path to the temporary file
|
|
|
|
tmp_file = os.path.join(integration.TMP, 'issue-2041-comment.txt')
|
|
|
|
# Write some data to it
|
2012-11-18 18:57:10 +00:00
|
|
|
salt.utils.fopen(tmp_file, 'w').write('hello\nworld\n')
|
2012-11-08 12:47:19 +00:00
|
|
|
# create the sls template
|
|
|
|
template_lines = [
|
2013-06-27 12:38:01 +00:00
|
|
|
'{0}:'.format(tmp_file),
|
|
|
|
' file.comment:',
|
|
|
|
' - regex: ^world'
|
2012-11-08 12:47:19 +00:00
|
|
|
]
|
|
|
|
template = '\n'.join(template_lines)
|
|
|
|
try:
|
2013-01-14 14:07:58 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', [template], timeout=120
|
|
|
|
)
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-08-18 04:49:18 +00:00
|
|
|
self.assertNotInSaltComment('Pattern already commented', ret)
|
2013-08-18 04:46:33 +00:00
|
|
|
self.assertInSaltComment('Commented lines successfully', ret)
|
2012-11-08 12:47:19 +00:00
|
|
|
|
|
|
|
# This next time, it is already commented.
|
2013-01-14 14:07:58 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', [template], timeout=120
|
|
|
|
)
|
2012-11-08 12:47:19 +00:00
|
|
|
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-08-18 04:46:33 +00:00
|
|
|
self.assertInSaltComment('Pattern already commented', ret)
|
2012-11-08 12:47:19 +00:00
|
|
|
except AssertionError:
|
|
|
|
shutil.copy(tmp_file, tmp_file + '.bak')
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
if os.path.isfile(tmp_file):
|
|
|
|
os.remove(tmp_file)
|
|
|
|
|
2012-11-08 19:33:51 +00:00
|
|
|
def test_issue_2379_file_append(self):
|
|
|
|
# Get a path to the temporary file
|
|
|
|
tmp_file = os.path.join(integration.TMP, 'issue-2379-file-append.txt')
|
|
|
|
# Write some data to it
|
2012-11-18 18:57:10 +00:00
|
|
|
salt.utils.fopen(tmp_file, 'w').write(
|
2012-11-08 19:33:51 +00:00
|
|
|
'hello\nworld\n' + # Some junk
|
|
|
|
'#PermitRootLogin yes\n' + # Commented text
|
|
|
|
'# PermitRootLogin yes\n' # Commented text with space
|
|
|
|
)
|
|
|
|
# create the sls template
|
|
|
|
template_lines = [
|
2013-06-27 12:38:01 +00:00
|
|
|
'{0}:'.format(tmp_file),
|
|
|
|
' file.append:',
|
|
|
|
' - text: PermitRootLogin yes'
|
2012-11-08 19:33:51 +00:00
|
|
|
]
|
|
|
|
template = '\n'.join(template_lines)
|
|
|
|
try:
|
|
|
|
ret = self.run_function('state.template_str', [template])
|
|
|
|
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-08-18 04:46:33 +00:00
|
|
|
self.assertInSaltComment('Appended 1 lines', ret)
|
2012-11-08 19:33:51 +00:00
|
|
|
except AssertionError:
|
|
|
|
shutil.copy(tmp_file, tmp_file + '.bak')
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
if os.path.isfile(tmp_file):
|
|
|
|
os.remove(tmp_file)
|
|
|
|
|
2012-11-28 17:35:25 +00:00
|
|
|
def test_issue_2726_mode_kwarg(self):
|
|
|
|
testcase_temp_dir = os.path.join(integration.TMP, 'issue_2726')
|
|
|
|
# Let's test for the wrong usage approach
|
|
|
|
bad_mode_kwarg_testfile = os.path.join(
|
|
|
|
testcase_temp_dir, 'bad_mode_kwarg', 'testfile'
|
|
|
|
)
|
|
|
|
bad_template = [
|
|
|
|
'{0}:'.format(bad_mode_kwarg_testfile),
|
|
|
|
' file.recurse:',
|
|
|
|
' - source: salt://testfile',
|
|
|
|
' - mode: 644'
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', ['\n'.join(bad_template)]
|
|
|
|
)
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
2012-11-28 17:57:44 +00:00
|
|
|
'\'mode\' is not allowed in \'file.recurse\'. Please use '
|
2013-08-18 04:46:33 +00:00
|
|
|
'\'file_mode\' and \'dir_mode\'.',
|
|
|
|
ret
|
2012-11-28 17:57:44 +00:00
|
|
|
)
|
|
|
|
self.assertNotInSaltComment(
|
2012-11-28 17:35:25 +00:00
|
|
|
'TypeError: managed() got multiple values for keyword '
|
2013-08-18 04:49:18 +00:00
|
|
|
'argument \'mode\'',
|
|
|
|
ret
|
2012-11-28 17:35:25 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(testcase_temp_dir):
|
|
|
|
shutil.rmtree(testcase_temp_dir)
|
|
|
|
|
|
|
|
# Now, the correct usage approach
|
|
|
|
good_mode_kwargs_testfile = os.path.join(
|
|
|
|
testcase_temp_dir, 'good_mode_kwargs', 'testappend'
|
|
|
|
)
|
|
|
|
good_template = [
|
|
|
|
'{0}:'.format(good_mode_kwargs_testfile),
|
|
|
|
' file.recurse:',
|
|
|
|
' - source: salt://testappend',
|
|
|
|
' - dir_mode: 744',
|
|
|
|
' - file_mode: 644',
|
|
|
|
]
|
|
|
|
try:
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', ['\n'.join(good_template)]
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(testcase_temp_dir):
|
|
|
|
shutil.rmtree(testcase_temp_dir)
|
|
|
|
|
2013-11-22 20:16:24 +00:00
|
|
|
def test_issue_8343_accumulated_require_in(self):
|
|
|
|
template_path = os.path.join(integration.TMP_STATE_TREE, 'issue-8343.sls')
|
|
|
|
testcase_filedest = os.path.join(integration.TMP, 'issue-8343.txt')
|
|
|
|
sls_template = [
|
|
|
|
'{0}:',
|
|
|
|
' file.managed:',
|
|
|
|
' - contents: |',
|
|
|
|
' #',
|
|
|
|
'',
|
|
|
|
'prepend-foo-accumulator-from-pillar:',
|
|
|
|
' file.accumulated:',
|
|
|
|
' - require_in:',
|
|
|
|
' - file: prepend-foo-management',
|
|
|
|
' - filename: {0}',
|
|
|
|
' - text: |',
|
|
|
|
' foo',
|
|
|
|
'',
|
|
|
|
'append-foo-accumulator-from-pillar:',
|
|
|
|
' file.accumulated:',
|
|
|
|
' - require_in:',
|
|
|
|
' - file: append-foo-management',
|
|
|
|
' - filename: {0}',
|
|
|
|
' - text: |',
|
|
|
|
' bar',
|
|
|
|
'',
|
|
|
|
'prepend-foo-management:',
|
|
|
|
' file.blockreplace:',
|
|
|
|
' - name: {0}',
|
|
|
|
' - marker_start: "#-- start salt managed zonestart -- PLEASE, DO NOT EDIT"',
|
|
|
|
' - marker_end: "#-- end salt managed zonestart --"',
|
|
|
|
" - content: ''",
|
2013-11-24 12:15:20 +00:00
|
|
|
' - prepend_if_not_found: True',
|
2013-11-22 20:16:24 +00:00
|
|
|
" - backup: '.bak'",
|
|
|
|
' - show_changes: True',
|
|
|
|
'',
|
|
|
|
'append-foo-management:',
|
|
|
|
' file.blockreplace:',
|
|
|
|
' - name: {0}',
|
|
|
|
' - marker_start: "#-- start salt managed zoneend -- PLEASE, DO NOT EDIT"',
|
|
|
|
' - marker_end: "#-- end salt managed zoneend --"',
|
|
|
|
" - content: ''",
|
|
|
|
' - append_if_not_found: True',
|
2013-11-24 12:15:20 +00:00
|
|
|
" - backup: '.bak2'",
|
2013-11-22 20:16:24 +00:00
|
|
|
' - show_changes: True',
|
|
|
|
'']
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(template_path, 'w') as fp_:
|
|
|
|
fp_.write('\n'.join(sls_template).format(testcase_filedest))
|
|
|
|
|
2013-11-22 20:16:24 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_function('state.sls', mods='issue-8343')
|
2014-11-21 19:51:56 +00:00
|
|
|
for name, step in six.iteritems(ret):
|
2013-11-24 12:15:20 +00:00
|
|
|
self.assertSaltTrueReturn({name: step})
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(testcase_filedest) as fp_:
|
|
|
|
contents = fp_.read().split('\n')
|
2013-11-22 20:16:24 +00:00
|
|
|
self.assertEqual(
|
2013-11-24 12:15:20 +00:00
|
|
|
['#-- start salt managed zonestart -- PLEASE, DO NOT EDIT',
|
2013-11-22 20:16:24 +00:00
|
|
|
'foo',
|
|
|
|
'',
|
|
|
|
'#-- end salt managed zonestart --',
|
2014-03-10 15:41:49 +00:00
|
|
|
'#',
|
|
|
|
'#-- start salt managed zoneend -- PLEASE, DO NOT EDIT',
|
|
|
|
'bar',
|
|
|
|
'',
|
|
|
|
'#-- end salt managed zoneend --',
|
|
|
|
''],
|
2014-07-15 01:30:28 +00:00
|
|
|
contents
|
2014-03-10 15:41:49 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(testcase_filedest):
|
|
|
|
os.unlink(testcase_filedest)
|
|
|
|
for filename in glob.glob('{0}.bak*'.format(testcase_filedest)):
|
|
|
|
os.unlink(filename)
|
|
|
|
|
2014-03-13 09:51:12 +00:00
|
|
|
def test_issue_11003_immutable_lazy_proxy_sum(self):
|
2014-03-10 15:41:49 +00:00
|
|
|
template_path = os.path.join(integration.TMP_STATE_TREE, 'issue-11003.sls')
|
|
|
|
testcase_filedest = os.path.join(integration.TMP, 'issue-11003.txt')
|
|
|
|
sls_template = [
|
2014-03-13 09:51:12 +00:00
|
|
|
'a{0}:',
|
|
|
|
' file.absent:',
|
|
|
|
' - name: {0}',
|
|
|
|
'',
|
|
|
|
'{0}:',
|
|
|
|
' file.managed:',
|
|
|
|
' - contents: |',
|
|
|
|
' #',
|
|
|
|
'',
|
2014-03-10 15:41:49 +00:00
|
|
|
'test-acc1:',
|
|
|
|
' file.accumulated:',
|
|
|
|
' - require_in:',
|
|
|
|
' - file: final',
|
|
|
|
' - filename: {0}',
|
|
|
|
' - text: |',
|
|
|
|
' bar',
|
2014-03-13 09:51:12 +00:00
|
|
|
'',
|
2014-03-10 15:41:49 +00:00
|
|
|
'test-acc2:',
|
|
|
|
' file.accumulated:',
|
|
|
|
' - watch_in:',
|
|
|
|
' - file: final',
|
|
|
|
' - filename: {0}',
|
|
|
|
' - text: |',
|
|
|
|
' baz',
|
2014-03-13 09:51:12 +00:00
|
|
|
'',
|
2014-03-10 15:41:49 +00:00
|
|
|
'final:',
|
|
|
|
' file.blockreplace:',
|
|
|
|
' - name: {0}',
|
2014-03-13 09:51:12 +00:00
|
|
|
' - marker_start: "#-- start managed zone PLEASE, DO NOT EDIT"',
|
|
|
|
' - marker_end: "#-- end managed zone"',
|
2014-03-10 15:41:49 +00:00
|
|
|
' - content: \'\'',
|
|
|
|
' - append_if_not_found: True',
|
|
|
|
' - show_changes: True'
|
|
|
|
]
|
|
|
|
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(template_path, 'w') as fp_:
|
|
|
|
fp_.write('\n'.join(sls_template).format(testcase_filedest))
|
|
|
|
|
2014-03-10 15:41:49 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_function('state.sls', mods='issue-11003')
|
2014-11-21 19:51:56 +00:00
|
|
|
for name, step in six.iteritems(ret):
|
2014-03-10 15:41:49 +00:00
|
|
|
self.assertSaltTrueReturn({name: step})
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(testcase_filedest) as fp_:
|
|
|
|
contents = fp_.read().split('\n')
|
2014-03-10 15:41:49 +00:00
|
|
|
self.assertEqual(
|
2014-03-13 09:51:12 +00:00
|
|
|
['#',
|
|
|
|
'#-- start managed zone PLEASE, DO NOT EDIT',
|
2013-11-22 20:16:24 +00:00
|
|
|
'bar',
|
|
|
|
'',
|
2014-03-13 09:51:12 +00:00
|
|
|
'baz',
|
|
|
|
'',
|
|
|
|
'#-- end managed zone',
|
2013-11-22 20:16:24 +00:00
|
|
|
''],
|
2014-07-15 01:30:28 +00:00
|
|
|
contents
|
2013-11-22 20:16:24 +00:00
|
|
|
)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(testcase_filedest):
|
|
|
|
os.unlink(testcase_filedest)
|
2013-11-24 12:15:20 +00:00
|
|
|
for filename in glob.glob('{0}.bak*'.format(testcase_filedest)):
|
|
|
|
os.unlink(filename)
|
2013-11-22 20:16:24 +00:00
|
|
|
|
2013-12-05 16:59:08 +00:00
|
|
|
def test_issue_8947_utf8_sls(self):
|
|
|
|
'''
|
|
|
|
Test some file operation with utf-8 chararacters on the sls
|
2013-12-09 20:02:25 +00:00
|
|
|
|
2013-12-05 16:59:08 +00:00
|
|
|
This is more generic than just a file test. Feel free to move
|
|
|
|
'''
|
|
|
|
# Get a path to the temporary file
|
2014-04-02 17:16:24 +00:00
|
|
|
# 한국어 시험 (korean)
|
2013-12-05 16:59:08 +00:00
|
|
|
# '\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4 \xec\x8b\x9c\xed\x97\x98' (utf-8)
|
|
|
|
# u'\ud55c\uad6d\uc5b4 \uc2dc\ud5d8' (unicode)
|
|
|
|
korean_1 = '한국어 시험'
|
|
|
|
korean_utf8_1 = ('\xed\x95\x9c\xea\xb5\xad\xec\x96\xb4'
|
|
|
|
' \xec\x8b\x9c\xed\x97\x98')
|
|
|
|
korean_unicode_1 = u'\ud55c\uad6d\uc5b4 \uc2dc\ud5d8'
|
|
|
|
korean_2 = '첫 번째 행'
|
|
|
|
korean_utf8_2 = '\xec\xb2\xab \xeb\xb2\x88\xec\xa7\xb8 \xed\x96\x89'
|
|
|
|
korean_unicode_2 = u'\uccab \ubc88\uc9f8 \ud589'
|
|
|
|
korean_3 = '마지막 행'
|
|
|
|
korean_utf8_3 = '\xeb\xa7\x88\xec\xa7\x80\xeb\xa7\x89 \xed\x96\x89'
|
|
|
|
korean_unicode_3 = u'\ub9c8\uc9c0\ub9c9 \ud589'
|
|
|
|
test_file = os.path.join(integration.TMP,
|
|
|
|
'salt_utf8_tests/'+korean_utf8_1+'.txt'
|
|
|
|
)
|
|
|
|
template_path = os.path.join(integration.TMP_STATE_TREE, 'issue-8947.sls')
|
|
|
|
# create the sls template
|
|
|
|
template_lines = [
|
|
|
|
'# -*- coding: utf-8 -*-',
|
|
|
|
'some-utf8-file-create:',
|
|
|
|
' file.managed:',
|
|
|
|
" - name: '{0}'".format(test_file),
|
|
|
|
" - contents: {0}".format(korean_utf8_1),
|
|
|
|
' - makedirs: True',
|
|
|
|
' - replace: True',
|
|
|
|
' - show_diff: True',
|
|
|
|
'some-utf8-file-create2:',
|
|
|
|
' file.managed:',
|
|
|
|
" - name: '{0}'".format(test_file),
|
|
|
|
' - contents: |',
|
|
|
|
' {0}'.format(korean_utf8_2),
|
|
|
|
' {0}'.format(korean_utf8_1),
|
|
|
|
' {0}'.format(korean_utf8_3),
|
|
|
|
' - replace: True',
|
|
|
|
' - show_diff: True',
|
|
|
|
'some-utf8-file-exists:',
|
|
|
|
' file.exists:',
|
|
|
|
" - name: '{0}'".format(test_file),
|
|
|
|
' - require:',
|
|
|
|
' - file: some-utf8-file-create2',
|
|
|
|
'some-utf8-file-content-test:',
|
|
|
|
' cmd.run:',
|
|
|
|
' - name: \'cat "{0}"\''.format(test_file),
|
|
|
|
' - require:',
|
|
|
|
' - file: some-utf8-file-exists',
|
|
|
|
'some-utf8-file-content-remove:',
|
|
|
|
' cmd.run:',
|
|
|
|
' - name: \'rm -f "{0}"\''.format(test_file),
|
|
|
|
' - require:',
|
|
|
|
' - cmd: some-utf8-file-content-test',
|
|
|
|
'some-utf8-file-removed:',
|
|
|
|
' file.missing:',
|
|
|
|
" - name: '{0}'".format(test_file),
|
|
|
|
' - require:',
|
|
|
|
' - cmd: some-utf8-file-content-remove',
|
|
|
|
]
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(template_path, 'w') as fp_:
|
|
|
|
fp_.write('\n'.join(template_lines))
|
2013-12-05 16:59:08 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_function('state.sls', mods='issue-8947')
|
|
|
|
if not isinstance(ret, dict):
|
|
|
|
raise AssertionError(
|
|
|
|
('Something went really wrong while testing this sls:'
|
|
|
|
' {0}').format(repr(ret))
|
|
|
|
)
|
|
|
|
# using unicode.encode('utf-8') we should get the same as
|
|
|
|
# an utf-8 string
|
|
|
|
expected = {
|
|
|
|
('file_|-some-utf8-file-create_|-{0}'
|
|
|
|
'_|-managed').format(test_file): {
|
|
|
|
'name': '{0}'.format(test_file),
|
|
|
|
'__run_num__': 0,
|
2013-12-09 20:02:25 +00:00
|
|
|
'comment': 'File {0} updated'.format(test_file),
|
|
|
|
'diff': 'New file'
|
2013-12-05 16:59:08 +00:00
|
|
|
},
|
|
|
|
('file_|-some-utf8-file-create2_|-{0}'
|
|
|
|
'_|-managed').format(test_file): {
|
|
|
|
'name': '{0}'.format(test_file),
|
|
|
|
'__run_num__': 1,
|
2013-12-09 20:02:25 +00:00
|
|
|
'comment': 'File {0} updated'.format(test_file),
|
|
|
|
'diff': 'Replace binary file with text file'
|
2013-12-05 16:59:08 +00:00
|
|
|
},
|
|
|
|
('file_|-some-utf8-file-exists_|-{0}'
|
|
|
|
'_|-exists').format(test_file): {
|
|
|
|
'name': '{0}'.format(test_file),
|
|
|
|
'__run_num__': 2,
|
2013-12-09 20:02:25 +00:00
|
|
|
'comment': 'Path {0} exists'.format(test_file)
|
2013-12-05 16:59:08 +00:00
|
|
|
},
|
|
|
|
('cmd_|-some-utf8-file-content-test_|-cat "{0}"'
|
|
|
|
'_|-run').format(test_file): {
|
|
|
|
'name': 'cat "{0}"'.format(test_file),
|
|
|
|
'__run_num__': 3,
|
2013-12-09 20:02:25 +00:00
|
|
|
'comment': 'Command "cat "{0}"" run'.format(test_file),
|
|
|
|
'stdout': '{0}\n{1}\n{2}'.format(
|
2013-12-05 16:59:08 +00:00
|
|
|
korean_unicode_2.encode('utf-8'),
|
|
|
|
korean_unicode_1.encode('utf-8'),
|
|
|
|
korean_unicode_3.encode('utf-8')
|
|
|
|
)
|
|
|
|
},
|
|
|
|
('cmd_|-some-utf8-file-content-remove_|-rm -f "{0}"'
|
|
|
|
'_|-run').format(test_file): {
|
|
|
|
'name': 'rm -f "{0}"'.format(test_file),
|
|
|
|
'__run_num__': 4,
|
2013-12-09 20:02:25 +00:00
|
|
|
'comment': 'Command "rm -f "{0}"" run'.format(test_file),
|
|
|
|
'stdout': ''
|
2013-12-05 16:59:08 +00:00
|
|
|
},
|
|
|
|
('file_|-some-utf8-file-removed_|-{0}'
|
|
|
|
'_|-missing').format(test_file): {
|
|
|
|
'name': '{0}'.format(test_file),
|
|
|
|
'__run_num__': 5,
|
2013-12-09 20:02:25 +00:00
|
|
|
'comment':
|
2013-12-05 16:59:08 +00:00
|
|
|
'Path {0} is missing'.format(test_file),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result = {}
|
2014-11-21 19:51:56 +00:00
|
|
|
for name, step in six.iteritems(ret):
|
2013-12-05 16:59:08 +00:00
|
|
|
self.assertSaltTrueReturn({name: step})
|
|
|
|
result.update({
|
|
|
|
name: {
|
2013-12-09 20:02:25 +00:00
|
|
|
'name': step['name'],
|
|
|
|
'__run_num__': step['__run_num__'],
|
|
|
|
'comment': step['comment']
|
2013-12-05 16:59:08 +00:00
|
|
|
}})
|
|
|
|
if 'diff' in step['changes']:
|
|
|
|
result[name]['diff'] = step['changes']['diff']
|
|
|
|
if 'stdout' in step['changes']:
|
|
|
|
result[name]['stdout'] = step['changes']['stdout']
|
2013-12-09 20:02:25 +00:00
|
|
|
|
|
|
|
self.maxDiff = None
|
|
|
|
|
2013-12-05 16:59:08 +00:00
|
|
|
self.assertEqual(expected, result)
|
2013-12-05 17:50:31 +00:00
|
|
|
cat_id = ('cmd_|-some-utf8-file-content-test_|-cat "{0}"'
|
|
|
|
'_|-run').format(test_file)
|
|
|
|
self.assertEqual(
|
|
|
|
result[cat_id]['stdout'],
|
|
|
|
korean_2 + '\n' + korean_1 + '\n' + korean_3
|
|
|
|
)
|
2013-12-05 16:59:08 +00:00
|
|
|
finally:
|
|
|
|
if os.path.isdir(test_file):
|
|
|
|
os.unlink(test_file)
|
|
|
|
os.unlink(template_path)
|
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2014-04-25 18:57:14 +00:00
|
|
|
@with_system_user_and_group('user12209', 'group12209',
|
|
|
|
on_existing='delete', delete=True)
|
|
|
|
def test_issue_12209_follow_symlinks(self, user, group):
|
2014-04-25 03:27:59 +00:00
|
|
|
'''
|
|
|
|
Ensure that symlinks are properly chowned when recursing (following
|
|
|
|
symlinks)
|
|
|
|
'''
|
|
|
|
tmp_dir = os.path.join(integration.TMP, 'test.12209')
|
|
|
|
|
|
|
|
# Cleanup the path if it already exists
|
|
|
|
if os.path.isdir(tmp_dir):
|
|
|
|
shutil.rmtree(tmp_dir)
|
|
|
|
elif os.path.isfile(tmp_dir):
|
|
|
|
os.remove(tmp_dir)
|
|
|
|
|
|
|
|
# Make the directories for this test
|
|
|
|
onedir = os.path.join(tmp_dir, 'one')
|
|
|
|
twodir = os.path.join(tmp_dir, 'two')
|
|
|
|
os.makedirs(onedir)
|
|
|
|
os.symlink(onedir, twodir)
|
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
try:
|
|
|
|
# Run the state
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', name=tmp_dir, follow_symlinks=True,
|
|
|
|
user=user, group=group, recurse=['user', 'group']
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
# Double-check, in case state mis-reported a True result. Since we are
|
|
|
|
# following symlinks, we expect twodir to still be owned by root, but
|
|
|
|
# onedir should be owned by the 'issue12209' user.
|
|
|
|
onestats = os.stat(onedir)
|
|
|
|
twostats = os.lstat(twodir)
|
|
|
|
self.assertEqual(pwd.getpwuid(onestats.st_uid).pw_name, user)
|
|
|
|
self.assertEqual(pwd.getpwuid(twostats.st_uid).pw_name, 'root')
|
|
|
|
self.assertEqual(grp.getgrgid(onestats.st_gid).gr_name, group)
|
|
|
|
self.assertEqual(grp.getgrgid(twostats.st_gid).gr_name, 'root')
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(tmp_dir):
|
|
|
|
shutil.rmtree(tmp_dir)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
@destructiveTest
|
|
|
|
@skipIf(os.geteuid() != 0, 'you must be root to run this test')
|
2014-04-25 18:57:14 +00:00
|
|
|
@with_system_user_and_group('user12209', 'group12209',
|
|
|
|
on_existing='delete', delete=True)
|
|
|
|
def test_issue_12209_no_follow_symlinks(self, user, group):
|
2014-04-25 03:27:59 +00:00
|
|
|
'''
|
|
|
|
Ensure that symlinks are properly chowned when recursing (not following
|
|
|
|
symlinks)
|
|
|
|
'''
|
|
|
|
tmp_dir = os.path.join(integration.TMP, 'test.12209')
|
|
|
|
|
|
|
|
# Cleanup the path if it already exists
|
|
|
|
if os.path.isdir(tmp_dir):
|
|
|
|
shutil.rmtree(tmp_dir)
|
|
|
|
elif os.path.isfile(tmp_dir):
|
|
|
|
os.remove(tmp_dir)
|
|
|
|
|
|
|
|
# Make the directories for this test
|
|
|
|
onedir = os.path.join(tmp_dir, 'one')
|
|
|
|
twodir = os.path.join(tmp_dir, 'two')
|
|
|
|
os.makedirs(onedir)
|
|
|
|
os.symlink(onedir, twodir)
|
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
try:
|
|
|
|
# Run the state
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', name=tmp_dir, follow_symlinks=False,
|
|
|
|
user=user, group=group, recurse=['user', 'group']
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2014-04-25 19:38:14 +00:00
|
|
|
# Double-check, in case state mis-reported a True result. Since we
|
|
|
|
# are not following symlinks, we expect twodir to now be owned by
|
|
|
|
# the 'issue12209' user, just link onedir.
|
|
|
|
onestats = os.stat(onedir)
|
|
|
|
twostats = os.lstat(twodir)
|
|
|
|
self.assertEqual(pwd.getpwuid(onestats.st_uid).pw_name, user)
|
|
|
|
self.assertEqual(pwd.getpwuid(twostats.st_uid).pw_name, user)
|
|
|
|
self.assertEqual(grp.getgrgid(onestats.st_gid).gr_name, group)
|
|
|
|
self.assertEqual(grp.getgrgid(twostats.st_gid).gr_name, group)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(tmp_dir):
|
|
|
|
shutil.rmtree(tmp_dir)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2014-07-14 22:40:03 +00:00
|
|
|
def test_template_local_file(self):
|
|
|
|
'''
|
|
|
|
Test a file.managed state with a local file as the source. Test both
|
|
|
|
with the file:// protocol designation prepended, and without it.
|
|
|
|
'''
|
|
|
|
source = tempfile.mkstemp()[-1]
|
|
|
|
dest = tempfile.mkstemp()[-1]
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(source, 'w') as fp_:
|
2014-07-14 22:40:03 +00:00
|
|
|
fp_.write('{{ foo }}\n')
|
|
|
|
|
2014-07-15 01:30:28 +00:00
|
|
|
try:
|
|
|
|
for prefix in ('file://', ''):
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=dest,
|
|
|
|
source=prefix + source,
|
|
|
|
template='jinja',
|
|
|
|
context={'foo': 'Hello world!'}
|
2014-07-14 22:40:03 +00:00
|
|
|
)
|
2014-07-15 01:30:28 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
os.remove(source)
|
|
|
|
os.remove(dest)
|
2014-07-14 22:40:03 +00:00
|
|
|
|
|
|
|
def test_template_local_file_noclobber(self):
|
|
|
|
'''
|
|
|
|
Test the case where a source file is in the minion's local filesystem,
|
|
|
|
and the source path is the same as the destination path.
|
|
|
|
'''
|
|
|
|
source = tempfile.mkstemp()[-1]
|
2014-07-15 01:30:28 +00:00
|
|
|
with salt.utils.fopen(source, 'w') as fp_:
|
2014-07-14 22:40:03 +00:00
|
|
|
fp_.write('{{ foo }}\n')
|
|
|
|
|
2014-07-15 01:30:28 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=source,
|
|
|
|
source=source,
|
|
|
|
template='jinja',
|
|
|
|
context={'foo': 'Hello world!'}
|
|
|
|
)
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertEqual(
|
|
|
|
ret[next(iter(ret))]['comment'],
|
|
|
|
('Unable to manage file: Source file cannot be the same as '
|
|
|
|
'destination')
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
os.remove(source)
|
2014-07-14 22:40:03 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(FileTest)
|