Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding unit tests for schema.py #417

Merged
merged 12 commits into from
Apr 5, 2023
21 changes: 10 additions & 11 deletions scripts/plugins/config/parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import sys

from munch import DefaultMunch
from ..logging import logger
from ..utilities import load_yaml
from .schema import SchemaObject
from plugins.logging import logger
from plugins.utilities import load_yaml
from plugins.config.schema import SchemaObject


def get_config_list(config_file, schema_file):
Expand All @@ -25,24 +25,23 @@ def get_config_list(config_file, schema_file):
f"Required config file was not found. \
To fix this please add the following file: [{e.filename}]"
)
sys.exit(101)
raise e
schema = convert_yaml_to_dict(schema_yaml)
schema_properties_list = generate_schema_keys(schema)
schema_list = generate_populated_schema_list(schema, schema_properties_list, config_yaml)
(
cli_config_list,
options_config_list,
required_config_list,
missing_required_config_list,
) = populate_parsed_configurations(schema_list)
if required_config_list:
if missing_required_config_list:
logger.warning("\n~~~~~ REQUIRED CONFIG ~~~~~")
for item in required_config_list:
for item in missing_required_config_list:
logger.error(
f"Configuration value: [{item.name}] is required. Please ensure you "
"set this configuration value in the plugins `bitops.config.yaml`"
)
logger.debug(item)
sys.exit(1)
sys.exit(1)
return cli_config_list, options_config_list


Expand Down Expand Up @@ -131,7 +130,7 @@ def populate_parsed_configurations(schema_list):
options_config_list = [
item for item in parsed_schema_list if item.schema_property_type == "options"
]
required_config_list = [
missing_required_config_list = [
item for item in parsed_schema_list if item.required is True and not item.value
]

Expand All @@ -144,4 +143,4 @@ def populate_parsed_configurations(schema_list):
logger.debug("\n~~~~~ BAD SCHEMA CONFIG ~~~~~")
for item in bad_config_list:
logger.debug(item)
return (cli_config_list, options_config_list, required_config_list)
return (cli_config_list, options_config_list, missing_required_config_list)
15 changes: 11 additions & 4 deletions scripts/plugins/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os
import re

from ..logging import logger
from ..settings import BITOPS_FAST_FAIL_MODE
from ..utilities import add_value_to_env
from plugins.logging import logger
from plugins.settings import BITOPS_FAST_FAIL_MODE
from plugins.utilities import add_value_to_env


class SchemaObject: # pylint: disable=too-many-instance-attributes
Expand Down Expand Up @@ -165,7 +165,14 @@ def _apply_data_type(data_type, convert_value):

if BITOPS_FAST_FAIL_MODE:
logger.error(f"Data type not supported: [{data_type}]")
sys.exit(101)
raise SchemaUnsupportedDataType(f"Data type not supported: [{data_type}]")

logger.warning(f"Data type not supported: [{data_type}]")
return None


class SchemaUnsupportedDataType(Exception):
"""Raised when an unsupported data type is passed in to a function"""

def __init__(self, message):
self.message = message
7 changes: 5 additions & 2 deletions scripts/plugins/deploy_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ def deploy_plugins(): # pylint: disable=too-many-locals,too-many-branches,too-m

if plugin_deploy_schema_parsing_flag:
logger.debug("running bitops schema parsing...")
cli_config_list, _ = get_config_list(opsrepo_config_file, plugin_schema_file)

try:
cli_config_list, _ = get_config_list(opsrepo_config_file, plugin_schema_file)
except FileNotFoundError:
logger.error("Schema and Configuration files are required. Exiting...")
sys.exit(2)
# Compose a CLI and export it as "BITOPS_{PLUGIN}_CLI}"
cli = PluginConfigCLI(cli_config_list)
os.environ[cli.env] = cli.command
Expand Down
2 changes: 2 additions & 0 deletions scripts/plugins/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def parse_config(dictionary, dotted_key_list):
BITOPS_ENV_logging_level = os.environ.get("BITOPS_LOGGING_LEVEL")
BITOPS_ENV_logging_filename = os.environ.get("BITOPS_LOGGING_FILENAME")
BITOPS_ENV_plugin_dir = os.environ.get("BITOPS_PLUGIN_DIR")
BITOPS_ENV_logging_path = os.environ.get("BITOPS_LOGGING_PATH")

BITOPS_ENV_default_folder = os.environ.get("BITOPS_DEFAULT_FOLDER")
# v2.0.0: Fallback to 'ENVIRONMENT' in case when 'BITOPS_ENVIRONMENT' is not set
Expand Down Expand Up @@ -143,6 +144,7 @@ def parse_config(dictionary, dotted_key_list):
)

BITOPS_LOGGING_PATH = get_first(
BITOPS_ENV_logging_path,
parse_config(bitops_user_configuration, "bitops.logging.path"),
parse_config(bitops_build_configuration, "bitops.logging.path"),
"/var/log/bitops",
Expand Down
5 changes: 3 additions & 2 deletions scripts/plugins/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ def add_value_to_env(export_env, value):

def load_yaml(filename: str) -> Union[dict, None]:
"""
This function attempts to load a YAML file from a given location,
and exits if the file is not found. It returns the loaded YAML file if successful.
This function attempts to load a YAML file from a given location.
It raises a FileNotFoundError exception if the file is not found
and returns the loaded YAML file if successful.
"""
out_yaml = None
with open(filename, "r", encoding="utf8") as stream:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
echo "In before_test.sh"

ls ../../

5 changes: 5 additions & 0 deletions scripts/tests/unit/assets/example.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
example:
cli:
example-file: this.example-file
options:
skip-example: true
17 changes: 17 additions & 0 deletions scripts/tests/unit/assets/example.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
example:
type: object
properties:
cli:
type: object
properties:
example-file:
type: string
parameter: example-file
export_env: EXAMPLE_FILE
options:
type: object
properties:
skip-example:
type: boolean
parameter: skip-example
export_env: SKIP_EXAMPLE
Loading