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

Add tests to class_generator #1942

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,
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.

Loading