Merge pull request #863 from YunoHost/fix_unit_test

Fix unit test and improve test stability
This commit is contained in:
Alexandre Aubin 2020-03-24 04:49:10 +01:00 committed by GitHub
commit 1289d32b7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 31 deletions

View file

@ -23,15 +23,18 @@ def is_boolean(value):
arg -- The string to check arg -- The string to check
Returns: Returns:
Boolean (is_boolean, boolean_value)
""" """
if isinstance(value, bool): if isinstance(value, bool):
return True return True, value
elif isinstance(value, basestring): elif isinstance(value, basestring):
return str(value).lower() in ['true', 'on', 'yes', 'false', 'off', 'no'] if str(value).lower() in ['true', 'on', 'yes', 'false', 'off', 'no']:
return True, str(value).lower() in ['true', 'on', 'yes']
else:
return False, None
else: else:
return False return False, None
# a settings entry is in the form of: # a settings entry is in the form of:
@ -114,7 +117,10 @@ def settings_set(key, value):
key_type = settings[key]["type"] key_type = settings[key]["type"]
if key_type == "bool": if key_type == "bool":
if not is_boolean(value): boolean_value = is_boolean(value)
if boolean_value[0]:
value = boolean_value[1]
else:
raise YunohostError('global_settings_bad_type_for_setting', setting=key, raise YunohostError('global_settings_bad_type_for_setting', setting=key,
received_type=type(value).__name__, expected_type=key_type) received_type=type(value).__name__, expected_type=key_type)
elif key_type == "int": elif key_type == "int":

View file

@ -13,6 +13,7 @@ from yunohost.app import app_install, app_remove, app_ssowatconf, _is_installed,
from yunohost.domain import _get_maindomain, domain_add, domain_remove, domain_list from yunohost.domain import _get_maindomain, domain_add, domain_remove, domain_list
from yunohost.utils.error import YunohostError from yunohost.utils.error import YunohostError
from yunohost.tests.test_permission import check_LDAP_db_integrity, check_permission_for_apps from yunohost.tests.test_permission import check_LDAP_db_integrity, check_permission_for_apps
from yunohost.permission import user_permission_list, permission_delete
def setup_function(function): def setup_function(function):
@ -29,37 +30,30 @@ def clean():
os.system("mkdir -p /etc/ssowat/") os.system("mkdir -p /etc/ssowat/")
app_ssowatconf() app_ssowatconf()
# Gotta first remove break yo system test_apps = ["break_yo_system", "legacy_app", "legacy_app__2", "full_domain_app"]
# because some remaining stuff might
# make the other app_remove crashs ;P
if _is_installed("break_yo_system"):
app_remove("break_yo_system")
if _is_installed("legacy_app"): for test_app in test_apps:
app_remove("legacy_app")
if _is_installed("full_domain_app"): if _is_installed(test_app):
app_remove("full_domain_app") app_remove(test_app)
to_remove = [] for filepath in glob.glob("/etc/nginx/conf.d/*.d/*%s*" % test_app):
to_remove += glob.glob("/etc/nginx/conf.d/*.d/*legacy*") os.remove(filepath)
to_remove += glob.glob("/etc/nginx/conf.d/*.d/*full_domain*") for folderpath in glob.glob("/etc/yunohost/apps/*%s*" % test_app):
to_remove += glob.glob("/etc/nginx/conf.d/*.d/*break_yo_system*") shutil.rmtree(folderpath, ignore_errors=True)
for filepath in to_remove: for folderpath in glob.glob("/var/www/*%s*" % test_app):
os.remove(filepath) shutil.rmtree(folderpath, ignore_errors=True)
to_remove = [] os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE %s' \"" % test_app)
to_remove += glob.glob("/etc/yunohost/apps/*legacy_app*") os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER %s@localhost'\"" % test_app)
to_remove += glob.glob("/etc/yunohost/apps/*full_domain_app*")
to_remove += glob.glob("/etc/yunohost/apps/*break_yo_system*")
to_remove += glob.glob("/var/www/*legacy*")
to_remove += glob.glob("/var/www/*full_domain*")
for folderpath in to_remove:
shutil.rmtree(folderpath, ignore_errors=True)
os.system("systemctl reset-failed nginx") # Reset failed quota for service to avoid running into start-limit rate ? os.system("systemctl reset-failed nginx") # Reset failed quota for service to avoid running into start-limit rate ?
os.system("systemctl start nginx") os.system("systemctl start nginx")
# Clean permissions
for permission_name in user_permission_list(short=True)["permissions"]:
if any(test_app in permission_name for test_app in test_apps):
permission_delete(permission_name, force=True)
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def check_LDAP_db_integrity_call(): def check_LDAP_db_integrity_call():
@ -74,7 +68,7 @@ def check_permission_for_apps_call():
yield yield
check_permission_for_apps() check_permission_for_apps()
@pytest.fixture(scope="session") @pytest.fixture(scope="module")
def secondary_domain(request): def secondary_domain(request):
if "example.test" not in domain_list()["domains"]: if "example.test" not in domain_list()["domains"]:

View file

@ -31,8 +31,8 @@ DUMMY_APP_CATALOG = """{
"bar": {"id": "bar", "level": 7, "category": "swag", "manifest":{"description": "Bar"}} "bar": {"id": "bar", "level": 7, "category": "swag", "manifest":{"description": "Bar"}}
}, },
"categories": [ "categories": [
{"id": "yolo", "description": "YoLo", "title": "Yolo"}, {"id": "yolo", "description": "YoLo", "title": {"en": "Yolo"}},
{"id": "swag", "description": "sWaG", "title": "Swag"} {"id": "swag", "description": "sWaG", "title": {"en": "Swag"}}
] ]
} }
""" """

View file

@ -14,6 +14,20 @@ from yunohost.domain import _get_maindomain
maindomain = _get_maindomain() maindomain = _get_maindomain()
dummy_password = "test123Ynh" dummy_password = "test123Ynh"
# Dirty patch of DNS resolution. Force the DNS to 127.0.0.1 address even if dnsmasq have the public address.
# Mainly used for 'can_access_webpage' function
import socket
dns_cache = {(maindomain, 443, 0, 1): [(2, 1, 6, '', ('127.0.0.1', 443))]}
prv_getaddrinfo = socket.getaddrinfo
def new_getaddrinfo(*args):
try:
return dns_cache[args]
except KeyError:
res = prv_getaddrinfo(*args)
dns_cache[args] = res
return res
socket.getaddrinfo = new_getaddrinfo
def clean_user_groups_permission(): def clean_user_groups_permission():
for u in user_list()['users']: for u in user_list()['users']: