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 parser.py #416

Merged
merged 4 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions scripts/plugins/config/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import plugins.settings as settings # pylint: disable=consider-using-from-import

from munch import DefaultMunch
from ..logging import logger
Expand All @@ -20,25 +21,26 @@ def get_config_list(config_file, schema_file):
try:
schema_yaml = load_yaml(schema_file)
config_yaml = load_yaml(config_file)
except FileNotFoundError:
except FileNotFoundError as exc:
if settings.BITOPS_RUN_MODE == "testing":
raise exc
sys.exit(2)
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 @@ -127,7 +129,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 @@ -140,4 +142,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)
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
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(inc_yaml, required=True):
"""
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
try:
Expand Down
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
18 changes: 18 additions & 0 deletions scripts/tests/unit/assets/example.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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