Skip to content

Commit

Permalink
examples(tests): Align examples plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZachiNachshon committed Mar 11, 2024
1 parent 4e3d7d4 commit b5fcb23
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from provisioner.infra.context import Context
from provisioner.shared.collaborators import CoreCollaborators
from provisioner_features_lib.remote.typer_remote_opts import CliRemoteOpts
from provisioner_features_lib.vcs.anchor_runner import (
from provisioner_features_lib.anchor.anchor_runner import (
AnchorCmdRunner,
AnchorRunnerCmdArgs,
)
Expand All @@ -30,6 +30,7 @@ def __init__(

def print(self) -> None:
if self.remote_opts:
print(self.remote_opts)
self.remote_opts.print()
logger.debug("AnchorCmdArgs: \n" + f" anchor_run_command: {self.anchor_run_command}\n")

Expand All @@ -43,10 +44,7 @@ def run(self, ctx: Context, args: AnchorCmdArgs) -> None:
ctx=ctx,
args=AnchorRunnerCmdArgs(
anchor_run_command=args.anchor_run_command,
github_organization=args.github_organization,
repository_name=args.repository_name,
branch_name=args.branch_name,
git_access_token=args.git_access_token,
vcs_opts=args.vcs_opts,
remote_opts=args.remote_opts,
),
collaborators=CoreCollaborators(ctx),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from provisioner.test_lib.test_env import TestEnv

from provisioner_examples_plugin.anchor.anchor_cmd import AnchorCmd, AnchorCmdArgs
from provisioner_features_lib.remote.typer_remote_opts_fakes import TestDataRemoteOpts
from provisioner_features_lib.vcs.typer_vcs_opts_fakes import TestDataVersionControlOpts

ANCHOR_RUN_COMMAND_RUNNER_PATH = "provisioner_features_lib.anchor.anchor_runner.AnchorCmdRunner"

Expand All @@ -25,22 +27,20 @@ class AnchorCmdTestShould(unittest.TestCase):
def test_anchor_cmd_run_with_expected_arguments(self, run_call: mock.MagicMock) -> None:
env = TestEnv.create()
ctx = env.get_context()
fake_vcs_cfg = TestDataVersionControlOpts().create_fake_cli_vcs_opts()
fake_remote_cfg = TestDataRemoteOpts().create_fake_cli_remote_opts()

def assertion_callback(args):
self.assertEqual(EXPECTED_ANCHOR_RUN_COMMAND, args.anchor_run_command)
self.assertEqual(EXPECTED_GITHUB_ORGANIZATION, args.github_organization)
self.assertEqual(EXPECTED_REPOSITORY_NAME, args.repository_name)
self.assertEqual(EXPECTED_BRANCH_NAME, args.branch_name)
self.assertEqual(EXPECTED_GIT_ACCESS_TOKEN, args.git_access_token)
self.assertEqual(fake_vcs_cfg, args.vcs_opts)
self.assertEqual(fake_remote_cfg, args.remote_opts)

AnchorCmd().run(
ctx=ctx,
args=AnchorCmdArgs(
anchor_run_command=EXPECTED_ANCHOR_RUN_COMMAND,
github_organization=EXPECTED_GITHUB_ORGANIZATION,
repository_name=EXPECTED_REPOSITORY_NAME,
branch_name=EXPECTED_BRANCH_NAME,
git_access_token=EXPECTED_GIT_ACCESS_TOKEN,
vcs_opts=fake_vcs_cfg,
remote_opts=fake_remote_cfg,
),
)
Assertion.expect_call_argument(self, run_call, arg_name="ctx", expected_value=env.get_context())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ def test_cli_anchor_cmd_with_args_success(self, run_call: mock.MagicMock) -> Non
"--verbose",
"examples",
"anchor",
f"--org={EXPECTED_GITHUB_ORGANIZATION}",
f"--repo={EXPECTED_REPOSITORY_NAME}",
f"--branch={EXPECTED_BRANCH_NAME}",
f"--git-access-token={EXPECTED_GIT_ACCESS_TOKEN}",
"run-command",
f"--anchor-run-command={EXPECTED_ANCHOR_RUN_COMMAND}",
f"--github-organization={EXPECTED_GITHUB_ORGANIZATION}",
f"--repository-name={EXPECTED_REPOSITORY_NAME}",
f"--branch-name={EXPECTED_BRANCH_NAME}",
f"--git-access-token={EXPECTED_GIT_ACCESS_TOKEN}",
],
)

def assertion_callback(args):
self.assertEqual(EXPECTED_ANCHOR_RUN_COMMAND, args.anchor_run_command)
self.assertEqual(EXPECTED_GITHUB_ORGANIZATION, args.github_organization)
self.assertEqual(EXPECTED_REPOSITORY_NAME, args.repository_name)
self.assertEqual(EXPECTED_BRANCH_NAME, args.branch_name)
self.assertEqual(EXPECTED_GIT_ACCESS_TOKEN, args.git_access_token)
self.assertEqual(EXPECTED_GITHUB_ORGANIZATION, args.vcs_opts.github_organization)
self.assertEqual(EXPECTED_REPOSITORY_NAME, args.vcs_opts.repository_name)
self.assertEqual(EXPECTED_BRANCH_NAME, args.vcs_opts.branch_name)
self.assertEqual(EXPECTED_GIT_ACCESS_TOKEN, args.vcs_opts.git_access_token)

Assertion.expect_call_arguments(self, run_call, arg_name="args", assertion_callable=assertion_callback)
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

from provisioner.test_lib.assertions import Assertion
from provisioner.test_lib.test_env import TestEnv
from provisioner_features_lib.remote.typer_remote_opts import CliRemoteOpts

from provisioner_examples_plugin.ansible.hello_world_runner import (
HelloWorldRunner,
HelloWorldRunnerArgs,
)
from provisioner_features_lib.remote.typer_remote_opts_fakes import TestDataRemoteOpts

ANSIBLE_HELLO_WORLD_RUNNER_PATH = "provisioner_examples_plugin.ansible.hello_world_runner.HelloWorldRunner"

EXPECTED_USERNAME = "test-user"

#
# To run these directly from the terminal use:
# poetry run coverage run -m pytest provisioner_examples_plugin/anchor/anchor_cmd_test.py
# poetry run coverage run -m pytest provisioner_examples_plugin/ansible/hello_world_runner_test.py
#
class HelloWorldRunnerTestShould(unittest.TestCase):
@mock.patch(f"{ANSIBLE_HELLO_WORLD_RUNNER_PATH}.run")
def test_ansible_hello_runner_run_with_expected_arguments(self, run_call: mock.MagicMock) -> None:
env = TestEnv.create()
ctx = env.get_context()
expected_remote_opts = CliRemoteOpts.maybe_get()
expected_remote_opts = TestDataRemoteOpts.create_fake_cli_remote_opts()

def assertion_callback(args):
self.assertEqual(expected_remote_opts, args.remote_opts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from provisioner.config.domain.config import ProvisionerConfig
from provisioner.domain.serialize import SerializationBase
from provisioner_features_lib.vcs.domain.config import VersionControlConfig
from provisioner_features_lib.remote.domain.config import RemoteConfig
from provisioner_features_lib.vcs.domain.config import VersionControlConfig

PLUGIN_NAME = "example-plugin"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#!/usr/bin/env python3

import yaml
from provisioner_examples_plugin.config.domain.config import ExamplesConfig
from provisioner_features_lib.remote.typer_remote_opts_fakes import TEST_REMOTE_CFG_YAML_TEXT

TEST_DATA_HELLO_WORLD_USERNAME = "test-username"

class TestDataExamplesConfig:
TEST_DATA_HELLO_WORLD_USERNAME = "test-username"
TEST_DATA_YAML_TEXT = f"""
hello_world:
username: {TEST_DATA_HELLO_WORLD_USERNAME}
"""

class TestDataExamplesConfig:

@staticmethod
def create_fake_dummy_config() -> ExamplesConfig:
return ExamplesConfig(
hello_world=ExamplesConfig.HelloWorldConfig(
username=TestDataExamplesConfig.TEST_DATA_HELLO_WORLD_USERNAME,
)
)
def create_fake_example_config() -> ExamplesConfig:
cfg_with_remote = TEST_DATA_YAML_TEXT + "\n" + TEST_REMOTE_CFG_YAML_TEXT
cfg_dict = yaml.safe_load(cfg_with_remote)
example_cfg = ExamplesConfig(cfg_dict)
return example_cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@

typer_remote_opts: TyperRemoteOpts = None


def append_to_cli(app: typer.Typer):
def load_config():
# Load plugin configuration
ConfigManager.instance().load_plugin_config(PLUGIN_NAME, CONFIG_INTERNAL_PATH, cls=ExamplesConfig)

def append_to_cli(app: typer.Typer):
examples_cfg = ConfigManager.instance().get_plugin_config(PLUGIN_NAME)
if examples_cfg.remote is None:
raise Exception("Remote configuration is mandatory and not found in plugin configuration")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from provisioner.config.domain.config import ProvisionerConfig
from provisioner.config.manager.config_manager import ConfigManager

from provisioner_examples_plugin import main
from provisioner_examples_plugin import main as example_plugin_main

PLUGIN_IMPORT_PATH = "provisioner_examples_plugin.main"

Expand All @@ -27,15 +27,16 @@
app = EntryPoint.create_typer(
title="Provision Everything Anywhere (install plugins from https://zachinachshon.com/provisioner)",
config_resolver_fn=lambda: ConfigManager.instance().load(
main.CONFIG_INTERNAL_PATH, CONFIG_USER_PATH, ProvisionerConfig, debug=debug_pre_init
example_plugin_main.CONFIG_INTERNAL_PATH, CONFIG_USER_PATH, ProvisionerConfig, debug=debug_pre_init
),
)

try:
logger.debug(f"Importing module {PLUGIN_IMPORT_PATH}")
plugin_main_module = importlib.import_module(PLUGIN_IMPORT_PATH)
logger.debug(f"Running module callback on {PLUGIN_IMPORT_PATH}")
main.append_to_cli(app)
example_plugin_main.load_config()
example_plugin_main.append_to_cli(app)
except Exception as ex:
logger.error(f"Failed to import module. import_path: {PLUGIN_IMPORT_PATH}, ex: {ex}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from provisioner.cli.entrypoint import EntryPoint
from provisioner.config.manager.config_manager import ConfigManager
from provisioner.domain.serialize import SerializationBase
from provisioner_features_lib.anchor.domain.config import VersionControlConfig
from provisioner_features_lib.anchor.typer_anchor_opts import TyperVersionControlOpts
from provisioner_features_lib.anchor.typer_anchor_opts_fakes import TestDataAnchorOpts
from provisioner_features_lib.remote.domain.config import RemoteConfig
from provisioner_features_lib.remote.typer_remote_opts import TyperRemoteOpts
from provisioner_features_lib.remote.typer_remote_opts_fakes import *
from provisioner_features_lib.vcs.domain.config import VersionControlConfig
from provisioner_features_lib.vcs.typer_vcs_opts_fakes import TestDataVersionControlOpts

from provisioner_examples_plugin.main import append_to_cli
from provisioner_examples_plugin.config.domain.config import ExamplesConfig
from provisioner_examples_plugin.config.domain.config_fakes import TestDataExamplesConfig

Expand All @@ -26,14 +25,11 @@
class FakeTestAppConfig(SerializationBase):

remote: RemoteConfig = None
anchor: VersionControlConfig = None
dummy: ExamplesConfig = None
vcs: VersionControlConfig = None
hello_world: ExamplesConfig.HelloWorldConfig = None

def __init__(self, remote: RemoteConfig, anchor: VersionControlConfig, dummy: ExamplesConfig) -> None:
super().__init__({})
self.remote = remote
self.anchor = anchor
self.dummy = dummy
def __init__(self, dict_obj: dict) -> None:
super().__init__(dict_obj)

def _try_parse_config(self, dict_obj: dict):
pass
Expand All @@ -43,41 +39,21 @@ def merge(self, other: "SerializationBase") -> "SerializationBase":


def generate_fake_config():
fake_anchor_config = TestDataAnchorOpts.create_fake_anchor_opts()._vcs_config
TyperVersionControlOpts.load(fake_anchor_config)

fake_remote_config = TestDataRemoteOpts.create_fake_remote_opts()._remote_config
TyperRemoteOpts.load(fake_remote_config)

ConfigManager.config = FakeTestAppConfig(
remote=fake_remote_config, anchor=fake_anchor_config, dummy=TestDataExamplesConfig.create_fake_dummy_config()
)


def register_remote_cli_args():
from provisioner_features_lib.remote.typer_remote_opts_callback import (
remote_args_callback,
)

remote_args_callback(
environment=TEST_DATA_ENVIRONMENT,
node_username=TEST_DATA_REMOTE_NODE_USERNAME_1,
node_password=TEST_DATA_REMOTE_NODE_PASSWORD_1,
ssh_private_key_file_path=TEST_DATA_REMOTE_SSH_PRIVATE_KEY_FILE_PATH_1,
ip_discovery_range=TEST_DATA_REMOTE_IP_DISCOVERY_RANGE,
)
return TestDataExamplesConfig.create_fake_example_config()

def register_fake_config(fake_cfg: FakeTestAppConfig):
ConfigManager.instance().config = fake_cfg
ConfigManager.instance().config.dict_obj = fake_cfg.__dict__
ConfigManager.instance().config.dict_obj["plugins"] = {}
ConfigManager.instance().config.dict_obj["plugins"]["example-plugin"] = fake_cfg

def register_module_cli_args():
from provisioner_examples_plugin.main import append_to_cli

append_to_cli(fake_app)


def get_fake_app():
try:
generate_fake_config()
register_remote_cli_args()
fake_cfg = generate_fake_config()
register_fake_config(fake_cfg)
register_module_cli_args()
except Exception as ex:
print(f"Fake provisioner example CLI commands failed to load. ex: {ex}, trace:\n{traceback.format_exc()}")
Expand Down

0 comments on commit b5fcb23

Please sign in to comment.