def test_file_changes_ownership(): from fabtools.files import owner from fabtools.require.files import file as require_file try: run('touch foo') assert owner('foo') == env.user require_file('foo', use_sudo=True) assert owner('foo') == 'root' finally: run_as_root('rm -f foo')
def test_permissions(): from fabtools.files import owner, group, mode from fabtools.require.files import directory as require_directory try: run_as_root('mkdir foo') require_directory('bar', use_sudo=True) assert owner('foo') == owner('bar') assert group('foo') == group('bar') assert mode('foo') == mode('bar') finally: run_as_root('rmdir foo bar')
def test_file_with_contents_has_correct_permissions(): from fabtools.files import owner, group, mode from fabtools.require.files import file as require_file try: run_as_root('echo "something" > foo') require_file('bar', contents='something', use_sudo=True) assert owner('foo') == owner('bar') assert group('foo') == group('bar') assert mode('foo') == mode('bar') finally: run_as_root('rm -f foo bar')
def test_empty_file_has_correct_permissions(): from fabtools.files import owner, group, mode from fabtools.require.files import file as require_file try: run_as_root('touch foo') require_file('bar', use_sudo=True) assert owner('foo') == owner('bar') assert group('foo') == group('bar') assert mode('foo') == mode('bar') finally: run_as_root('rm -f foo bar')
def git_require_sudo_user(): """ Test working_copy() with sudo as a user """ from fabric.api import cd, sudo from fabtools.files import group, is_dir, owner from fabtools import require require.user('gituser', group='gitgroup') require.git.working_copy(REMOTE_URL, path='wc_nobody', use_sudo=True, user='******') assert is_dir('wc_nobody') assert is_dir('wc_nobody/.git') with cd('wc_nobody'): remotes = sudo('git remote -v', user='******') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch', user='******') assert branch == '* master' assert owner('wc_nobody') == 'gituser' assert group('wc_nobody') == 'gitgroup'
def test_git_require_sudo_user(gituser): """ Test working_copy() with sudo as a user """ from fabtools.require.git import working_copy username, groupname = gituser with cd('/tmp'): try: working_copy(REMOTE_URL, path='wc_nobody', use_sudo=True, user=username) assert is_dir('wc_nobody') assert is_dir('wc_nobody/.git') with cd('wc_nobody'): remotes = sudo('git remote -v', user=username) assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' assert _current_branch() == 'master' assert owner('wc_nobody') == username assert group('wc_nobody') == groupname finally: run_as_root('rm -rf wc_nobody')
def test_git_require_sudo(): """ Test working_copy() with sudo """ from fabtools.require.git import working_copy try: working_copy(REMOTE_URL, path='wc_root', use_sudo=True) assert is_dir('wc_root') assert is_dir('wc_root/.git') with cd('wc_root'): remotes = run('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' assert _current_branch() == 'master' assert owner('wc_root') == 'root' assert group('wc_root') == 'root' finally: run_as_root('rm -rf wc_root')
def git_require_sudo_user(): """ Test working_copy() with sudo as a user """ from fabric.api import cd, sudo from fabtools.files import group, is_dir, owner from fabtools import require require.user("gituser", group="gitgroup") require.git.working_copy(REMOTE_URL, path="wc_nobody", use_sudo=True, user="******") assert is_dir("wc_nobody") assert is_dir("wc_nobody/.git") with cd("wc_nobody"): remotes = sudo("git remote -v", user="******") assert ( remotes == "origin\thttps://github.com/disko/fabtools.git (fetch)\r\n" "origin\thttps://github.com/disko/fabtools.git (push)" ) branch = sudo("git branch", user="******") assert branch == "* master" assert owner("wc_nobody") == "gituser" assert group("wc_nobody") == "gitgroup"
def test_initial_owner_requirement(users): from fabtools.require import directory try: directory('testdir', owner='testuser', use_sudo=True) assert is_dir('testdir') assert owner('testdir') == 'testuser' finally: run_as_root('rmdir testdir')
def test_directory_creation(): from fabtools.require import directory try: directory('testdir') assert is_dir('testdir') assert owner('testdir') == env.user finally: run('rmdir testdir')
def bzr_wc_sudo(): """ Test working copy with sudo. """ test = 'bzr_wc_sudo' wt = '%s-test-%s' % (DIR, test) puts(magenta('Executing test: %s' % test)) from fabric.api import sudo from fabtools.files import group, is_dir, owner from fabtools import require assert not is_dir(wt) require.bazaar.working_copy(REMOTE_URL, wt, use_sudo=True) assert_wc_exists(wt) assert owner(wt) == 'root' assert group(wt) == 'root'
def bzr_wc_sudo_user(): """ Test working copy with sudo as a user. """ test = 'bzr_wc_sudo_user' wt = '%s-test-%s' % (DIR, test) puts(magenta('Executing test: %s' % test)) from fabric.api import cd, sudo from fabtools.files import group, is_dir, owner from fabtools import require require.user('bzruser', group='bzrgroup') assert not is_dir(wt) require.bazaar.working_copy(REMOTE_URL, wt, use_sudo=True, user='******') assert_wc_exists(wt) assert owner(wt) == 'bzruser' assert group(wt) == 'bzrgroup'
def git_require(): """ Test high level git tools. These tests should also cover the low level tools as all of them are called indirectly. """ require.deb.package('git') from fabtools.require.git import working_copy with cd('/tmp'): # clean up... sudo('rm -rf *') # working_copy(remote_url, path=None, branch="master", update=True, # use_sudo=False, user=None) # Test with remote_url only working_copy(remote_url) assert is_dir('fabtools') assert is_dir('fabtools/.git') with cd('fabtools'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' # Test with remote_url and path working_copy(remote_url, path='wc') assert is_dir('wc') assert is_dir('wc/.git') with cd('wc'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' # Test that nothing is upated sudo('tar cf wc_old.tar wc') old_md5 = sudo('md5sum wc_old.tar').split(' ')[0] working_copy(remote_url, path='wc', update=False) sudo('tar cf wc_new.tar wc') new_md5 = sudo('md5sum wc_new.tar').split(' ')[0] assert old_md5 == new_md5 # Test checkout of a branch working_copy(remote_url, path='wc', branch="test_git") assert is_dir('wc') assert is_dir('wc/.git') with cd('wc'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == 'master\r\n* test_git' # Test use_sudo without user working_copy(remote_url, path='wc_root', use_sudo=True) assert is_dir('wc_root') assert is_dir('wc_root/.git') with cd('wc_root'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' assert owner('wc_root') == 'root' assert group('wc_root') == 'root' # Test use_sudo with user nobody working_copy(remote_url, path='wc_nobody', use_sudo=True, user='******') assert is_dir('wc_nobody') assert is_dir('wc_nobody/.git') with cd('wc_nobody'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' assert owner('wc_nobody') == 'nobody' assert group('wc_nobody') == 'nogroup'
def git_require(): """ Test high level git tools. These tests should also cover the low level tools as all of them are called indirectly. """ from fabric.api import cd, sudo from fabtools import require from fabtools.files import group, is_dir, owner from fabtools.system import distrib_family from fabtools.require.git import working_copy family = distrib_family() if family == 'debian': require.deb.package('git-core') elif family == 'redhat': require.rpm.package('git') with cd('/tmp'): # clean up... sudo('rm -rf *') # working_copy(remote_url, path=None, branch="master", update=True, # use_sudo=False, user=None) # Test with remote_url only working_copy(remote_url) assert is_dir('fabtools') assert is_dir('fabtools/.git') with cd('fabtools'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' # Test with remote_url and path working_copy(remote_url, path='wc') assert is_dir('wc') assert is_dir('wc/.git') with cd('wc'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' # Test that nothing is upated sudo('tar cf wc_old.tar wc') old_md5 = sudo('md5sum wc_old.tar').split(' ')[0] working_copy(remote_url, path='wc', update=False) sudo('tar cf wc_new.tar wc') new_md5 = sudo('md5sum wc_new.tar').split(' ')[0] assert old_md5 == new_md5 # Test checkout of a branch working_copy(remote_url, path='wc', branch="test_git") assert is_dir('wc') assert is_dir('wc/.git') with cd('wc'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == 'master\r\n* test_git' # Test use_sudo without user working_copy(remote_url, path='wc_root', use_sudo=True) assert is_dir('wc_root') assert is_dir('wc_root/.git') with cd('wc_root'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' assert owner('wc_root') == 'root' assert group('wc_root') == 'root' # Test use_sudo with user nobody working_copy(remote_url, path='wc_nobody', use_sudo=True, user='******') assert is_dir('wc_nobody') assert is_dir('wc_nobody/.git') with cd('wc_nobody'): remotes = sudo('git remote -v') assert remotes == \ 'origin\thttps://github.com/disko/fabtools.git (fetch)\r\n' \ 'origin\thttps://github.com/disko/fabtools.git (push)' branch = sudo('git branch') assert branch == '* master' assert owner('wc_nobody') == 'nobody' if family == 'debian': assert group('wc_nobody') == 'nogroup' elif family == 'redhat': assert group('wc_nobody') == 'nobody'