2013-12-11 00:45:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2014-05-22 00:59:02 +00:00
|
|
|
:codeauthor: :email:`Mike Place <mp@saltstack.com>`
|
2013-12-11 00:45:54 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting import skipIf
|
2014-02-08 07:50:12 +00:00
|
|
|
from salttesting.helpers import ensure_in_syspath
|
2014-02-12 15:36:17 +00:00
|
|
|
from salttesting.mock import patch, NO_MOCK, NO_MOCK_REASON
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-06-04 12:42:17 +00:00
|
|
|
ensure_in_syspath('../..')
|
2013-12-11 00:45:54 +00:00
|
|
|
|
|
|
|
# Import Python libs
|
2014-07-09 22:03:59 +00:00
|
|
|
import copy
|
2013-12-11 00:45:54 +00:00
|
|
|
import os
|
2014-08-21 20:22:54 +00:00
|
|
|
import logging
|
|
|
|
import pwd
|
2013-12-11 00:45:54 +00:00
|
|
|
import shutil
|
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import integration
|
|
|
|
from salt.fileserver import gitfs
|
|
|
|
|
|
|
|
gitfs.__opts__ = {'gitfs_remotes': [''],
|
2013-12-12 18:31:11 +00:00
|
|
|
'gitfs_root': '',
|
2014-07-09 22:03:59 +00:00
|
|
|
'fileserver_backend': ['git'],
|
2013-12-12 18:31:11 +00:00
|
|
|
'gitfs_base': 'master',
|
|
|
|
'fileserver_events': True,
|
2014-06-05 08:20:09 +00:00
|
|
|
'transport': 'zeromq',
|
|
|
|
'gitfs_mountpoint': '',
|
|
|
|
'gitfs_env_whitelist': [],
|
2014-08-21 03:23:45 +00:00
|
|
|
'gitfs_env_blacklist': [],
|
|
|
|
'gitfs_user': '',
|
|
|
|
'gitfs_password': '',
|
|
|
|
'gitfs_insecure_auth': False,
|
|
|
|
'gitfs_privkey': '',
|
|
|
|
'gitfs_pubkey': '',
|
|
|
|
'gitfs_passphrase': ''
|
2013-12-12 00:02:26 +00:00
|
|
|
}
|
|
|
|
|
2014-06-15 12:18:36 +00:00
|
|
|
LOAD = {'saltenv': 'base'}
|
2013-12-11 00:45:54 +00:00
|
|
|
|
2014-08-21 20:22:54 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-12-11 00:45:54 +00:00
|
|
|
try:
|
|
|
|
import git
|
|
|
|
GITFS_AVAILABLE = True
|
|
|
|
except ImportError:
|
2014-08-21 20:22:54 +00:00
|
|
|
GITFS_AVAILABLE = False
|
2013-12-11 00:45:54 +00:00
|
|
|
|
|
|
|
if not gitfs.__virtual__():
|
2013-12-11 20:07:36 +00:00
|
|
|
GITFS_AVAILABLE = False
|
2013-12-11 00:45:54 +00:00
|
|
|
|
|
|
|
|
2014-06-01 06:05:52 +00:00
|
|
|
@skipIf(not GITFS_AVAILABLE, "GitFS could not be loaded. Skipping GitFS tests!")
|
2014-02-08 07:50:12 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2013-12-11 00:45:54 +00:00
|
|
|
class GitFSTest(integration.ModuleCase):
|
2014-06-15 12:18:36 +00:00
|
|
|
maxDiff = None
|
|
|
|
|
2013-12-11 00:45:54 +00:00
|
|
|
def setUp(self):
|
|
|
|
'''
|
|
|
|
We don't want to check in another .git dir into GH because that just gets messy.
|
|
|
|
Instead, we'll create a temporary repo on the fly for the tests to examine.
|
|
|
|
'''
|
2013-12-12 18:31:11 +00:00
|
|
|
self.integration_base_files = os.path.join(integration.FILES, 'file', 'base')
|
|
|
|
self.tmp_repo_dir = os.path.join(integration.TMP, 'gitfs_root')
|
2013-12-11 00:45:54 +00:00
|
|
|
|
|
|
|
# Create the dir if it doesn't already exist
|
|
|
|
|
2013-12-12 18:31:11 +00:00
|
|
|
try:
|
|
|
|
shutil.copytree(self.integration_base_files, self.tmp_repo_dir + '/')
|
2014-07-09 22:03:59 +00:00
|
|
|
except OSError:
|
2013-12-12 18:31:11 +00:00
|
|
|
# We probably caught an error because files already exist. Ignore
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
2014-07-09 00:45:12 +00:00
|
|
|
repo = git.Repo(self.tmp_repo_dir)
|
|
|
|
except git.exc.InvalidGitRepositoryError:
|
|
|
|
repo = git.Repo.init(self.tmp_repo_dir)
|
|
|
|
|
2014-08-21 19:35:36 +00:00
|
|
|
if 'USERNAME' not in os.environ:
|
2014-08-21 20:22:54 +00:00
|
|
|
try:
|
|
|
|
os.environ['USERNAME'] = pwd.getpwuid(os.geteuid()).pw_name
|
|
|
|
except AttributeError:
|
|
|
|
log.error('Unable to get effective username, falling back to '
|
|
|
|
'\'root\'.')
|
|
|
|
os.environ['USERNAME'] = 'root'
|
|
|
|
|
2014-07-09 00:45:12 +00:00
|
|
|
repo.index.add([x for x in os.listdir(self.tmp_repo_dir)
|
|
|
|
if x != '.git'])
|
|
|
|
repo.index.commit('Test')
|
2013-12-12 18:31:11 +00:00
|
|
|
|
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2013-12-12 18:31:11 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir']}):
|
|
|
|
gitfs.update()
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-07-09 22:03:59 +00:00
|
|
|
def tearDown(self):
|
|
|
|
'''
|
|
|
|
Remove the temporary git repository and gitfs cache directory to ensure
|
|
|
|
a clean environment for each test.
|
|
|
|
'''
|
|
|
|
shutil.rmtree(self.tmp_repo_dir)
|
|
|
|
shutil.rmtree(os.path.join(self.master_opts['cachedir'], 'gitfs'))
|
|
|
|
|
2014-08-21 03:23:45 +00:00
|
|
|
#@skipIf(True, 'Skipping tests temporarily')
|
2013-12-12 00:02:26 +00:00
|
|
|
def test_file_list(self):
|
2013-12-12 18:31:11 +00:00
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2013-12-12 18:31:11 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir']}):
|
2014-06-15 12:18:36 +00:00
|
|
|
ret = gitfs.file_list(LOAD)
|
2013-12-12 18:31:11 +00:00
|
|
|
self.assertIn('testfile', ret)
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-08-21 03:23:45 +00:00
|
|
|
#@skipIf(True, 'Skipping tests temporarily')
|
2013-12-12 00:02:26 +00:00
|
|
|
def test_find_file(self):
|
2013-12-12 18:31:11 +00:00
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2013-12-12 18:31:11 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir']}):
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-07-09 22:03:59 +00:00
|
|
|
path = os.path.join(self.master_opts['cachedir'],
|
|
|
|
'gitfs/refs',
|
|
|
|
LOAD['saltenv'],
|
|
|
|
'testfile')
|
|
|
|
|
|
|
|
ret = gitfs.find_file('testfile')
|
|
|
|
expected_ret = {'path': path, 'rel': 'testfile'}
|
2013-12-12 18:31:11 +00:00
|
|
|
self.assertDictEqual(ret, expected_ret)
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-08-21 03:23:45 +00:00
|
|
|
#@skipIf(True, 'Skipping tests temporarily')
|
2013-12-12 00:02:26 +00:00
|
|
|
def test_dir_list(self):
|
2013-12-12 18:31:11 +00:00
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2013-12-12 18:31:11 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir']}):
|
2014-06-15 12:18:36 +00:00
|
|
|
ret = gitfs.dir_list(LOAD)
|
2013-12-12 18:31:11 +00:00
|
|
|
self.assertIn('grail', ret)
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-08-21 03:23:45 +00:00
|
|
|
#@skipIf(True, 'Skipping tests temporarily')
|
2013-12-12 00:02:26 +00:00
|
|
|
def test_envs(self):
|
2013-12-12 18:31:11 +00:00
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2013-12-12 18:31:11 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir']}):
|
|
|
|
ret = gitfs.envs()
|
|
|
|
self.assertIn('base', ret)
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-08-21 03:23:45 +00:00
|
|
|
#@skipIf(True, 'Skipping tests temporarily')
|
2014-07-09 22:03:59 +00:00
|
|
|
def test_file_hash_sha1(self):
|
2014-07-09 00:45:12 +00:00
|
|
|
'''
|
2014-07-09 22:03:59 +00:00
|
|
|
NOTE: This test requires that gitfs.find_file is executed to ensure
|
|
|
|
that the file exists in the gitfs cache.
|
2014-07-09 00:45:12 +00:00
|
|
|
'''
|
2014-07-09 22:03:59 +00:00
|
|
|
target = 'testfile'
|
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
|
|
|
'sock_dir': self.master_opts['sock_dir']}):
|
|
|
|
# This needs to be in its own patch because we are using a
|
|
|
|
# different hash_type for this test (sha1) from the one the master
|
|
|
|
# is using (md5), which will cause the find_file to fail when the
|
|
|
|
# repo URI is hashed to determine the cachedir.
|
|
|
|
gitfs.find_file(target)
|
|
|
|
|
2013-12-12 18:31:11 +00:00
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2013-12-12 18:31:11 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir'],
|
2014-06-16 21:37:01 +00:00
|
|
|
'hash_type': 'sha1'}):
|
2014-07-09 22:03:59 +00:00
|
|
|
tmp_load = copy.deepcopy(LOAD)
|
2014-06-17 18:04:53 +00:00
|
|
|
tmp_load['loc'] = 0
|
2014-07-09 22:03:59 +00:00
|
|
|
tmp_load['path'] = target
|
|
|
|
fnd = {'rel': target,
|
2014-07-09 00:45:12 +00:00
|
|
|
'path': os.path.join(gitfs.__opts__['cachedir'],
|
|
|
|
'gitfs/refs',
|
|
|
|
tmp_load['saltenv'],
|
|
|
|
tmp_load['path'])}
|
|
|
|
|
2014-06-15 12:24:39 +00:00
|
|
|
ret = gitfs.file_hash(tmp_load, fnd)
|
2014-07-09 00:45:12 +00:00
|
|
|
self.assertDictEqual(
|
|
|
|
{'hash_type': 'sha1',
|
|
|
|
'hsum': '6b18d04b61238ba13b5e4626b13ac5fb7432b5e2'},
|
|
|
|
ret)
|
2013-12-12 00:02:26 +00:00
|
|
|
|
2014-08-21 03:23:45 +00:00
|
|
|
#@skipIf(True, 'Skipping tests temporarily')
|
2013-12-12 00:02:26 +00:00
|
|
|
def test_serve_file(self):
|
2014-07-09 00:45:12 +00:00
|
|
|
'''
|
2014-07-09 22:03:59 +00:00
|
|
|
NOTE: This test requires that gitfs.find_file is executed to ensure
|
|
|
|
that the file exists in the gitfs cache.
|
2014-07-09 00:45:12 +00:00
|
|
|
'''
|
2014-07-09 22:03:59 +00:00
|
|
|
target = 'testfile'
|
2014-06-15 12:25:21 +00:00
|
|
|
with patch.dict(gitfs.__opts__, {'cachedir': self.master_opts['cachedir'],
|
2014-07-09 00:45:12 +00:00
|
|
|
'gitfs_remotes': ['file://' + self.tmp_repo_dir],
|
2014-06-15 12:25:21 +00:00
|
|
|
'sock_dir': self.master_opts['sock_dir'],
|
|
|
|
'file_buffer_size': 262144}):
|
2014-07-09 22:03:59 +00:00
|
|
|
tmp_load = copy.deepcopy(LOAD)
|
2014-06-15 12:25:21 +00:00
|
|
|
tmp_load['loc'] = 0
|
2014-07-09 22:03:59 +00:00
|
|
|
tmp_load['path'] = target
|
|
|
|
fnd = {'rel': target,
|
2014-07-09 00:45:12 +00:00
|
|
|
'path': os.path.join(gitfs.__opts__['cachedir'],
|
|
|
|
'gitfs/refs',
|
|
|
|
tmp_load['saltenv'],
|
|
|
|
tmp_load['path'])}
|
2013-12-12 18:31:11 +00:00
|
|
|
|
2014-07-09 22:03:59 +00:00
|
|
|
gitfs.find_file(tmp_load['path'])
|
2014-06-15 12:18:36 +00:00
|
|
|
ret = gitfs.serve_file(tmp_load, fnd)
|
2013-12-12 18:31:11 +00:00
|
|
|
self.assertDictEqual({
|
2014-07-09 22:03:59 +00:00
|
|
|
'data': 'Scene 24\n\n \n OLD MAN: Ah, hee he he ha!\n '
|
|
|
|
'ARTHUR: And this enchanter of whom you speak, he '
|
|
|
|
'has seen the grail?\n OLD MAN: Ha ha he he he '
|
|
|
|
'he!\n ARTHUR: Where does he live? Old man, where '
|
|
|
|
'does he live?\n OLD MAN: He knows of a cave, a '
|
|
|
|
'cave which no man has entered.\n ARTHUR: And the '
|
|
|
|
'Grail... The Grail is there?\n OLD MAN: Very much '
|
|
|
|
'danger, for beyond the cave lies the Gorge\n of '
|
|
|
|
'Eternal Peril, which no man has ever crossed.\n '
|
|
|
|
'ARTHUR: But the Grail! Where is the Grail!?\n OLD '
|
|
|
|
'MAN: Seek you the Bridge of Death.\n ARTHUR: The '
|
|
|
|
'Bridge of Death, which leads to the Grail?\n OLD '
|
|
|
|
'MAN: Hee hee ha ha!\n\n',
|
|
|
|
'dest': target},
|
2014-06-05 08:24:52 +00:00
|
|
|
ret)
|
2013-12-12 18:31:11 +00:00
|
|
|
|
2013-12-11 00:45:54 +00:00
|
|
|
|
2013-12-12 00:02:26 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
integration.run_tests(GitFSTest)
|