Testing utils.filesystem.chmod

- changing existing file mode with proper permissions
- changing existing folder mode recursively with proper permissions
- changing existing folder mode recursively with mode for files and mode
  for folders with proper permissions

- non existant or no permissions prevent changing mode of file / folder
This commit is contained in:
Raphael Marvie 2018-10-05 11:04:21 +02:00 committed by ljf
parent 9d82e3ac28
commit b8d10b71c3

View file

@ -9,6 +9,7 @@ thus are dependant on this implementation. This is fragile but allow for a
first instroduction of tests before doing refactorings. first instroduction of tests before doing refactorings.
""" """
import mock import mock
import pytest import pytest
@ -359,105 +360,80 @@ def test_rm_cannot_remove_folder_without_permission(rmtree, isdir):
filesystem.rm(foldername, recursive=True) filesystem.rm(foldername, recursive=True)
################################################################################ ########################################################################
## Test file remove # # Changing permissions
################################################################################ ########################################################################
# #
# changing file permissions
@mock.patch('os.chmod')
def test_chmod_update_file_permissions(chmod):
filename = 'file.txt'
mode = '0644'
filesystem.chmod(filename, mode)
chmod.assert_called_with(filename, mode)
@mock.patch('os.chmod')
def test_chmod_cannot_update_file_without_permission(chmod):
filename = 'file.txt'
mode = '0644'
chmod.side_effect = PermissionError
with pytest.raises(MoulinetteError):
filesystem.chmod(filename, mode)
@mock.patch('os.chmod')
def test_chmod_cannot_update_non_existant_file(chmod):
filename = 'file.txt'
mode = '0644'
chmod.side_effect = FileNotFoundError
with pytest.raises(MoulinetteError):
filesystem.chmod(filename, mode)
# #
#def test_remove_file(): # changing folder permissions
#
# rm(TMP_TEST_FILE) @mock.patch('os.walk')
# assert not os.path.exists(TMP_TEST_FILE) @mock.patch('os.path.isdir')
# @mock.patch('os.chmod')
# def test_chmod_recursive_update_folder_permissions(chmod, isdir, walk):
#def test_remove_file_badpermissions(): foldername = 'folder'
# mode = '0644'
# switch_to_non_root_user() isdir.return_value = True # foldername is a folder
# with pytest.raises(MoulinetteError): walk.return_value = [(foldername, ['subfolder'], ['file.txt'])]
# rm(TMP_TEST_FILE)
# filesystem.chmod(foldername, mode, recursive=True)
#
#def test_remove_directory(): calls = [mock.call('folder', mode),
# mock.call('folder/subfolder', mode),
# rm(TMP_TEST_DIR, recursive=True) mock.call('folder/file.txt', mode)]
# assert not os.path.exists(TMP_TEST_DIR)
# chmod.assert_has_calls(calls)
#
################################################################################
## Test permission change # @mock.patch('os.walk')
################################################################################ @mock.patch('os.path.isdir')
# @mock.patch('os.chmod')
# def test_chmod_recursive_update_folder_permissions_with_fmode(chmod, isdir, walk):
#def get_permissions(file_path): foldername = 'folder'
# from stat import ST_MODE mode = '0755'
# return (pwd.getpwuid(os.stat(file_path).st_uid).pw_name, fmode = '0644'
# pwd.getpwuid(os.stat(file_path).st_gid).pw_name, isdir.return_value = True # foldername is a folder
# oct(os.stat(file_path)[ST_MODE])[-3:]) walk.return_value = [(foldername, ['subfolder'], ['file.txt'])]
#
# filesystem.chmod(foldername, mode, fmode=fmode, recursive=True)
## FIXME - should split the test of chown / chmod as independent tests
#def set_permissions(f, owner, group, perms): calls = [mock.call('folder', mode),
# chown(f, owner, group) mock.call('folder/subfolder', mode),
# chmod(f, perms) mock.call('folder/file.txt', fmode)]
#
# chmod.assert_has_calls(calls)
#def test_setpermissions_file():
#
# # Check we're at the default permissions # eof
# assert get_permissions(TMP_TEST_FILE) == ("root", "root", "700")
#
# # Change the permissions
# set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
#
# # Check the permissions got changed
# assert get_permissions(TMP_TEST_FILE) == (NON_ROOT_USER, NON_ROOT_GROUP, "111")
#
# # Change the permissions again
# set_permissions(TMP_TEST_FILE, "root", "root", 0o777)
#
# # Check the permissions got changed
# assert get_permissions(TMP_TEST_FILE) == ("root", "root", "777")
#
#
#def test_setpermissions_directory():
#
# # Check we're at the default permissions
# assert get_permissions(TMP_TEST_DIR) == ("root", "root", "755")
#
# # Change the permissions
# set_permissions(TMP_TEST_DIR, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
#
# # Check the permissions got changed
# assert get_permissions(TMP_TEST_DIR) == (NON_ROOT_USER, NON_ROOT_GROUP, "111")
#
# # Change the permissions again
# set_permissions(TMP_TEST_DIR, "root", "root", 0o777)
#
# # Check the permissions got changed
# assert get_permissions(TMP_TEST_DIR) == ("root", "root", "777")
#
#
#def test_setpermissions_permissiondenied():
#
# switch_to_non_root_user()
#
# with pytest.raises(MoulinetteError):
# set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
#
#
#def test_setpermissions_badfile():
#
# with pytest.raises(MoulinetteError):
# set_permissions("/foo/bar/yolo", NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
#
#
#def test_setpermissions_baduser():
#
# with pytest.raises(MoulinetteError):
# set_permissions(TMP_TEST_FILE, "foo", NON_ROOT_GROUP, 0o111)
#
#
#def test_setpermissions_badgroup():
#
# with pytest.raises(MoulinetteError):
# set_permissions(TMP_TEST_FILE, NON_ROOT_USER, "foo", 0o111)