2013-11-27 11:19:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2012-06-30 23:56:02 +00:00
|
|
|
# Import python libs
|
2014-11-21 19:05:13 +00:00
|
|
|
from __future__ import absolute_import
|
2012-07-01 06:31:01 +00:00
|
|
|
import getpass
|
2012-06-30 23:56:02 +00:00
|
|
|
import grp
|
2013-05-03 09:47:08 +00:00
|
|
|
import pwd
|
2012-06-30 23:56:02 +00:00
|
|
|
import os
|
2012-07-03 14:50:02 +00:00
|
|
|
import shutil
|
2012-06-30 23:56:02 +00:00
|
|
|
import sys
|
|
|
|
|
2014-01-29 18:01:28 +00:00
|
|
|
from salttesting.mock import patch, MagicMock
|
2013-06-30 13:19:16 +00:00
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
# Import Salt Testing libs
|
2013-06-24 19:06:49 +00:00
|
|
|
from salttesting import skipIf
|
2013-06-27 11:04:26 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import integration
|
|
|
|
import salt.utils
|
2013-06-30 13:19:16 +00:00
|
|
|
from salt.modules import file as filemod
|
2012-06-30 23:56:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FileModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validate the file module
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
self.myfile = os.path.join(integration.TMP, 'myfile')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(self.myfile, 'w+') as fp:
|
2013-06-27 11:04:26 +00:00
|
|
|
fp.write('Hello\n')
|
2012-07-03 14:50:02 +00:00
|
|
|
self.mydir = os.path.join(integration.TMP, 'mydir/isawesome')
|
2012-08-25 13:21:38 +00:00
|
|
|
if not os.path.isdir(self.mydir):
|
|
|
|
# left behind... Don't fail because of this!
|
|
|
|
os.makedirs(self.mydir)
|
2012-11-22 11:13:49 +00:00
|
|
|
self.mysymlink = os.path.join(integration.TMP, 'mysymlink')
|
|
|
|
if os.path.islink(self.mysymlink):
|
|
|
|
os.remove(self.mysymlink)
|
|
|
|
os.symlink(self.myfile, self.mysymlink)
|
|
|
|
self.mybadsymlink = os.path.join(integration.TMP, 'mybadsymlink')
|
|
|
|
if os.path.islink(self.mybadsymlink):
|
|
|
|
os.remove(self.mybadsymlink)
|
2013-05-01 23:06:17 +00:00
|
|
|
os.symlink('/nonexistentpath', self.mybadsymlink)
|
2012-06-30 23:56:02 +00:00
|
|
|
super(FileModuleTest, self).setUp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2013-11-10 15:36:39 +00:00
|
|
|
if os.path.isfile(self.myfile):
|
|
|
|
os.remove(self.myfile)
|
2012-11-22 11:13:49 +00:00
|
|
|
if os.path.islink(self.mysymlink):
|
|
|
|
os.remove(self.mysymlink)
|
|
|
|
if os.path.islink(self.mybadsymlink):
|
|
|
|
os.remove(self.mybadsymlink)
|
2012-07-03 14:50:02 +00:00
|
|
|
shutil.rmtree(self.mydir, ignore_errors=True)
|
2012-06-30 23:56:02 +00:00
|
|
|
super(FileModuleTest, self).tearDown()
|
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-07-01 00:33:34 +00:00
|
|
|
def test_chown(self):
|
2012-07-01 06:31:01 +00:00
|
|
|
user = getpass.getuser()
|
2012-07-01 00:33:34 +00:00
|
|
|
if sys.platform == 'darwin':
|
|
|
|
group = 'staff'
|
2014-09-04 08:23:59 +00:00
|
|
|
elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
|
2013-05-03 09:47:08 +00:00
|
|
|
group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
|
2012-08-01 18:36:49 +00:00
|
|
|
ret = self.run_function('file.chown', arg=[self.myfile, user, group])
|
2012-07-01 00:33:34 +00:00
|
|
|
self.assertIsNone(ret)
|
|
|
|
fstat = os.stat(self.myfile)
|
2012-08-01 18:36:49 +00:00
|
|
|
self.assertEqual(fstat.st_uid, os.getuid())
|
|
|
|
self.assertEqual(fstat.st_gid, grp.getgrnam(group).gr_gid)
|
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-07-01 00:33:34 +00:00
|
|
|
def test_chown_no_user(self):
|
|
|
|
user = 'notanyuseriknow'
|
2013-05-03 09:47:08 +00:00
|
|
|
group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
|
2012-08-01 18:36:49 +00:00
|
|
|
ret = self.run_function('file.chown', arg=[self.myfile, user, group])
|
2012-07-01 00:33:34 +00:00
|
|
|
self.assertIn('not exist', ret)
|
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-07-01 00:33:34 +00:00
|
|
|
def test_chown_no_user_no_group(self):
|
|
|
|
user = 'notanyuseriknow'
|
|
|
|
group = 'notanygroupyoushoulduse'
|
2012-08-01 18:36:49 +00:00
|
|
|
ret = self.run_function('file.chown', arg=[self.myfile, user, group])
|
2012-07-01 00:33:34 +00:00
|
|
|
self.assertIn('Group does not exist', ret)
|
|
|
|
self.assertIn('User does not exist', ret)
|
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-07-01 00:33:34 +00:00
|
|
|
def test_chown_no_path(self):
|
2012-07-01 06:31:01 +00:00
|
|
|
user = getpass.getuser()
|
2012-07-01 00:33:34 +00:00
|
|
|
if sys.platform == 'darwin':
|
|
|
|
group = 'staff'
|
2014-09-04 08:23:59 +00:00
|
|
|
elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
|
2013-05-03 09:47:08 +00:00
|
|
|
group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
|
2012-07-01 00:33:34 +00:00
|
|
|
ret = self.run_function('file.chown',
|
|
|
|
arg=['/tmp/nosuchfile', user, group])
|
|
|
|
self.assertIn('File not found', ret)
|
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-07-01 00:33:34 +00:00
|
|
|
def test_chown_noop(self):
|
|
|
|
user = ''
|
|
|
|
group = ''
|
2012-08-01 18:36:49 +00:00
|
|
|
ret = self.run_function('file.chown', arg=[self.myfile, user, group])
|
2012-07-01 00:33:34 +00:00
|
|
|
self.assertIsNone(ret)
|
|
|
|
fstat = os.stat(self.myfile)
|
2012-08-01 18:36:49 +00:00
|
|
|
self.assertEqual(fstat.st_uid, os.getuid())
|
|
|
|
self.assertEqual(fstat.st_gid, os.getgid())
|
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-06-30 23:56:02 +00:00
|
|
|
def test_chgrp(self):
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
group = 'everyone'
|
2014-09-04 08:23:59 +00:00
|
|
|
elif sys.platform.startswith(('linux', 'freebsd', 'openbsd')):
|
2013-05-03 09:47:08 +00:00
|
|
|
group = grp.getgrgid(pwd.getpwuid(os.getuid()).pw_gid).gr_name
|
2012-08-01 18:36:49 +00:00
|
|
|
ret = self.run_function('file.chgrp', arg=[self.myfile, group])
|
2012-06-30 23:56:02 +00:00
|
|
|
self.assertIsNone(ret)
|
|
|
|
fstat = os.stat(self.myfile)
|
2012-08-01 18:36:49 +00:00
|
|
|
self.assertEqual(fstat.st_gid, grp.getgrnam(group).gr_gid)
|
2012-07-01 00:33:34 +00:00
|
|
|
|
2013-09-14 18:59:15 +00:00
|
|
|
@skipIf(salt.utils.is_windows(), 'No chgrp on Windows')
|
2012-07-01 00:33:34 +00:00
|
|
|
def test_chgrp_failure(self):
|
|
|
|
group = 'thisgroupdoesntexist'
|
2012-08-01 18:36:49 +00:00
|
|
|
ret = self.run_function('file.chgrp', arg=[self.myfile, group])
|
2012-07-01 00:33:34 +00:00
|
|
|
self.assertIn('not exist', ret)
|
2012-07-03 14:50:02 +00:00
|
|
|
|
2012-10-17 18:06:17 +00:00
|
|
|
def test_patch(self):
|
|
|
|
if not self.run_function('cmd.has_exec', ['patch']):
|
|
|
|
self.skipTest('patch is not installed')
|
|
|
|
|
|
|
|
src_patch = os.path.join(
|
|
|
|
integration.FILES, 'file', 'base', 'hello.patch')
|
|
|
|
src_file = os.path.join(integration.TMP, 'src.txt')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src_file, 'w+') as fp:
|
2013-06-27 11:04:26 +00:00
|
|
|
fp.write('Hello\n')
|
2012-10-17 18:06:17 +00:00
|
|
|
|
|
|
|
# dry-run should not modify src_file
|
|
|
|
ret = self.minion_run('file.patch', src_file, src_patch, dry_run=True)
|
|
|
|
assert ret['retcode'] == 0, repr(ret)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src_file) as fp:
|
2012-10-17 18:06:17 +00:00
|
|
|
self.assertEqual(fp.read(), 'Hello\n')
|
|
|
|
|
|
|
|
ret = self.minion_run('file.patch', src_file, src_patch)
|
|
|
|
assert ret['retcode'] == 0, repr(ret)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src_file) as fp:
|
2012-10-17 18:06:17 +00:00
|
|
|
self.assertEqual(fp.read(), 'Hello world\n')
|
|
|
|
|
2012-07-03 14:50:02 +00:00
|
|
|
def test_remove_file(self):
|
2013-11-10 15:36:11 +00:00
|
|
|
ret = self.run_function('file.remove', arg=[self.myfile])
|
2012-07-03 14:50:02 +00:00
|
|
|
self.assertTrue(ret)
|
|
|
|
|
|
|
|
def test_remove_dir(self):
|
2013-11-10 15:36:11 +00:00
|
|
|
ret = self.run_function('file.remove', arg=[self.mydir])
|
2012-07-03 14:50:02 +00:00
|
|
|
self.assertTrue(ret)
|
|
|
|
|
2012-11-22 11:13:49 +00:00
|
|
|
def test_remove_symlink(self):
|
2013-11-10 15:36:11 +00:00
|
|
|
ret = self.run_function('file.remove', arg=[self.mysymlink])
|
2012-11-22 11:13:49 +00:00
|
|
|
self.assertTrue(ret)
|
|
|
|
|
|
|
|
def test_remove_broken_symlink(self):
|
2013-11-10 15:36:11 +00:00
|
|
|
ret = self.run_function('file.remove', arg=[self.mybadsymlink])
|
2012-11-22 11:13:49 +00:00
|
|
|
self.assertTrue(ret)
|
|
|
|
|
2012-07-03 14:50:02 +00:00
|
|
|
def test_cannot_remove(self):
|
2013-11-10 15:30:42 +00:00
|
|
|
ret = self.run_function('file.remove', arg=['tty'])
|
2012-07-03 14:50:02 +00:00
|
|
|
self.assertEqual(
|
2013-09-21 23:51:50 +00:00
|
|
|
'ERROR executing \'file.remove\': File path must be absolute.', ret
|
2012-08-01 18:36:49 +00:00
|
|
|
)
|
2012-07-20 06:21:01 +00:00
|
|
|
|
2013-06-30 13:19:16 +00:00
|
|
|
def test_source_list_for_single_file_returns_unchanged(self):
|
|
|
|
ret = self.run_function('file.source_list', ['salt://http/httpd.conf',
|
|
|
|
'filehash', 'base'])
|
|
|
|
self.assertItemsEqual(ret, ['salt://http/httpd.conf', 'filehash'])
|
|
|
|
|
|
|
|
def test_source_list_for_list_returns_existing_file(self):
|
|
|
|
filemod.__salt__ = {
|
|
|
|
'cp.list_master': MagicMock(
|
|
|
|
return_value=['http/httpd.conf.fallback']),
|
|
|
|
'cp.list_master_dirs': MagicMock(return_value=[]),
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = filemod.source_list(['salt://http/httpd.conf',
|
|
|
|
'salt://http/httpd.conf.fallback'],
|
|
|
|
'filehash', 'base')
|
|
|
|
self.assertItemsEqual(ret, ['salt://http/httpd.conf.fallback',
|
|
|
|
'filehash'])
|
|
|
|
|
|
|
|
def test_source_list_for_list_returns_file_from_other_env(self):
|
|
|
|
def list_master(env):
|
|
|
|
dct = {'base': [], 'dev': ['http/httpd.conf']}
|
|
|
|
return dct[env]
|
|
|
|
filemod.__salt__ = {
|
|
|
|
'cp.list_master': MagicMock(side_effect=list_master),
|
|
|
|
'cp.list_master_dirs': MagicMock(return_value=[]),
|
|
|
|
}
|
2013-11-06 23:36:40 +00:00
|
|
|
ret = filemod.source_list(['salt://http/httpd.conf?saltenv=dev',
|
2013-06-30 13:19:16 +00:00
|
|
|
'salt://http/httpd.conf.fallback'],
|
|
|
|
'filehash', 'base')
|
2013-11-06 23:36:40 +00:00
|
|
|
self.assertItemsEqual(ret, ['salt://http/httpd.conf?saltenv=dev',
|
2013-06-30 13:19:16 +00:00
|
|
|
'filehash'])
|
|
|
|
|
|
|
|
def test_source_list_for_list_returns_file_from_dict(self):
|
|
|
|
filemod.__salt__ = {
|
|
|
|
'cp.list_master': MagicMock(return_value=['http/httpd.conf']),
|
|
|
|
'cp.list_master_dirs': MagicMock(return_value=[]),
|
|
|
|
}
|
|
|
|
ret = filemod.source_list(
|
|
|
|
[{'salt://http/httpd.conf': ''}], 'filehash', 'base')
|
|
|
|
self.assertItemsEqual(ret, ['salt://http/httpd.conf', 'filehash'])
|
|
|
|
|
|
|
|
@patch('salt.modules.file.os.remove')
|
|
|
|
def test_source_list_for_list_returns_file_from_dict_via_http(self, remove):
|
|
|
|
remove.return_value = None
|
|
|
|
filemod.__salt__ = {
|
|
|
|
'cp.list_master': MagicMock(return_value=[]),
|
|
|
|
'cp.list_master_dirs': MagicMock(return_value=[]),
|
|
|
|
'cp.get_url': MagicMock(return_value='/tmp/http.conf'),
|
|
|
|
}
|
|
|
|
ret = filemod.source_list(
|
|
|
|
[{'http://t.est.com/http/httpd.conf': 'filehash'}], '', 'base')
|
|
|
|
self.assertItemsEqual(ret, ['http://t.est.com/http/httpd.conf',
|
|
|
|
'filehash'])
|
|
|
|
|
2013-06-24 22:53:59 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(FileModuleTest)
|