From 84b1f97d8eeac25d0f85e510ccd95b630f7da0b1 Mon Sep 17 00:00:00 2001 From: jpike Date: Tue, 11 Mar 2025 13:09:00 -0400 Subject: [PATCH] Adding a app config file Information related to apps can be added here. Change-Id: Ic022e342d3f11b3344eb7390615c1ee0477d0d88 --- config/app/files/default.json5 | 5 ++ config/app/objects/app_config.py | 49 +++++++++++++++++++ .../configuration_file_locations_manager.py | 36 ++++++++++++++ config/configuration_manager.py | 19 +++++++ testcases/conftest.py | 1 + unit_tests/config/app/app_config_test.py | 35 +++++++++++++ unit_tests/config/app/custom_app_config.json5 | 5 ++ 7 files changed, 150 insertions(+) create mode 100644 config/app/files/default.json5 create mode 100644 config/app/objects/app_config.py create mode 100644 unit_tests/config/app/app_config_test.py create mode 100644 unit_tests/config/app/custom_app_config.json5 diff --git a/config/app/files/default.json5 b/config/app/files/default.json5 new file mode 100644 index 00000000..2ddd7c67 --- /dev/null +++ b/config/app/files/default.json5 @@ -0,0 +1,5 @@ +{ + base_application_path: "/usr/local/share/applications/helm/", + istio_app_name: "istio", + metric_server_app_name: "metrics-server" +} \ No newline at end of file diff --git a/config/app/objects/app_config.py b/config/app/objects/app_config.py new file mode 100644 index 00000000..ff779e83 --- /dev/null +++ b/config/app/objects/app_config.py @@ -0,0 +1,49 @@ +import json5 + + +class AppConfig: + """ + Class to hold App config + """ + + def __init__(self, config: str): + try: + json_data = open(config) + except FileNotFoundError: + print(f"Could not find the app config file: {config}") + raise + + app_dict = json5.load(json_data) + self.base_application_path = app_dict["base_application_path"] + self.istio_app_name = app_dict["istio_app_name"] + self.metric_server_app_name = app_dict["metric_server_app_name"] + + def get_base_application_path(self) -> str: + """ + Getter for base application path + + Returns: + str: the base application path + + """ + return self.base_application_path + + def get_istio_app_name(self) -> str: + """ + Getter for istio app name + + Returns: + str: the istio app name path + + """ + return self.istio_app_name + + def get_metric_server_app_name(self) -> str: + """ + Getter for metric server app name + + Returns: + str: the metric server app name + + """ + return self.metric_server_app_name diff --git a/config/configuration_file_locations_manager.py b/config/configuration_file_locations_manager.py index da5d0c8b..2e65ddd7 100644 --- a/config/configuration_file_locations_manager.py +++ b/config/configuration_file_locations_manager.py @@ -19,6 +19,7 @@ class ConfigurationFileLocationsManager: self.rest_api_config_file = None self.security_config_file = None self.usm_config_file = None + self.app_config_file = None def set_configs_from_pytest_args(self, session: Session): """ @@ -70,6 +71,10 @@ class ConfigurationFileLocationsManager: if usm_config_file: self.set_security_config_file(usm_config_file) + app_config_file = session.config.getoption("--app_config_file") + if app_config_file: + self.set_app_config_file(app_config_file) + def set_configs_from_options_parser(self, parser: OptionParser = None): """ Sets the config files from options parser. @@ -124,6 +129,10 @@ class ConfigurationFileLocationsManager: if usm_config_file: self.set_usm_config_file(usm_config_file) + app_config_file = options.app_config_file + if app_config_file: + self.set_app_config_file(app_config_file) + def _add_options(self, parser: OptionParser): """ Adds the command line options we can expect. @@ -214,6 +223,14 @@ class ConfigurationFileLocationsManager: help="The Usm config file", ) + parser.add_option( + "--app_config_file", + action="store", + type="str", + dest="app_config_file", + help="The app config file", + ) + options, args = parser.parse_args() return options @@ -425,3 +442,22 @@ class ConfigurationFileLocationsManager: """ self.usm_config_file = usm_config_file + + def get_app_config_file(self) -> str: + """ + Getter for app config file + + Returns: + str: the app config file + """ + return self.app_config_file + + def set_app_config_file(self, app_config_file: str): + """ + Setter for app config file + + Args: + app_config_file (str): the app config file + + """ + self.app_config_file = app_config_file diff --git a/config/configuration_manager.py b/config/configuration_manager.py index 316889e5..fb14d914 100644 --- a/config/configuration_manager.py +++ b/config/configuration_manager.py @@ -1,3 +1,4 @@ +from config.app.objects.app_config import AppConfig from config.configuration_file_locations_manager import ConfigurationFileLocationsManager from config.database.objects.database_config import DatabaseConfig from config.docker.objects.docker_config import DockerConfig @@ -29,6 +30,7 @@ class ConfigurationManagerClass: self.rest_api_config: RestAPIConfig = None self.security_config: SecurityConfig = None self.usm_config: UsmConfig = None + self.app_config: AppConfig = None self.configuration_locations_manager = None def is_config_loaded(self) -> bool: @@ -96,6 +98,10 @@ class ConfigurationManagerClass: if not usm_config_file: usm_config_file = get_stx_resource_path("config/usm/files/default.json5") + app_config_file = config_file_locations.get_app_config_file() + if not app_config_file: + app_config_file = get_stx_resource_path("config/app/files/default.json5") + if not self.loaded: try: self.lab_config = LabConfig(lab_config_file) @@ -108,6 +114,7 @@ class ConfigurationManagerClass: self.rest_api_config = RestAPIConfig(rest_api_config_file) self.security_config = SecurityConfig(security_config_file) self.usm_config = UsmConfig(usm_config_file) + self.app_config = AppConfig(app_config_file) self.loaded = True except FileNotFoundError as e: print(f"Unable to load the config using file: {str(e.filename)} ") @@ -213,6 +220,16 @@ class ConfigurationManagerClass: """ return self.usm_config + def get_app_config(self) -> AppConfig: + """ + Getter for app config + + Returns: + AppConfig: the app config + + """ + return self.app_config + def get_config_pytest_args(self) -> [str]: """ Returns the configuration file locations as pytest args. @@ -242,6 +259,8 @@ class ConfigurationManagerClass: pytest_config_args.append(f"--security_config_file={self.configuration_locations_manager.get_security_config_file()}") if self.configuration_locations_manager.usm_config_file: pytest_config_args.append(f"--usm_config_file={self.configuration_locations_manager.get_usm_config_file()}") + if self.configuration_locations_manager.app_config_file: + pytest_config_args.append(f"--app_config_file={self.configuration_locations_manager.get_app_config_file()}") return pytest_config_args diff --git a/testcases/conftest.py b/testcases/conftest.py index 558a4d0e..a1e7b5c1 100644 --- a/testcases/conftest.py +++ b/testcases/conftest.py @@ -25,6 +25,7 @@ def pytest_addoption(parser: Any): parser.addoption("--rest_api_config_file", action="store") parser.addoption("--security_config_file", action="store") parser.addoption("--usm_config_file", action="store") + parser.addoption("--app_config_file", action="store") def pytest_sessionstart(session: Any): diff --git a/unit_tests/config/app/app_config_test.py b/unit_tests/config/app/app_config_test.py new file mode 100644 index 00000000..c6d64ae6 --- /dev/null +++ b/unit_tests/config/app/app_config_test.py @@ -0,0 +1,35 @@ +from config.configuration_file_locations_manager import ConfigurationFileLocationsManager +from config.configuration_manager import ConfigurationManagerClass +from framework.resources.resource_finder import get_stx_resource_path + + +def test_default_app_config(): + """ + Tests that the default app configuration is as expected. + + """ + configuration_manager = ConfigurationManagerClass() + config_file_locations = ConfigurationFileLocationsManager() + configuration_manager.load_configs(config_file_locations) + default_config = configuration_manager.get_app_config() + assert default_config is not None, "Default app config wasn't loaded successfully" + assert default_config.get_base_application_path() == "/usr/local/share/applications/helm/", "default base path was incorrect" + assert default_config.get_istio_app_name() == "istio", "istio default app name was incorrect" + assert default_config.get_metric_server_app_name() == "metrics-server", "metric server default name was incorrect" + + +def test_custom_app_config(): + """ + Tests that we can load a custom app configuration. + """ + custom_file = get_stx_resource_path("unit_tests/config/app/custom_app_config.json5") + configuration_manager = ConfigurationManagerClass() + config_file_locations = ConfigurationFileLocationsManager() + config_file_locations.set_app_config_file(custom_file) + configuration_manager.load_configs(config_file_locations) + + custom_config = configuration_manager.get_app_config() + assert custom_config is not None, "Default app config wasn't loaded successfully" + assert custom_config.get_base_application_path() == "fake_path", "custom base path was incorrect" + assert custom_config.get_istio_app_name() == "istio_custom", "istio custom app name was incorrect" + assert custom_config.get_metric_server_app_name() == "metrics-server_custom", "metric server custom name was incorrect" diff --git a/unit_tests/config/app/custom_app_config.json5 b/unit_tests/config/app/custom_app_config.json5 new file mode 100644 index 00000000..d0e28229 --- /dev/null +++ b/unit_tests/config/app/custom_app_config.json5 @@ -0,0 +1,5 @@ +{ + base_application_path: "fake_path", + istio_app_name: "istio_custom", + metric_server_app_name: "metrics-server_custom" +} \ No newline at end of file