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
|
|
|
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
# Import Python libs
|
2017-12-15 18:14:18 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2016-08-23 16:27:03 +00:00
|
|
|
import errno
|
2017-03-01 16:57:06 +00:00
|
|
|
import logging
|
2014-07-14 22:40:03 +00:00
|
|
|
import os
|
2016-07-07 16:16:35 +00:00
|
|
|
import re
|
2015-11-19 21:42:15 +00:00
|
|
|
import sys
|
2014-07-14 22:40:03 +00:00
|
|
|
import shutil
|
|
|
|
import stat
|
2015-06-30 20:26:59 +00:00
|
|
|
import textwrap
|
2014-12-17 10:46:21 +00:00
|
|
|
import filecmp
|
2012-11-06 12:44:53 +00:00
|
|
|
|
2017-03-01 16:57:06 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-06-27 12:38:01 +00:00
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf
|
2018-08-03 21:46:01 +00:00
|
|
|
from tests.support.paths import BASE_FILES, FILES, TMP, TMP_STATE_TREE
|
2017-04-21 20:35:51 +00:00
|
|
|
from tests.support.helpers import (
|
2018-06-29 14:39:02 +00:00
|
|
|
destructiveTest,
|
2017-04-21 20:35:51 +00:00
|
|
|
skip_if_not_root,
|
|
|
|
with_system_user_and_group,
|
2018-04-14 02:41:02 +00:00
|
|
|
with_tempdir,
|
2018-03-07 21:45:38 +00:00
|
|
|
with_tempfile,
|
2017-04-21 20:35:51 +00:00
|
|
|
Webserver,
|
2018-08-22 19:21:29 +00:00
|
|
|
destructiveTest,
|
|
|
|
dedent,
|
2017-04-21 20:35:51 +00:00
|
|
|
)
|
2017-04-02 16:09:47 +00:00
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
2013-06-27 12:38:01 +00:00
|
|
|
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
# Import Salt libs
|
2018-03-15 21:31:29 +00:00
|
|
|
import salt.utils.data
|
2017-07-18 16:31:01 +00:00
|
|
|
import salt.utils.files
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
import salt.utils.path
|
|
|
|
import salt.utils.platform
|
2018-03-15 21:31:29 +00:00
|
|
|
import salt.utils.stringutils
|
2012-05-13 04:31:58 +00:00
|
|
|
|
2016-08-02 20:33:21 +00:00
|
|
|
HAS_PWD = True
|
|
|
|
try:
|
|
|
|
import pwd
|
|
|
|
except ImportError:
|
|
|
|
HAS_PWD = False
|
|
|
|
|
|
|
|
HAS_GRP = True
|
|
|
|
try:
|
|
|
|
import grp
|
|
|
|
except ImportError:
|
|
|
|
HAS_GRP = False
|
|
|
|
|
2014-11-21 19:51:56 +00:00
|
|
|
# Import 3rd-party libs
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
from salt.ext import six
|
2017-02-10 18:53:23 +00:00
|
|
|
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
|
2014-11-21 19:51:56 +00:00
|
|
|
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
IS_WINDOWS = salt.utils.platform.is_windows()
|
2016-05-05 14:05:55 +00:00
|
|
|
|
2018-04-13 03:16:56 +00:00
|
|
|
BINARY_FILE = b'GIF89a\x01\x00\x01\x00\x80\x00\x00\x05\x04\x04\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'
|
|
|
|
|
2016-08-02 23:10:40 +00:00
|
|
|
if IS_WINDOWS:
|
2016-08-02 21:57:39 +00:00
|
|
|
FILEPILLAR = 'C:\\Windows\\Temp\\filepillar-python'
|
|
|
|
FILEPILLARDEF = 'C:\\Windows\\Temp\\filepillar-defaultvalue'
|
|
|
|
FILEPILLARGIT = 'C:\\Windows\\Temp\\filepillar-bar'
|
|
|
|
else:
|
|
|
|
FILEPILLAR = '/tmp/filepillar-python'
|
|
|
|
FILEPILLARDEF = '/tmp/filepillar-defaultvalue'
|
|
|
|
FILEPILLARGIT = '/tmp/filepillar-bar'
|
2015-05-02 20:16:56 +00:00
|
|
|
|
|
|
|
|
2016-07-21 03:26:10 +00:00
|
|
|
def _test_managed_file_mode_keep_helper(testcase, local=False):
|
|
|
|
'''
|
|
|
|
DRY helper function to run the same test with a local or remote path
|
|
|
|
'''
|
2017-09-13 23:10:15 +00:00
|
|
|
name = os.path.join(TMP, 'scene33')
|
2018-08-03 21:46:01 +00:00
|
|
|
grail_fs_path = os.path.join(BASE_FILES, 'grail', 'scene33')
|
2017-09-13 23:10:15 +00:00
|
|
|
grail = 'salt://grail/scene33' if not local else grail_fs_path
|
2016-07-21 03:26:10 +00:00
|
|
|
|
|
|
|
# Get the current mode so that we can put the file back the way we
|
|
|
|
# found it when we're done.
|
2017-11-13 23:03:17 +00:00
|
|
|
grail_fs_mode = int(testcase.run_function('file.get_mode', [grail_fs_path]), 8)
|
2017-12-02 05:34:12 +00:00
|
|
|
initial_mode = 0o770
|
|
|
|
new_mode_1 = 0o600
|
|
|
|
new_mode_2 = 0o644
|
2016-07-21 03:26:10 +00:00
|
|
|
|
|
|
|
# Set the initial mode, so we can be assured that when we set the mode
|
|
|
|
# to "keep", we're actually changing the permissions of the file to the
|
|
|
|
# new mode.
|
|
|
|
ret = testcase.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
mode=oct(initial_mode),
|
|
|
|
source=grail,
|
|
|
|
)
|
2016-08-29 22:25:29 +00:00
|
|
|
|
2016-08-29 22:12:06 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
testcase.assertSaltFalseReturn(ret)
|
|
|
|
return
|
2016-08-29 22:25:29 +00:00
|
|
|
|
|
|
|
testcase.assertSaltTrueReturn(ret)
|
2016-08-29 22:12:06 +00:00
|
|
|
|
2016-07-21 03:26:10 +00:00
|
|
|
try:
|
|
|
|
# Update the mode on the fileserver (pass 1)
|
|
|
|
os.chmod(grail_fs_path, new_mode_1)
|
|
|
|
ret = testcase.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
mode='keep',
|
|
|
|
source=grail,
|
|
|
|
)
|
|
|
|
testcase.assertSaltTrueReturn(ret)
|
|
|
|
managed_mode = stat.S_IMODE(os.stat(name).st_mode)
|
|
|
|
testcase.assertEqual(oct(managed_mode), oct(new_mode_1))
|
|
|
|
# Update the mode on the fileserver (pass 2)
|
|
|
|
# This assures us that if the file in file_roots was originally set
|
|
|
|
# to the same mode as new_mode_1, we definitely get an updated mode
|
|
|
|
# this time.
|
|
|
|
os.chmod(grail_fs_path, new_mode_2)
|
|
|
|
ret = testcase.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
mode='keep',
|
|
|
|
source=grail,
|
|
|
|
)
|
|
|
|
testcase.assertSaltTrueReturn(ret)
|
|
|
|
managed_mode = stat.S_IMODE(os.stat(name).st_mode)
|
|
|
|
testcase.assertEqual(oct(managed_mode), oct(new_mode_2))
|
|
|
|
except Exception:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
# Set the mode of the file in the file_roots back to what it
|
|
|
|
# originally was.
|
|
|
|
os.chmod(grail_fs_path, grail_fs_mode)
|
|
|
|
|
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class FileTest(ModuleCase, SaltReturnAssertsMixin):
|
2012-05-13 04:31:58 +00:00
|
|
|
'''
|
|
|
|
Validate the file state
|
|
|
|
'''
|
2018-03-07 21:45:38 +00:00
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
remove files created in previous tests
|
|
|
|
'''
|
2018-06-29 14:39:02 +00:00
|
|
|
user = 'salt'
|
|
|
|
if user in str(self.run_function('user.list_users', [user])):
|
|
|
|
self.run_function('user.delete', [user])
|
|
|
|
|
2018-03-07 21:45:38 +00:00
|
|
|
for path in (FILEPILLAR, FILEPILLARDEF, FILEPILLARGIT):
|
|
|
|
try:
|
|
|
|
os.remove(path)
|
|
|
|
except OSError as exc:
|
2018-09-08 00:36:32 +00:00
|
|
|
if exc.errno != errno.ENOENT:
|
2018-03-07 21:45:38 +00:00
|
|
|
log.error('Failed to remove %s: %s', path, exc)
|
2012-08-05 18:23:12 +00:00
|
|
|
|
2012-05-13 04:31:58 +00:00
|
|
|
def test_symlink(self):
|
|
|
|
'''
|
|
|
|
file.symlink
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'symlink')
|
|
|
|
tgt = os.path.join(TMP, 'target')
|
2016-08-10 22:40:23 +00:00
|
|
|
|
|
|
|
# Windows must have a source directory to link to
|
2017-03-02 19:54:10 +00:00
|
|
|
if IS_WINDOWS and not os.path.isdir(tgt):
|
2016-08-10 22:40:23 +00:00
|
|
|
os.mkdir(tgt)
|
|
|
|
|
2016-08-17 19:40:31 +00:00
|
|
|
# Windows cannot create a symlink if it already exists
|
2017-03-02 19:54:10 +00:00
|
|
|
if IS_WINDOWS and self.run_function('file.is_link', [name]):
|
2016-08-17 19:40:31 +00:00
|
|
|
self.run_function('file.remove', [name])
|
|
|
|
|
2012-05-13 04:31:58 +00:00
|
|
|
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
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'symlink2')
|
|
|
|
tgt = os.path.join(TMP, 'target')
|
2012-05-13 04:41:47 +00:00
|
|
|
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
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'file_to_kill')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.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
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(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
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'link_to_kill')
|
2016-08-10 23:21:24 +00:00
|
|
|
tgt = '{0}.tgt'.format(name)
|
|
|
|
|
|
|
|
# Windows must have a source directory to link to
|
2017-03-02 19:54:10 +00:00
|
|
|
if IS_WINDOWS and not os.path.isdir(tgt):
|
2016-08-10 23:21:24 +00:00
|
|
|
os.mkdir(tgt)
|
|
|
|
|
|
|
|
if not self.run_function('file.is_link', [name]):
|
|
|
|
self.run_function('file.symlink', [tgt, name])
|
|
|
|
|
2012-05-13 04:55:28 +00:00
|
|
|
ret = self.run_state('file.absent', name=name)
|
2016-08-10 23:21:24 +00:00
|
|
|
|
2012-12-07 16:25:26 +00:00
|
|
|
try:
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2016-08-10 23:21:24 +00:00
|
|
|
self.assertFalse(self.run_function('file.is_link', [name]))
|
2012-12-07 16:25:26 +00:00
|
|
|
finally:
|
2016-08-10 23:21:24 +00:00
|
|
|
if self.run_function('file.is_link', [name]):
|
|
|
|
self.run_function('file.remove', [name])
|
2012-05-13 04:58:26 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_test_absent(self, name):
|
2012-05-13 04:58:26 +00:00
|
|
|
'''
|
|
|
|
file.absent test interface
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.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)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltNoneReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(name))
|
2012-05-13 05:19:39 +00:00
|
|
|
|
|
|
|
def test_managed(self):
|
|
|
|
'''
|
|
|
|
file.managed
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'grail_scene33')
|
2012-05-13 05:19:39 +00:00
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.managed', name=name, source='salt://grail/scene33'
|
|
|
|
)
|
2018-08-03 21:46:01 +00:00
|
|
|
src = os.path.join(BASE_FILES, 'grail', 'scene33')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(src, 'r') as fp_:
|
2012-05-13 05:19:39 +00:00
|
|
|
master_data = fp_.read()
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.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
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'grail_scene33')
|
2014-03-17 20:25:17 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, mode='0770', source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
|
2016-08-29 21:59:17 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
expected = 'The \'mode\' option is not supported on Windows'
|
2018-08-17 13:31:40 +00:00
|
|
|
self.assertEqual(ret[list(ret)[0]]['comment'], expected)
|
2016-08-29 21:59:17 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
2016-08-29 22:25:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(name).st_mode
|
|
|
|
)
|
|
|
|
self.assertEqual(oct(desired_mode), oct(resulting_mode))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-03-17 20:25:17 +00:00
|
|
|
|
2016-07-21 03:08:16 +00:00
|
|
|
def test_managed_file_mode_keep(self):
|
|
|
|
'''
|
|
|
|
Test using "mode: keep" in a file.managed state
|
|
|
|
'''
|
2016-07-21 03:26:10 +00:00
|
|
|
_test_managed_file_mode_keep_helper(self, local=False)
|
2016-07-21 03:08:16 +00:00
|
|
|
|
2016-07-21 03:26:10 +00:00
|
|
|
def test_managed_file_mode_keep_local_source(self):
|
|
|
|
'''
|
|
|
|
Test using "mode: keep" in a file.managed state, with a local file path
|
|
|
|
as the source.
|
|
|
|
'''
|
|
|
|
_test_managed_file_mode_keep_helper(self, local=True)
|
2016-07-21 03:08:16 +00:00
|
|
|
|
2014-03-17 20:25:17 +00:00
|
|
|
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
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'grail_scene33')
|
2014-03-17 20:25:17 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, mode=oct(initial_mode), source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
|
2016-08-29 22:12:06 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
expected = 'The \'mode\' option is not supported on Windows'
|
2018-08-17 13:31:40 +00:00
|
|
|
self.assertEqual(ret[list(ret)[0]]['comment'], expected)
|
2016-08-29 22:12:06 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
2016-08-29 22:25:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
resulting_mode = stat.S_IMODE(
|
|
|
|
os.stat(name).st_mode
|
|
|
|
)
|
|
|
|
self.assertEqual(oct(initial_mode), oct(resulting_mode))
|
|
|
|
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'grail_scene33')
|
2016-08-29 22:25:29 +00:00
|
|
|
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)
|
2014-03-17 20:25:17 +00:00
|
|
|
|
|
|
|
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
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'grail_scene33')
|
2014-03-17 20:25:17 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, replace=True, mode=oct(initial_mode), source='salt://grail/scene33'
|
|
|
|
)
|
|
|
|
|
2016-08-29 22:12:06 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
expected = 'The \'mode\' option is not supported on Windows'
|
2018-08-17 13:31:40 +00:00
|
|
|
self.assertEqual(ret[list(ret)[0]]['comment'], expected)
|
2016-08-29 22:12:06 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
2016-08-29 22:25:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
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-03-17 20:25:17 +00:00
|
|
|
|
2016-07-07 16:16:35 +00:00
|
|
|
def test_managed_file_with_grains_data(self):
|
|
|
|
'''
|
|
|
|
Test to ensure we can render grains data into a managed
|
|
|
|
file.
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
grain_path = os.path.join(TMP, 'file-grain-test')
|
2016-08-03 16:28:29 +00:00
|
|
|
state_file = 'file-grainget'
|
|
|
|
|
2018-10-24 05:55:12 +00:00
|
|
|
self.run_function('state.sls', [state_file], pillar={'grain_path': grain_path})
|
2016-08-02 21:57:39 +00:00
|
|
|
self.assertTrue(os.path.exists(grain_path))
|
2016-08-29 21:22:17 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(grain_path, 'r') as fp_:
|
2016-08-29 21:29:47 +00:00
|
|
|
file_contents = fp_.readlines()
|
2016-08-29 21:22:17 +00:00
|
|
|
|
2018-08-23 15:52:49 +00:00
|
|
|
if IS_WINDOWS:
|
2018-08-13 19:23:41 +00:00
|
|
|
match = '^minion\r\n'
|
|
|
|
else:
|
|
|
|
match = '^minion\n'
|
|
|
|
self.assertTrue(re.match(match, file_contents[0]))
|
2016-07-07 16:16:35 +00:00
|
|
|
|
2016-05-05 14:05:55 +00:00
|
|
|
def test_managed_file_with_pillar_sls(self):
|
|
|
|
'''
|
|
|
|
Test to ensure pillar data in sls file
|
|
|
|
is rendered properly and file is created.
|
|
|
|
'''
|
2016-08-03 16:28:29 +00:00
|
|
|
state_name = 'file-pillarget'
|
2016-08-02 23:17:11 +00:00
|
|
|
|
2016-05-05 14:05:55 +00:00
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2017-03-03 00:29:43 +00:00
|
|
|
# Check to make sure the file was created
|
2016-05-05 14:05:55 +00:00
|
|
|
check_file = self.run_function('file.file_exists', [FILEPILLAR])
|
|
|
|
self.assertTrue(check_file)
|
|
|
|
|
|
|
|
def test_managed_file_with_pillardefault_sls(self):
|
|
|
|
'''
|
|
|
|
Test to ensure when pillar data is not available
|
|
|
|
in sls file with pillar.get it uses the default
|
|
|
|
value.
|
|
|
|
'''
|
2016-08-03 16:28:29 +00:00
|
|
|
state_name = 'file-pillardefaultget'
|
2016-08-02 23:17:11 +00:00
|
|
|
|
2016-05-05 14:05:55 +00:00
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2017-03-03 00:29:43 +00:00
|
|
|
# Check to make sure the file was created
|
2016-05-05 14:05:55 +00:00
|
|
|
check_file = self.run_function('file.file_exists', [FILEPILLARDEF])
|
|
|
|
self.assertTrue(check_file)
|
|
|
|
|
2017-03-02 19:41:11 +00:00
|
|
|
@skip_if_not_root
|
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
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(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
|
|
|
|
)
|
2016-08-29 22:25:29 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
expected = 'The \'mode\' option is not supported on Windows'
|
2018-08-17 13:31:40 +00:00
|
|
|
self.assertEqual(ret[list(ret)[0]]['comment'], expected)
|
2016-08-29 22:25:29 +00:00
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
return
|
|
|
|
|
2013-11-12 06:29:55 +00:00
|
|
|
resulting_mode = stat.S_IMODE(
|
2017-03-06 18:12:22 +00:00
|
|
|
os.stat(os.path.join(TMP, 'a')).st_mode
|
2013-11-12 06:29:55 +00:00
|
|
|
)
|
2017-03-06 18:12:22 +00:00
|
|
|
resulting_owner = pwd.getpwuid(os.stat(os.path.join(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
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(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
|
|
|
|
2016-02-08 01:24:38 +00:00
|
|
|
def test_managed_show_changes_false(self):
|
2013-03-13 11:07:56 +00:00
|
|
|
'''
|
|
|
|
file.managed test interface
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(TMP, 'grail_not_scene33')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(name, 'wb') as fp_:
|
2018-01-22 16:56:19 +00:00
|
|
|
fp_.write(b'test_managed_show_changes_false\n')
|
2013-03-13 11:07:56 +00:00
|
|
|
|
|
|
|
ret = self.run_state(
|
2013-04-25 15:36:19 +00:00
|
|
|
'file.managed', name=name, source='salt://grail/scene33',
|
2016-02-08 01:24:38 +00:00
|
|
|
show_changes=False
|
2013-03-13 11:07:56 +00:00
|
|
|
)
|
|
|
|
|
2014-11-21 19:51:56 +00:00
|
|
|
changes = next(six.itervalues(ret))['changes']
|
2016-02-08 01:24:38 +00:00
|
|
|
self.assertEqual('<show_changes=False>', changes['diff'])
|
2013-03-13 11:07:56 +00:00
|
|
|
|
2016-08-11 19:35:17 +00:00
|
|
|
@skipIf(IS_WINDOWS, 'Don\'t know how to fix for Windows')
|
2015-05-02 20:16:56 +00:00
|
|
|
def test_managed_escaped_file_path(self):
|
|
|
|
'''
|
|
|
|
file.managed test that 'salt://|' protects unusual characters in file path
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
funny_file = salt.utils.files.mkstemp(prefix='?f!le? n@=3&', suffix='.file type')
|
2015-05-02 20:16:56 +00:00
|
|
|
funny_file_name = os.path.split(funny_file)[1]
|
|
|
|
funny_url = 'salt://|' + funny_file_name
|
2018-08-03 21:46:01 +00:00
|
|
|
funny_url_path = os.path.join(BASE_FILES, funny_file_name)
|
2015-05-02 20:16:56 +00:00
|
|
|
|
|
|
|
state_name = 'funny_file'
|
|
|
|
state_file_name = state_name + '.sls'
|
2018-08-03 21:46:01 +00:00
|
|
|
state_file = os.path.join(BASE_FILES, state_file_name)
|
2015-05-02 20:16:56 +00:00
|
|
|
state_key = 'file_|-{0}_|-{0}_|-managed'.format(funny_file)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
self.addCleanup(os.remove, state_file)
|
|
|
|
self.addCleanup(os.remove, funny_file)
|
|
|
|
self.addCleanup(os.remove, funny_url_path)
|
2015-05-02 20:16:56 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(funny_url_path, 'w'):
|
|
|
|
pass
|
|
|
|
with salt.utils.files.fopen(state_file, 'w') as fp_:
|
|
|
|
fp_.write(textwrap.dedent('''\
|
|
|
|
{0}:
|
|
|
|
file.managed:
|
|
|
|
- source: {1}
|
|
|
|
- makedirs: True
|
|
|
|
'''.format(funny_file, funny_url)))
|
2015-05-02 20:16:56 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
self.assertTrue(ret[state_key]['result'])
|
2015-05-02 20:16:56 +00:00
|
|
|
|
2015-06-30 20:26:59 +00:00
|
|
|
def test_managed_contents(self):
|
|
|
|
'''
|
|
|
|
test file.managed with contents that is a boolean, string, integer,
|
|
|
|
float, list, and dictionary
|
|
|
|
'''
|
|
|
|
state_name = 'file-FileTest-test_managed_contents'
|
|
|
|
state_filename = state_name + '.sls'
|
2018-08-03 21:46:01 +00:00
|
|
|
state_file = os.path.join(BASE_FILES, state_filename)
|
2015-06-30 20:26:59 +00:00
|
|
|
|
|
|
|
managed_files = {}
|
|
|
|
state_keys = {}
|
|
|
|
for typ in ('bool', 'str', 'int', 'float', 'list', 'dict'):
|
2018-11-29 15:29:23 +00:00
|
|
|
managed_files[typ] = salt.utils.files.mkstemp()
|
2016-08-10 23:21:24 +00:00
|
|
|
state_keys[typ] = 'file_|-{0} file_|-{1}_|-managed'.format(typ, managed_files[typ])
|
|
|
|
try:
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(state_file, 'w') as fd_:
|
2016-08-10 23:21:24 +00:00
|
|
|
fd_.write(textwrap.dedent('''\
|
|
|
|
bool file:
|
|
|
|
file.managed:
|
|
|
|
- name: {bool}
|
|
|
|
- contents: True
|
|
|
|
|
|
|
|
str file:
|
|
|
|
file.managed:
|
|
|
|
- name: {str}
|
|
|
|
- contents: Salt was here.
|
|
|
|
|
|
|
|
int file:
|
|
|
|
file.managed:
|
|
|
|
- name: {int}
|
|
|
|
- contents: 340282366920938463463374607431768211456
|
|
|
|
|
|
|
|
float file:
|
|
|
|
file.managed:
|
|
|
|
- name: {float}
|
|
|
|
- contents: 1.7518e-45 # gravitational coupling constant
|
|
|
|
|
|
|
|
list file:
|
|
|
|
file.managed:
|
|
|
|
- name: {list}
|
|
|
|
- contents: [1, 1, 2, 3, 5, 8, 13]
|
|
|
|
|
|
|
|
dict file:
|
|
|
|
file.managed:
|
|
|
|
- name: {dict}
|
|
|
|
- contents:
|
|
|
|
C: charge
|
|
|
|
P: parity
|
|
|
|
T: time
|
|
|
|
'''.format(**managed_files)))
|
2015-06-30 20:26:59 +00:00
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
for typ in state_keys:
|
|
|
|
self.assertTrue(ret[state_keys[typ]]['result'])
|
|
|
|
self.assertIn('diff', ret[state_keys[typ]]['changes'])
|
|
|
|
finally:
|
|
|
|
os.remove(state_file)
|
|
|
|
for typ in managed_files:
|
|
|
|
os.remove(managed_files[typ])
|
|
|
|
|
2017-03-02 19:54:10 +00:00
|
|
|
@skip_if_not_root
|
|
|
|
@skipIf(IS_WINDOWS, 'Windows does not support "mode" kwarg. Skipping.')
|
2018-04-14 02:41:02 +00:00
|
|
|
@skipIf(not salt.utils.path.which('visudo'), 'sudo is missing')
|
2017-01-27 18:28:54 +00:00
|
|
|
def test_managed_check_cmd(self):
|
|
|
|
'''
|
|
|
|
Test file.managed passing a basic check_cmd kwarg. See Issue #38111.
|
|
|
|
'''
|
2017-06-05 21:16:54 +00:00
|
|
|
r_group = 'root'
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
if salt.utils.platform.is_darwin():
|
2017-06-05 21:16:54 +00:00
|
|
|
r_group = 'wheel'
|
2017-03-02 19:33:14 +00:00
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name='/tmp/sudoers',
|
|
|
|
user='root',
|
2017-06-05 21:16:54 +00:00
|
|
|
group=r_group,
|
2017-03-02 19:33:14 +00:00
|
|
|
mode=440,
|
|
|
|
check_cmd='visudo -c -s -f'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertInSaltComment('Empty file', ret)
|
|
|
|
self.assertEqual(ret['file_|-/tmp/sudoers_|-/tmp/sudoers_|-managed']['changes'],
|
|
|
|
{'new': 'file /tmp/sudoers created', 'mode': '0440'})
|
|
|
|
finally:
|
|
|
|
# Clean Up File
|
|
|
|
if os.path.exists('/tmp/sudoers'):
|
|
|
|
os.remove('/tmp/sudoers')
|
2017-01-27 18:28:54 +00:00
|
|
|
|
2017-12-02 05:34:12 +00:00
|
|
|
def test_managed_local_source_with_source_hash(self):
|
|
|
|
'''
|
|
|
|
Make sure that we enforce the source_hash even with local files
|
|
|
|
'''
|
2017-12-07 22:33:03 +00:00
|
|
|
name = os.path.join(TMP, 'local_source_with_source_hash')
|
2018-08-03 21:46:01 +00:00
|
|
|
local_path = os.path.join(BASE_FILES, 'grail', 'scene33')
|
2017-12-02 05:34:12 +00:00
|
|
|
actual_hash = '567fd840bf1548edc35c48eb66cdd78bfdfcccff'
|
2018-08-23 15:52:49 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
# CRLF vs LF causes a different hash on windows
|
2018-08-13 19:23:41 +00:00
|
|
|
actual_hash = 'f658a0ec121d9c17088795afcc6ff3c43cb9842a'
|
2017-12-02 05:34:12 +00:00
|
|
|
# Reverse the actual hash
|
|
|
|
bad_hash = actual_hash[::-1]
|
|
|
|
|
|
|
|
def remove_file():
|
|
|
|
try:
|
|
|
|
os.remove(name)
|
|
|
|
except OSError as exc:
|
|
|
|
if exc.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def do_test(clean=False):
|
|
|
|
for proto in ('file://', ''):
|
|
|
|
source = proto + local_path
|
|
|
|
log.debug('Trying source %s', source)
|
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=source,
|
|
|
|
source_hash='sha1={0}'.format(bad_hash))
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
ret = ret[next(iter(ret))]
|
|
|
|
# Shouldn't be any changes
|
|
|
|
self.assertFalse(ret['changes'])
|
|
|
|
# Check that we identified a hash mismatch
|
|
|
|
self.assertIn(
|
|
|
|
'does not match actual checksum', ret['comment'])
|
|
|
|
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=source,
|
|
|
|
source_hash='sha1={0}'.format(actual_hash))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
if clean:
|
|
|
|
remove_file()
|
|
|
|
|
|
|
|
remove_file()
|
|
|
|
log.debug('Trying with nonexistant destination file')
|
|
|
|
do_test()
|
|
|
|
log.debug('Trying with destination file already present')
|
2017-12-11 23:43:33 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w'):
|
2017-12-02 05:34:12 +00:00
|
|
|
pass
|
|
|
|
try:
|
|
|
|
do_test(clean=False)
|
|
|
|
finally:
|
|
|
|
remove_file()
|
|
|
|
|
|
|
|
def test_managed_local_source_does_not_exist(self):
|
|
|
|
'''
|
|
|
|
Make sure that we exit gracefully when a local source doesn't exist
|
|
|
|
'''
|
2017-12-07 22:33:03 +00:00
|
|
|
name = os.path.join(TMP, 'local_source_does_not_exist')
|
2018-08-03 21:46:01 +00:00
|
|
|
local_path = os.path.join(BASE_FILES, 'grail', 'scene99')
|
2017-12-02 05:34:12 +00:00
|
|
|
|
|
|
|
for proto in ('file://', ''):
|
|
|
|
source = proto + local_path
|
|
|
|
log.debug('Trying source %s', source)
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=source)
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
ret = ret[next(iter(ret))]
|
|
|
|
# Shouldn't be any changes
|
|
|
|
self.assertFalse(ret['changes'])
|
|
|
|
# Check that we identified a hash mismatch
|
|
|
|
self.assertIn(
|
|
|
|
'does not exist', ret['comment'])
|
|
|
|
|
2018-07-02 20:56:49 +00:00
|
|
|
def test_managed_source_hash_indifferent_case(self):
|
|
|
|
'''
|
|
|
|
Test passing a source_hash as an uppercase hash.
|
|
|
|
|
|
|
|
This is a regression test for Issue #38914 and Issue #48230 (test=true use).
|
|
|
|
'''
|
|
|
|
name = os.path.join(TMP, 'source_hash_indifferent_case')
|
2018-07-23 19:02:27 +00:00
|
|
|
state_name = 'file_|-{0}_|' \
|
|
|
|
'-{0}_|-managed'.format(name)
|
2018-08-03 21:46:01 +00:00
|
|
|
local_path = os.path.join(BASE_FILES, 'hello_world.txt')
|
2018-07-02 20:56:49 +00:00
|
|
|
actual_hash = 'c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31'
|
2018-08-23 15:52:49 +00:00
|
|
|
if IS_WINDOWS:
|
2018-08-13 19:23:41 +00:00
|
|
|
# CRLF vs LF causes a differnt hash on windows
|
|
|
|
actual_hash = '92b772380a3f8e27a93e57e6deeca6c01da07f5aadce78bb2fbb20de10a66925'
|
2018-07-02 20:56:49 +00:00
|
|
|
uppercase_hash = actual_hash.upper()
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Lay down tmp file to test against
|
|
|
|
self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=local_path,
|
|
|
|
source_hash=actual_hash
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test uppercase source_hash: should return True with no changes
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=local_path,
|
|
|
|
source_hash=uppercase_hash
|
|
|
|
)
|
|
|
|
assert ret[state_name]['result'] is True
|
|
|
|
assert ret[state_name]['pchanges'] == {}
|
|
|
|
assert ret[state_name]['changes'] == {}
|
|
|
|
|
|
|
|
# Test uppercase source_hash using test=true
|
|
|
|
# Should return True with no changes
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=local_path,
|
|
|
|
source_hash=uppercase_hash,
|
|
|
|
test=True
|
|
|
|
)
|
|
|
|
assert ret[state_name]['result'] is True
|
|
|
|
assert ret[state_name]['pchanges'] == {}
|
|
|
|
assert ret[state_name]['changes'] == {}
|
|
|
|
|
|
|
|
finally:
|
|
|
|
# Clean Up File
|
|
|
|
if os.path.exists(name):
|
|
|
|
os.remove(name)
|
|
|
|
|
2018-08-03 22:30:17 +00:00
|
|
|
@with_tempfile(create=False)
|
|
|
|
def test_managed_latin1_diff(self, name):
|
|
|
|
'''
|
|
|
|
Tests that latin-1 file contents are represented properly in the diff
|
|
|
|
'''
|
|
|
|
# Lay down the initial file
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source='salt://issue-48777/old.html')
|
|
|
|
ret = ret[next(iter(ret))]
|
|
|
|
assert ret['result'] is True, ret
|
|
|
|
|
|
|
|
# Replace it with the new file and check the diff
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source='salt://issue-48777/new.html')
|
|
|
|
ret = ret[next(iter(ret))]
|
|
|
|
assert ret['result'] is True, ret
|
2018-08-21 21:04:59 +00:00
|
|
|
diff_lines = ret['changes']['diff'].split(os.linesep)
|
2018-08-03 22:30:17 +00:00
|
|
|
assert '+räksmörgås' in diff_lines, diff_lines
|
|
|
|
|
2018-08-23 20:37:03 +00:00
|
|
|
@with_tempfile()
|
2018-08-16 19:53:32 +00:00
|
|
|
def test_managed_keep_source_false_salt(self, name):
|
|
|
|
'''
|
|
|
|
This test ensures that we properly clean the cached file if keep_source
|
|
|
|
is set to False, for source files using a salt:// URL
|
|
|
|
'''
|
|
|
|
source = 'salt://grail/scene33'
|
|
|
|
saltenv = 'base'
|
|
|
|
|
|
|
|
# Run the state
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
source=source,
|
|
|
|
saltenv=saltenv,
|
|
|
|
keep_source=False)
|
|
|
|
ret = ret[next(iter(ret))]
|
|
|
|
assert ret['result'] is True
|
|
|
|
|
|
|
|
# Now make sure that the file is not cached
|
|
|
|
result = self.run_function('cp.is_cached', [source, saltenv])
|
|
|
|
assert result == '', 'File is still cached at {0}'.format(result)
|
|
|
|
|
2012-05-13 05:38:24 +00:00
|
|
|
def test_directory(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(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))
|
|
|
|
|
2017-01-12 15:32:26 +00:00
|
|
|
def test_directory_symlink_dry_run(self):
|
|
|
|
'''
|
|
|
|
Ensure that symlinks are followed when file.directory is run with
|
|
|
|
test=True
|
|
|
|
'''
|
|
|
|
try:
|
2017-03-06 18:12:22 +00:00
|
|
|
tmp_dir = os.path.join(TMP, 'pgdata')
|
|
|
|
sym_dir = os.path.join(TMP, 'pg_data')
|
2017-01-12 15:32:26 +00:00
|
|
|
|
2017-09-13 23:10:15 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
self.run_function('file.mkdir', [tmp_dir, 'Administrators'])
|
|
|
|
else:
|
|
|
|
os.mkdir(tmp_dir, 0o700)
|
|
|
|
|
2017-09-13 20:50:30 +00:00
|
|
|
self.run_function('file.symlink', [tmp_dir, sym_dir])
|
2017-01-12 15:32:26 +00:00
|
|
|
|
2017-09-13 23:10:15 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', test=True, name=sym_dir,
|
|
|
|
follow_symlinks=True, win_owner='Administrators')
|
|
|
|
else:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', test=True, name=sym_dir,
|
|
|
|
follow_symlinks=True, mode=700)
|
2017-01-12 15:32:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(tmp_dir):
|
2017-09-13 20:50:30 +00:00
|
|
|
self.run_function('file.remove', [tmp_dir])
|
2017-01-12 15:32:26 +00:00
|
|
|
if os.path.islink(sym_dir):
|
2017-09-13 20:50:30 +00:00
|
|
|
self.run_function('file.remove', [sym_dir])
|
2017-01-12 15:32:26 +00:00
|
|
|
|
2017-03-03 00:29:43 +00:00
|
|
|
@skip_if_not_root
|
2016-08-02 23:10:40 +00:00
|
|
|
@skipIf(IS_WINDOWS, 'Mode not available in Windows')
|
2016-04-22 16:11:58 +00:00
|
|
|
def test_directory_max_depth(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
Test the max_depth option by iteratively increasing the depth and
|
|
|
|
checking that no changes deeper than max_depth have been attempted
|
|
|
|
'''
|
|
|
|
|
|
|
|
def _get_oct_mode(name):
|
|
|
|
'''
|
|
|
|
Return a string octal representation of the permissions for name
|
|
|
|
'''
|
2017-10-12 18:17:24 +00:00
|
|
|
return salt.utils.files.normalize_mode(oct(os.stat(name).st_mode & 0o777))
|
2016-04-22 16:11:58 +00:00
|
|
|
|
2017-03-06 18:12:22 +00:00
|
|
|
top = os.path.join(TMP, 'top_dir')
|
2016-04-22 16:11:58 +00:00
|
|
|
sub = os.path.join(top, 'sub_dir')
|
|
|
|
subsub = os.path.join(sub, 'sub_sub_dir')
|
|
|
|
dirs = [top, sub, subsub]
|
|
|
|
|
|
|
|
initial_mode = '0111'
|
|
|
|
changed_mode = '0555'
|
2018-09-12 20:25:06 +00:00
|
|
|
|
|
|
|
initial_modes = {0: {sub: '0755',
|
|
|
|
subsub: '0111'},
|
|
|
|
1: {sub: '0111',
|
|
|
|
subsub: '0111'},
|
|
|
|
2: {sub: '0111',
|
|
|
|
subsub: '0111'}}
|
2016-04-22 16:11:58 +00:00
|
|
|
|
|
|
|
if not os.path.isdir(subsub):
|
|
|
|
os.makedirs(subsub, int(initial_mode, 8))
|
|
|
|
|
|
|
|
try:
|
|
|
|
for depth in range(0, 3):
|
|
|
|
ret = self.run_state('file.directory',
|
|
|
|
name=top,
|
|
|
|
max_depth=depth,
|
|
|
|
dir_mode=changed_mode,
|
|
|
|
recurse=['mode'])
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
for changed_dir in dirs[0:depth+1]:
|
|
|
|
self.assertEqual(changed_mode,
|
|
|
|
_get_oct_mode(changed_dir))
|
|
|
|
for untouched_dir in dirs[depth+1:]:
|
2018-09-08 00:36:32 +00:00
|
|
|
# Beginning in Python 3.7, os.makedirs no longer sets
|
|
|
|
# the mode of intermediate directories to the mode that
|
|
|
|
# is passed.
|
|
|
|
if sys.version_info >= (3, 7):
|
2018-09-12 20:25:06 +00:00
|
|
|
_mode = initial_modes[depth][untouched_dir]
|
|
|
|
self.assertEqual(_mode,
|
2018-09-08 00:36:32 +00:00
|
|
|
_get_oct_mode(untouched_dir))
|
|
|
|
else:
|
|
|
|
self.assertEqual(initial_mode,
|
|
|
|
_get_oct_mode(untouched_dir))
|
2016-04-22 16:11:58 +00:00
|
|
|
finally:
|
|
|
|
shutil.rmtree(top)
|
|
|
|
|
2012-05-13 06:27:00 +00:00
|
|
|
def test_test_directory(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
name = os.path.join(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
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_directory_clean(self, base_dir):
|
2013-06-11 00:22:19 +00:00
|
|
|
'''
|
|
|
|
file.directory with clean=True
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'directory_clean_dir')
|
|
|
|
os.mkdir(name)
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
straydir = os.path.join(name, 'straydir')
|
|
|
|
if not os.path.isdir(straydir):
|
|
|
|
os.makedirs(straydir)
|
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(os.path.join(straydir, 'strayfile2'), 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
ret = self.run_state('file.directory', name=name, clean=True)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.exists(strayfile))
|
|
|
|
self.assertFalse(os.path.exists(straydir))
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_directory_clean_exclude(self, base_dir):
|
2013-06-11 00:22:19 +00:00
|
|
|
'''
|
|
|
|
file.directory with clean=True and exclude_pat set
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'directory_clean_dir')
|
2013-06-11 00:22:19 +00:00
|
|
|
if not os.path.isdir(name):
|
|
|
|
os.makedirs(name)
|
|
|
|
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
straydir = os.path.join(name, 'straydir')
|
|
|
|
if not os.path.isdir(straydir):
|
|
|
|
os.makedirs(straydir)
|
|
|
|
|
|
|
|
strayfile2 = os.path.join(straydir, 'strayfile2')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile2, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
keepfile = os.path.join(straydir, 'keepfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(keepfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2016-08-11 19:35:17 +00:00
|
|
|
exclude_pat = 'E@^straydir(|/keepfile)$'
|
2017-03-02 19:54:10 +00:00
|
|
|
if IS_WINDOWS:
|
2016-08-11 19:35:17 +00:00
|
|
|
exclude_pat = 'E@^straydir(|\\\\keepfile)$'
|
|
|
|
|
2013-06-11 00:22:19 +00:00
|
|
|
ret = self.run_state('file.directory',
|
|
|
|
name=name,
|
|
|
|
clean=True,
|
2016-08-11 19:35:17 +00:00
|
|
|
exclude_pat=exclude_pat)
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(os.path.exists(strayfile))
|
|
|
|
self.assertFalse(os.path.exists(strayfile2))
|
|
|
|
self.assertTrue(os.path.exists(keepfile))
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2018-08-23 15:52:49 +00:00
|
|
|
@skipIf(IS_WINDOWS, 'Skip on windows')
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_test_directory_clean_exclude(self, base_dir):
|
2013-06-11 00:22:19 +00:00
|
|
|
'''
|
2016-08-11 19:35:17 +00:00
|
|
|
file.directory with test=True, clean=True and exclude_pat set
|
2018-08-14 18:37:53 +00:00
|
|
|
|
|
|
|
Skipped on windows because clean and exclude_pat not supported by
|
|
|
|
salt.sates.file._check_directory_win
|
2013-06-11 00:22:19 +00:00
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'directory_clean_dir')
|
|
|
|
os.mkdir(name)
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
straydir = os.path.join(name, 'straydir')
|
|
|
|
if not os.path.isdir(straydir):
|
|
|
|
os.makedirs(straydir)
|
|
|
|
|
|
|
|
strayfile2 = os.path.join(straydir, 'strayfile2')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile2, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
|
|
|
keepfile = os.path.join(straydir, 'keepfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(keepfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2016-08-11 19:35:17 +00:00
|
|
|
exclude_pat = 'E@^straydir(|/keepfile)$'
|
2017-03-02 19:54:10 +00:00
|
|
|
if IS_WINDOWS:
|
2016-08-11 19:35:17 +00:00
|
|
|
exclude_pat = 'E@^straydir(|\\\\keepfile)$'
|
|
|
|
|
2013-06-11 00:22:19 +00:00
|
|
|
ret = self.run_state('file.directory',
|
|
|
|
test=True,
|
|
|
|
name=name,
|
|
|
|
clean=True,
|
2016-08-11 19:35:17 +00:00
|
|
|
exclude_pat=exclude_pat)
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2014-11-21 19:51:56 +00:00
|
|
|
comment = next(six.itervalues(ret))['comment']
|
2016-08-11 19:35:17 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
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)
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2018-11-29 15:29:23 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_directory_clean_require_in(self, name):
|
2015-10-07 10:06:35 +00:00
|
|
|
'''
|
|
|
|
file.directory test with clean=True and require_in file
|
|
|
|
'''
|
|
|
|
state_name = 'file-FileTest-test_directory_clean_require_in'
|
|
|
|
state_filename = state_name + '.sls'
|
2018-08-03 21:46:01 +00:00
|
|
|
state_file = os.path.join(BASE_FILES, state_filename)
|
2015-10-07 10:06:35 +00:00
|
|
|
|
2018-11-29 15:29:23 +00:00
|
|
|
wrong_file = os.path.join(name, "wrong")
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(wrong_file, "w") as fp:
|
2015-10-07 10:06:35 +00:00
|
|
|
fp.write("foo")
|
2018-11-29 15:29:23 +00:00
|
|
|
good_file = os.path.join(name, "bar")
|
2015-10-07 10:06:35 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(state_file, 'w') as fp:
|
2015-10-07 10:06:35 +00:00
|
|
|
self.addCleanup(lambda: os.remove(state_file))
|
|
|
|
fp.write(textwrap.dedent('''\
|
|
|
|
some_dir:
|
|
|
|
file.directory:
|
2018-11-29 15:29:23 +00:00
|
|
|
- name: {name}
|
2015-10-07 10:06:35 +00:00
|
|
|
- clean: true
|
|
|
|
|
|
|
|
{good_file}:
|
|
|
|
file.managed:
|
|
|
|
- require_in:
|
|
|
|
- file: some_dir
|
2018-11-29 15:29:23 +00:00
|
|
|
'''.format(name=name, good_file=good_file)))
|
2015-10-07 10:06:35 +00:00
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
self.assertTrue(os.path.exists(good_file))
|
|
|
|
self.assertFalse(os.path.exists(wrong_file))
|
|
|
|
|
2018-11-29 15:29:23 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_directory_clean_require_in_with_id(self, name):
|
2015-10-07 10:06:35 +00:00
|
|
|
'''
|
|
|
|
file.directory test with clean=True and require_in file with an ID
|
|
|
|
different from the file name
|
|
|
|
'''
|
|
|
|
state_name = 'file-FileTest-test_directory_clean_require_in_with_id'
|
|
|
|
state_filename = state_name + '.sls'
|
2018-08-03 21:46:01 +00:00
|
|
|
state_file = os.path.join(BASE_FILES, state_filename)
|
2015-10-07 10:06:35 +00:00
|
|
|
|
2018-11-29 15:29:23 +00:00
|
|
|
wrong_file = os.path.join(name, "wrong")
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(wrong_file, "w") as fp:
|
2015-10-07 10:06:35 +00:00
|
|
|
fp.write("foo")
|
2018-11-29 15:29:23 +00:00
|
|
|
good_file = os.path.join(name, "bar")
|
2015-10-07 10:06:35 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(state_file, 'w') as fp:
|
2015-10-07 10:06:35 +00:00
|
|
|
self.addCleanup(lambda: os.remove(state_file))
|
|
|
|
fp.write(textwrap.dedent('''\
|
|
|
|
some_dir:
|
|
|
|
file.directory:
|
2018-11-29 15:29:23 +00:00
|
|
|
- name: {name}
|
2015-10-07 10:06:35 +00:00
|
|
|
- clean: true
|
|
|
|
|
|
|
|
some_file:
|
|
|
|
file.managed:
|
|
|
|
- name: {good_file}
|
|
|
|
- require_in:
|
|
|
|
- file: some_dir
|
2018-11-29 15:29:23 +00:00
|
|
|
'''.format(name=name, good_file=good_file)))
|
2015-10-07 10:06:35 +00:00
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
self.assertTrue(os.path.exists(good_file))
|
|
|
|
self.assertFalse(os.path.exists(wrong_file))
|
|
|
|
|
2018-11-29 15:29:23 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_directory_clean_require_with_name(self, name):
|
2015-10-07 12:53:04 +00:00
|
|
|
'''
|
|
|
|
file.directory test with clean=True and require with a file state
|
|
|
|
relatively to the state's name, not its ID.
|
|
|
|
'''
|
|
|
|
state_name = 'file-FileTest-test_directory_clean_require_in_with_id'
|
|
|
|
state_filename = state_name + '.sls'
|
2018-08-03 21:46:01 +00:00
|
|
|
state_file = os.path.join(BASE_FILES, state_filename)
|
2015-10-07 12:53:04 +00:00
|
|
|
|
2018-11-29 15:29:23 +00:00
|
|
|
wrong_file = os.path.join(name, "wrong")
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(wrong_file, "w") as fp:
|
2015-10-07 12:53:04 +00:00
|
|
|
fp.write("foo")
|
2018-11-29 15:29:23 +00:00
|
|
|
good_file = os.path.join(name, "bar")
|
2015-10-07 12:53:04 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(state_file, 'w') as fp:
|
2015-10-07 12:53:04 +00:00
|
|
|
self.addCleanup(lambda: os.remove(state_file))
|
|
|
|
fp.write(textwrap.dedent('''\
|
|
|
|
some_dir:
|
|
|
|
file.directory:
|
2018-11-29 15:29:23 +00:00
|
|
|
- name: {name}
|
2015-10-07 12:53:04 +00:00
|
|
|
- clean: true
|
|
|
|
- require:
|
|
|
|
# This requirement refers to the name of the following
|
|
|
|
# state, not its ID.
|
|
|
|
- file: {good_file}
|
|
|
|
|
|
|
|
some_file:
|
|
|
|
file.managed:
|
|
|
|
- name: {good_file}
|
2018-11-29 15:29:23 +00:00
|
|
|
'''.format(name=name, good_file=good_file)))
|
2015-10-07 12:53:04 +00:00
|
|
|
|
|
|
|
ret = self.run_function('state.sls', [state_name])
|
|
|
|
self.assertTrue(os.path.exists(good_file))
|
|
|
|
self.assertFalse(os.path.exists(wrong_file))
|
2013-06-11 00:22:19 +00:00
|
|
|
|
2018-08-07 22:46:24 +00:00
|
|
|
def test_directory_broken_symlink(self):
|
|
|
|
'''
|
|
|
|
Ensure that file.directory works even if a directory
|
|
|
|
contains broken symbolic link
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
tmp_dir = os.path.join(TMP, 'foo')
|
|
|
|
null_file = '{0}/null'.format(tmp_dir)
|
|
|
|
broken_link = '{0}/broken'.format(tmp_dir)
|
|
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
self.run_function('file.mkdir', [tmp_dir, 'Administrators'])
|
|
|
|
else:
|
|
|
|
os.mkdir(tmp_dir, 0o700)
|
|
|
|
|
|
|
|
self.run_function('file.symlink', [null_file, broken_link])
|
|
|
|
|
|
|
|
if IS_WINDOWS:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', name=tmp_dir, recurse={'mode'},
|
|
|
|
follow_symlinks=True, win_owner='Administrators')
|
|
|
|
else:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', name=tmp_dir, recurse={'mode'},
|
|
|
|
file_mode=644, dir_mode=755)
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
if os.path.isdir(tmp_dir):
|
|
|
|
self.run_function('file.remove', [tmp_dir])
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_recurse(self, name):
|
2012-05-13 06:54:28 +00:00
|
|
|
'''
|
|
|
|
file.recurse
|
|
|
|
'''
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.recurse', name=name, source='salt://grail')
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '36', 'scene')))
|
2012-05-13 06:54:28 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_recurse_specific_env(self, dir1, dir2):
|
2013-11-02 18:31:14 +00:00
|
|
|
'''
|
|
|
|
file.recurse passing __env__
|
|
|
|
'''
|
|
|
|
ret = self.run_state('file.recurse',
|
2018-04-14 02:41:02 +00:00
|
|
|
name=dir1,
|
2013-11-02 18:31:14 +00:00
|
|
|
source='salt://holy',
|
|
|
|
__env__='prod')
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(dir1, '32', 'scene')))
|
2013-11-02 18:31:14 +00:00
|
|
|
|
2013-11-12 06:29:55 +00:00
|
|
|
ret = self.run_state('file.recurse',
|
2018-04-14 02:41:02 +00:00
|
|
|
name=dir2,
|
2013-11-12 06:29:55 +00:00
|
|
|
source='salt://holy',
|
|
|
|
saltenv='prod')
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(dir2, '32', 'scene')))
|
2013-11-12 06:29:55 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_recurse_specific_env_in_url(self, dir1, dir2):
|
2016-10-21 12:44:58 +00:00
|
|
|
'''
|
|
|
|
file.recurse passing __env__
|
|
|
|
'''
|
|
|
|
ret = self.run_state('file.recurse',
|
2018-04-14 02:41:02 +00:00
|
|
|
name=dir1,
|
2016-10-21 12:44:58 +00:00
|
|
|
source='salt://holy?saltenv=prod')
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(dir1, '32', 'scene')))
|
2016-10-21 12:44:58 +00:00
|
|
|
|
|
|
|
ret = self.run_state('file.recurse',
|
2018-04-14 02:41:02 +00:00
|
|
|
name=dir2,
|
2016-10-21 12:44:58 +00:00
|
|
|
source='salt://holy?saltenv=prod')
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(dir2, '32', 'scene')))
|
2013-11-12 06:29:55 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_test_recurse(self, name):
|
2012-05-13 07:02:05 +00:00
|
|
|
'''
|
|
|
|
file.recurse test interface
|
|
|
|
'''
|
|
|
|
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))
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_test_recurse_specific_env(self, dir1, dir2):
|
2013-11-02 18:31:14 +00:00
|
|
|
'''
|
|
|
|
file.recurse test interface
|
|
|
|
'''
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
test=True,
|
2018-04-14 02:41:02 +00:00
|
|
|
name=dir1,
|
2013-11-02 18:31:14 +00:00
|
|
|
source='salt://holy',
|
|
|
|
__env__='prod'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn(ret)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertFalse(os.path.isfile(os.path.join(dir1, '32', 'scene')))
|
|
|
|
self.assertFalse(os.path.exists(dir1))
|
2013-11-02 18:31:14 +00:00
|
|
|
|
2013-11-12 06:29:55 +00:00
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
test=True,
|
2018-04-14 02:41:02 +00:00
|
|
|
name=dir2,
|
2013-11-12 06:29:55 +00:00
|
|
|
source='salt://holy',
|
|
|
|
saltenv='prod'
|
|
|
|
)
|
|
|
|
self.assertSaltNoneReturn(ret)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertFalse(os.path.isfile(os.path.join(dir2, '32', 'scene')))
|
|
|
|
self.assertFalse(os.path.exists(dir2))
|
2013-11-12 06:29:55 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_recurse_template(self, name):
|
2012-10-25 00:02:01 +00:00
|
|
|
'''
|
|
|
|
file.recurse with jinja template enabled
|
|
|
|
'''
|
|
|
|
_ts = 'TEMPLATE TEST STRING'
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.recurse', name=name, source='salt://grail',
|
2012-11-06 06:26:08 +00:00
|
|
|
template='jinja', defaults={'spam': _ts})
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
with salt.utils.files.fopen(os.path.join(name, 'scene33'), 'r') as fp_:
|
|
|
|
contents = fp_.read()
|
|
|
|
self.assertIn(_ts, contents)
|
2012-10-25 00:02:01 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_recurse_clean(self, name):
|
2012-10-25 00:02:01 +00:00
|
|
|
'''
|
|
|
|
file.recurse with clean=True
|
|
|
|
'''
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
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
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(os.path.join(name, '36'), 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
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)
|
2018-04-14 02:41:02 +00:00
|
|
|
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')))
|
2012-05-13 07:02:05 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_recurse_clean_specific_env(self, name):
|
2013-11-02 18:31:14 +00:00
|
|
|
'''
|
|
|
|
file.recurse with clean=True and __env__=prod
|
|
|
|
'''
|
|
|
|
strayfile = os.path.join(name, 'strayfile')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(strayfile, 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-11-02 18:31:14 +00:00
|
|
|
|
|
|
|
# Corner cases: replacing file with a directory and vice versa
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(os.path.join(name, '32'), 'w'):
|
2016-08-11 16:45:24 +00:00
|
|
|
pass
|
2013-11-02 18:31:14 +00:00
|
|
|
os.makedirs(os.path.join(name, 'scene34'))
|
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
name=name,
|
|
|
|
source='salt://holy',
|
|
|
|
clean=True,
|
|
|
|
__env__='prod')
|
2018-04-14 02:41:02 +00:00
|
|
|
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')))
|
2013-11-02 18:31:14 +00:00
|
|
|
|
2018-08-23 15:52:49 +00:00
|
|
|
@skipIf(IS_WINDOWS, 'Skip on windows')
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_recurse_issue_34945(self, base_dir):
|
2016-08-09 19:02:26 +00:00
|
|
|
'''
|
|
|
|
This tests the case where the source dir for the file.recurse state
|
|
|
|
does not contain any files (only subdirectories), and the dir_mode is
|
|
|
|
being managed. For a long time, this corner case resulted in the top
|
|
|
|
level of the destination directory being created with the wrong initial
|
|
|
|
permissions, a problem that would be corrected later on in the
|
|
|
|
file.recurse state via running state.directory. However, the
|
|
|
|
file.directory state only gets called when there are files to be
|
|
|
|
managed in that directory, and when the source directory contains only
|
|
|
|
subdirectories, the incorrectly-set initial perms would not be
|
|
|
|
repaired.
|
|
|
|
|
|
|
|
This was fixed in https://github.com/saltstack/salt/pull/35309
|
2018-08-13 19:23:41 +00:00
|
|
|
|
|
|
|
Skipped on windows because dir_mode is not supported.
|
2016-08-09 19:02:26 +00:00
|
|
|
'''
|
|
|
|
dir_mode = '2775'
|
|
|
|
issue_dir = 'issue-34945'
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, issue_dir)
|
2016-08-09 19:02:26 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
name=name,
|
|
|
|
source='salt://' + issue_dir,
|
|
|
|
dir_mode=dir_mode)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
actual_dir_mode = oct(stat.S_IMODE(os.stat(name).st_mode))[-4:]
|
|
|
|
self.assertEqual(dir_mode, actual_dir_mode)
|
2016-08-09 19:02:26 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_recurse_issue_40578(self, name):
|
2018-01-29 23:10:10 +00:00
|
|
|
'''
|
|
|
|
This ensures that the state doesn't raise an exception when it
|
|
|
|
encounters a file with a unicode filename in the process of invoking
|
|
|
|
file.source_list.
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_state('file.recurse',
|
|
|
|
name=name,
|
|
|
|
source='salt://соль')
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2018-08-23 15:52:49 +00:00
|
|
|
if six.PY2 and IS_WINDOWS:
|
2018-08-22 04:53:12 +00:00
|
|
|
# Providing unicode to os.listdir so that we avoid having listdir
|
|
|
|
# try to decode the filenames using the systemencoding on windows
|
|
|
|
# python 2.
|
|
|
|
files = os.listdir(name.decode('utf-8'))
|
|
|
|
else:
|
|
|
|
files = salt.utils.data.decode(os.listdir(name), normalize=True)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertEqual(
|
2018-08-22 04:53:12 +00:00
|
|
|
sorted(files),
|
2018-04-14 02:41:02 +00:00
|
|
|
sorted(['foo.txt', 'спам.txt', 'яйца.txt']),
|
|
|
|
)
|
2018-01-29 23:10:10 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_replace(self, name):
|
2013-09-20 23:01:57 +00:00
|
|
|
'''
|
|
|
|
file.replace
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
2013-09-20 23:01:57 +00:00
|
|
|
fp_.write('change_me')
|
|
|
|
|
|
|
|
ret = self.run_state('file.replace',
|
|
|
|
name=name, pattern='change', repl='salt', backup=False)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertIn('salt', fp_.read())
|
2013-09-20 23:01:57 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2013-09-20 23:01:57 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18612(self, base_dir):
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
|
|
|
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'
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(path_test, 'w+') as fp_test_:
|
2014-12-17 10:46:21 +00:00
|
|
|
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))
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the number of lines didn't change, even after invoking 'file.replace' 3 times
|
|
|
|
with salt.utils.files.fopen(path_test, 'r') as fp_test_:
|
|
|
|
self.assertTrue((sum(1 for _ in fp_test_) == 1))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the replacement succeeded
|
|
|
|
with salt.utils.files.fopen(path_test, 'r') as fp_test_:
|
|
|
|
self.assertTrue(fp_test_.read().startswith('en_US.UTF-8'))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18612_prepend(self, base_dir):
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
|
|
|
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:
|
|
|
|
|
2016-08-11 19:35:17 +00:00
|
|
|
The tested multifile contains multiple lines not matching the pattern or replacement in any way
|
2014-12-17 10:46:21 +00:00
|
|
|
The replacement pattern should be prepended to the file
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612_prepend'
|
|
|
|
path_in = os.path.join(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.in'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.out'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18612_append(self, base_dir):
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
|
|
|
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:
|
|
|
|
|
2016-08-11 19:35:17 +00:00
|
|
|
The tested multifile contains multiple lines not matching the pattern or replacement in any way
|
2014-12-17 10:46:21 +00:00
|
|
|
The replacement pattern should be appended to the file
|
|
|
|
'''
|
|
|
|
test_name = 'test_replace_issue_18612_append'
|
|
|
|
path_in = os.path.join(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.in'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.out'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
|
|
|
# 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))
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18612_append_not_found_content(self, base_dir):
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
|
|
|
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:
|
|
|
|
|
2016-08-11 19:35:17 +00:00
|
|
|
The tested multifile contains multiple lines not matching the pattern or replacement in any way
|
2014-12-17 10:46:21 +00:00
|
|
|
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(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.in'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.out'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
|
|
|
# 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!'
|
|
|
|
))
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all runs of 'file.replace' reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18612_change_mid_line_with_comment(self, base_dir):
|
2014-12-17 10:46:21 +00:00
|
|
|
'''
|
|
|
|
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(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.in'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
|
|
|
path_out = os.path.join(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.out'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
|
|
|
# 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',
|
2018-08-13 19:23:41 +00:00
|
|
|
name=path_test, pattern='^#foo=bar($|(?=\r\n))', repl='foo=salt', append_if_not_found=True))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the resulting file contains the expected lines
|
|
|
|
self.assertTrue(filecmp.cmp(path_test, path_out))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure the initial file was properly backed up
|
|
|
|
self.assertTrue(filecmp.cmp(path_test + '.bak', path_in))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all 'file.replace' runs reported success
|
|
|
|
for item in ret:
|
|
|
|
self.assertSaltTrueReturn(item)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18841_no_changes(self, base_dir):
|
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(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.in'.format(test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
)
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the file content didn't change
|
|
|
|
self.assertTrue(filecmp.cmp(path_in, path_test))
|
2014-12-18 00:51:08 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure no backup file was created
|
|
|
|
self.assertFalse(os.path.exists(path_test + '.bak'))
|
2014-12-18 00:51:08 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure the file's mtime didn't change
|
|
|
|
self.assertTrue(fstats_post.st_mtime, fstats_orig.st_mtime-age)
|
2014-12-18 00:51:08 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all 'file.replace' runs reported success
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-12-18 00:51:08 +00:00
|
|
|
|
2016-08-31 14:16:40 +00:00
|
|
|
def test_serialize(self):
|
|
|
|
'''
|
|
|
|
Test to ensure that file.serialize returns a data structure that's
|
|
|
|
both serialized and formatted properly
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
path_test = os.path.join(TMP, 'test_serialize')
|
2016-08-31 14:16:40 +00:00
|
|
|
ret = self.run_state('file.serialize',
|
|
|
|
name=path_test,
|
|
|
|
dataset={'name': 'naive',
|
|
|
|
'description': 'A basic test',
|
|
|
|
'a_list': ['first_element', 'second_element'],
|
|
|
|
'finally': 'the last item'},
|
|
|
|
formatter='json')
|
|
|
|
|
2018-09-18 22:22:14 +00:00
|
|
|
with salt.utils.files.fopen(path_test, 'rb') as fp_:
|
|
|
|
serialized_file = salt.utils.stringutils.to_unicode(fp_.read())
|
2016-08-31 14:16:40 +00:00
|
|
|
|
2018-09-20 04:47:07 +00:00
|
|
|
# The JSON serializer uses LF even on OSes where os.path.sep is CRLF.
|
|
|
|
expected_file = '\n'.join([
|
2018-08-13 19:23:41 +00:00
|
|
|
'{',
|
|
|
|
' "a_list": [',
|
|
|
|
' "first_element",',
|
|
|
|
' "second_element"',
|
|
|
|
' ],',
|
|
|
|
' "description": "A basic test",',
|
|
|
|
' "finally": "the last item",',
|
|
|
|
' "name": "naive"',
|
|
|
|
'}',
|
|
|
|
'',
|
|
|
|
])
|
2016-08-31 14:16:40 +00:00
|
|
|
self.assertEqual(serialized_file, expected_file)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_replace_issue_18841_omit_backup(self, base_dir):
|
2014-12-18 00:51:08 +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
|
|
|
|
|
|
|
|
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(
|
2017-03-06 18:12:22 +00:00
|
|
|
FILES, 'file.replace', '{0}.in'.format(test_name)
|
2014-12-18 00:51:08 +00:00
|
|
|
)
|
2018-04-14 02:41:02 +00:00
|
|
|
path_test = os.path.join(base_dir, test_name)
|
2014-12-18 00:51:08 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, the file content didn't change
|
|
|
|
self.assertTrue(filecmp.cmp(path_in, path_test))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure no backup file was created
|
|
|
|
self.assertFalse(os.path.exists(path_test + '.bak'))
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure the file's mtime didn't change
|
|
|
|
self.assertTrue(fstats_post.st_mtime, fstats_orig.st_mtime-age)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# ensure, all 'file.replace' runs reported success
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-12-17 10:46:21 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_comment(self, name):
|
2012-05-13 07:35:01 +00:00
|
|
|
'''
|
|
|
|
file.comment
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
# write a line to file
|
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('comment_me')
|
2016-12-01 23:52:03 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# Look for changes with test=True: return should be "None" at the first run
|
|
|
|
ret = self.run_state('file.comment', test=True, name=name, regex='^comment')
|
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-12-07 16:25:26 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# comment once
|
|
|
|
ret = self.run_state('file.comment', name=name, regex='^comment')
|
|
|
|
# result is positive
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
# line is commented
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertTrue(fp_.read().startswith('#comment'))
|
2016-08-11 19:35:17 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# comment twice
|
|
|
|
ret = self.run_state('file.comment', name=name, regex='^comment')
|
2016-12-01 23:52:03 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# result is still positive
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
# line is still commented
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertTrue(fp_.read().startswith('#comment'))
|
2016-12-01 23:52:03 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# Test previously commented file returns "True" now and not "None" with test=True
|
|
|
|
ret = self.run_state('file.comment', test=True, name=name, regex='^comment')
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 07:38:31 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_test_comment(self, name):
|
2012-05-13 07:38:31 +00:00
|
|
|
'''
|
|
|
|
file.comment test interface
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('comment_me')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.comment', test=True, name=name, regex='.*comment.*',
|
|
|
|
)
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('#comment', fp_.read())
|
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 07:54:20 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_uncomment(self, name):
|
2012-05-13 07:54:20 +00:00
|
|
|
'''
|
|
|
|
file.uncomment
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#comment_me')
|
|
|
|
ret = self.run_state('file.uncomment', name=name, regex='^comment')
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('#comment', fp_.read())
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 07:54:20 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_test_uncomment(self, name):
|
2012-05-13 07:54:20 +00:00
|
|
|
'''
|
|
|
|
file.comment test interface
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#comment_me')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.uncomment', test=True, name=name, regex='^comment.*'
|
|
|
|
)
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertIn('#comment', fp_.read())
|
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 08:00:01 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_append(self, name):
|
2012-05-13 08:00:01 +00:00
|
|
|
'''
|
|
|
|
file.append
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#salty!')
|
|
|
|
ret = self.run_state('file.append', name=name, text='cheese')
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertIn('cheese', fp_.read())
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 08:00:01 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_test_append(self, name):
|
2012-05-13 08:02:20 +00:00
|
|
|
'''
|
|
|
|
file.append test interface
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
with salt.utils.files.fopen(name, 'w+') as fp_:
|
|
|
|
fp_.write('#salty!')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', test=True, name=name, text='cheese'
|
|
|
|
)
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('cheese', fp_.read())
|
|
|
|
self.assertSaltNoneReturn(ret)
|
2012-05-13 08:06:46 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_append_issue_1864_makedirs(self, base_dir):
|
2012-08-28 11:05:50 +00:00
|
|
|
'''
|
2015-09-27 06:06:51 +00:00
|
|
|
file.append but create directories if needed as an option, and create
|
|
|
|
the file if it doesn't exist
|
2012-08-28 11:05:50 +00:00
|
|
|
'''
|
|
|
|
fname = 'append_issue_1864_makedirs'
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, fname)
|
2017-09-13 23:10:15 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# Non existing file get's touched
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', name=name, text='cheese', makedirs=True
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-08-28 11:05:50 +00:00
|
|
|
|
|
|
|
# Nested directory and file get's touched
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'issue_1864', fname)
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', name=name, text='cheese', makedirs=True
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2015-07-15 13:29:43 +00:00
|
|
|
|
2017-09-13 23:10:15 +00:00
|
|
|
# Parent directory exists but file does not and makedirs is False
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'issue_1864', fname + '2')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.append', name=name, text='cheese'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(name))
|
2012-08-28 11:05:50 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_prepend_issue_27401_makedirs(self, base_dir):
|
2015-09-27 06:06:51 +00:00
|
|
|
'''
|
|
|
|
file.prepend but create directories if needed as an option, and create
|
|
|
|
the file if it doesn't exist
|
|
|
|
'''
|
|
|
|
fname = 'prepend_issue_27401'
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, fname)
|
|
|
|
|
|
|
|
# Non existing file get's touched
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.prepend', name=name, text='cheese', makedirs=True
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2015-09-27 06:06:51 +00:00
|
|
|
|
|
|
|
# Nested directory and file get's touched
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'issue_27401', fname)
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.prepend', name=name, text='cheese', makedirs=True
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2015-09-27 06:06:51 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# Parent directory exists but file does not and makedirs is False
|
|
|
|
name = os.path.join(base_dir, 'issue_27401', fname + '2')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.prepend', name=name, text='cheese'
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isfile(name))
|
2015-09-27 06:06:51 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_touch(self, name):
|
2012-05-13 08:06:46 +00:00
|
|
|
'''
|
|
|
|
file.touch
|
|
|
|
'''
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.touch', name=name)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertTrue(os.path.isfile(name))
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-05-13 08:06:46 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile(create=False)
|
|
|
|
def test_test_touch(self, name):
|
2012-05-13 08:07:54 +00:00
|
|
|
'''
|
|
|
|
file.touch test interface
|
|
|
|
'''
|
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
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_touch_directory(self, base_dir):
|
2012-08-05 18:23:12 +00:00
|
|
|
'''
|
|
|
|
file.touch a directory
|
|
|
|
'''
|
2018-04-14 02:41:02 +00:00
|
|
|
name = os.path.join(base_dir, 'touch_test_dir')
|
|
|
|
os.mkdir(name)
|
2012-08-05 18:23:12 +00:00
|
|
|
|
|
|
|
ret = self.run_state('file.touch', name=name)
|
2018-04-14 02:41:02 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
2012-08-05 18:23:12 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_2227_file_append(self, base_dir):
|
2012-10-12 10:45:27 +00:00
|
|
|
'''
|
|
|
|
Text to append includes a percent symbol
|
|
|
|
'''
|
|
|
|
# let's make use of existing state to create a file with contents to
|
|
|
|
# test against
|
2018-04-14 02:41:02 +00:00
|
|
|
tmp_file_append = os.path.join(base_dir, 'test.append')
|
|
|
|
|
|
|
|
self.run_state('file.touch', name=tmp_file_append)
|
|
|
|
self.run_state(
|
|
|
|
'file.append',
|
|
|
|
name=tmp_file_append,
|
|
|
|
source='salt://testappend/firstif')
|
|
|
|
self.run_state(
|
|
|
|
'file.append',
|
|
|
|
name=tmp_file_append,
|
|
|
|
source='salt://testappend/secondif')
|
2012-10-12 10:45:27 +00:00
|
|
|
|
|
|
|
# Now our real test
|
|
|
|
try:
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.append',
|
|
|
|
name=tmp_file_append,
|
|
|
|
text="HISTTIMEFORMAT='%F %T '")
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(tmp_file_append, 'r') as fp_:
|
2014-07-15 01:30:28 +00:00
|
|
|
contents_pre = fp_.read()
|
2012-10-12 10:45:27 +00:00
|
|
|
|
|
|
|
# It should not append text again
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.append',
|
|
|
|
name=tmp_file_append,
|
|
|
|
text="HISTTIMEFORMAT='%F %T '")
|
2012-12-07 16:25:26 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-10-12 10:45:27 +00:00
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(tmp_file_append, 'r') as fp_:
|
2014-07-15 01:30:28 +00:00
|
|
|
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:
|
2017-02-10 18:53:23 +00:00
|
|
|
if os.path.exists(tmp_file_append):
|
|
|
|
shutil.copy(tmp_file_append, tmp_file_append + '.bak')
|
2012-11-08 12:47:19 +00:00
|
|
|
raise
|
2012-10-12 10:45:27 +00:00
|
|
|
|
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')
|
2017-03-06 18:12:22 +00:00
|
|
|
src_file = os.path.join(TMP, 'src.txt')
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.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)
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.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(
|
2017-02-23 17:21:33 +00:00
|
|
|
'Hash mismatch after patch was applied',
|
2013-08-18 04:46:33 +00:00
|
|
|
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
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_2401_file_comment(self, base_dir):
|
2012-11-08 12:47:19 +00:00
|
|
|
# Get a path to the temporary file
|
2018-04-14 02:41:02 +00:00
|
|
|
tmp_file = os.path.join(base_dir, 'issue-2041-comment.txt')
|
2012-11-08 12:47:19 +00:00
|
|
|
# Write some data to it
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(tmp_file, 'w') as fp_:
|
2016-08-11 16:45:24 +00:00
|
|
|
fp_.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
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_2379_file_append(self, base_dir):
|
2012-11-08 19:33:51 +00:00
|
|
|
# Get a path to the temporary file
|
2018-04-14 02:41:02 +00:00
|
|
|
tmp_file = os.path.join(base_dir, 'issue-2379-file-append.txt')
|
2012-11-08 19:33:51 +00:00
|
|
|
# Write some data to it
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(tmp_file, 'w') as fp_:
|
2016-08-11 16:45:24 +00:00
|
|
|
fp_.write(
|
|
|
|
'hello\nworld\n' # Some junk
|
|
|
|
'#PermitRootLogin yes\n' # Commented text
|
|
|
|
'# PermitRootLogin yes\n' # Commented text with space
|
|
|
|
)
|
2012-11-08 19:33:51 +00:00
|
|
|
# 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
|
|
|
|
|
2016-08-02 23:17:11 +00:00
|
|
|
@skipIf(IS_WINDOWS, 'Mode not available in Windows')
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir(create=False)
|
|
|
|
@with_tempdir(create=False)
|
|
|
|
def test_issue_2726_mode_kwarg(self, dir1, dir2):
|
2012-11-28 17:35:25 +00:00
|
|
|
# Let's test for the wrong usage approach
|
|
|
|
bad_mode_kwarg_testfile = os.path.join(
|
2018-04-14 02:41:02 +00:00
|
|
|
dir1, 'bad_mode_kwarg', 'testfile'
|
2012-11-28 17:35:25 +00:00
|
|
|
)
|
|
|
|
bad_template = [
|
|
|
|
'{0}:'.format(bad_mode_kwarg_testfile),
|
|
|
|
' file.recurse:',
|
|
|
|
' - source: salt://testfile',
|
|
|
|
' - mode: 644'
|
|
|
|
]
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', [os.linesep.join(bad_template)]
|
|
|
|
)
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertInSaltComment(
|
|
|
|
'\'mode\' is not allowed in \'file.recurse\'. Please use '
|
|
|
|
'\'file_mode\' and \'dir_mode\'.',
|
|
|
|
ret
|
|
|
|
)
|
|
|
|
self.assertNotInSaltComment(
|
|
|
|
'TypeError: managed() got multiple values for keyword '
|
|
|
|
'argument \'mode\'',
|
|
|
|
ret
|
|
|
|
)
|
2012-11-28 17:35:25 +00:00
|
|
|
|
|
|
|
# Now, the correct usage approach
|
|
|
|
good_mode_kwargs_testfile = os.path.join(
|
2018-04-14 02:41:02 +00:00
|
|
|
dir2, 'good_mode_kwargs', 'testappend'
|
2012-11-28 17:35:25 +00:00
|
|
|
)
|
|
|
|
good_template = [
|
|
|
|
'{0}:'.format(good_mode_kwargs_testfile),
|
|
|
|
' file.recurse:',
|
|
|
|
' - source: salt://testappend',
|
|
|
|
' - dir_mode: 744',
|
|
|
|
' - file_mode: 644',
|
|
|
|
]
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'state.template_str', [os.linesep.join(good_template)]
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2012-11-28 17:35:25 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_8343_accumulated_require_in(self, base_dir):
|
2017-03-06 18:12:22 +00:00
|
|
|
template_path = os.path.join(TMP_STATE_TREE, 'issue-8343.sls')
|
2018-04-14 02:41:02 +00:00
|
|
|
testcase_filedest = os.path.join(base_dir, 'issue-8343.txt')
|
2018-08-13 19:23:41 +00:00
|
|
|
if os.path.exists(template_path):
|
|
|
|
os.remove(template_path)
|
|
|
|
if os.path.exists(testcase_filedest):
|
|
|
|
os.remove(testcase_filedest)
|
2013-11-22 20:16:24 +00:00
|
|
|
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',
|
|
|
|
'']
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(template_path, 'w') as fp_:
|
2016-08-02 21:57:39 +00:00
|
|
|
fp_.write(
|
|
|
|
os.linesep.join(sls_template).format(testcase_filedest))
|
2014-07-15 01:30:28 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_function('state.sls', mods='issue-8343')
|
|
|
|
for name, step in six.iteritems(ret):
|
|
|
|
self.assertSaltTrueReturn({name: step})
|
|
|
|
with salt.utils.files.fopen(testcase_filedest) as fp_:
|
|
|
|
contents = fp_.read().split(os.linesep)
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
'#-- start salt managed zonestart -- PLEASE, DO NOT EDIT',
|
|
|
|
'foo',
|
|
|
|
'#-- end salt managed zonestart --',
|
|
|
|
'#',
|
|
|
|
'#-- start salt managed zoneend -- PLEASE, DO NOT EDIT',
|
|
|
|
'bar',
|
|
|
|
'#-- end salt managed zoneend --',
|
|
|
|
'']
|
|
|
|
|
2018-08-25 19:19:07 +00:00
|
|
|
self.assertEqual([salt.utils.stringutils.to_str(line) for line in expected], contents)
|
2014-03-10 15:41:49 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_11003_immutable_lazy_proxy_sum(self, base_dir):
|
2016-08-11 19:35:17 +00:00
|
|
|
# causes the Import-Module ServerManager error on Windows
|
2017-03-06 18:12:22 +00:00
|
|
|
template_path = os.path.join(TMP_STATE_TREE, 'issue-11003.sls')
|
2018-04-14 02:41:02 +00:00
|
|
|
testcase_filedest = os.path.join(base_dir, 'issue-11003.txt')
|
2014-03-10 15:41:49 +00:00
|
|
|
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'
|
|
|
|
]
|
|
|
|
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(template_path, 'w') as fp_:
|
2016-08-02 21:57:39 +00:00
|
|
|
fp_.write(os.linesep.join(sls_template).format(testcase_filedest))
|
2014-07-15 01:30:28 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_function('state.sls', mods='issue-11003')
|
|
|
|
for name, step in six.iteritems(ret):
|
|
|
|
self.assertSaltTrueReturn({name: step})
|
|
|
|
with salt.utils.files.fopen(testcase_filedest) as fp_:
|
|
|
|
contents = fp_.read().split(os.linesep)
|
|
|
|
|
|
|
|
begin = contents.index(
|
|
|
|
'#-- start managed zone PLEASE, DO NOT EDIT') + 1
|
|
|
|
end = contents.index('#-- end managed zone')
|
|
|
|
block_contents = contents[begin:end]
|
|
|
|
for item in ('', 'bar', 'baz'):
|
|
|
|
block_contents.remove(item)
|
|
|
|
self.assertEqual(block_contents, [])
|
2013-11-22 20:16:24 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_8947_utf8_sls(self, base_dir):
|
2013-12-05 16:59:08 +00:00
|
|
|
'''
|
2016-08-02 21:57:39 +00:00
|
|
|
Test some file operation with utf-8 characters 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
|
|
|
|
'''
|
2018-04-17 20:15:53 +00:00
|
|
|
self.maxDiff = None
|
Update file state/execution modules and associated files with unicode_literals
This updates the file state and execution modules to use
unicode_literals. Since the serializers and the cmd module are touched
by the file state/exec module, those are also updated here, as well as
the cmd state module, for good measure.
Additionally, I found that salt.utils.data.decode_dict (and decode_list)
are misnamed for what they actually do. Since they *encode* the
contents, the functions should be named encode_dict and encode_list,
respectively. And we also should have counterparts which actually
decode, so I've added them. The compatibility functions from salt.utils
still use the old "decode" names to preserve backward compatibility, but
they now invoke the renamed "encode" functions in salt.utils.data. Note
that this means that the compatibility functions
salt.utils.decode_dict/list, and their cognates in salt.utils.data now
do different things, but since the move to salt.utils.data is also
happening in the Oxygen release this is as good a time as any to correct
this oversight.
I've updated the jinja filter docs to include information on the renamed
jinja filters, and also added a section on jinja filter renaming to the
Oxygen release notes. There was another filter that I renamed during the
process of moving functions from salt.utils which I did not properly
document in the release notes, so this is now included along with the
others.
2017-12-12 16:30:33 +00:00
|
|
|
korean_1 = '한국어 시험'
|
|
|
|
korean_2 = '첫 번째 행'
|
|
|
|
korean_3 = '마지막 행'
|
2018-04-14 02:41:02 +00:00
|
|
|
test_file = os.path.join(base_dir, '{0}.txt'.format(korean_1))
|
2018-03-26 00:39:33 +00:00
|
|
|
test_file_encoded = test_file
|
2017-03-06 18:12:22 +00:00
|
|
|
template_path = os.path.join(TMP_STATE_TREE, 'issue-8947.sls')
|
2013-12-05 16:59:08 +00:00
|
|
|
# create the sls template
|
2018-04-17 20:15:53 +00:00
|
|
|
template = textwrap.dedent('''\
|
|
|
|
some-utf8-file-create:
|
|
|
|
file.managed:
|
|
|
|
- name: {test_file}
|
|
|
|
- contents: {korean_1}
|
|
|
|
- makedirs: True
|
|
|
|
- replace: True
|
|
|
|
- show_diff: True
|
|
|
|
some-utf8-file-create2:
|
|
|
|
file.managed:
|
|
|
|
- name: {test_file}
|
|
|
|
- contents: |
|
|
|
|
{korean_2}
|
|
|
|
{korean_1}
|
|
|
|
{korean_3}
|
|
|
|
- replace: True
|
|
|
|
- show_diff: True
|
2018-08-23 23:21:20 +00:00
|
|
|
'''.format(**locals()))
|
|
|
|
|
|
|
|
if not salt.utils.platform.is_windows():
|
|
|
|
template += textwrap.dedent('''\
|
2018-04-17 20:15:53 +00:00
|
|
|
some-utf8-file-content-test:
|
|
|
|
cmd.run:
|
|
|
|
- name: 'cat "{test_file}"'
|
|
|
|
- require:
|
|
|
|
- file: some-utf8-file-create2
|
|
|
|
'''.format(**locals()))
|
|
|
|
|
|
|
|
# Save template file
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
with salt.utils.files.fopen(template_path, 'wb') as fp_:
|
2018-04-17 20:15:53 +00:00
|
|
|
fp_.write(salt.utils.stringutils.to_bytes(template))
|
|
|
|
|
2013-12-05 16:59:08 +00:00
|
|
|
try:
|
2018-04-17 20:15:53 +00:00
|
|
|
result = self.run_function('state.sls', mods='issue-8947')
|
|
|
|
if not isinstance(result, dict):
|
2013-12-05 16:59:08 +00:00
|
|
|
raise AssertionError(
|
|
|
|
('Something went really wrong while testing this sls:'
|
2018-04-17 20:15:53 +00:00
|
|
|
' {0}').format(repr(result))
|
2013-12-05 16:59:08 +00:00
|
|
|
)
|
2015-11-19 21:10:29 +00:00
|
|
|
# difflib produces different output on python 2.6 than on >=2.7
|
|
|
|
if sys.version_info < (2, 7):
|
Update file state/execution modules and associated files with unicode_literals
This updates the file state and execution modules to use
unicode_literals. Since the serializers and the cmd module are touched
by the file state/exec module, those are also updated here, as well as
the cmd state module, for good measure.
Additionally, I found that salt.utils.data.decode_dict (and decode_list)
are misnamed for what they actually do. Since they *encode* the
contents, the functions should be named encode_dict and encode_list,
respectively. And we also should have counterparts which actually
decode, so I've added them. The compatibility functions from salt.utils
still use the old "decode" names to preserve backward compatibility, but
they now invoke the renamed "encode" functions in salt.utils.data. Note
that this means that the compatibility functions
salt.utils.decode_dict/list, and their cognates in salt.utils.data now
do different things, but since the move to salt.utils.data is also
happening in the Oxygen release this is as good a time as any to correct
this oversight.
I've updated the jinja filter docs to include information on the renamed
jinja filters, and also added a section on jinja filter renaming to the
Oxygen release notes. There was another filter that I renamed during the
process of moving functions from salt.utils which I did not properly
document in the release notes, so this is now included along with the
others.
2017-12-12 16:30:33 +00:00
|
|
|
diff = '--- \n+++ \n@@ -1,1 +1,3 @@\n'
|
2015-11-19 21:10:29 +00:00
|
|
|
else:
|
Update file state/execution modules and associated files with unicode_literals
This updates the file state and execution modules to use
unicode_literals. Since the serializers and the cmd module are touched
by the file state/exec module, those are also updated here, as well as
the cmd state module, for good measure.
Additionally, I found that salt.utils.data.decode_dict (and decode_list)
are misnamed for what they actually do. Since they *encode* the
contents, the functions should be named encode_dict and encode_list,
respectively. And we also should have counterparts which actually
decode, so I've added them. The compatibility functions from salt.utils
still use the old "decode" names to preserve backward compatibility, but
they now invoke the renamed "encode" functions in salt.utils.data. Note
that this means that the compatibility functions
salt.utils.decode_dict/list, and their cognates in salt.utils.data now
do different things, but since the move to salt.utils.data is also
happening in the Oxygen release this is as good a time as any to correct
this oversight.
I've updated the jinja filter docs to include information on the renamed
jinja filters, and also added a section on jinja filter renaming to the
Oxygen release notes. There was another filter that I renamed during the
process of moving functions from salt.utils which I did not properly
document in the release notes, so this is now included along with the
others.
2017-12-12 16:30:33 +00:00
|
|
|
diff = '--- \n+++ \n@@ -1 +1,3 @@\n'
|
|
|
|
diff += (
|
2018-08-21 21:04:59 +00:00
|
|
|
'+첫 번째 행{0}'
|
|
|
|
' 한국어 시험{0}'
|
|
|
|
'+마지막 행{0}'
|
|
|
|
).format(os.linesep)
|
2018-04-17 20:15:53 +00:00
|
|
|
|
|
|
|
ret = {x.split('_|-')[1]: y for x, y in six.iteritems(result)}
|
|
|
|
|
|
|
|
# Confirm initial creation of file
|
2013-12-05 17:50:31 +00:00
|
|
|
self.assertEqual(
|
2018-04-17 20:15:53 +00:00
|
|
|
ret['some-utf8-file-create']['comment'],
|
|
|
|
'File {0} updated'.format(test_file_encoded)
|
2013-12-05 17:50:31 +00:00
|
|
|
)
|
2018-04-17 20:15:53 +00:00
|
|
|
self.assertEqual(
|
|
|
|
ret['some-utf8-file-create']['changes'],
|
|
|
|
{'diff': 'New file'}
|
|
|
|
)
|
|
|
|
|
|
|
|
# Confirm file was modified and that the diff was as expected
|
|
|
|
self.assertEqual(
|
|
|
|
ret['some-utf8-file-create2']['comment'],
|
|
|
|
'File {0} updated'.format(test_file_encoded)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
ret['some-utf8-file-create2']['changes'],
|
|
|
|
{'diff': diff}
|
|
|
|
)
|
2018-08-23 23:21:20 +00:00
|
|
|
if salt.utils.platform.is_windows():
|
|
|
|
import subprocess
|
|
|
|
import win32api
|
2018-09-10 13:14:24 +00:00
|
|
|
p = subprocess.Popen(
|
|
|
|
salt.utils.stringutils.to_str(
|
|
|
|
'type {}'.format(win32api.GetShortPathName(test_file))),
|
2018-08-23 23:21:20 +00:00
|
|
|
shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
p.poll()
|
|
|
|
out = p.stdout.read()
|
|
|
|
self.assertEqual(
|
|
|
|
out.decode('utf-8'),
|
|
|
|
os.linesep.join((korean_2, korean_1, korean_3)) + os.linesep
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
|
|
|
ret['some-utf8-file-content-test']['comment'],
|
2018-08-24 19:09:37 +00:00
|
|
|
'Command "cat "{0}"" run'.format(
|
2018-08-23 23:21:20 +00:00
|
|
|
test_file_encoded
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
ret['some-utf8-file-content-test']['changes']['stdout'],
|
|
|
|
'\n'.join((korean_2, korean_1, korean_3))
|
|
|
|
)
|
2013-12-05 16:59:08 +00:00
|
|
|
finally:
|
2018-04-14 02:41:02 +00:00
|
|
|
try:
|
|
|
|
os.remove(template_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2013-12-05 16:59:08 +00:00
|
|
|
|
2017-03-02 19:41:11 +00:00
|
|
|
@skip_if_not_root
|
2016-08-02 20:33:21 +00:00
|
|
|
@skipIf(not HAS_PWD, "pwd not available. Skipping test")
|
|
|
|
@skipIf(not HAS_GRP, "grp not available. Skipping test")
|
2014-04-25 18:57:14 +00:00
|
|
|
@with_system_user_and_group('user12209', 'group12209',
|
|
|
|
on_existing='delete', delete=True)
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_12209_follow_symlinks(self, tempdir, user, group):
|
2014-04-25 03:27:59 +00:00
|
|
|
'''
|
|
|
|
Ensure that symlinks are properly chowned when recursing (following
|
|
|
|
symlinks)
|
|
|
|
'''
|
|
|
|
# Make the directories for this test
|
2018-04-14 02:41:02 +00:00
|
|
|
onedir = os.path.join(tempdir, 'one')
|
|
|
|
twodir = os.path.join(tempdir, 'two')
|
|
|
|
os.mkdir(onedir)
|
2014-04-25 03:27:59 +00:00
|
|
|
os.symlink(onedir, twodir)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# Run the state
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', name=tempdir, follow_symlinks=True,
|
|
|
|
user=user, group=group, recurse=['user', 'group']
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2018-04-14 02:41:02 +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)
|
|
|
|
if salt.utils.path.which('id'):
|
|
|
|
root_group = self.run_function('user.primary_group', ['root'])
|
|
|
|
self.assertEqual(grp.getgrgid(twostats.st_gid).gr_name, root_group)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2017-03-02 19:41:11 +00:00
|
|
|
@skip_if_not_root
|
2016-08-02 20:33:21 +00:00
|
|
|
@skipIf(not HAS_PWD, "pwd not available. Skipping test")
|
|
|
|
@skipIf(not HAS_GRP, "grp not available. Skipping test")
|
2014-04-25 18:57:14 +00:00
|
|
|
@with_system_user_and_group('user12209', 'group12209',
|
|
|
|
on_existing='delete', delete=True)
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_12209_no_follow_symlinks(self, tempdir, user, group):
|
2014-04-25 03:27:59 +00:00
|
|
|
'''
|
|
|
|
Ensure that symlinks are properly chowned when recursing (not following
|
|
|
|
symlinks)
|
|
|
|
'''
|
|
|
|
# Make the directories for this test
|
2018-04-14 02:41:02 +00:00
|
|
|
onedir = os.path.join(tempdir, 'one')
|
|
|
|
twodir = os.path.join(tempdir, 'two')
|
|
|
|
os.mkdir(onedir)
|
2014-04-25 03:27:59 +00:00
|
|
|
os.symlink(onedir, twodir)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
# Run the state
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.directory', name=tempdir, follow_symlinks=False,
|
|
|
|
user=user, group=group, recurse=['user', 'group']
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2018-04-14 02:41:02 +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)
|
2014-04-25 03:27:59 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile(create=False)
|
|
|
|
@with_tempfile()
|
|
|
|
def test_template_local_file(self, source, dest):
|
2014-07-14 22:40:03 +00:00
|
|
|
'''
|
|
|
|
Test a file.managed state with a local file as the source. Test both
|
|
|
|
with the file:// protocol designation prepended, and without it.
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(source, 'w') as fp_:
|
2014-07-14 22:40:03 +00:00
|
|
|
fp_.write('{{ foo }}\n')
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
for prefix in ('file://', ''):
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=dest,
|
|
|
|
source=prefix + source,
|
|
|
|
template='jinja',
|
|
|
|
context={'foo': 'Hello world!'}
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2014-07-14 22:40:03 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_template_local_file_noclobber(self, source):
|
2014-07-14 22:40:03 +00:00
|
|
|
'''
|
|
|
|
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.
|
|
|
|
'''
|
2017-07-18 16:31:01 +00:00
|
|
|
with salt.utils.files.fopen(source, 'w') as fp_:
|
2014-07-14 22:40:03 +00:00
|
|
|
fp_.write('{{ foo }}\n')
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=source,
|
|
|
|
source=source,
|
|
|
|
template='jinja',
|
|
|
|
context={'foo': 'Hello world!'}
|
|
|
|
)
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
self.assertIn(
|
|
|
|
('Source file cannot be the same as destination'),
|
|
|
|
ret[next(iter(ret))]['comment'],
|
|
|
|
)
|
2014-07-14 22:40:03 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile(create=False)
|
|
|
|
@with_tempfile(create=False)
|
|
|
|
def test_issue_25250_force_copy_deletes(self, source, dest):
|
2015-07-24 19:28:15 +00:00
|
|
|
'''
|
|
|
|
ensure force option in copy state does not delete target file
|
|
|
|
'''
|
2017-03-06 18:12:22 +00:00
|
|
|
shutil.copyfile(os.path.join(FILES, 'hosts'), source)
|
|
|
|
shutil.copyfile(os.path.join(FILES, 'file/base/cheese'), dest)
|
2015-07-24 19:28:15 +00:00
|
|
|
|
|
|
|
self.run_state('file.copy', name=dest, source=source, force=True)
|
|
|
|
self.assertTrue(os.path.exists(dest))
|
|
|
|
self.assertTrue(filecmp.cmp(source, dest))
|
|
|
|
|
|
|
|
os.remove(source)
|
|
|
|
os.remove(dest)
|
|
|
|
|
2018-06-29 14:39:02 +00:00
|
|
|
@destructiveTest
|
2018-07-12 13:48:40 +00:00
|
|
|
@with_tempfile()
|
2018-06-29 14:39:02 +00:00
|
|
|
def test_file_copy_make_dirs(self, source):
|
|
|
|
'''
|
|
|
|
ensure make_dirs creates correct user perms
|
|
|
|
'''
|
|
|
|
shutil.copyfile(os.path.join(FILES, 'hosts'), source)
|
|
|
|
dest = os.path.join(TMP, 'dir1', 'dir2', 'copied_file.txt')
|
|
|
|
|
|
|
|
user = 'salt'
|
2018-07-09 19:47:37 +00:00
|
|
|
mode = '0644'
|
2018-06-29 14:39:02 +00:00
|
|
|
self.run_function('user.add', [user])
|
2018-07-09 19:47:37 +00:00
|
|
|
ret = self.run_state('file.copy', name=dest, source=source, user=user,
|
|
|
|
makedirs=True, mode=mode)
|
2018-06-29 14:39:02 +00:00
|
|
|
file_checks = [dest, os.path.join(TMP, 'dir1'), os.path.join(TMP, 'dir1', 'dir2')]
|
|
|
|
for check in file_checks:
|
2018-07-09 19:47:37 +00:00
|
|
|
user_check = self.run_function('file.get_user', [check])
|
|
|
|
mode_check = self.run_function('file.get_mode', [check])
|
|
|
|
assert user_check == user
|
2018-07-11 18:52:54 +00:00
|
|
|
assert salt.utils.files.normalize_mode(mode_check) == mode
|
2018-06-29 14:39:02 +00:00
|
|
|
|
2016-02-12 19:21:50 +00:00
|
|
|
def test_contents_pillar_with_pillar_list(self):
|
|
|
|
'''
|
|
|
|
This tests for any regressions for this issue:
|
|
|
|
https://github.com/saltstack/salt/issues/30934
|
|
|
|
'''
|
2016-08-03 16:28:29 +00:00
|
|
|
state_file = 'file_contents_pillar'
|
2016-08-02 21:57:39 +00:00
|
|
|
|
|
|
|
ret = self.run_function('state.sls', mods=state_file)
|
2016-02-12 19:21:50 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
2015-07-24 19:28:15 +00:00
|
|
|
|
2018-04-13 03:16:56 +00:00
|
|
|
def test_binary_contents(self):
|
|
|
|
'''
|
|
|
|
This tests to ensure that binary contents do not cause a traceback.
|
|
|
|
'''
|
|
|
|
name = os.path.join(TMP, '1px.gif')
|
|
|
|
try:
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed',
|
|
|
|
name=name,
|
|
|
|
contents=BINARY_FILE)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(name)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2018-07-01 19:19:48 +00:00
|
|
|
@skip_if_not_root
|
|
|
|
@skipIf(not HAS_PWD, "pwd not available. Skipping test")
|
|
|
|
@skipIf(not HAS_GRP, "grp not available. Skipping test")
|
|
|
|
@with_system_user_and_group('user12209', 'group12209',
|
|
|
|
on_existing='delete', delete=True)
|
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_48336_file_managed_mode_setuid(self, tempdir, user, group):
|
|
|
|
'''
|
|
|
|
Ensure that mode is correct with changing of ownership and group
|
|
|
|
symlinks)
|
|
|
|
'''
|
|
|
|
tempfile = os.path.join(tempdir, 'temp_file_issue_48336')
|
|
|
|
|
|
|
|
# Run the state
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=tempfile,
|
|
|
|
user=user, group=group, mode='4750',
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
# Check that the owner and group are correct, and
|
|
|
|
# the mode is what we expect
|
|
|
|
temp_file_stats = os.stat(tempfile)
|
2018-07-02 16:06:22 +00:00
|
|
|
|
|
|
|
# Normalize the mode
|
2018-07-02 15:51:55 +00:00
|
|
|
temp_file_mode = six.text_type(oct(stat.S_IMODE(temp_file_stats.st_mode)))
|
2018-07-02 16:06:22 +00:00
|
|
|
temp_file_mode = salt.utils.files.normalize_mode(temp_file_mode)
|
|
|
|
|
2018-07-02 15:51:55 +00:00
|
|
|
self.assertEqual(temp_file_mode, '4750')
|
2018-07-01 19:19:48 +00:00
|
|
|
self.assertEqual(pwd.getpwuid(temp_file_stats.st_uid).pw_name, user)
|
|
|
|
self.assertEqual(grp.getgrgid(temp_file_stats.st_gid).gr_name, group)
|
|
|
|
|
2018-08-09 02:47:40 +00:00
|
|
|
@with_tempdir()
|
|
|
|
def test_issue_48557(self, tempdir):
|
|
|
|
tempfile = os.path.join(tempdir, 'temp_file_issue_48557')
|
2018-08-09 02:55:23 +00:00
|
|
|
with salt.utils.files.fopen(tempfile, 'wb') as fp:
|
2018-08-09 02:47:40 +00:00
|
|
|
fp.write(os.linesep.join([
|
2018-08-09 02:55:23 +00:00
|
|
|
'test1',
|
2018-08-09 02:47:40 +00:00
|
|
|
'test2',
|
|
|
|
'test3',
|
|
|
|
'',
|
|
|
|
]).encode('utf-8'))
|
|
|
|
ret = self.run_state('file.line',
|
|
|
|
name=tempfile,
|
|
|
|
after='test2',
|
|
|
|
mode='insert',
|
|
|
|
content='test4')
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2018-08-09 02:55:23 +00:00
|
|
|
with salt.utils.files.fopen(tempfile, 'rb') as fp:
|
2018-08-09 02:47:40 +00:00
|
|
|
content = fp.read()
|
|
|
|
self.assertEqual(content, os.linesep.join([
|
|
|
|
'test1',
|
|
|
|
'test2',
|
|
|
|
'test4',
|
|
|
|
'test3',
|
|
|
|
'',
|
|
|
|
]).encode('utf-8'))
|
|
|
|
|
2019-01-19 23:11:49 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_issue_50221(self, name):
|
2019-01-22 17:46:32 +00:00
|
|
|
expected = 'abc{0}{0}{0}'.format(os.linesep)
|
2019-01-19 23:11:49 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'pillar.get',
|
|
|
|
['issue-50221']
|
|
|
|
)
|
2019-01-22 17:46:32 +00:00
|
|
|
assert ret == expected
|
2019-01-19 23:11:49 +00:00
|
|
|
ret = self.run_function(
|
|
|
|
'state.apply',
|
|
|
|
['issue-50221'],
|
|
|
|
pillar={
|
|
|
|
'name': name
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2019-01-22 17:46:32 +00:00
|
|
|
with salt.utils.files.fopen(name, 'r') as fp:
|
2019-01-19 23:11:49 +00:00
|
|
|
contents = fp.read()
|
2019-01-20 01:18:39 +00:00
|
|
|
assert contents == expected
|
2019-01-19 23:11:49 +00:00
|
|
|
|
2019-01-18 01:50:13 +00:00
|
|
|
def test_managed_file_issue_51208(self):
|
|
|
|
'''
|
|
|
|
Test to ensure we can handle a file with escaped double-quotes
|
|
|
|
'''
|
|
|
|
name = os.path.join(TMP, 'issue_51208.txt')
|
|
|
|
ret = self.run_state(
|
|
|
|
'file.managed', name=name, source='salt://issue-51208/vimrc.stub'
|
|
|
|
)
|
|
|
|
src = os.path.join(BASE_FILES, 'issue-51208', 'vimrc.stub')
|
|
|
|
with salt.utils.files.fopen(src, 'r') as fp_:
|
|
|
|
master_data = fp_.read()
|
|
|
|
with salt.utils.files.fopen(name, 'r') as fp_:
|
|
|
|
minion_data = fp_.read()
|
|
|
|
self.assertEqual(master_data, minion_data)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2018-03-07 21:45:38 +00:00
|
|
|
|
|
|
|
class BlockreplaceTest(ModuleCase, SaltReturnAssertsMixin):
|
|
|
|
marker_start = '# start'
|
|
|
|
marker_end = '# end'
|
2018-08-22 19:21:29 +00:00
|
|
|
content = dedent(six.text_type('''\
|
|
|
|
Line 1 of block
|
|
|
|
Line 2 of block
|
|
|
|
'''))
|
|
|
|
without_block = dedent(six.text_type('''\
|
|
|
|
Hello world!
|
|
|
|
|
|
|
|
# comment here
|
|
|
|
'''))
|
|
|
|
with_non_matching_block = dedent(six.text_type('''\
|
|
|
|
Hello world!
|
|
|
|
|
|
|
|
# start
|
|
|
|
No match here
|
|
|
|
# end
|
|
|
|
# comment here
|
|
|
|
'''))
|
|
|
|
with_non_matching_block_and_marker_end_not_after_newline = dedent(six.text_type('''\
|
|
|
|
Hello world!
|
|
|
|
|
|
|
|
# start
|
|
|
|
No match here# end
|
|
|
|
# comment here
|
|
|
|
'''))
|
|
|
|
with_matching_block = dedent(six.text_type('''\
|
|
|
|
Hello world!
|
|
|
|
|
|
|
|
# start
|
|
|
|
Line 1 of block
|
|
|
|
Line 2 of block
|
|
|
|
# end
|
|
|
|
# comment here
|
|
|
|
'''))
|
|
|
|
with_matching_block_and_extra_newline = dedent(six.text_type('''\
|
|
|
|
Hello world!
|
|
|
|
|
|
|
|
# start
|
|
|
|
Line 1 of block
|
|
|
|
Line 2 of block
|
|
|
|
|
|
|
|
# end
|
|
|
|
# comment here
|
|
|
|
'''))
|
|
|
|
with_matching_block_and_marker_end_not_after_newline = dedent(six.text_type('''\
|
|
|
|
Hello world!
|
|
|
|
|
|
|
|
# start
|
|
|
|
Line 1 of block
|
|
|
|
Line 2 of block# end
|
|
|
|
# comment here
|
|
|
|
'''))
|
2018-03-07 21:45:38 +00:00
|
|
|
content_explicit_posix_newlines = ('Line 1 of block\n'
|
|
|
|
'Line 2 of block\n')
|
|
|
|
content_explicit_windows_newlines = ('Line 1 of block\r\n'
|
|
|
|
'Line 2 of block\r\n')
|
|
|
|
without_block_explicit_posix_newlines = ('Hello world!\n\n'
|
|
|
|
'# comment here\n')
|
|
|
|
without_block_explicit_windows_newlines = ('Hello world!\r\n\r\n'
|
|
|
|
'# comment here\r\n')
|
|
|
|
with_block_prepended_explicit_posix_newlines = ('# start\n'
|
|
|
|
'Line 1 of block\n'
|
|
|
|
'Line 2 of block\n'
|
|
|
|
'# end\n'
|
|
|
|
'Hello world!\n\n'
|
|
|
|
'# comment here\n')
|
|
|
|
with_block_prepended_explicit_windows_newlines = ('# start\r\n'
|
|
|
|
'Line 1 of block\r\n'
|
|
|
|
'Line 2 of block\r\n'
|
|
|
|
'# end\r\n'
|
|
|
|
'Hello world!\r\n\r\n'
|
|
|
|
'# comment here\r\n')
|
|
|
|
with_block_appended_explicit_posix_newlines = ('Hello world!\n\n'
|
|
|
|
'# comment here\n'
|
|
|
|
'# start\n'
|
|
|
|
'Line 1 of block\n'
|
|
|
|
'Line 2 of block\n'
|
|
|
|
'# end\n')
|
|
|
|
with_block_appended_explicit_windows_newlines = ('Hello world!\r\n\r\n'
|
|
|
|
'# comment here\r\n'
|
|
|
|
'# start\r\n'
|
|
|
|
'Line 1 of block\r\n'
|
|
|
|
'Line 2 of block\r\n'
|
|
|
|
'# end\r\n')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _write(dest, content):
|
2018-03-15 21:31:29 +00:00
|
|
|
with salt.utils.files.fopen(dest, 'wb') as fp_:
|
|
|
|
fp_.write(salt.utils.stringutils.to_bytes(content))
|
2018-03-07 21:45:38 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _read(src):
|
2018-03-15 21:31:29 +00:00
|
|
|
with salt.utils.files.fopen(src, 'rb') as fp_:
|
|
|
|
return salt.utils.stringutils.to_unicode(fp_.read())
|
2018-03-07 21:45:38 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_prepend(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when prepend_if_not_found=True and block doesn't
|
|
|
|
exist in file.
|
|
|
|
'''
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.marker_start + os.linesep + self.content + \
|
|
|
|
self.marker_end + os.linesep + self.without_block
|
2018-03-07 21:45:38 +00:00
|
|
|
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_prepend_append_newline(self, name):
|
2016-05-05 14:05:55 +00:00
|
|
|
'''
|
2018-03-07 21:45:38 +00:00
|
|
|
Test blockreplace when prepend_if_not_found=True and block doesn't
|
|
|
|
exist in file. Test with append_newline explicitly set to True.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.marker_start + os.linesep + self.content + \
|
|
|
|
os.linesep + self.marker_end + os.linesep + self.without_block
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.marker_start + os.linesep + self.content + \
|
|
|
|
self.marker_end + os.linesep + self.without_block
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_prepend_no_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when prepend_if_not_found=True and block doesn't
|
|
|
|
exist in file. Test with append_newline explicitly set to False.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.marker_start + os.linesep + self.content + \
|
|
|
|
self.marker_end + os.linesep + self.without_block
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.marker_start + os.linesep + \
|
|
|
|
self.content.rstrip('\r\n') + self.marker_end + os.linesep + \
|
2018-03-07 21:45:38 +00:00
|
|
|
self.without_block
|
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_append(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when append_if_not_found=True and block doesn't
|
|
|
|
exist in file.
|
|
|
|
'''
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.without_block + self.marker_start + os.linesep + \
|
|
|
|
self.content + self.marker_end + os.linesep
|
2018-03-07 21:45:38 +00:00
|
|
|
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_append_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when append_if_not_found=True and block doesn't
|
|
|
|
exist in file. Test with append_newline explicitly set to True.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.without_block + self.marker_start + os.linesep + \
|
|
|
|
self.content + os.linesep + self.marker_end + os.linesep
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.without_block + self.marker_start + os.linesep + \
|
|
|
|
self.content + self.marker_end + os.linesep
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_append_no_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when append_if_not_found=True and block doesn't
|
|
|
|
exist in file. Test with append_newline explicitly set to False.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.without_block + self.marker_start + os.linesep + \
|
|
|
|
self.content + self.marker_end + os.linesep
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
2018-08-19 20:08:46 +00:00
|
|
|
expected = self.without_block + self.marker_start + os.linesep + \
|
|
|
|
self.content.rstrip('\r\n') + self.marker_end + os.linesep
|
2018-03-07 21:45:38 +00:00
|
|
|
self._write(name, self.without_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), expected)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_prepend_auto_line_separator(self, name):
|
|
|
|
'''
|
|
|
|
This tests the line separator auto-detection when prepending the block
|
|
|
|
'''
|
|
|
|
# POSIX newlines to Windows newlines
|
|
|
|
self._write(name, self.without_block_explicit_windows_newlines)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_posix_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_prepended_explicit_windows_newlines)
|
|
|
|
# Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_posix_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_prepended_explicit_windows_newlines)
|
|
|
|
|
|
|
|
# Windows newlines to POSIX newlines
|
|
|
|
self._write(name, self.without_block_explicit_posix_newlines)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_windows_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_prepended_explicit_posix_newlines)
|
|
|
|
# Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_windows_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
prepend_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_prepended_explicit_posix_newlines)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_append_auto_line_separator(self, name):
|
|
|
|
'''
|
|
|
|
This tests the line separator auto-detection when appending the block
|
|
|
|
'''
|
|
|
|
# POSIX newlines to Windows newlines
|
|
|
|
self._write(name, self.without_block_explicit_windows_newlines)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_posix_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_appended_explicit_windows_newlines)
|
|
|
|
# Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_posix_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_appended_explicit_windows_newlines)
|
|
|
|
|
|
|
|
# Windows newlines to POSIX newlines
|
|
|
|
self._write(name, self.without_block_explicit_posix_newlines)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_windows_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_appended_explicit_posix_newlines)
|
|
|
|
# Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content_explicit_windows_newlines,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_if_not_found=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_block_appended_explicit_posix_newlines)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_non_matching_block(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists but its contents are not a
|
|
|
|
match.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.with_non_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.with_non_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_non_matching_block_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists but its contents are not a
|
|
|
|
match. Test with append_newline explicitly set to True.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.with_non_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.with_non_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_non_matching_block_no_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists but its contents are not a
|
|
|
|
match. Test with append_newline explicitly set to False.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.with_non_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.with_non_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_non_matching_block_and_marker_not_after_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists but its contents are not a
|
|
|
|
match, and the marker_end is not directly preceded by a newline.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_non_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_non_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_non_matching_block_and_marker_not_after_newline_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists but its contents are not a match,
|
|
|
|
and the marker_end is not directly preceded by a newline. Test with
|
|
|
|
append_newline explicitly set to True.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_non_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_non_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_non_matching_block_and_marker_not_after_newline_no_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists but its contents are not a match,
|
|
|
|
and the marker_end is not directly preceded by a newline. Test with
|
|
|
|
append_newline explicitly set to False.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_non_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_non_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_matching_block(self, name):
|
2016-05-05 14:05:55 +00:00
|
|
|
'''
|
2018-03-07 21:45:38 +00:00
|
|
|
Test blockreplace when block exists and its contents are a match. No
|
|
|
|
changes should be made.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.with_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.with_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_matching_block_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists and its contents are a match. Test
|
|
|
|
with append_newline explicitly set to True. This will result in an
|
|
|
|
extra newline when the content ends in a newline, and will not when the
|
|
|
|
content does not end in a newline.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.with_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.with_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_matching_block_no_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists and its contents are a match. Test
|
|
|
|
with append_newline explicitly set to False. This will result in the
|
|
|
|
marker_end not being directly preceded by a newline when the content
|
|
|
|
does not end in a newline.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(name, self.with_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(name, self.with_matching_block)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_matching_block_and_marker_not_after_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists and its contents are a match, but
|
|
|
|
the marker_end is not directly preceded by a newline.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_matching_block_and_marker_not_after_newline_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists and its contents are a match, but
|
|
|
|
the marker_end is not directly preceded by a newline. Test with
|
|
|
|
append_newline explicitly set to True. This will result in an extra
|
|
|
|
newline when the content ends in a newline, and will not when the
|
|
|
|
content does not end in a newline.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_extra_newline)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile()
|
2018-03-07 21:45:38 +00:00
|
|
|
def test_matching_block_and_marker_not_after_newline_no_append_newline(self, name):
|
|
|
|
'''
|
|
|
|
Test blockreplace when block exists and its contents are a match, but
|
|
|
|
the marker_end is not directly preceded by a newline. Test with
|
|
|
|
append_newline explicitly set to False.
|
|
|
|
'''
|
|
|
|
# Pass 1: content ends in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertTrue(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
# Pass 1a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content,
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(self._read(name), self.with_matching_block)
|
|
|
|
|
|
|
|
# Pass 2: content does not end in newline
|
|
|
|
self._write(
|
|
|
|
name,
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
|
|
|
# Pass 2a: Re-run state, no changes should be made
|
|
|
|
ret = self.run_state('file.blockreplace',
|
|
|
|
name=name,
|
|
|
|
content=self.content.rstrip('\r\n'),
|
|
|
|
marker_start=self.marker_start,
|
|
|
|
marker_end=self.marker_end,
|
|
|
|
append_newline=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
self.assertFalse(ret[next(iter(ret))]['changes'])
|
|
|
|
self.assertEqual(
|
|
|
|
self._read(name),
|
|
|
|
self.with_matching_block_and_marker_end_not_after_newline)
|
2016-05-05 14:05:55 +00:00
|
|
|
|
2018-09-26 06:26:42 +00:00
|
|
|
@with_tempfile()
|
|
|
|
def test_issue_49043(self, name):
|
|
|
|
ret = self.run_function(
|
|
|
|
'state.sls',
|
|
|
|
mods='issue-49043',
|
|
|
|
pillar={'name': name},
|
|
|
|
)
|
|
|
|
log.error("ret = %s", repr(ret))
|
2018-09-29 00:06:36 +00:00
|
|
|
diff = '--- \n+++ \n@@ -0,0 +1,3 @@\n'
|
|
|
|
diff += dedent('''\
|
2018-09-26 06:26:42 +00:00
|
|
|
+#-- start managed zone --
|
|
|
|
+äöü
|
|
|
|
+#-- end managed zone --
|
|
|
|
''')
|
|
|
|
job = 'file_|-somefile-blockreplace_|-{}_|-blockreplace'.format(name)
|
|
|
|
self.assertEqual(
|
|
|
|
ret[job]['changes']['diff'],
|
|
|
|
diff)
|
|
|
|
|
2017-03-01 16:57:06 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class RemoteFileTest(ModuleCase, SaltReturnAssertsMixin):
|
2017-03-01 16:57:06 +00:00
|
|
|
'''
|
|
|
|
Uses a local tornado webserver to test http(s) file.managed states with and
|
|
|
|
without skip_verify
|
|
|
|
'''
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-04-21 20:35:51 +00:00
|
|
|
cls.webserver = Webserver()
|
|
|
|
cls.webserver.start()
|
|
|
|
cls.source = cls.webserver.url('grail/scene33')
|
2018-08-23 15:52:49 +00:00
|
|
|
if IS_WINDOWS:
|
|
|
|
# CRLF vs LF causes a different hash on windows
|
2018-08-13 19:23:41 +00:00
|
|
|
cls.source_hash = '21438b3d5fd2c0028bcab92f7824dc69'
|
|
|
|
else:
|
|
|
|
cls.source_hash = 'd2feb3beb323c79fc7a0f44f1408b4a3'
|
2017-03-01 16:57:06 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2017-04-21 20:35:51 +00:00
|
|
|
cls.webserver.stop()
|
2017-03-01 16:57:06 +00:00
|
|
|
|
2018-04-14 02:41:02 +00:00
|
|
|
@with_tempfile(create=False)
|
|
|
|
def setUp(self, name): # pylint: disable=arguments-differ
|
|
|
|
self.name = name
|
2017-03-01 16:57:06 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
|
|
|
os.remove(self.name)
|
|
|
|
except OSError as exc:
|
|
|
|
if exc.errno != errno.ENOENT:
|
|
|
|
raise exc
|
|
|
|
|
2018-08-16 19:53:32 +00:00
|
|
|
def run_state(self, *args, **kwargs):
|
|
|
|
ret = super(RemoteFileTest, self).run_state(*args, **kwargs)
|
|
|
|
log.debug('ret = %s', ret)
|
|
|
|
return ret
|
|
|
|
|
2017-03-01 16:57:06 +00:00
|
|
|
def test_file_managed_http_source_no_hash(self):
|
|
|
|
'''
|
|
|
|
Test a remote file with no hash
|
|
|
|
'''
|
|
|
|
ret = self.run_state('file.managed',
|
|
|
|
name=self.name,
|
2017-04-21 20:35:51 +00:00
|
|
|
source=self.source,
|
2017-03-01 16:57:06 +00:00
|
|
|
skip_verify=False)
|
|
|
|
# This should fail because no hash was provided
|
|
|
|
self.assertSaltFalseReturn(ret)
|
|
|
|
|
|
|
|
def test_file_managed_http_source(self):
|
|
|
|
'''
|
|
|
|
Test a remote file with no hash
|
|
|
|
'''
|
|
|
|
ret = self.run_state('file.managed',
|
|
|
|
name=self.name,
|
2017-04-21 20:35:51 +00:00
|
|
|
source=self.source,
|
|
|
|
source_hash=self.source_hash,
|
2017-03-01 16:57:06 +00:00
|
|
|
skip_verify=False)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
def test_file_managed_http_source_skip_verify(self):
|
|
|
|
'''
|
|
|
|
Test a remote file using skip_verify
|
|
|
|
'''
|
|
|
|
ret = self.run_state('file.managed',
|
|
|
|
name=self.name,
|
2017-04-21 20:35:51 +00:00
|
|
|
source=self.source,
|
2017-03-01 16:57:06 +00:00
|
|
|
skip_verify=True)
|
|
|
|
self.assertSaltTrueReturn(ret)
|
2018-07-24 16:42:07 +00:00
|
|
|
|
2018-08-16 19:53:32 +00:00
|
|
|
def test_file_managed_keep_source_false_http(self):
|
|
|
|
'''
|
|
|
|
This test ensures that we properly clean the cached file if keep_source
|
|
|
|
is set to False, for source files using an http:// URL
|
|
|
|
'''
|
|
|
|
# Run the state
|
|
|
|
ret = self.run_state('file.managed',
|
|
|
|
name=self.name,
|
|
|
|
source=self.source,
|
|
|
|
source_hash=self.source_hash,
|
|
|
|
keep_source=False)
|
|
|
|
ret = ret[next(iter(ret))]
|
|
|
|
assert ret['result'] is True
|
|
|
|
|
|
|
|
# Now make sure that the file is not cached
|
|
|
|
result = self.run_function('cp.is_cached', [self.source])
|
|
|
|
assert result == '', 'File is still cached at {0}'.format(result)
|
|
|
|
|
|
|
|
|
2018-07-26 20:04:51 +00:00
|
|
|
WIN_TEST_FILE = 'c:/testfile'
|
|
|
|
|
2018-07-24 16:42:07 +00:00
|
|
|
|
|
|
|
@destructiveTest
|
2018-07-27 15:57:34 +00:00
|
|
|
@skipIf(not IS_WINDOWS, 'windows test only')
|
2018-07-24 16:42:07 +00:00
|
|
|
class WinFileTest(ModuleCase):
|
|
|
|
'''
|
|
|
|
Test for the file state on Windows
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
2018-07-26 20:04:51 +00:00
|
|
|
self.run_state('file.managed', name=WIN_TEST_FILE, makedirs=True, contents='Only a test')
|
2018-07-24 16:42:07 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2018-07-26 20:04:51 +00:00
|
|
|
self.run_state('file.absent', name=WIN_TEST_FILE)
|
2018-07-24 16:42:07 +00:00
|
|
|
|
|
|
|
def test_file_managed(self):
|
|
|
|
'''
|
|
|
|
Test file.managed on Windows
|
|
|
|
'''
|
2018-07-26 20:04:51 +00:00
|
|
|
self.assertTrue(self.run_state('file.exists', name=WIN_TEST_FILE))
|
2018-07-24 16:42:07 +00:00
|
|
|
|
|
|
|
def test_file_copy(self):
|
|
|
|
'''
|
|
|
|
Test file.copy on Windows
|
|
|
|
'''
|
2018-07-26 20:04:51 +00:00
|
|
|
ret = self.run_state('file.copy', name='c:/testfile_copy', makedirs=True, source=WIN_TEST_FILE)
|
2018-07-24 16:42:07 +00:00
|
|
|
self.assertTrue(ret)
|
|
|
|
|
|
|
|
def test_file_comment(self):
|
|
|
|
'''
|
|
|
|
Test file.comment on Windows
|
|
|
|
'''
|
2018-07-26 20:04:51 +00:00
|
|
|
self.run_state('file.comment', name=WIN_TEST_FILE, regex='^Only')
|
2018-07-27 15:57:34 +00:00
|
|
|
with salt.utils.files.fopen(WIN_TEST_FILE, 'r') as fp_:
|
2018-07-24 16:42:07 +00:00
|
|
|
self.assertTrue(fp_.read().startswith('#Only'))
|
|
|
|
|
|
|
|
def test_file_replace(self):
|
|
|
|
'''
|
|
|
|
Test file.replace on Windows
|
|
|
|
'''
|
2018-07-26 20:04:51 +00:00
|
|
|
self.run_state('file.replace', name=WIN_TEST_FILE, pattern='test', repl='testing')
|
2018-07-27 15:57:34 +00:00
|
|
|
with salt.utils.files.fopen(WIN_TEST_FILE, 'r') as fp_:
|
2018-07-24 16:42:07 +00:00
|
|
|
self.assertIn('testing', fp_.read())
|
|
|
|
|
|
|
|
def test_file_absent(self):
|
|
|
|
'''
|
|
|
|
Test file.absent on Windows
|
|
|
|
'''
|
2018-07-26 20:04:51 +00:00
|
|
|
ret = self.run_state('file.absent', name=WIN_TEST_FILE)
|
2018-07-24 16:42:07 +00:00
|
|
|
self.assertTrue(ret)
|