Introducing first test for moulinette/utils/filesystem

- try to read a non existent file raises an Exception
This commit is contained in:
Raphael Marvie 2018-10-04 12:30:27 +02:00 committed by ljf
parent 1c90091132
commit ef4a693408
4 changed files with 289 additions and 312 deletions

View file

@ -8,15 +8,15 @@ sys.path.append("..")
############################################################################### ###############################################################################
old_init = moulinette.core.Moulinette18n.__init__ # old_init = moulinette.core.Moulinette18n.__init__
#
#
def monkey_path_i18n_init(self, package, default_locale="en"): # def monkey_path_i18n_init(self, package, default_locale="en"):
old_init(self, package, default_locale) # old_init(self, package, default_locale)
self.load_namespace("moulinette") # self.load_namespace("moulinette")
#
#
moulinette.core.Moulinette18n.__init__ = monkey_path_i18n_init # moulinette.core.Moulinette18n.__init__ = monkey_path_i18n_init
############################################################################### ###############################################################################
@ -24,25 +24,25 @@ moulinette.core.Moulinette18n.__init__ = monkey_path_i18n_init
############################################################################### ###############################################################################
old_translate = moulinette.core.Translator.translate #old_translate = moulinette.core.Translator.translate
#
#
def new_translate(self, key, *args, **kwargs): #def new_translate(self, key, *args, **kwargs):
#
if key not in list(self._translations[self.default_locale].keys()): # if key not in list(self._translations[self.default_locale].keys()):
raise KeyError("Unable to retrieve key %s for default locale !" % key) # raise KeyError("Unable to retrieve key %s for default locale !" % key)
#
return old_translate(self, key, *args, **kwargs) # return old_translate(self, key, *args, **kwargs)
#
#
moulinette.core.Translator.translate = new_translate #moulinette.core.Translator.translate = new_translate
#
#
def new_m18nn(self, key, *args, **kwargs): #def new_m18nn(self, key, *args, **kwargs):
return self._global.translate(key, *args, **kwargs) # return self._global.translate(key, *args, **kwargs)
#
#
moulinette.core.Moulinette18n.g = new_m18nn #moulinette.core.Moulinette18n.g = new_m18nn
############################################################################### ###############################################################################

View file

@ -1,295 +1,264 @@
# encoding: utf-8
# General python lib """
import os Testing moulinette utils filesystem
import pwd """
import mock
import pytest import pytest
# Moulinette specific # Moulinette specific
from moulinette.core import MoulinetteError from moulinette.core import MoulinetteError
from moulinette.utils.filesystem import (read_file, read_json, from moulinette.utils import filesystem
write_to_file, append_to_file,
write_to_json,
rm,
chmod, chown)
# We define a dummy context with test folders and files
TEST_URL = "https://some.test.url/yolo.txt"
TMP_TEST_DIR = "/tmp/test_iohelpers"
TMP_TEST_FILE = "%s/foofile" % TMP_TEST_DIR
TMP_TEST_JSON = "%s/barjson" % TMP_TEST_DIR
NON_ROOT_USER = "admin"
NON_ROOT_GROUP = "mail"
def setup_function(function):
os.system("rm -rf %s" % TMP_TEST_DIR)
os.system("mkdir %s" % TMP_TEST_DIR)
os.system("echo 'foo\nbar' > %s" % TMP_TEST_FILE)
os.system("echo '{ \"foo\":\"bar\" }' > %s" % TMP_TEST_JSON)
os.system("chmod 700 %s" % TMP_TEST_FILE)
os.system("chmod 700 %s" % TMP_TEST_JSON)
def teardown_function(function):
os.seteuid(0)
os.system("rm -rf /tmp/test_iohelpers/")
# Helper to try stuff as non-root
def switch_to_non_root_user():
nonrootuser = pwd.getpwnam(NON_ROOT_USER).pw_uid
os.seteuid(nonrootuser)
############################################################################### ###############################################################################
# Test file read # # Test file read #
############################################################################### ###############################################################################
@mock.patch('os.path.isfile')
def test_read_file(): def test_unknown_file_cannot_be_read(isfile):
isfile.return_value = False
content = read_file(TMP_TEST_FILE)
assert content == "foo\nbar\n"
def test_read_file_badfile():
with pytest.raises(MoulinetteError): with pytest.raises(MoulinetteError):
read_file(TMP_TEST_FILE + "nope") filesystem.read_file("non existent file")
#
def test_read_file_badpermissions(): #
#def test_read_file():
switch_to_non_root_user() #
with pytest.raises(MoulinetteError): # content = read_file(TMP_TEST_FILE)
read_file(TMP_TEST_FILE) # assert content == "foo\nbar\n"
#
#
def test_read_json(): #def test_read_file_badpermissions():
#
content = read_json(TMP_TEST_JSON) # switch_to_non_root_user()
assert "foo" in list(content.keys()) # with pytest.raises(MoulinetteError):
assert content["foo"] == "bar" # read_file(TMP_TEST_FILE)
#
#
def test_read_json_badjson(): #def test_read_json():
#
os.system("echo '{ not valid json lol }' > %s" % TMP_TEST_JSON) # content = read_json(TMP_TEST_JSON)
# assert "foo" in list(content.keys())
with pytest.raises(MoulinetteError): # assert content["foo"] == "bar"
read_json(TMP_TEST_JSON) #
#
#def test_read_json_badjson():
############################################################################### #
# Test file write # # os.system("echo '{ not valid json lol }' > %s" % TMP_TEST_JSON)
############################################################################### #
# with pytest.raises(MoulinetteError):
# read_json(TMP_TEST_JSON)
def test_write_to_existing_file(): #
#
assert os.path.exists(TMP_TEST_FILE) ################################################################################
write_to_file(TMP_TEST_FILE, "yolo\nswag") ## Test file write #
assert read_file(TMP_TEST_FILE) == "yolo\nswag" ################################################################################
#
#
def test_write_to_new_file(): #def test_write_to_existing_file():
#
new_file = "%s/barfile" % TMP_TEST_DIR # assert os.path.exists(TMP_TEST_FILE)
assert not os.path.exists(new_file) # write_to_file(TMP_TEST_FILE, "yolo\nswag")
write_to_file(new_file, "yolo\nswag") # assert read_file(TMP_TEST_FILE) == "yolo\nswag"
assert os.path.exists(new_file) #
assert read_file(new_file) == "yolo\nswag" #
#def test_write_to_new_file():
#
def test_write_to_existing_file_badpermissions(): # new_file = "%s/barfile" % TMP_TEST_DIR
# assert not os.path.exists(new_file)
assert os.path.exists(TMP_TEST_FILE) # write_to_file(new_file, "yolo\nswag")
switch_to_non_root_user() # assert os.path.exists(new_file)
with pytest.raises(MoulinetteError): # assert read_file(new_file) == "yolo\nswag"
write_to_file(TMP_TEST_FILE, "yolo\nswag") #
#
#def test_write_to_existing_file_badpermissions():
def test_write_to_new_file_badpermissions(): #
# assert os.path.exists(TMP_TEST_FILE)
switch_to_non_root_user() # switch_to_non_root_user()
new_file = "%s/barfile" % TMP_TEST_DIR # with pytest.raises(MoulinetteError):
assert not os.path.exists(new_file) # write_to_file(TMP_TEST_FILE, "yolo\nswag")
with pytest.raises(MoulinetteError): #
write_to_file(new_file, "yolo\nswag") #
#def test_write_to_new_file_badpermissions():
#
def test_write_to_folder(): # switch_to_non_root_user()
# new_file = "%s/barfile" % TMP_TEST_DIR
with pytest.raises(AssertionError): # assert not os.path.exists(new_file)
write_to_file(TMP_TEST_DIR, "yolo\nswag") # with pytest.raises(MoulinetteError):
# write_to_file(new_file, "yolo\nswag")
#
def test_write_inside_nonexistent_folder(): #
#def test_write_to_folder():
with pytest.raises(AssertionError): #
write_to_file("/toto/test", "yolo\nswag") # with pytest.raises(AssertionError):
# write_to_file(TMP_TEST_DIR, "yolo\nswag")
#
def test_write_to_file_with_a_list(): #
#def test_write_inside_nonexistent_folder():
assert os.path.exists(TMP_TEST_FILE) #
write_to_file(TMP_TEST_FILE, ["yolo", "swag"]) # with pytest.raises(AssertionError):
assert read_file(TMP_TEST_FILE) == "yolo\nswag" # write_to_file("/toto/test", "yolo\nswag")
#
#
def test_append_to_existing_file(): #def test_write_to_file_with_a_list():
#
assert os.path.exists(TMP_TEST_FILE) # assert os.path.exists(TMP_TEST_FILE)
append_to_file(TMP_TEST_FILE, "yolo\nswag") # write_to_file(TMP_TEST_FILE, ["yolo", "swag"])
assert read_file(TMP_TEST_FILE) == "foo\nbar\nyolo\nswag" # assert read_file(TMP_TEST_FILE) == "yolo\nswag"
#
#
def test_append_to_new_file(): #def test_append_to_existing_file():
#
new_file = "%s/barfile" % TMP_TEST_DIR # assert os.path.exists(TMP_TEST_FILE)
assert not os.path.exists(new_file) # append_to_file(TMP_TEST_FILE, "yolo\nswag")
append_to_file(new_file, "yolo\nswag") # assert read_file(TMP_TEST_FILE) == "foo\nbar\nyolo\nswag"
assert os.path.exists(new_file) #
assert read_file(new_file) == "yolo\nswag" #
#def test_append_to_new_file():
#
def text_write_dict_to_json(): # new_file = "%s/barfile" % TMP_TEST_DIR
# assert not os.path.exists(new_file)
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]} # append_to_file(new_file, "yolo\nswag")
write_to_json(TMP_TEST_FILE, dummy_dict) # assert os.path.exists(new_file)
j = read_json(TMP_TEST_FILE) # assert read_file(new_file) == "yolo\nswag"
assert "foo" in list(j.keys()) #
assert "bar" in list(j.keys()) #
assert j["foo"] == 42 #def text_write_dict_to_json():
assert j["bar"] == ["a", "b", "c"] #
assert read_file(TMP_TEST_FILE) == "foo\nbar\nyolo\nswag" # dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
# write_to_json(TMP_TEST_FILE, dummy_dict)
# j = read_json(TMP_TEST_FILE)
def text_write_list_to_json(): # assert "foo" in list(j.keys())
# assert "bar" in list(j.keys())
dummy_list = ["foo", "bar", "baz"] # assert j["foo"] == 42
write_to_json(TMP_TEST_FILE, dummy_list) # assert j["bar"] == ["a", "b", "c"]
j = read_json(TMP_TEST_FILE) # assert read_file(TMP_TEST_FILE) == "foo\nbar\nyolo\nswag"
assert j == ["foo", "bar", "baz"] #
#
#def text_write_list_to_json():
def test_write_to_json_badpermissions(): #
# dummy_list = ["foo", "bar", "baz"]
switch_to_non_root_user() # write_to_json(TMP_TEST_FILE, dummy_list)
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]} # j = read_json(TMP_TEST_FILE)
with pytest.raises(MoulinetteError): # assert j == ["foo", "bar", "baz"]
write_to_json(TMP_TEST_FILE, dummy_dict) #
#
#def test_write_to_json_badpermissions():
def test_write_json_inside_nonexistent_folder(): #
# switch_to_non_root_user()
with pytest.raises(AssertionError): # dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
write_to_file("/toto/test.json", ["a", "b"]) # with pytest.raises(MoulinetteError):
# write_to_json(TMP_TEST_FILE, dummy_dict)
#
############################################################################### #
# Test file remove # #def test_write_json_inside_nonexistent_folder():
############################################################################### #
# with pytest.raises(AssertionError):
# write_to_file("/toto/test.json", ["a", "b"])
def test_remove_file(): #
#
rm(TMP_TEST_FILE) ################################################################################
assert not os.path.exists(TMP_TEST_FILE) ## Test file remove #
################################################################################
#
def test_remove_file_badpermissions(): #
#def test_remove_file():
switch_to_non_root_user() #
with pytest.raises(MoulinetteError): # rm(TMP_TEST_FILE)
rm(TMP_TEST_FILE) # assert not os.path.exists(TMP_TEST_FILE)
#
#
def test_remove_directory(): #def test_remove_file_badpermissions():
#
rm(TMP_TEST_DIR, recursive=True) # switch_to_non_root_user()
assert not os.path.exists(TMP_TEST_DIR) # with pytest.raises(MoulinetteError):
# rm(TMP_TEST_FILE)
#
############################################################################### #
# Test permission change # #def test_remove_directory():
############################################################################### #
# rm(TMP_TEST_DIR, recursive=True)
# assert not os.path.exists(TMP_TEST_DIR)
def get_permissions(file_path): #
from stat import ST_MODE #
return (pwd.getpwuid(os.stat(file_path).st_uid).pw_name, ################################################################################
pwd.getpwuid(os.stat(file_path).st_gid).pw_name, ## Test permission change #
oct(os.stat(file_path)[ST_MODE])[-3:]) ################################################################################
#
#
# FIXME - should split the test of chown / chmod as independent tests #def get_permissions(file_path):
def set_permissions(f, owner, group, perms): # from stat import ST_MODE
chown(f, owner, group) # return (pwd.getpwuid(os.stat(file_path).st_uid).pw_name,
chmod(f, perms) # pwd.getpwuid(os.stat(file_path).st_gid).pw_name,
# oct(os.stat(file_path)[ST_MODE])[-3:])
#
def test_setpermissions_file(): #
## FIXME - should split the test of chown / chmod as independent tests
# Check we're at the default permissions #def set_permissions(f, owner, group, perms):
assert get_permissions(TMP_TEST_FILE) == ("root", "root", "700") # chown(f, owner, group)
# chmod(f, perms)
# Change the permissions #
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111) #
#def test_setpermissions_file():
# Check the permissions got changed #
assert get_permissions(TMP_TEST_FILE) == (NON_ROOT_USER, NON_ROOT_GROUP, "111") # # Check we're at the default permissions
# assert get_permissions(TMP_TEST_FILE) == ("root", "root", "700")
# Change the permissions again #
set_permissions(TMP_TEST_FILE, "root", "root", 0o777) # # 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) == ("root", "root", "777") # # Check the permissions got changed
# assert get_permissions(TMP_TEST_FILE) == (NON_ROOT_USER, NON_ROOT_GROUP, "111")
#
def test_setpermissions_directory(): # # Change the permissions again
# set_permissions(TMP_TEST_FILE, "root", "root", 0o777)
# Check we're at the default permissions #
assert get_permissions(TMP_TEST_DIR) == ("root", "root", "755") # # Check the permissions got changed
# assert get_permissions(TMP_TEST_FILE) == ("root", "root", "777")
# Change the permissions #
set_permissions(TMP_TEST_DIR, NON_ROOT_USER, NON_ROOT_GROUP, 0o111) #
#def test_setpermissions_directory():
# Check the permissions got changed #
assert get_permissions(TMP_TEST_DIR) == (NON_ROOT_USER, NON_ROOT_GROUP, "111") # # Check we're at the default permissions
# assert get_permissions(TMP_TEST_DIR) == ("root", "root", "755")
# Change the permissions again #
set_permissions(TMP_TEST_DIR, "root", "root", 0o777) # # 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) == ("root", "root", "777") # # Check the permissions got changed
# assert get_permissions(TMP_TEST_DIR) == (NON_ROOT_USER, NON_ROOT_GROUP, "111")
#
def test_setpermissions_permissiondenied(): # # Change the permissions again
# set_permissions(TMP_TEST_DIR, "root", "root", 0o777)
switch_to_non_root_user() #
# # Check the permissions got changed
with pytest.raises(MoulinetteError): # assert get_permissions(TMP_TEST_DIR) == ("root", "root", "777")
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111) #
#
#def test_setpermissions_permissiondenied():
def test_setpermissions_badfile(): #
# switch_to_non_root_user()
with pytest.raises(MoulinetteError): #
set_permissions("/foo/bar/yolo", NON_ROOT_USER, NON_ROOT_GROUP, 0o111) # with pytest.raises(MoulinetteError):
# set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
#
def test_setpermissions_baduser(): #
#def test_setpermissions_badfile():
with pytest.raises(MoulinetteError): #
set_permissions(TMP_TEST_FILE, "foo", NON_ROOT_GROUP, 0o111) # with pytest.raises(MoulinetteError):
# set_permissions("/foo/bar/yolo", NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
#
def test_setpermissions_badgroup(): #
#def test_setpermissions_baduser():
with pytest.raises(MoulinetteError): #
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, "foo", 0o111) # 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)

View file

@ -4,8 +4,7 @@
# creating test filesystem structure # creating test filesystem structure
$ mkdir tests $ mkdir -p tests/moulinette/{authenticators,interfaces,utils}
$ mkdir tests/moulinette/{authenticators,interfaces,utils}
# move existing tests to new structure # move existing tests to new structure
@ -20,4 +19,5 @@
touch tests/$folder/test_$file touch tests/$folder/test_$file
done done
# renamed multiprocessing.process.Process to BaseProcess

8
requirements.txt Normal file
View file

@ -0,0 +1,8 @@
mock
argcomplete
pytest-mock
requests-mock
bottle
gnupg
ldap
pyyaml