"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tests/unit/states/test_file.py" between
salt-3002.1.tar.gz and salt-3002.2.tar.gz

About: SaltStack is a systems management software for data center automation, cloud orchestration, server provisioning, configuration management and more. Community version.

test_file.py  (salt-3002.1):test_file.py  (salt-3002.2)
# -*- coding: utf-8 -*- import collections
from __future__ import absolute_import, print_function, unicode_literals
import logging import logging
import os import os
import plistlib import plistlib
import pprint import pprint
import shutil import shutil
from datetime import datetime from datetime import datetime
import msgpack import msgpack
import salt.modules.file as filemod import salt.modules.file as filemod
import salt.serializers.json as jsonserializer import salt.serializers.json as jsonserializer
skipping to change at line 27 skipping to change at line 24
import salt.serializers.python as pythonserializer import salt.serializers.python as pythonserializer
import salt.serializers.yaml as yamlserializer import salt.serializers.yaml as yamlserializer
import salt.states.file as filestate import salt.states.file as filestate
import salt.utils.files import salt.utils.files
import salt.utils.json import salt.utils.json
import salt.utils.platform import salt.utils.platform
import salt.utils.win_functions import salt.utils.win_functions
import salt.utils.yaml import salt.utils.yaml
from salt.exceptions import CommandExecutionError from salt.exceptions import CommandExecutionError
from salt.ext.six.moves import range from salt.ext.six.moves import range
from tests.support.helpers import destructiveTest, slowTest from tests.support.helpers import dedent, destructiveTest, slowTest, with_tempfi le
from tests.support.mixins import LoaderModuleMockMixin from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, Mock, call, mock_open, patch from tests.support.mock import MagicMock, Mock, call, mock_open, patch
from tests.support.runtests import RUNTIME_VARS from tests.support.runtests import RUNTIME_VARS
from tests.support.unit import TestCase, skipIf from tests.support.unit import TestCase, skipIf
try: try:
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
HAS_DATEUTIL = True HAS_DATEUTIL = True
except ImportError: except ImportError:
skipping to change at line 165 skipping to change at line 162
{"file.manage_file": returner, "config.manage_mode": manage_mode_moc k}, {"file.manage_file": returner, "config.manage_mode": manage_mode_moc k},
): ):
ret = filestate.managed( ret = filestate.managed(
"/tmp/foo", contents="hi", contents_pillar="foo:bar" "/tmp/foo", contents="hi", contents_pillar="foo:bar"
) )
self.assertEqual(False, ret["result"]) self.assertEqual(False, ret["result"])
def test_contents_pillar_doesnt_add_more_newlines(self): def test_contents_pillar_doesnt_add_more_newlines(self):
# make sure the newline # make sure the newline
pillar_value = "i am the pillar value{0}".format(os.linesep) pillar_value = "i am the pillar value{}".format(os.linesep)
self.run_contents_pillar(pillar_value, expected=pillar_value) self.run_contents_pillar(pillar_value, expected=pillar_value)
def run_contents_pillar(self, pillar_value, expected): def run_contents_pillar(self, pillar_value, expected):
returner = MagicMock(return_value=None) returner = MagicMock(return_value=None)
path = "/tmp/foo" path = "/tmp/foo"
pillar_path = "foo:bar" pillar_path = "foo:bar"
# the values don't matter here # the values don't matter here
pillar_mock = MagicMock(return_value=pillar_value) pillar_mock = MagicMock(return_value=pillar_value)
skipping to change at line 251 skipping to change at line 248
filestate.__salt__, filestate.__salt__,
{ {
"config.manage_mode": mock_t, "config.manage_mode": mock_t,
"file.user_to_uid": mock_empty, "file.user_to_uid": mock_empty,
"file.group_to_gid": mock_empty, "file.group_to_gid": mock_empty,
"user.info": mock_empty, "user.info": mock_empty,
"user.current": mock_user, "user.current": mock_user,
}, },
): ):
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "User {0} does not exist".format(user) comt = "User {} does not exist".format(user)
ret = return_val({"comment": comt, "name": name}) ret = return_val({"comment": comt, "name": name})
else: else:
comt = "User {0} does not exist. Group {1} does not exist.".form at( comt = "User {} does not exist. Group {} does not exist.".format (
user, group user, group
) )
ret = return_val({"comment": comt, "name": name}) ret = return_val({"comment": comt, "name": name})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
skipping to change at line 276 skipping to change at line 273
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
"file.group_to_gid": mock_gid, "file.group_to_gid": mock_gid,
"file.is_link": mock_f, "file.is_link": mock_f,
"user.info": mock_empty, "user.info": mock_empty,
"user.current": mock_user, "user.current": mock_user,
}, },
), patch.dict(filestate.__opts__, {"test": True}), patch.object( ), patch.dict(filestate.__opts__, {"test": True}), patch.object(
os.path, "exists", mock_f os.path, "exists", mock_f
): ):
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "User {0} does not exist".format(user) comt = "User {} does not exist".format(user)
ret = return_val( ret = return_val(
{"comment": comt, "result": False, "name": name, "changes": {}} {"comment": comt, "result": False, "name": name, "changes": {}}
) )
else: else:
comt = "Symlink {0} to {1} is set for creation".format(name, tar get) comt = "Symlink {} to {} is set for creation".format(name, targe t)
ret = return_val( ret = return_val(
{"comment": comt, "result": None, "changes": {"new": name}} {"comment": comt, "result": None, "changes": {"new": name}}
) )
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
skipping to change at line 305 skipping to change at line 302
"file.is_link": mock_f, "file.is_link": mock_f,
"user.info": mock_empty, "user.info": mock_empty,
"user.current": mock_user, "user.current": mock_user,
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", mock_f os.path, "isdir", mock_f
), patch.object( ), patch.object(
os.path, "exists", mock_f os.path, "exists", mock_f
): ):
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "User {0} does not exist".format(user) comt = "User {} does not exist".format(user)
ret = return_val( ret = return_val(
{"comment": comt, "result": False, "name": name, "changes": {}} {"comment": comt, "result": False, "name": name, "changes": {}}
) )
else: else:
comt = "Directory {0} for symlink is not present".format(test_di r) comt = "Directory {} for symlink is not present".format(test_dir )
ret = return_val({"comment": comt, "result": False, "changes": { }}) ret = return_val({"comment": comt, "result": False, "changes": { }})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"config.manage_mode": mock_t, "config.manage_mode": mock_t,
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
skipping to change at line 335 skipping to change at line 332
"user.current": mock_user, "user.current": mock_user,
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", mock_t os.path, "isdir", mock_t
), patch.object( ), patch.object(
salt.states.file, "_check_symlink_ownership", mock_t salt.states.file, "_check_symlink_ownership", mock_t
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "Symlink {0} is present and owned by {1}".format(name, us er) comt = "Symlink {} is present and owned by {}".format(name, user )
else: else:
comt = "Symlink {0} is present and owned by {1}:{2}".format( comt = "Symlink {} is present and owned by {}:{}".format(
name, user, group name, user, group
) )
ret = return_val({"comment": comt, "result": True, "changes": {}}) ret = return_val({"comment": comt, "result": True, "changes": {}})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
skipping to change at line 366 skipping to change at line 363
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", mock_t os.path, "isdir", mock_t
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.object( ), patch.object(
os.path, "lexists", mock_t os.path, "lexists", mock_t
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
comt = ( comt = (
"Symlink & backup dest exists and Force not set. {0} -> " "Symlink & backup dest exists and Force not set. {} -> "
"{1} - backup: {2}".format(name, target, os.path.join(test_dir, "{} - backup: {}".format(name, target, os.path.join(test_dir, "S
"SALT")) ALT"))
) )
ret.update({"comment": comt, "result": False, "changes": {}}) ret.update({"comment": comt, "result": False, "changes": {}})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink( filestate.symlink(
name, target, user=user, group=group, backupname="SALT" name, target, user=user, group=group, backupname="SALT"
), ),
ret, ret,
) )
with patch.dict( with patch.dict(
skipping to change at line 397 skipping to change at line 394
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_t os.path, "isfile", mock_t
), patch.object( ), patch.object(
os.path, "isdir", mock_t os.path, "isdir", mock_t
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
comt = "Backupname must be an absolute path or a file name: {0}".for mat( comt = "Backupname must be an absolute path or a file name: {}".form at(
"tmp/SALT" "tmp/SALT"
) )
ret.update({"comment": comt, "result": False, "changes": {}}) ret.update({"comment": comt, "result": False, "changes": {}})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink( filestate.symlink(
name, target, user=user, group=group, backupname="tmp/SALT" name, target, user=user, group=group, backupname="tmp/SALT"
), ),
ret, ret,
) )
skipping to change at line 428 skipping to change at line 425
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", mock_t os.path, "isdir", mock_t
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_t os.path, "isfile", mock_t
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
comt = "File exists where the symlink {0} should be".format(name) comt = "File exists where the symlink {} should be".format(name)
ret = return_val({"comment": comt, "changes": {}, "result": False}) ret = return_val({"comment": comt, "changes": {}, "result": False})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"config.manage_mode": mock_t, "config.manage_mode": mock_t,
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
skipping to change at line 455 skipping to change at line 452
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", MagicMock(side_effect=[True, False]) os.path, "isdir", MagicMock(side_effect=[True, False])
), patch.object( ), patch.object(
os.path, "isdir", mock_t os.path, "isdir", mock_t
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
comt = "Directory exists where the symlink {0} should be".format(nam e) comt = "Directory exists where the symlink {} should be".format(name )
ret = return_val({"comment": comt, "result": False, "changes": {}}) ret = return_val({"comment": comt, "result": False, "changes": {}})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"config.manage_mode": mock_t, "config.manage_mode": mock_t,
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
skipping to change at line 480 skipping to change at line 477
"user.info": mock_t, "user.info": mock_t,
"file.lchown": mock_f, "file.lchown": mock_f,
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", MagicMock(side_effect=[True, False]) os.path, "isdir", MagicMock(side_effect=[True, False])
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
comt = "Unable to create new symlink {0} -> {1}: ".format(name, targ et) comt = "Unable to create new symlink {} -> {}: ".format(name, target )
ret = return_val({"comment": comt, "result": False, "changes": {}}) ret = return_val({"comment": comt, "result": False, "changes": {}})
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"config.manage_mode": mock_t, "config.manage_mode": mock_t,
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
skipping to change at line 509 skipping to change at line 506
}, },
), patch.dict(filestate.__opts__, {"test": False}), patch.object( ), patch.dict(filestate.__opts__, {"test": False}), patch.object(
os.path, "isdir", MagicMock(side_effect=[True, False]) os.path, "isdir", MagicMock(side_effect=[True, False])
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
), patch( ), patch(
"salt.states.file._check_symlink_ownership", return_value=True "salt.states.file._check_symlink_ownership", return_value=True
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
): ):
comt = "Created new symlink {0} -> {1}".format(name, target) comt = "Created new symlink {} -> {}".format(name, target)
ret = return_val( ret = return_val(
{"comment": comt, "result": True, "changes": {"new": name}} {"comment": comt, "result": True, "changes": {"new": name}}
) )
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
skipping to change at line 543 skipping to change at line 540
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
), patch( ), patch(
"salt.utils.win_functions.get_sid_from_name", return_value="test-sid " "salt.utils.win_functions.get_sid_from_name", return_value="test-sid "
), patch( ), patch(
"salt.states.file._set_symlink_ownership", return_value=False "salt.states.file._set_symlink_ownership", return_value=False
), patch( ), patch(
"salt.states.file._check_symlink_ownership", return_value=False "salt.states.file._check_symlink_ownership", return_value=False
): ):
comt = ( comt = (
"Created new symlink {0} -> {1}, but was unable to set " "Created new symlink {} -> {}, but was unable to set "
"ownership to {2}:{3}".format(name, target, user, group) "ownership to {}:{}".format(name, target, user, group)
) )
ret = return_val( ret = return_val(
{"comment": comt, "result": False, "changes": {"new": name}} {"comment": comt, "result": False, "changes": {"new": name}}
) )
self.assertDictEqual( self.assertDictEqual(
filestate.symlink(name, target, user=user, group=group), ret filestate.symlink(name, target, user=user, group=group), ret
) )
@skipIf(salt.utils.platform.is_windows(), "Do not run on Windows") @skipIf(salt.utils.platform.is_windows(), "Do not run on Windows")
def test_hardlink(self): def test_hardlink(self):
skipping to change at line 604 skipping to change at line 601
self.assertDictEqual(filestate.hardlink("", target), ret) self.assertDictEqual(filestate.hardlink("", target), ret)
# User validation for dir_mode # User validation for dir_mode
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_empty} filestate.__salt__, {"file.user_to_uid": mock_empty}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.group_to_gid": mock_gid} filestate.__salt__, {"file.group_to_gid": mock_gid}
), patch.object( ), patch.object(
os.path, "isabs", mock_t os.path, "isabs", mock_t
): ):
expected = "User {0} does not exist".format(user) expected = "User {} does not exist".format(user)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Group validation for dir_mode # Group validation for dir_mode
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.group_to_gid": mock_empty} filestate.__salt__, {"file.group_to_gid": mock_empty}
), patch.object( ), patch.object(
os.path, "isabs", mock_t os.path, "isabs", mock_t
): ):
expected = "Group {0} does not exist".format(group) expected = "Group {} does not exist".format(group)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Absolute path for name # Absolute path for name
nonabs = "./non-existent-path/to/non-existent-file" nonabs = "./non-existent-path/to/non-existent-file"
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}): ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}):
expected = "Specified file {0} is not an absolute path".format(nonab s) expected = "Specified file {} is not an absolute path".format(nonabs )
ret = return_val(comment=expected, name=nonabs) ret = return_val(comment=expected, name=nonabs)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(nonabs, target, user=user, group=group), ret filestate.hardlink(nonabs, target, user=user, group=group), ret
) )
# Absolute path for target # Absolute path for target
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}): ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}):
expected = "Specified target {0} is not an absolute path".format(non abs) expected = "Specified target {} is not an absolute path".format(nona bs)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, nonabs, user=user, group=group), ret filestate.hardlink(name, nonabs, user=user, group=group), ret
) )
# Test option -- nonexistent target # Test option -- nonexistent target
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.group_to_gid": mock_gid} filestate.__salt__, {"file.group_to_gid": mock_gid}
), patch.object( ), patch.object(
os.path, "exists", mock_f os.path, "exists", mock_f
), patch.dict( ), patch.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = "Target {0} for hard link does not exist".format(target) expected = "Target {} for hard link does not exist".format(target)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Test option -- target is a directory # Test option -- target is a directory
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.group_to_gid": mock_gid} filestate.__salt__, {"file.group_to_gid": mock_gid}
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.dict( ), patch.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = "Unable to hard link from directory {0}".format(test_dir) expected = "Unable to hard link from directory {}".format(test_dir)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, test_dir, user=user, group=group), ret filestate.hardlink(name, test_dir, user=user, group=group), ret
) )
# Test option -- name is a directory # Test option -- name is a directory
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = "Unable to hard link to directory {0}".format(test_dir) expected = "Unable to hard link to directory {}".format(test_dir)
ret = return_val(comment=expected, name=test_dir) ret = return_val(comment=expected, name=test_dir)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(test_dir, target, user=user, group=group), re t filestate.hardlink(test_dir, target, user=user, group=group), re t
) )
# Test option -- name does not exist # Test option -- name does not exist
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = "Hard link {0} to {1} is set for creation".format(name, t arget) expected = "Hard link {} to {} is set for creation".format(name, tar get)
changes = dict(new=name) changes = dict(new=name)
ret = return_val(result=None, comment=expected, name=name, changes=c hanges) ret = return_val(result=None, comment=expected, name=name, changes=c hanges)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Test option -- hardlink matches # Test option -- hardlink matches
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__salt__, {"file.is_hardlink": mock_t} filestate.__salt__, {"file.is_hardlink": mock_t}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_stats} filestate.__salt__, {"file.stats": mock_stats}
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.dict( ), patch.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = "The hard link {0} is presently targetting {1}".format( expected = "The hard link {} is presently targetting {}".format(
name, target name, target
) )
ret = return_val(result=True, comment=expected, name=name) ret = return_val(result=True, comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Test option -- hardlink does not match # Test option -- hardlink does not match
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__salt__, {"file.is_hardlink": mock_t} filestate.__salt__, {"file.is_hardlink": mock_t}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.dict( ), patch.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = "Link {0} target is set to be changed to {1}".format( expected = "Link {} target is set to be changed to {}".format(name,
name, target target)
)
changes = dict(change=name) changes = dict(change=name)
ret = return_val(result=None, comment=expected, name=name, changes=c hanges) ret = return_val(result=None, comment=expected, name=name, changes=c hanges)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Test option -- force removal # Test option -- force removal
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__salt__, {"file.is_hardlink": mock_f} filestate.__salt__, {"file.is_hardlink": mock_f}
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.dict( ), patch.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = ( expected = (
"The file or directory {0} is set for removal to " "The file or directory {} is set for removal to "
"make way for a new hard link targeting {1}".format(name, target "make way for a new hard link targeting {}".format(name, target)
)
) )
ret = return_val(result=None, comment=expected, name=name) ret = return_val(result=None, comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, force=True, user=user, group=gr oup), filestate.hardlink(name, target, force=True, user=user, group=gr oup),
ret, ret,
) )
# Test option -- without force removal # Test option -- without force removal
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__salt__, {"file.is_hardlink": mock_f} filestate.__salt__, {"file.is_hardlink": mock_f}
), patch.object( ), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
), patch.dict( ), patch.dict(
filestate.__opts__, {"test": True} filestate.__opts__, {"test": True}
): ):
expected = ( expected = (
"File or directory exists where the hard link {0} " "File or directory exists where the hard link {} "
"should be. Did you mean to use force?".format(name) "should be. Did you mean to use force?".format(name)
) )
ret = return_val(result=False, comment=expected, name=name) ret = return_val(result=False, comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, force=False, user=user, group=g roup), filestate.hardlink(name, target, force=False, user=user, group=g roup),
ret, ret,
) )
# Target is a directory # Target is a directory
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}): ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}):
expected = "Unable to hard link from directory {0}".format(test_dir) expected = "Unable to hard link from directory {}".format(test_dir)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, test_dir, user=user, group=group), ret filestate.hardlink(name, test_dir, user=user, group=group), ret
) )
# Name is a directory # Name is a directory
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}): ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}):
expected = "Unable to hard link to directory {0}".format(test_dir) expected = "Unable to hard link to directory {}".format(test_dir)
ret = return_val(comment=expected, name=test_dir) ret = return_val(comment=expected, name=test_dir)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(test_dir, target, user=user, group=group), re t filestate.hardlink(test_dir, target, user=user, group=group), re t
) )
# Try overwrite file with link # Try overwrite file with link
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__salt__, {"file.is_hardlink": mock_f} filestate.__salt__, {"file.is_hardlink": mock_f}
), patch.object( ), patch.object(
os.path, "isfile", mock_t os.path, "isfile", mock_t
): ):
expected = "File exists where the hard link {0} should be".format(na me) expected = "File exists where the hard link {} should be".format(nam e)
ret = return_val(comment=expected, name=name) ret = return_val(comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Try overwrite link with same # Try overwrite link with same
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
filestate.__salt__, {"file.is_hardlink": mock_t} filestate.__salt__, {"file.is_hardlink": mock_t}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_stats} filestate.__salt__, {"file.stats": mock_stats}
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
): ):
expected = "Target of hard link {0} is already pointing " "to {1}".f ormat( expected = "Target of hard link {} is already pointing " "to {}".for mat(
name, target name, target
) )
ret = return_val(result=True, comment=expected, name=name) ret = return_val(result=True, comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Really overwrite link with same # Really overwrite link with same
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
skipping to change at line 851 skipping to change at line 846
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.link": mock_t} filestate.__salt__, {"file.link": mock_t}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os, "remove", mock_t os, "remove", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
): ):
expected = "Set target of hard link {0} -> {1}".format(name, target) expected = "Set target of hard link {} -> {}".format(name, target)
changes = dict(new=name) changes = dict(new=name)
ret = return_val(result=True, comment=expected, name=name, changes=c hanges) ret = return_val(result=True, comment=expected, name=name, changes=c hanges)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Fail at overwriting link with same # Fail at overwriting link with same
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
skipping to change at line 873 skipping to change at line 868
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.link": mock_execerror} filestate.__salt__, {"file.link": mock_execerror}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os, "remove", mock_t os, "remove", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
): ):
expected = "Unable to set target of hard link {0} -> " "{1}: {2}".fo rmat( expected = "Unable to set target of hard link {} -> " "{}: {}".forma t(
name, target, "" name, target, ""
) )
ret = return_val(result=False, comment=expected, name=name) ret = return_val(result=False, comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Make new link # Make new link
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
skipping to change at line 896 skipping to change at line 891
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.link": mock_f} filestate.__salt__, {"file.link": mock_f}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os, "remove", mock_t os, "remove", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
): ):
expected = "Created new hard link {0} -> {1}".format(name, target) expected = "Created new hard link {} -> {}".format(name, target)
changes = dict(new=name) changes = dict(new=name)
ret = return_val(result=True, comment=expected, name=name, changes=c hanges) ret = return_val(result=True, comment=expected, name=name, changes=c hanges)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Fail while making new link # Fail while making new link
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict( ), patch.dict(filestate.__salt__, {"file.group_to_gid": mock_gid}), patc h.dict(
skipping to change at line 918 skipping to change at line 913
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.link": mock_execerror} filestate.__salt__, {"file.link": mock_execerror}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os, "remove", mock_t os, "remove", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_f os.path, "isfile", mock_f
): ):
expected = "Unable to create new hard link {0} -> " "{1}: {2}".forma t( expected = "Unable to create new hard link {} -> " "{}: {}".format(
name, target, "" name, target, ""
) )
ret = return_val(result=False, comment=expected, name=name) ret = return_val(result=False, comment=expected, name=name)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, group=group), ret filestate.hardlink(name, target, user=user, group=group), ret
) )
# Force making new link over file # Force making new link over file
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
filestate.__salt__, {"file.user_to_uid": mock_uid} filestate.__salt__, {"file.user_to_uid": mock_uid}
skipping to change at line 941 skipping to change at line 936
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.link": mock_t} filestate.__salt__, {"file.link": mock_t}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os, "remove", mock_t os, "remove", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_t os.path, "isfile", mock_t
): ):
expected = "Created new hard link {0} -> {1}".format(name, target) expected = "Created new hard link {} -> {}".format(name, target)
changes = dict(new=name) changes = dict(new=name)
changes["forced"] = "File for hard link was forcibly replaced" changes["forced"] = "File for hard link was forcibly replaced"
ret = return_val(result=True, comment=expected, name=name, changes=c hanges) ret = return_val(result=True, comment=expected, name=name, changes=c hanges)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, force=True, group=gr oup), filestate.hardlink(name, target, user=user, force=True, group=gr oup),
ret, ret,
) )
# Force making new link over file but error out # Force making new link over file but error out
with patch.dict(filestate.__salt__, patches), patch.dict( with patch.dict(filestate.__salt__, patches), patch.dict(
skipping to change at line 965 skipping to change at line 960
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.link": mock_execerror} filestate.__salt__, {"file.link": mock_execerror}
), patch.dict( ), patch.dict(
filestate.__salt__, {"file.stats": mock_nothing} filestate.__salt__, {"file.stats": mock_nothing}
), patch.object( ), patch.object(
os, "remove", mock_t os, "remove", mock_t
), patch.object( ), patch.object(
os.path, "isfile", mock_t os.path, "isfile", mock_t
): ):
expected = "Unable to create new hard link {0} -> " "{1}: {2}".forma t( expected = "Unable to create new hard link {} -> " "{}: {}".format(
name, target, "" name, target, ""
) )
changes = dict(forced="File for hard link was forcibly replaced") changes = dict(forced="File for hard link was forcibly replaced")
ret = return_val(result=False, comment=expected, name=name, changes= changes) ret = return_val(result=False, comment=expected, name=name, changes= changes)
self.assertDictEqual( self.assertDictEqual(
filestate.hardlink(name, target, user=user, force=True, group=gr oup), filestate.hardlink(name, target, user=user, force=True, group=gr oup),
ret, ret,
) )
# 'absent' function tests: 1 # 'absent' function tests: 1
skipping to change at line 996 skipping to change at line 991
mock_file = MagicMock(side_effect=[True, CommandExecutionError]) mock_file = MagicMock(side_effect=[True, CommandExecutionError])
mock_tree = MagicMock(side_effect=[True, OSError]) mock_tree = MagicMock(side_effect=[True, OSError])
comt = "Must provide name to file.absent" comt = "Must provide name to file.absent"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
with patch.object(os.path, "islink", MagicMock(return_value=False)): with patch.object(os.path, "islink", MagicMock(return_value=False)):
self.assertDictEqual(filestate.absent(""), ret) self.assertDictEqual(filestate.absent(""), ret)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
comt = 'Refusing to make "/" absent' comt = 'Refusing to make "/" absent'
ret.update({"comment": comt, "name": "/"}) ret.update({"comment": comt, "name": "/"})
self.assertDictEqual(filestate.absent("/"), ret) self.assertDictEqual(filestate.absent("/"), ret)
with patch.object(os.path, "isfile", mock_t): with patch.object(os.path, "isfile", mock_t):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "File {0} is set for removal".format(name) comt = "File {} is set for removal".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"name": name, "name": name,
"result": None, "result": None,
"changes": {"removed": "/fake/file.conf"}, "changes": {"removed": "/fake/file.conf"},
} }
) )
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.dict(filestate.__salt__, {"file.remove": mock_fil e}): with patch.dict(filestate.__salt__, {"file.remove": mock_fil e}):
comt = "Removed file {0}".format(name) comt = "Removed file {}".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"result": True, "result": True,
"changes": {"removed": name}, "changes": {"removed": name},
} }
) )
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
comt = "Removed file {0}".format(name) comt = "Removed file {}".format(name)
ret.update({"comment": "", "result": False, "changes": { }}) ret.update({"comment": "", "result": False, "changes": { }})
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
with patch.object(os.path, "isfile", mock_f): with patch.object(os.path, "isfile", mock_f):
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "Directory {0} is set for removal".format(name) comt = "Directory {} is set for removal".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"changes": {"removed": name}, "changes": {"removed": name},
"result": None, "result": None,
} }
) )
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.dict(filestate.__salt__, {"file.remove": mock _tree}): with patch.dict(filestate.__salt__, {"file.remove": mock _tree}):
comt = "Removed directory {0}".format(name) comt = "Removed directory {}".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"result": True, "result": True,
"changes": {"removed": name}, "changes": {"removed": name},
} }
) )
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
comt = "Failed to remove directory {0}".format(name) comt = "Failed to remove directory {}".format(name)
ret.update( ret.update(
{"comment": comt, "result": False, "changes": {} } {"comment": comt, "result": False, "changes": {} }
) )
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "File {0} is not present".format(name) comt = "File {} is not present".format(name)
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.absent(name), ret) self.assertDictEqual(filestate.absent(name), ret)
# 'exists' function tests: 1 # 'exists' function tests: 1
def test_exists(self): def test_exists(self):
""" """
Test to verify that the named file or directory is present or exists. Test to verify that the named file or directory is present or exists.
""" """
name = "/etc/grub.conf" name = "/etc/grub.conf"
skipping to change at line 1089 skipping to change at line 1084
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
comt = "Must provide name to file.exists" comt = "Must provide name to file.exists"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.exists(""), ret) self.assertDictEqual(filestate.exists(""), ret)
with patch.object(os.path, "exists", mock_f): with patch.object(os.path, "exists", mock_f):
comt = "Specified path {0} does not exist".format(name) comt = "Specified path {} does not exist".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.exists(name), ret) self.assertDictEqual(filestate.exists(name), ret)
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
comt = "Path {0} exists".format(name) comt = "Path {} exists".format(name)
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.exists(name), ret) self.assertDictEqual(filestate.exists(name), ret)
# 'missing' function tests: 1 # 'missing' function tests: 1
def test_missing(self): def test_missing(self):
""" """
Test to verify that the named file or directory is missing. Test to verify that the named file or directory is missing.
""" """
name = "/etc/grub.conf" name = "/etc/grub.conf"
skipping to change at line 1116 skipping to change at line 1111
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
comt = "Must provide name to file.missing" comt = "Must provide name to file.missing"
ret.update({"comment": comt, "name": "", "changes": {}}) ret.update({"comment": comt, "name": "", "changes": {}})
self.assertDictEqual(filestate.missing(""), ret) self.assertDictEqual(filestate.missing(""), ret)
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
comt = "Specified path {0} exists".format(name) comt = "Specified path {} exists".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.missing(name), ret) self.assertDictEqual(filestate.missing(name), ret)
with patch.object(os.path, "exists", mock_f): with patch.object(os.path, "exists", mock_f):
comt = "Path {0} is missing".format(name) comt = "Path {} is missing".format(name)
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.missing(name), ret) self.assertDictEqual(filestate.missing(name), ret)
# 'managed' function tests: 1 # 'managed' function tests: 1
def test_file_managed_should_fall_back_to_binary(self): def test_file_managed_should_fall_back_to_binary(self):
expected_contents = b"\x8b" expected_contents = b"\x8b"
filename = "/tmp/blarg" filename = "/tmp/blarg"
mock_manage = MagicMock(return_value={"fnord": "fnords"}) mock_manage = MagicMock(return_value={"fnord": "fnords"})
with patch( with patch(
skipping to change at line 1265 skipping to change at line 1260
"file.manage_file": mock_ex, "file.manage_file": mock_ex,
"cmd.run_all": mock_cmd_fail, "cmd.run_all": mock_cmd_fail,
}, },
): ):
comt = "Destination file name is required" comt = "Destination file name is required"
ret.update({"comment": comt, "name": "", "changes": {}}) ret.update({"comment": comt, "name": "", "changes": {}})
self.assertDictEqual(filestate.managed(""), ret) self.assertDictEqual(filestate.managed(""), ret)
with patch.object(os.path, "isfile", mock_f): with patch.object(os.path, "isfile", mock_f):
comt = ( comt = (
"File {0} is not present and is not set for " "File {} is not present and is not set for "
"creation".format(name) "creation".format(name)
) )
ret.update({"comment": comt, "name": name, "result": True}) ret.update({"comment": comt, "name": name, "result": True})
self.assertDictEqual(filestate.managed(name, create=False), ret) self.assertDictEqual(filestate.managed(name, create=False), ret)
# Group argument is ignored on Windows systems. Group is set to # Group argument is ignored on Windows systems. Group is set to
# user # user
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "User salt is not available Group salt" " is not avai lable" comt = "User salt is not available Group salt" " is not avai lable"
else: else:
comt = ( comt = (
"User salt is not available Group saltstack" " is not av ailable" "User salt is not available Group saltstack" " is not av ailable"
) )
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual( self.assertDictEqual(
filestate.managed(name, user=user, group=group), ret filestate.managed(name, user=user, group=group), ret
) )
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(n ame) comt = "Specified file {} is not an absolute path".format(na me)
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual( self.assertDictEqual(
filestate.managed(name, user=user, group=group), ret filestate.managed(name, user=user, group=group), ret
) )
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
comt = "Specified target {0} is a directory".format(name ) comt = "Specified target {} is a directory".format(name)
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.managed(name, user=user, group=group), ret filestate.managed(name, user=user, group=group), ret
) )
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
comt = "Context must be formed as a dict" comt = "Context must be formed as a dict"
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.managed( filestate.managed(
skipping to change at line 1337 skipping to change at line 1332
group=group, group=group,
contents="A", contents="A",
contents_grains="B", contents_grains="B",
contents_pillar="C", contents_pillar="C",
), ),
ret, ret,
) )
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "File {0} not updated".format(name) comt = "File {} not updated".format(name)
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.managed( filestate.managed(
name, user=user, group=group, replace=Fa lse name, user=user, group=group, replace=Fa lse
), ),
ret, ret,
) )
comt = "The file {0} is in the correct state".fo rmat( comt = "The file {} is in the correct state".for mat(
name name
) )
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual( self.assertDictEqual(
filestate.managed( filestate.managed(
name, user=user, contents="A", group=gro up name, user=user, contents="A", group=gro up
), ),
ret, ret,
) )
skipping to change at line 1434 skipping to change at line 1429
) )
comt = "Unable to manage file: " comt = "Unable to manage file: "
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.managed(name, user=user, group=gro up), ret filestate.managed(name, user=user, group=gro up), ret
) )
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
mock_ret = MagicMock(return_value=ret) mock_ret = MagicMock(return_value=ret)
comt = "File {0} not updated".format(name) comt = "File {} not updated".format(name)
else: else:
perms = {"luser": user, "lmode": "0644", "lgroup": g roup} perms = {"luser": user, "lmode": "0644", "lgroup": g roup}
mock_ret = MagicMock(return_value=(ret, perms)) mock_ret = MagicMock(return_value=(ret, perms))
comt = ( comt = (
"File {0} will be updated with " "File {} will be updated with "
"permissions 0400 from its current " "permissions 0400 from its current "
"state of 0644".format(name) "state of 0644".format(name)
) )
with patch.dict( with patch.dict(
filestate.__salt__, {"file.check_perms": mock_ret} filestate.__salt__, {"file.check_perms": mock_ret}
): ):
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
with patch.dict(filestate.__opts__, {"test": Tru e}): with patch.dict(filestate.__opts__, {"test": Tru e}):
ret.update({"comment": comt}) ret.update({"comment": comt})
skipping to change at line 1528 skipping to change at line 1523
"G12", "G12",
"G12", "G12",
"G12", "G12",
"G12", "G12",
"G12", "G12",
] ]
) )
mock_check = MagicMock( mock_check = MagicMock(
return_value=( return_value=(
None, None,
'The directory "{0}" will be changed'.format(name), 'The directory "{}" will be changed'.format(name),
{name: {"directory": "new"}}, {name: {"directory": "new"}},
) )
) )
mock_error = CommandExecutionError mock_error = CommandExecutionError
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"config.manage_mode": mock_t, "config.manage_mode": mock_t,
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
"file.group_to_gid": mock_gid, "file.group_to_gid": mock_gid,
skipping to change at line 1556 skipping to change at line 1551
"salt.states.file._check_directory_win", mock_check "salt.states.file._check_directory_win", mock_check
): ):
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "" comt = ""
else: else:
comt = "User salt is not available Group saltstack" " is not ava ilable" comt = "User salt is not available Group saltstack" " is not ava ilable"
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.directory(name, user=user, group=grou p), ret) self.assertDictEqual(filestate.directory(name, user=user, group=grou p), ret)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.directory(name, user=user, group=group), ret filestate.directory(name, user=user, group=group), ret
) )
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object( with patch.object(
os.path, os.path,
"isfile", "isfile",
MagicMock(side_effect=[True, True, False, True, True, True, False]), MagicMock(side_effect=[True, True, False, True, True, True, False]),
skipping to change at line 1579 skipping to change at line 1574
comt = "File exists where the backup target" " A should go" comt = "File exists where the backup target" " A should go"
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.directory( filestate.directory(
name, user=user, group=group, backupname="A" name, user=user, group=group, backupname="A"
), ),
ret, ret,
) )
with patch.object(os.path, "isfile", mock_t): with patch.object(os.path, "isfile", mock_t):
comt = "Specified location {0} exists and is a file".for comt = "Specified location {} exists and is a file".form
mat( at(name)
name
)
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.directory(name, user=user, group=group), r et filestate.directory(name, user=user, group=group), r et
) )
with patch.object(os.path, "islink", mock_t): with patch.object(os.path, "islink", mock_t):
comt = "Specified location {0} exists and is a symlink". format( comt = "Specified location {} exists and is a symlink".f ormat(
name name
) )
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.directory(name, user=user, group=group), r et filestate.directory(name, user=user, group=group), r et
) )
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = 'The directory "{0}" will be changed' "".form at(name) comt = 'The directory "{}" will be changed' "".forma t(name)
else: else:
comt = ( comt = (
"The following files will be changed:\n{0}:" "The following files will be changed:\n{}:"
" directory - new\n".format(name) " directory - new\n".format(name)
) )
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"result": None, "result": None,
"changes": {name: {"directory": "new"}}, "changes": {name: {"directory": "new"}},
} }
) )
self.assertDictEqual( self.assertDictEqual(
filestate.directory(name, user=user, group=group), r et filestate.directory(name, user=user, group=group), r et
) )
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
comt = "No directory to create {0} in".format(name) comt = "No directory to create {} in".format(name)
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual( self.assertDictEqual(
filestate.directory(name, user=user, group=group ), ret filestate.directory(name, user=user, group=group ), ret
) )
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
isdir_side_effect = [False, True, False] isdir_side_effect = [False, True, False]
else: else:
isdir_side_effect = [True, False, True, False] isdir_side_effect = [True, False, True, False]
with patch.object( with patch.object(
os.path, "isdir", MagicMock(side_effect=isdir_side_e ffect) os.path, "isdir", MagicMock(side_effect=isdir_side_e ffect)
): ):
comt = "Failed to create directory {0}".format(name) comt = "Failed to create directory {}".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"result": False, "result": False,
"changes": {name: "New Dir"}, "changes": {name: "New Dir"},
} }
) )
self.assertDictEqual( self.assertDictEqual(
filestate.directory(name, user=user, group=group ), ret filestate.directory(name, user=user, group=group ), ret
) )
skipping to change at line 1706 skipping to change at line 1699
filestate.__salt__, {"file.check_perms": mock_perms} filestate.__salt__, {"file.check_perms": mock_perms}
): ):
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
self.assertDictEqual( self.assertDictEqual(
filestate.directory( filestate.directory(
name, user=user, recurse=recurse, group= group name, user=user, recurse=recurse, group= group
), ),
ret, ret,
) )
comt = "Directory {0} updated".format(name) comt = "Directory {} updated".format(name)
ret = { ret = {
"name": name, "name": name,
"result": True, "result": True,
"comment": comt, "comment": comt,
"changes": { "changes": {
"group": "group", "group": "group",
"mode": "0777", "mode": "0777",
"user": "user", "user": "user",
}, },
} }
skipping to change at line 1802 skipping to change at line 1795
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = "User salt is not available Group salt" " is not availabl e" comt = "User salt is not available Group salt" " is not availabl e"
else: else:
comt = "User salt is not available Group saltstack" " is not ava ilable" comt = "User salt is not available Group saltstack" " is not ava ilable"
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual( self.assertDictEqual(
filestate.recurse(name, source, user=user, group=group), ret filestate.recurse(name, source, user=user, group=group), ret
) )
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual(filestate.recurse(name, source), ret) self.assertDictEqual(filestate.recurse(name, source), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
comt = "Invalid source '1' (must be a salt:// URI)" comt = "Invalid source '1' (must be a salt:// URI)"
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual(filestate.recurse(name, 1), ret) self.assertDictEqual(filestate.recurse(name, 1), ret)
comt = "Invalid source '//code/flask' (must be a salt:// URI)" comt = "Invalid source '//code/flask' (must be a salt:// URI)"
ret.update({"comment": comt}) ret.update({"comment": comt})
skipping to change at line 1828 skipping to change at line 1821
comt = ( comt = (
"The directory 'code/flask' does not exist" "The directory 'code/flask' does not exist"
" on the salt fileserver in saltenv 'base'" " on the salt fileserver in saltenv 'base'"
) )
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual(filestate.recurse(name, source), ret) self.assertDictEqual(filestate.recurse(name, source), ret)
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
comt = "The path {0} exists and is not a directory".form at(name) comt = "The path {} exists and is not a directory".forma t(name)
ret.update({"comment": comt}) ret.update({"comment": comt})
self.assertDictEqual(filestate.recurse(name, source), re t) self.assertDictEqual(filestate.recurse(name, source), re t)
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
comt = "The directory {0} is in the correct state".format(na me) comt = "The directory {} is in the correct state".format(nam e)
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.recurse(name, source), ret) self.assertDictEqual(filestate.recurse(name, source), ret)
# 'replace' function tests: 1 # 'replace' function tests: 1
def test_replace(self): def test_replace(self):
""" """
Test to maintain an edit in a file. Test to maintain an edit in a file.
""" """
name = "/etc/grub.conf" name = "/etc/grub.conf"
skipping to change at line 1856 skipping to change at line 1849
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
comt = "Must provide name to file.replace" comt = "Must provide name to file.replace"
ret.update({"comment": comt, "name": "", "changes": {}}) ret.update({"comment": comt, "name": "", "changes": {}})
self.assertDictEqual(filestate.replace("", pattern, repl), ret) self.assertDictEqual(filestate.replace("", pattern, repl), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.replace(name, pattern, repl), ret) self.assertDictEqual(filestate.replace(name, pattern, repl), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
with patch.dict(filestate.__salt__, {"file.replace": mock_f}): with patch.dict(filestate.__salt__, {"file.replace": mock_f}):
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
comt = "No changes needed to be made" comt = "No changes needed to be made"
ret.update({"comment": comt, "name": name, "result": Tru e}) ret.update({"comment": comt, "name": name, "result": Tru e})
self.assertDictEqual( self.assertDictEqual(
skipping to change at line 1891 skipping to change at line 1884
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
comt = "Must provide name to file.blockreplace" comt = "Must provide name to file.blockreplace"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.blockreplace(""), ret) self.assertDictEqual(filestate.blockreplace(""), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.blockreplace(name), ret) self.assertDictEqual(filestate.blockreplace(name), ret)
with patch.object(os.path, "isabs", mock_t), patch.object( with patch.object(os.path, "isabs", mock_t), patch.object(
os.path, "exists", mock_t os.path, "exists", mock_t
): ):
with patch.dict(filestate.__salt__, {"file.blockreplace": mock_t }): with patch.dict(filestate.__salt__, {"file.blockreplace": mock_t }):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "Changes would be made" comt = "Changes would be made"
ret.update( ret.update(
skipping to change at line 1925 skipping to change at line 1918
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
comt = "Must provide name to file.comment" comt = "Must provide name to file.comment"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.comment("", regex), ret) self.assertDictEqual(filestate.comment("", regex), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.comment(name, regex), ret) self.assertDictEqual(filestate.comment(name, regex), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{"file.search": MagicMock(side_effect=[False, True, False, F alse])}, {"file.search": MagicMock(side_effect=[False, True, False, F alse])},
): ):
comt = "Pattern already commented" comt = "Pattern already commented"
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.comment(name, regex), ret) self.assertDictEqual(filestate.comment(name, regex), ret)
comt = "{0}: Pattern not found".format(regex) comt = "{}: Pattern not found".format(regex)
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual(filestate.comment(name, regex), ret) self.assertDictEqual(filestate.comment(name, regex), ret)
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"file.search": MagicMock(side_effect=[True, True, True]) , "file.search": MagicMock(side_effect=[True, True, True]) ,
"file.comment": mock_t, "file.comment": mock_t,
"file.comment_line": mock_t, "file.comment_line": mock_t,
}, },
): ):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "File {0} is set to be updated".format(name) comt = "File {} is set to be updated".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"result": None, "result": None,
"changes": {name: "updated"}, "changes": {name: "updated"},
} }
) )
self.assertDictEqual(filestate.comment(name, regex), ret ) self.assertDictEqual(filestate.comment(name, regex), ret )
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
skipping to change at line 1989 skipping to change at line 1982
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
comt = "Must provide name to file.uncomment" comt = "Must provide name to file.uncomment"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.uncomment("", regex), ret) self.assertDictEqual(filestate.uncomment("", regex), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
mock = MagicMock(side_effect=[False, True, False, False, True, True, True]) mock = MagicMock(side_effect=[False, True, False, False, True, True, True])
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.uncomment(name, regex), ret) self.assertDictEqual(filestate.uncomment(name, regex), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"file.search": mock, "file.search": mock,
"file.uncomment": mock_t, "file.uncomment": mock_t,
"file.comment_line": mock_t, "file.comment_line": mock_t,
}, },
): ):
comt = "Pattern already uncommented" comt = "Pattern already uncommented"
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.uncomment(name, regex), ret) self.assertDictEqual(filestate.uncomment(name, regex), ret)
comt = "{0}: Pattern not found".format(regex) comt = "{}: Pattern not found".format(regex)
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual(filestate.uncomment(name, regex), ret) self.assertDictEqual(filestate.uncomment(name, regex), ret)
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "File {0} is set to be updated".format(name) comt = "File {} is set to be updated".format(name)
ret.update( ret.update(
{ {
"comment": comt, "comment": comt,
"result": None, "result": None,
"changes": {name: "updated"}, "changes": {name: "updated"},
} }
) )
self.assertDictEqual(filestate.uncomment(name, regex), r et) self.assertDictEqual(filestate.uncomment(name, regex), r et)
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
skipping to change at line 2079 skipping to change at line 2072
"The following files will be changed:\n/tmp/etc:" " directory - new\n" "The following files will be changed:\n/tmp/etc:" " directory - new\n"
) )
changes = {"/tmp/etc": {"directory": "new"}} changes = {"/tmp/etc": {"directory": "new"}}
if salt.utils.platform.is_windows(): if salt.utils.platform.is_windows():
comt = 'The directory "c:\\tmp\\etc" will be changed' comt = 'The directory "c:\\tmp\\etc" will be changed'
changes = {"c:\\tmp\\etc": {"directory": "new"}} changes = {"c:\\tmp\\etc": {"directory": "new"}}
ret.update({"comment": comt, "name": name, "changes": changes}) ret.update({"comment": comt, "name": name, "changes": changes})
self.assertDictEqual(filestate.prepend(name, makedirs=True), ret) self.assertDictEqual(filestate.prepend(name, makedirs=True), ret)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "changes": {}}) ret.update({"comment": comt, "changes": {}})
self.assertDictEqual(filestate.prepend(name), ret) self.assertDictEqual(filestate.prepend(name), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
comt = "Failed to load template file {0}".format(source) comt = "Failed to load template file {}".format(source)
ret.update({"comment": comt, "name": source, "data": []}) ret.update({"comment": comt, "name": source, "data": []})
self.assertDictEqual(filestate.prepend(name, source=source), ret) self.assertDictEqual(filestate.prepend(name, source=source), ret)
ret.pop("data", None) ret.pop("data", None)
ret.update({"name": name}) ret.update({"name": name})
with patch.object( with patch.object(
salt.utils.files, "fopen", MagicMock(mock_open(read_data ="")) salt.utils.files, "fopen", MagicMock(mock_open(read_data =""))
): ):
with patch.dict(filestate.__utils__, {"files.is_text": m ock_f}): with patch.dict(filestate.__utils__, {"files.is_text": m ock_f}):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
change = {"diff": "Replace binary file"} change = {"diff": "Replace binary file"}
comt = "File {0} is set to be updated".format(na me) comt = "File {} is set to be updated".format(nam e)
ret.update( ret.update(
{"comment": comt, "result": None, "changes": change} {"comment": comt, "result": None, "changes": change}
) )
self.assertDictEqual( self.assertDictEqual(
filestate.prepend(name, text=text), ret filestate.prepend(name, text=text), ret
) )
with patch.dict(filestate.__opts__, {"test": False}) : with patch.dict(filestate.__opts__, {"test": False}) :
comt = "Prepended 1 lines" comt = "Prepended 1 lines"
ret.update( ret.update(
skipping to change at line 2132 skipping to change at line 2125
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
comt = "Must provide name to file.touch" comt = "Must provide name to file.touch"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.touch(""), ret) self.assertDictEqual(filestate.touch(""), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.touch(name), ret) self.assertDictEqual(filestate.touch(name), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "exists", mock_f): with patch.object(os.path, "exists", mock_f):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "File {0} is set to be created".format(name) comt = "File {} is set to be created".format(name)
ret.update( ret.update(
{"comment": comt, "result": None, "changes": {"new": nam e}} {"comment": comt, "result": None, "changes": {"new": nam e}}
) )
self.assertDictEqual(filestate.touch(name), ret) self.assertDictEqual(filestate.touch(name), ret)
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
comt = "Directory not present to touch file {0}".format(name ) comt = "Directory not present to touch file {}".format(name)
ret.update({"comment": comt, "result": False, "changes": {}} ) ret.update({"comment": comt, "result": False, "changes": {}} )
self.assertDictEqual(filestate.touch(name), ret) self.assertDictEqual(filestate.touch(name), ret)
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
with patch.dict(filestate.__salt__, {"file.touch": mock_t}): with patch.dict(filestate.__salt__, {"file.touch": mock_t}):
comt = "Created empty file {0}".format(name) comt = "Created empty file {}".format(name)
ret.update( ret.update(
{"comment": comt, "result": True, "changes": {"new": name}} {"comment": comt, "result": True, "changes": {"new": name}}
) )
self.assertDictEqual(filestate.touch(name), ret) self.assertDictEqual(filestate.touch(name), ret)
# 'copy' function tests: 1 # 'copy' function tests: 1
def test_copy(self): def test_copy(self):
""" """
Test if the source file exists on the system, copy it to the named file. Test if the source file exists on the system, copy it to the named file.
skipping to change at line 2184 skipping to change at line 2177
self.assertDictEqual(filestate.copy_("", source), ret) self.assertDictEqual(filestate.copy_("", source), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
mock_uid = MagicMock(side_effect=[""]) mock_uid = MagicMock(side_effect=[""])
mock_gid = MagicMock(side_effect=[""]) mock_gid = MagicMock(side_effect=[""])
mock_user = MagicMock(return_value=user) mock_user = MagicMock(return_value=user)
mock_grp = MagicMock(return_value=group) mock_grp = MagicMock(return_value=group)
mock_io = MagicMock(side_effect=IOError) mock_io = MagicMock(side_effect=IOError)
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.copy_(name, source), ret) self.assertDictEqual(filestate.copy_(name, source), ret)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "exists", mock_f): with patch.object(os.path, "exists", mock_f):
comt = 'Source file "{0}" is not present'.format(source) comt = 'Source file "{}" is not present'.format(source)
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual(filestate.copy_(name, source), ret) self.assertDictEqual(filestate.copy_(name, source), ret)
with patch.object(os.path, "exists", mock_t): with patch.object(os.path, "exists", mock_t):
with patch.dict( with patch.dict(
filestate.__salt__, filestate.__salt__,
{ {
"file.user_to_uid": mock_uid, "file.user_to_uid": mock_uid,
"file.group_to_gid": mock_gid, "file.group_to_gid": mock_gid,
"file.get_user": mock_user, "file.get_user": mock_user,
skipping to change at line 2224 skipping to change at line 2217
comt = ( comt = (
"User salt is not available Group saltstack" "User salt is not available Group saltstack"
" is not available" " is not available"
) )
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual( self.assertDictEqual(
filestate.copy_(name, source, user=user, group=group), r et filestate.copy_(name, source, user=user, group=group), r et
) )
comt1 = ( comt1 = (
'Failed to delete "{0}" in preparation for' 'Failed to delete "{}" in preparation for'
" forced move".format(name) " forced move".format(name)
) )
comt2 = ( comt2 = (
'The target file "{0}" exists and will not be ' 'The target file "{}" exists and will not be '
"overwritten".format(name) "overwritten".format(name)
) )
comt3 = 'File "{0}" is set to be copied to "{1}"'.format( comt3 = 'File "{}" is set to be copied to "{}"'.format(sourc
source, name e, name)
)
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
with patch.object(os.path, "lexists", mock_t): with patch.object(os.path, "lexists", mock_t):
with patch.dict(filestate.__opts__, {"test": False}) : with patch.dict(filestate.__opts__, {"test": False}) :
with patch.dict( with patch.dict(
filestate.__salt__, {"file.remove": mock_io} filestate.__salt__, {"file.remove": mock_io}
): ):
ret.update({"comment": comt1, "result": Fals e}) ret.update({"comment": comt1, "result": Fals e})
self.assertDictEqual( self.assertDictEqual(
filestate.copy_( filestate.copy_(
name, source, preserve=True, force=T rue name, source, preserve=True, force=T rue
skipping to change at line 2290 skipping to change at line 2281
comt = "Must provide name to file.rename" comt = "Must provide name to file.rename"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.rename("", source), ret) self.assertDictEqual(filestate.rename("", source), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
mock_lex = MagicMock(side_effect=[False, True, True]) mock_lex = MagicMock(side_effect=[False, True, True])
with patch.object(os.path, "isabs", mock_f): with patch.object(os.path, "isabs", mock_f):
comt = "Specified file {0} is not an absolute path".format(name) comt = "Specified file {} is not an absolute path".format(name)
ret.update({"comment": comt, "name": name}) ret.update({"comment": comt, "name": name})
self.assertDictEqual(filestate.rename(name, source), ret) self.assertDictEqual(filestate.rename(name, source), ret)
mock_lex = MagicMock(return_value=False) mock_lex = MagicMock(return_value=False)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
comt = ( comt = 'Source file "{}" has already been moved out of ' "place"
'Source file "{0}" has already been moved out of ' .format(
"place".format(source) source
) )
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.rename(name, source), ret) self.assertDictEqual(filestate.rename(name, source), ret)
mock_lex = MagicMock(side_effect=[True, True, True]) mock_lex = MagicMock(side_effect=[True, True, True])
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
comt = ( comt = (
'The target file "{0}" exists and will not be ' 'The target file "{}" exists and will not be '
"overwritten".format(name) "overwritten".format(name)
) )
ret.update({"comment": comt, "result": True}) ret.update({"comment": comt, "result": True})
self.assertDictEqual(filestate.rename(name, source), ret) self.assertDictEqual(filestate.rename(name, source), ret)
mock_lex = MagicMock(side_effect=[True, True, True]) mock_lex = MagicMock(side_effect=[True, True, True])
mock_rem = MagicMock(side_effect=IOError) mock_rem = MagicMock(side_effect=IOError)
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
comt = ( comt = (
'Failed to delete "{0}" in preparation for ' 'Failed to delete "{}" in preparation for '
"forced move".format(name) "forced move".format(name)
) )
with patch.dict(filestate.__salt__, {"file.remove": mock_rem }): with patch.dict(filestate.__salt__, {"file.remove": mock_rem }):
ret.update({"name": name, "comment": comt, "result": Fal se}) ret.update({"name": name, "comment": comt, "result": Fal se})
self.assertDictEqual( self.assertDictEqual(
filestate.rename(name, source, force=True), ret filestate.rename(name, source, force=True), ret
) )
mock_lex = MagicMock(side_effect=[True, False, False]) mock_lex = MagicMock(side_effect=[True, False, False])
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = 'File "{0}" is set to be moved to "{1}"'.format(sourc e, name) comt = 'File "{}" is set to be moved to "{}"'.format(source, name)
ret.update({"name": name, "comment": comt, "result": None}) ret.update({"name": name, "comment": comt, "result": None})
self.assertDictEqual(filestate.rename(name, source), ret) self.assertDictEqual(filestate.rename(name, source), ret)
mock_lex = MagicMock(side_effect=[True, False, False]) mock_lex = MagicMock(side_effect=[True, False, False])
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
with patch.object(os.path, "isdir", mock_f): with patch.object(os.path, "isdir", mock_f):
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
comt = "The target directory /tmp is not present" comt = "The target directory /tmp is not present"
ret.update({"name": name, "comment": comt, "result": Fal se}) ret.update({"name": name, "comment": comt, "result": Fal se})
skipping to change at line 2355 skipping to change at line 2345
mock_lex = MagicMock(side_effect=[True, False, False]) mock_lex = MagicMock(side_effect=[True, False, False])
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
with patch.object(os.path, "islink", mock_f): with patch.object(os.path, "islink", mock_f):
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.object( with patch.object(
shutil, "move", MagicMock(side_effect=IOError) shutil, "move", MagicMock(side_effect=IOError)
): ):
comt = 'Failed to move "{0}" to "{1}"'.format( comt = 'Failed to move "{}" to "{}"'.format(
source, name source, name
) )
ret.update( ret.update(
{"name": name, "comment": comt, "result": Fa lse} {"name": name, "comment": comt, "result": Fa lse}
) )
self.assertDictEqual( self.assertDictEqual(
filestate.rename(name, source), ret filestate.rename(name, source), ret
) )
mock_lex = MagicMock(side_effect=[True, False, False]) mock_lex = MagicMock(side_effect=[True, False, False])
with patch.object(os.path, "isabs", mock_t): with patch.object(os.path, "isabs", mock_t):
with patch.object(os.path, "lexists", mock_lex): with patch.object(os.path, "lexists", mock_lex):
with patch.object(os.path, "isdir", mock_t): with patch.object(os.path, "isdir", mock_t):
with patch.object(os.path, "islink", mock_f): with patch.object(os.path, "islink", mock_f):
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.object(shutil, "move", MagicMock()): with patch.object(shutil, "move", MagicMock()):
comt = 'Moved "{0}" to "{1}"'.format(source, nam e) comt = 'Moved "{}" to "{}"'.format(source, name)
ret.update( ret.update(
{ {
"name": name, "name": name,
"comment": comt, "comment": comt,
"result": True, "result": True,
"changes": {name: source}, "changes": {name: source},
} }
) )
self.assertDictEqual( self.assertDictEqual(
filestate.rename(name, source), ret filestate.rename(name, source), ret
skipping to change at line 2432 skipping to change at line 2422
with patch.dict( with patch.dict(
filestate.__low__, filestate.__low__,
{ {
"require_in": [{"file": "A"}], "require_in": [{"file": "A"}],
"watch_in": [{"B": "C"}], "watch_in": [{"B": "C"}],
"__sls__": "SLS", "__sls__": "SLS",
"__id__": "ID", "__id__": "ID",
}, },
): ):
comt = "Accumulator {0} for file {1} " "was charged by text".for mat( comt = "Accumulator {} for file {} " "was charged by text".forma t(
name, filename name, filename
) )
ret.update({"comment": comt, "name": name, "result": True}) ret.update({"comment": comt, "name": name, "result": True})
self.assertDictEqual(filestate.accumulated(name, filename, text) , ret) self.assertDictEqual(filestate.accumulated(name, filename, text) , ret)
# 'serialize' function tests: 1 # 'serialize' function tests: 1
def test_serialize_into_managed_file(self): def test_serialize_into_managed_file(self):
""" """
Test to serializes dataset and store it into managed file. Test to serializes dataset and store it into managed file.
skipping to change at line 2455 skipping to change at line 2445
ret = {"name": name, "result": False, "comment": "", "changes": {}} ret = {"name": name, "result": False, "comment": "", "changes": {}}
comt = "Must provide name to file.serialize" comt = "Must provide name to file.serialize"
ret.update({"comment": comt, "name": ""}) ret.update({"comment": comt, "name": ""})
self.assertDictEqual(filestate.serialize(""), ret) self.assertDictEqual(filestate.serialize(""), ret)
mock_t = MagicMock(return_value=True) mock_t = MagicMock(return_value=True)
mock_f = MagicMock(return_value=False) mock_f = MagicMock(return_value=False)
with patch.object(os.path, "isfile", mock_f): with patch.object(os.path, "isfile", mock_f):
comt = "File {0} is not present and is not set for " "creation". format( comt = "File {} is not present and is not set for " "creation".f ormat(
name name
) )
ret.update({"comment": comt, "name": name, "result": True}) ret.update({"comment": comt, "name": name, "result": True})
self.assertDictEqual(filestate.serialize(name, create=False), re t) self.assertDictEqual(filestate.serialize(name, create=False), re t)
comt = "Only one of 'dataset' and 'dataset_pillar' is permitted" comt = "Only one of 'dataset' and 'dataset_pillar' is permitted"
ret.update({"comment": comt, "result": False}) ret.update({"comment": comt, "result": False})
self.assertDictEqual( self.assertDictEqual(
filestate.serialize(name, dataset=True, dataset_pillar=True), re t filestate.serialize(name, dataset=True, dataset_pillar=True), re t
) )
skipping to change at line 2494 skipping to change at line 2484
filestate.serialize(name, dataset=True, formatter="A"), ret filestate.serialize(name, dataset=True, formatter="A"), ret
) )
mock_changes = MagicMock(return_value=True) mock_changes = MagicMock(return_value=True)
mock_no_changes = MagicMock(return_value=False) mock_no_changes = MagicMock(return_value=False)
# __opts__['test']=True with changes # __opts__['test']=True with changes
with patch.dict( with patch.dict(
filestate.__salt__, {"file.check_managed_changes": mock_changes} filestate.__salt__, {"file.check_managed_changes": mock_changes}
): ):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "Dataset will be serialized and stored into {0}".form at(name) comt = "Dataset will be serialized and stored into {}".forma t(name)
ret.update({"comment": comt, "result": None, "changes": True }) ret.update({"comment": comt, "result": None, "changes": True })
self.assertDictEqual( self.assertDictEqual(
filestate.serialize(name, dataset=True, formatter="pytho n"), ret filestate.serialize(name, dataset=True, formatter="pytho n"), ret
) )
# __opts__['test']=True without changes # __opts__['test']=True without changes
with patch.dict( with patch.dict(
filestate.__salt__, {"file.check_managed_changes": mock_no_chang es} filestate.__salt__, {"file.check_managed_changes": mock_no_chang es}
): ):
with patch.dict(filestate.__opts__, {"test": True}): with patch.dict(filestate.__opts__, {"test": True}):
comt = "The file {0} is in the correct state".format(name) comt = "The file {} is in the correct state".format(name)
ret.update({"comment": comt, "result": True, "changes": Fals e}) ret.update({"comment": comt, "result": True, "changes": Fals e})
self.assertDictEqual( self.assertDictEqual(
filestate.serialize(name, dataset=True, formatter="pytho n"), ret filestate.serialize(name, dataset=True, formatter="pytho n"), ret
) )
mock = MagicMock(return_value=ret) mock = MagicMock(return_value=ret)
with patch.dict(filestate.__opts__, {"test": False}): with patch.dict(filestate.__opts__, {"test": False}):
with patch.dict(filestate.__salt__, {"file.manage_file": mock}): with patch.dict(filestate.__salt__, {"file.manage_file": mock}):
comt = "Dataset will be serialized and stored into {0}".form at(name) comt = "Dataset will be serialized and stored into {}".forma t(name)
ret.update({"comment": comt, "result": None}) ret.update({"comment": comt, "result": None})
self.assertDictEqual( self.assertDictEqual(
filestate.serialize(name, dataset=True, formatter="pytho n"), ret filestate.serialize(name, dataset=True, formatter="pytho n"), ret
) )
# 'mknod' function tests: 1 # 'mknod' function tests: 1
def test_mknod(self): def test_mknod(self):
""" """
Test to create a special file similar to the 'nix mknod command. Test to create a special file similar to the 'nix mknod command.
skipping to change at line 2732 skipping to change at line 2722
) )
retained_files = sorted(list(retained_files), reverse=True) retained_files = sorted(list(retained_files), reverse=True)
expected_ret["changes"] = { expected_ret["changes"] = {
"retained": retained_files, "retained": retained_files,
"deleted": deleted_files, "deleted": deleted_files,
"ignored": ignored_files, "ignored": ignored_files,
} }
if test: if test:
expected_ret["result"] = None expected_ret["result"] = None
expected_ret["comment"] = ( expected_ret["comment"] = (
"{0} backups would have been removed from {1}.\n" "{} backups would have been removed from {}.\n"
"".format(len(deleted_files), fake_name) "".format(len(deleted_files), fake_name)
) )
else: else:
expected_ret["comment"] = ( expected_ret["comment"] = (
"{0} backups were removed from {1}.\n" "{} backups were removed from {}.\n"
"".format(len(deleted_files), fake_name) "".format(len(deleted_files), fake_name)
) )
mock_remove.assert_has_calls( mock_remove.assert_has_calls(
[call(os.path.join(fake_name, x)) for x in deleted_files ], [call(os.path.join(fake_name, x)) for x in deleted_files ],
any_order=True, any_order=True,
) )
self.assertDictEqual(actual_ret, expected_ret) self.assertDictEqual(actual_ret, expected_ret)
run_checks(isdir=mock_f) run_checks(isdir=mock_f)
run_checks() run_checks()
run_checks(test=True) run_checks(test=True)
run_checks(strptime_format=fake_strptime_format) run_checks(strptime_format=fake_strptime_format)
run_checks(strptime_format=fake_strptime_format, test=True) run_checks(strptime_format=fake_strptime_format, test=True)
@with_tempfile()
def test_file_keyvalue_key_values(self, fpath):
"""
test file.keyvalue when using key_values kwarg
"""
content = dedent(
"""\
#PermitRootLogin prohibit-password
#StrictMode yes
"""
)
with salt.utils.files.fopen(fpath, "w+") as fp_:
fp_.write(content)
ret = filestate.keyvalue(
name=fpath,
key_values=collections.OrderedDict(PermitRootLogin="yes"),
separator=" ",
uncomment="#",
key_ignore_case=True,
)
with salt.utils.files.fopen(fpath, "r") as fp_:
f_contents = fp_.read()
self.assertIn("PermitRootLogin yes", f_contents)
self.assertIn("#StrictMode yes", f_contents)
@with_tempfile()
def test_file_keyvalue_empty(self, fpath):
"""
test file.keyvalue when key_values is empty
"""
content = dedent(
"""\
#PermitRootLogin prohibit-password
#StrictMode yes
"""
)
with salt.utils.files.fopen(fpath, "w+") as fp_:
fp_.write(content)
ret = filestate.keyvalue(
name=fpath,
key_values={},
separator=" ",
uncomment="#",
key_ignore_case=True,
)
self.assertEqual(
ret["comment"],
"file.keyvalue key and value not supplied and key_values is empty",
)
with salt.utils.files.fopen(fpath, "r") as fp_:
f_contents = fp_.read()
self.assertNotIn("PermitRootLogin yes", f_contents)
self.assertIn("#StrictMode yes", f_contents)
@with_tempfile()
def test_file_keyvalue_not_dict(self, fpath):
"""
test file.keyvalue when key_values not a dict
"""
content = dedent(
"""\
#PermitRootLogin prohibit-password
#StrictMode yes
"""
)
with salt.utils.files.fopen(fpath, "w+") as fp_:
fp_.write(content)
ret = filestate.keyvalue(
name=fpath,
key_values=["PermiteRootLogin", "yes"],
separator=" ",
uncomment="#",
key_ignore_case=True,
)
self.assertEqual(
ret["comment"],
"file.keyvalue key and value not supplied and key_values is not a di
ctionary",
)
with salt.utils.files.fopen(fpath, "r") as fp_:
f_contents = fp_.read()
self.assertNotIn("PermitRootLogin yes", f_contents)
self.assertIn("#StrictMode yes", f_contents)
class TestFindKeepFiles(TestCase): class TestFindKeepFiles(TestCase):
@skipIf(salt.utils.platform.is_windows(), "Do not run on Windows") @skipIf(salt.utils.platform.is_windows(), "Do not run on Windows")
def test__find_keep_files_unix(self): def test__find_keep_files_unix(self):
keep = filestate._find_keep_files( keep = filestate._find_keep_files(
"/test/parent_folder", ["/test/parent_folder/meh.txt"] "/test/parent_folder", ["/test/parent_folder/meh.txt"]
) )
expected = [ expected = [
"/", "/",
"/test", "/test",
"/test/parent_folder", "/test/parent_folder",
skipping to change at line 2834 skipping to change at line 2916
exp = { exp = {
"name": name, "name": name,
"changes": { "changes": {
"removed": [ "removed": [
os.path.join("test", "test1", "file1"), os.path.join("test", "test1", "file1"),
os.path.join("test", "test2", "file2"), os.path.join("test", "test2", "file2"),
os.path.join("test", "file3"), os.path.join("test", "file3"),
] ]
}, },
"result": True, "result": True,
"comment": "Removed 3 files or directories from directory {0}".forma t(name), "comment": "Removed 3 files or directories from directory {}".format (name),
} }
self.assertDictEqual(exp, ret) self.assertDictEqual(exp, ret)
assert remove.call_count == 3 assert remove.call_count == 3
remove.reset_mock() remove.reset_mock()
with patch("os.walk", return_value=walker), patch( with patch("os.walk", return_value=walker), patch(
"os.path.islink", return_value=False "os.path.islink", return_value=False
), patch("os.path.getatime", return_value=today_delta.total_seconds()), patch( ), patch("os.path.getatime", return_value=today_delta.total_seconds()), patch(
"os.path.getsize", return_value=10 "os.path.getsize", return_value=10
), patch.dict( ), patch.dict(
skipping to change at line 2865 skipping to change at line 2947
"removed": [ "removed": [
os.path.join("test", "test1", "file1"), os.path.join("test", "test1", "file1"),
os.path.join("test", "test2", "file2"), os.path.join("test", "test2", "file2"),
os.path.join("test", "test2", "test3"), os.path.join("test", "test2", "test3"),
os.path.join("test", "file3"), os.path.join("test", "file3"),
os.path.join("test", "test1"), os.path.join("test", "test1"),
os.path.join("test", "test2"), os.path.join("test", "test2"),
] ]
}, },
"result": True, "result": True,
"comment": "Removed 6 files or directories from directory {0}".forma t(name), "comment": "Removed 6 files or directories from directory {}".format (name),
} }
self.assertDictEqual(exp, ret) self.assertDictEqual(exp, ret)
assert remove.call_count == 6 assert remove.call_count == 6
def test__bad_input(self): def test__bad_input(self):
exp = { exp = {
"name": "test/", "name": "test/",
"changes": {}, "changes": {},
"result": False, "result": False,
"comment": "Specified file test/ is not an absolute path", "comment": "Specified file test/ is not an absolute path",
skipping to change at line 2990 skipping to change at line 3072
"__instance_id__": "", "__instance_id__": "",
"__low__": {}, "__low__": {},
"__utils__": {}, "__utils__": {},
} }
} }
def test_selinux_change(self): def test_selinux_change(self):
file_name = "/tmp/some-test-file" file_name = "/tmp/some-test-file"
check_perms_result = [ check_perms_result = [
{ {
"comment": "The file {0} is set to be changed".format(file_name) , "comment": "The file {} is set to be changed".format(file_name),
"changes": { "changes": {
"selinux": { "selinux": {
"New": "User: unconfined_u Type: lost_found_t", "New": "User: unconfined_u Type: lost_found_t",
"Old": "User: system_u Type: user_tmp_t", "Old": "User: system_u Type: user_tmp_t",
} }
}, },
"name": file_name, "name": file_name,
"result": True, "result": True,
}, },
{"luser": "root", "lmode": "0644", "lgroup": "root"}, {"luser": "root", "lmode": "0644", "lgroup": "root"},
 End of changes. 109 change blocks. 
125 lines changed or deleted 210 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)