From 45008a55ad1de6813d11c54debb33bdee3bc25a0 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Sun, 13 Oct 2024 20:36:07 +0500 Subject: [PATCH] Add unit tests for catalog plugin --- .../test_custom_scale_version_checks.py | 40 +++++ .../plugins/catalog/test_get_app_details.py | 152 +++++++++++++++++ .../test_min_max_scale_version_update.py | 155 ++++++++++++++++++ .../test_retrieve_cached_versions_data.py | 80 +++++++++ 4 files changed, 427 insertions(+) create mode 100644 src/middlewared/middlewared/pytest/unit/plugins/catalog/test_custom_scale_version_checks.py create mode 100644 src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py create mode 100644 src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py create mode 100644 src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_custom_scale_version_checks.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_custom_scale_version_checks.py new file mode 100644 index 0000000000000..ade32d9760930 --- /dev/null +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_custom_scale_version_checks.py @@ -0,0 +1,40 @@ +import pytest + +from middlewared.plugins.catalog.apps_util import custom_scale_version_checks + + +@pytest.mark.parametrize('min_version, max_version, sys_scale_version, expected', [ + ( + '21.0', + '23.1', + '22.01', + '' + ), + ( + '22.15', + '21.05', + '22.01', + 'Your TrueNAS system version (22.01) is less than the minimum version (22.15) required by this application.' + ), + ( + '22.01', + '23.01', + '22.01', + '' + ), + ( + '22.01', + '23.02', + '24.05', + 'Your TrueNAS system version (24.05) is greater than the maximum version (23.02) required by this application.' + ), + ( + '22.01', + '21.03', + '22.0', + 'Unable to determine your TrueNAS system version' + ) +]) +def test_custom_scale_version(min_version, max_version, sys_scale_version, expected): + result = custom_scale_version_checks(min_version, max_version, sys_scale_version) + assert result == expected diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py new file mode 100644 index 0000000000000..7c35fbadc23ce --- /dev/null +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_get_app_details.py @@ -0,0 +1,152 @@ +import pytest + +from middlewared.plugins.catalog.apps_util import get_app_details + + +@pytest.mark.parametrize('app_data, versions, expected', [ + ( + { + 'app_readme': '', + 'categories': [ + 'media' + ], + 'description': '', + 'healthy': True, + 'healthy_error': None, + 'home': 'https://actualbudget.org', + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget', + 'latest_version': '1.1.11', + 'latest_app_version': '24.10.1', + 'latest_human_version': '24.10.1_1.1.11', + 'last_update': '12-02-21 00:00:00', + 'name': 'actual-budget', + 'recommended': False, + 'title': 'Actual Budget', + }, + { + '1.0.1': { + 'name': 'chia', + 'categories': [], + 'app_readme': None, + 'location': '/mnt/mypool/ix-applications/catalogs/' + 'github_com_truenas_charts_git_master/charts/chia', + 'healthy': True, + 'supported': True, + 'healthy_error': None, + 'supported': True, + 'required_features': [], + 'version': '1.0.1', + 'human_version': '1.15.12', + 'home': None, + 'readme': None, + 'changelog': None, + 'last_update': '1200-20-00 00:00:00', + 'app_metadata': { + 'name': 'chia', + 'train': 'stable', + 'version': '1.0.1', + 'app_version': '1.0.1', + 'title': 'chia', + 'description': 'desc', + 'home': 'None', + }, + 'schema': { + "groups": [], + "questions": [] + } + } + }, + { + 'app_readme': '', + 'categories': ['media'], + 'description': '', + 'healthy': True, + 'healthy_error': None, + 'home': 'https://actualbudget.org', + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget', + 'latest_version': '1.1.11', + 'latest_app_version': '24.10.1', + 'latest_human_version': '24.10.1_1.1.11', + 'last_update': '12-02-21 00:00:00', + 'name': 'actual-budget', + 'recommended': False, + 'title': 'Actual Budget', + 'versions': { + '1.0.1': { + 'name': 'chia', + 'categories': [], + 'app_readme': None, + 'location': '/path/to/app/1.0.1', + 'healthy': True, + 'supported': True, + 'healthy_error': None, + 'required_features': [], + 'version': '1.0.1', + 'human_version': '1.15.12', + 'home': None, + 'readme': None, + 'changelog': None, + 'last_update': '1200-20-00 00:00:00', + 'app_metadata': { + 'name': 'chia', + 'train': 'stable', + 'version': '1.0.1', + 'app_version': '1.0.1', + 'title': 'chia', + 'description': 'desc', + 'home': 'None', + }, + 'schema': { + 'groups': [], + 'questions': [] + }, + 'values': {} + } + } + } + ), + ( + { + 'app_readme': '', + 'categories': [ + 'media' + ], + 'description': '', + 'healthy': True, + 'healthy_error': None, + 'home': 'https://actualbudget.org', + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget', + 'latest_version': '1.1.11', + 'latest_app_version': '24.10.1', + 'latest_human_version': '24.10.1_1.1.11', + 'last_update': '12-02-21 00:00:00', + 'name': 'actual-budget', + 'recommended': False, + 'title': 'Actual Budget', + }, + {}, + { + 'app_readme': '', + 'categories': ['media'], + 'description': '', + 'healthy': True, + 'healthy_error': None, + 'home': 'https://actualbudget.org', + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget', + 'latest_version': '1.1.11', + 'latest_app_version': '24.10.1', + 'latest_human_version': '24.10.1_1.1.11', + 'last_update': '12-02-21 00:00:00', + 'name': 'actual-budget', + 'recommended': False, + 'title': 'Actual Budget', + 'versions': {} + } + ), +]) +def test_get_app_details(mocker, app_data, versions, expected): + mocker.patch('middlewared.plugins.catalog.apps_util.normalize_questions') + mocker.patch('middlewared.plugins.catalog.apps_util.retrieve_cached_versions_data', return_value=versions) + if isinstance(expected, dict): + result = get_app_details('/path/to/app', app_data, {}) + assert expected == result diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py new file mode 100644 index 0000000000000..c553cf728c222 --- /dev/null +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_min_max_scale_version_update.py @@ -0,0 +1,155 @@ +import pytest + +from middlewared.plugins.catalog.apps_util import minimum_scale_version_check_update + + +@pytest.mark.parametrize('version_data, expected', [ + ( + { + 'healthy': True, + 'supported': True, + 'healthy_error': None, + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.11', + 'last_update': '2024-10-09 20:30:25', + 'human_version': '24.10.1_1.1.11', + 'chart_metadata': { + 'annotations': { + 'min_scale_version': '21.01', + 'max_scale_version': '24.04' + } + }, + 'version': '1.1.11', + 'schema': { + 'groups': [ + { + 'name': 'Actual Budget Configuration', + 'description': 'Configure Actual Budget' + } + ] + } + }, + { + 'healthy': True, + 'supported': False, + 'healthy_error': None, + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.11', + 'last_update': '2024-10-09 20:30:25', + 'human_version': '24.10.1_1.1.11', + 'chart_metadata': { + 'annotations': { + 'min_scale_version': '21.01', + 'max_scale_version': '24.04' + } + }, + 'version': '1.1.11', + 'schema': { + 'groups': [ + { + 'name': 'Actual Budget Configuration', + 'description': 'Configure Actual Budget' + } + ] + } + } + ), + ( + { + 'healthy': True, + 'supported': True, + 'healthy_error': None, + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.11', + 'last_update': '2024-10-09 20:30:25', + 'human_version': '24.10.1_1.1.11', + 'chart_metadata': { + 'annotations': { + 'min_scale_version': '21.01', + 'max_scale_version': '27.04' + } + }, + 'version': '1.1.11', + 'schema': { + 'groups': [ + { + 'name': 'Actual Budget Configuration', + 'description': 'Configure Actual Budget' + } + ] + } + }, + { + 'healthy': True, + 'supported': True, + 'healthy_error': None, + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.11', + 'last_update': '2024-10-09 20:30:25', + 'human_version': '24.10.1_1.1.11', + 'chart_metadata': { + 'annotations': { + 'min_scale_version': '21.01', + 'max_scale_version': '27.04' + } + }, + 'version': '1.1.11', + 'schema': { + 'groups': [ + { + 'name': 'Actual Budget Configuration', + 'description': 'Configure Actual Budget' + } + ] + } + } + ), + ( + { + 'healthy': True, + 'supported': True, + 'healthy_error': None, + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.11', + 'last_update': '2024-10-09 20:30:25', + 'human_version': '24.10.1_1.1.11', + 'chart_metadata': { + 'annotations': { + 'min_scale_version': '26.04', + 'max_scale_version': '24.04' + } + }, + 'version': '1.1.11', + 'schema': { + 'groups': [ + { + 'name': 'Actual Budget Configuration', + 'description': 'Configure Actual Budget' + } + ] + } + }, + { + 'healthy': True, + 'supported': False, + 'healthy_error': None, + 'location': '/mnt/.ix-apps/truenas_catalog/trains/community/actual-budget/1.1.11', + 'last_update': '2024-10-09 20:30:25', + 'human_version': '24.10.1_1.1.11', + 'chart_metadata': { + 'annotations': { + 'min_scale_version': '26.04', + 'max_scale_version': '24.04' + } + }, + 'version': '1.1.11', + 'schema': { + 'groups': [ + { + 'name': 'Actual Budget Configuration', + 'description': 'Configure Actual Budget' + } + ] + } + } + ), +]) +def test_min_max_scale_version_update(mocker, version_data, expected): + mocker.patch('middlewared.plugins.catalog.apps_util.sw_info', return_value={'version': '25.04.0'}) + result = minimum_scale_version_check_update(version_data) + assert result == expected diff --git a/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py new file mode 100644 index 0000000000000..002aa282fb671 --- /dev/null +++ b/src/middlewared/middlewared/pytest/unit/plugins/catalog/test_retrieve_cached_versions_data.py @@ -0,0 +1,80 @@ +import textwrap + +import pytest + +from middlewared.plugins.catalog.apps_util import retrieve_cached_versions_data +from middlewared.service import CallError + + +@pytest.mark.parametrize('file, should_work', [ + ( + ''' + version: 1.0.1 + ''', + False + ), + ( + ''' + { + 'versions': '1.0.1' + } + ''', + False + ), + ( + None, + False + ), + ( + textwrap.dedent( + ''' + { + "1.0.1": { + "name": "chia", + "categories": [], + "app_readme": null, + "location": "/mnt/mypool/ix-applications/catalogs/github_com_truenas_charts_git_master/charts/chia", + "healthy": true, + "supported": true, + "healthy_error": null, + "required_features": [], + "version": "1.0.1", + "human_version": "1.15.12", + "home": null, + "readme": null, + "changelog": null, + "last_update": "2024-10-09 00:00:00", + "app_metadata": { + "name": "chia", + "train": "stable", + "version": "1.0.1", + "app_version": "1.0.1", + "title": "chia", + "description": "desc", + "home": "", + "sources": [], + "maintainers": [], + "run_as_context": [], + "capabilities": [], + "host_mounts": [] + }, + "schema": { + "groups": [], + "questions": [] + } + } + } + ''' + ), + True + ), +]) +def test_retrieve_caches_versions_data(mocker, file, should_work): + mock_file = mocker.mock_open(read_data=file) + mocker.patch('builtins.open', mock_file) + if should_work: + result = retrieve_cached_versions_data('/path/to/app', 'actual-budget') + assert isinstance(result, dict) + else: + with pytest.raises(CallError): + retrieve_cached_versions_data('/path/to/app', 'actual-budget')