Skip to content

Commit

Permalink
Add tests to class_generator (#1942)
Browse files Browse the repository at this point in the history
* Add tests to class_generator

* Add tests to class_generator
  • Loading branch information
myakove authored Jul 28, 2024
1 parent c8df431 commit 949d158
Show file tree
Hide file tree
Showing 14 changed files with 1,778 additions and 2,913 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fcn_exclude_functions =
template,
meta,
tmp_path_factory,
tmpdir_factory,

enable-extensions =
FCN,
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ package.json
package-lock.json

# class generator script
*-debug.json
script/resource/debug/*-debug.json
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ repos:
rev: v1.5.0
hooks:
- id: detect-secrets
args: [--exclude-files=scripts/resource/tests/manifests/Pod-debug.json]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.4
Expand Down
96 changes: 60 additions & 36 deletions scripts/resource/class_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,69 +421,53 @@ def get_user_args_from_interactive() -> Tuple[str, str]:
return kind, api_link


@click.command("Resource class generator")
@click.option(
"-k",
"--kind",
type=click.STRING,
help="The Kind to generate the class for, Needs working cluster with admin privileges",
)
@click.option(
"-l",
"--api-link",
help="A link to the resource doc/api in the web",
)
@click.option(
"-o",
"--overwrite",
is_flag=True,
help="Output file overwrite existing file if passed",
)
@click.option("-d", "--debug", is_flag=True, help="Save all command output to debug file")
@click.option("-i", "--interactive", is_flag=True, help="Enable interactive mode")
@click.option("--dry-run", is_flag=True, help="Run the script without writing to file")
@click.option("--debug-file", type=click.Path(exists=True), help="Run the script from debug file. (generated by -d)")
def main(
kind: str, api_link: str, overwrite: bool, interactive: bool, dry_run: bool, debug: bool, debug_file: str
) -> None:
def class_generator(
kind: str = "",
api_link: str = "",
overwrite: bool = False,
interactive: bool = False,
dry_run: bool = False,
debug: bool = False,
process_debug_file: str = "",
generated_file_output_dir: str = "ocp_resources",
) -> str:
"""
Generates a class for a given Kind.
"""
debug_content: Dict[str, str] = {}
output_debug_file_path = os.path.join(os.path.dirname(__file__), "debug", f"{kind}-debug.json")

if debug_file:
dry_run = True
if process_debug_file:
debug = False
with open(debug_file) as fd:
with open(process_debug_file) as fd:
debug_content = json.load(fd)

kind_data = debug_content["explain"]
namespaced = debug_content["namespace"] == "1"
namespaced = debug_content["namespace"].strip() == "1"
api_link = "https://debug.explain"

else:
if not check_cluster_available():
LOGGER.error(
"Cluster not available, The script needs a running cluster and admin privileges to get the explain output"
)
return
return ""

if interactive:
kind, api_link = get_user_args_from_interactive()

if not kind or not api_link:
LOGGER.error("Kind or API link not provided")
return
return ""

validate_api_link_schema(value=api_link)

if not check_kind_exists(kind=kind):
return
return ""

explain_output = get_kind_data(kind=kind, debug=debug, output_debug_file_path=output_debug_file_path)
if not explain_output:
return
return ""

namespaced = explain_output["namespaced"]
kind_data = explain_output["data"]
Expand All @@ -497,16 +481,56 @@ def main(
debug_content=debug_content,
)
if not resource_dict:
return
return ""

generate_resource_file_from_dict(resource_dict=resource_dict, overwrite=overwrite, dry_run=dry_run)
generated_py_file = generate_resource_file_from_dict(
resource_dict=resource_dict, overwrite=overwrite, dry_run=dry_run, output_dir=generated_file_output_dir
)

if not dry_run:
run_command(command=shlex.split("pre-commit run --all-files"), verify_stderr=False, check=False)
run_command(
command=shlex.split(f"pre-commit run --files {generated_py_file}"), verify_stderr=False, check=False
)

if debug:
LOGGER.info(f"Debug output saved to {output_debug_file_path}")

return generated_py_file


@click.command("Resource class generator")
@click.option(
"-k",
"--kind",
type=click.STRING,
help="The Kind to generate the class for, Needs working cluster with admin privileges",
)
@click.option(
"-l",
"--api-link",
help="A link to the resource doc/api in the web",
)
@click.option(
"-o",
"--overwrite",
is_flag=True,
help="Output file overwrite existing file if passed",
)
@click.option("-d", "--debug", is_flag=True, help="Save all command output to debug file")
@click.option("-i", "--interactive", is_flag=True, help="Enable interactive mode")
@click.option("--dry-run", is_flag=True, help="Run the script without writing to file")
@click.option("--debug-file", type=click.Path(exists=True), help="Run the script from debug file. (generated by -d)")
def main(kind: str, api_link: str, overwrite: bool, interactive: bool, dry_run: bool, debug: bool, debug_file: str):
return class_generator(
kind=kind,
api_link=api_link,
overwrite=overwrite,
interactive=interactive,
dry_run=dry_run,
debug=debug,
process_debug_file=debug_file,
)


if __name__ == "__main__":
main()
148 changes: 0 additions & 148 deletions scripts/resource/tests/manifests/DataScienceCluster.explain

This file was deleted.

12 changes: 12 additions & 0 deletions scripts/resource/tests/manifests/Deployment-debug.json

Large diffs are not rendered by default.

Loading

0 comments on commit 949d158

Please sign in to comment.