From 8da5aa055d46530a65e4cbba6a792a0b63874d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Sun, 29 Dec 2019 21:06:28 +0100 Subject: [PATCH 1/6] Improve stability of unit tests --- src/yunohost/tests/test_apps.py | 28 +++++++++++++++++++++++++++ src/yunohost/tests/test_permission.py | 14 ++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/yunohost/tests/test_apps.py b/src/yunohost/tests/test_apps.py index d9102edbe..a85a36061 100644 --- a/src/yunohost/tests/test_apps.py +++ b/src/yunohost/tests/test_apps.py @@ -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.utils.error import YunohostError 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): @@ -60,6 +61,33 @@ def clean(): os.system("systemctl reset-failed nginx") # Reset failed quota for service to avoid running into start-limit rate ? os.system("systemctl start nginx") + # Clean permission + for permission_name in user_permission_list(short=True)["permissions"]: + if "legacy_app" in permission_name or \ + "full_domain_app" in permission_name or \ + "break_yo_system" in permission_name: + permission_delete(permission_name, force=True) + + # Clean database + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE legacy_app' \"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER legacy_app@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE legacy_app__2'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER legacy_app__2@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE legacy_app__3'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER legacy_app__3@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE full_domain'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER full_domain@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE full_domain__2'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER full_domain__2@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE full_domain__3'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER full_domain__3@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE break_yo_system'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER break_yo_system@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE break_yo_system__2'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER break_yo_system__2@localhost'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE break_yo_system__3'\"") + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER break_yo_system__3@localhost'\"") + @pytest.fixture(autouse=True) def check_LDAP_db_integrity_call(): diff --git a/src/yunohost/tests/test_permission.py b/src/yunohost/tests/test_permission.py index 636d9b4b1..c8ff77493 100644 --- a/src/yunohost/tests/test_permission.py +++ b/src/yunohost/tests/test_permission.py @@ -14,6 +14,20 @@ from yunohost.domain import _get_maindomain maindomain = _get_maindomain() 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(): for u in user_list()['users']: From 19fc1806a4d4aec584f76cbfc48253eb0022b02b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Sun, 29 Dec 2019 23:12:03 +0100 Subject: [PATCH 2/6] Fix app catalog tests --- src/yunohost/tests/test_appscatalog.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yunohost/tests/test_appscatalog.py b/src/yunohost/tests/test_appscatalog.py index a51d1085e..c3ece7907 100644 --- a/src/yunohost/tests/test_appscatalog.py +++ b/src/yunohost/tests/test_appscatalog.py @@ -31,8 +31,8 @@ DUMMY_APP_CATALOG = """{ "bar": {"id": "bar", "level": 7, "category": "swag", "manifest":{"description": "Bar"}} }, "categories": [ - {"id": "yolo", "description": "YoLo", "title": "Yolo"}, - {"id": "swag", "description": "sWaG", "title": "Swag"} + {"id": "yolo", "description": "YoLo", "title": {"en": "Yolo"}}, + {"id": "swag", "description": "sWaG", "title": {"en": "Swag"}} ] } """ From 97f50e396cad5a7d0ac31c8cfce18ae0352beba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Sun, 29 Dec 2019 23:36:43 +0100 Subject: [PATCH 3/6] Fix settings boolean value management --- src/yunohost/settings.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/yunohost/settings.py b/src/yunohost/settings.py index 2427f8677..72477e4de 100644 --- a/src/yunohost/settings.py +++ b/src/yunohost/settings.py @@ -23,15 +23,18 @@ def is_boolean(value): arg -- The string to check Returns: - Boolean + (is_boolean, boolean_value) """ if isinstance(value, bool): - return True + return True, value 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: - return False + return False, None # a settings entry is in the form of: @@ -114,7 +117,10 @@ def settings_set(key, value): key_type = settings[key]["type"] 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, received_type=type(value).__name__, expected_type=key_type) elif key_type == "int": From edd607402520153c534878f142fe3b7a1d0c8ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Mon, 30 Dec 2019 00:02:57 +0100 Subject: [PATCH 4/6] Force remove domain --- src/yunohost/tests/test_apps.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/yunohost/tests/test_apps.py b/src/yunohost/tests/test_apps.py index a85a36061..85cd85442 100644 --- a/src/yunohost/tests/test_apps.py +++ b/src/yunohost/tests/test_apps.py @@ -109,7 +109,7 @@ def secondary_domain(request): domain_add("example.test") def remove_example_domain(): - domain_remove("example.test") + domain_remove("example.test", force=True) request.addfinalizer(remove_example_domain) return "example.test" From 7e8a00b9dc2e7b47065cccd22e96a27e96f69bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josu=C3=A9=20Tille?= Date: Mon, 30 Dec 2019 00:11:01 +0100 Subject: [PATCH 5/6] Change scope secondary domain managment --- src/yunohost/tests/test_apps.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yunohost/tests/test_apps.py b/src/yunohost/tests/test_apps.py index 85cd85442..fdfcc9176 100644 --- a/src/yunohost/tests/test_apps.py +++ b/src/yunohost/tests/test_apps.py @@ -102,14 +102,14 @@ def check_permission_for_apps_call(): yield check_permission_for_apps() -@pytest.fixture(scope="session") +@pytest.fixture(scope="module") def secondary_domain(request): if "example.test" not in domain_list()["domains"]: domain_add("example.test") def remove_example_domain(): - domain_remove("example.test", force=True) + domain_remove("example.test") request.addfinalizer(remove_example_domain) return "example.test" From 94c066dc53f347af09bdc5058521f0ca0de21abe Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Tue, 24 Mar 2020 04:47:21 +0100 Subject: [PATCH 6/6] Factorize stuff in cleaning function to avoid repeating so much stuff --- src/yunohost/tests/test_apps.py | 62 ++++++++------------------------- 1 file changed, 14 insertions(+), 48 deletions(-) diff --git a/src/yunohost/tests/test_apps.py b/src/yunohost/tests/test_apps.py index fdfcc9176..6bc625a91 100644 --- a/src/yunohost/tests/test_apps.py +++ b/src/yunohost/tests/test_apps.py @@ -30,65 +30,31 @@ def clean(): os.system("mkdir -p /etc/ssowat/") app_ssowatconf() - # Gotta first remove break yo system - # because some remaining stuff might - # make the other app_remove crashs ;P - if _is_installed("break_yo_system"): - app_remove("break_yo_system") + test_apps = ["break_yo_system", "legacy_app", "legacy_app__2", "full_domain_app"] - if _is_installed("legacy_app"): - app_remove("legacy_app") + for test_app in test_apps: - if _is_installed("full_domain_app"): - app_remove("full_domain_app") + if _is_installed(test_app): + app_remove(test_app) - to_remove = [] - to_remove += glob.glob("/etc/nginx/conf.d/*.d/*legacy*") - to_remove += glob.glob("/etc/nginx/conf.d/*.d/*full_domain*") - to_remove += glob.glob("/etc/nginx/conf.d/*.d/*break_yo_system*") - for filepath in to_remove: - os.remove(filepath) + for filepath in glob.glob("/etc/nginx/conf.d/*.d/*%s*" % test_app): + os.remove(filepath) + for folderpath in glob.glob("/etc/yunohost/apps/*%s*" % test_app): + shutil.rmtree(folderpath, ignore_errors=True) + for folderpath in glob.glob("/var/www/*%s*" % test_app): + shutil.rmtree(folderpath, ignore_errors=True) - to_remove = [] - to_remove += glob.glob("/etc/yunohost/apps/*legacy_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("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE %s' \"" % test_app) + os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER %s@localhost'\"" % test_app) os.system("systemctl reset-failed nginx") # Reset failed quota for service to avoid running into start-limit rate ? os.system("systemctl start nginx") - # Clean permission + # Clean permissions for permission_name in user_permission_list(short=True)["permissions"]: - if "legacy_app" in permission_name or \ - "full_domain_app" in permission_name or \ - "break_yo_system" in permission_name: + if any(test_app in permission_name for test_app in test_apps): permission_delete(permission_name, force=True) - # Clean database - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE legacy_app' \"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER legacy_app@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE legacy_app__2'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER legacy_app__2@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE legacy_app__3'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER legacy_app__3@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE full_domain'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER full_domain@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE full_domain__2'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER full_domain__2@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE full_domain__3'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER full_domain__3@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE break_yo_system'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER break_yo_system@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE break_yo_system__2'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER break_yo_system__2@localhost'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP DATABASE break_yo_system__3'\"") - os.system("bash -c \"mysql -u root --password=$(cat /etc/yunohost/mysql) 2>/dev/null <<< 'DROP USER break_yo_system__3@localhost'\"") - - @pytest.fixture(autouse=True) def check_LDAP_db_integrity_call(): check_LDAP_db_integrity()