From 62b87949368213edc10b1c4db5bf51ee6371d70f Mon Sep 17 00:00:00 2001 From: novirium Date: Sun, 7 Feb 2021 12:29:52 +0800 Subject: [PATCH] Fold Agent.load into __init__ --- shepherd/agent/cli.py | 10 +++---- shepherd/agent/core.py | 59 ++++++++++++++++++------------------------ tests/test_core.py | 49 ++++++++++++++++------------------- 3 files changed, 50 insertions(+), 68 deletions(-) diff --git a/shepherd/agent/cli.py b/shepherd/agent/cli.py index 087d13c..c668b48 100644 --- a/shepherd/agent/cli.py +++ b/shepherd/agent/cli.py @@ -63,10 +63,6 @@ def cli(ctx, default_config_path, local_operation, only_default_layer, new_devic version_text = pkg_resources.get_distribution("shepherd") log.info(F"Shepherd Agent [{version_text}]") - agent = core.Agent() - ctx.ensure_object(SimpleNamespace) - ctx.obj.agent = agent - # Drop down to subcommand if it doesn't need default config file processing if ctx.invoked_subcommand == "template": return @@ -99,7 +95,9 @@ def cli(ctx, default_config_path, local_operation, only_default_layer, new_devic if only_default_layer: use_custom_config = False - agent.load(default_config_path, use_custom_config, control_enabled, new_device_mode) + agent = core.Agent(default_config_path, use_custom_config, control_enabled, new_device_mode) + ctx.ensure_object(SimpleNamespace) + ctx.obj.agent = agent # Drop down to subcommands that needed a config compiled if ctx.invoked_subcommand == "test": @@ -231,8 +229,6 @@ def template(ctx, plugin_name, include_all, config_path, plugin_dir): a new file (if it doesn't yet exist). """ - agent = ctx.obj.agent - echo_heading("Shepherd - Template") if not plugin_dir: diff --git a/shepherd/agent/core.py b/shepherd/agent/core.py index 98b165b..5c1eb3a 100644 --- a/shepherd/agent/core.py +++ b/shepherd/agent/core.py @@ -62,8 +62,18 @@ class Agent(): Holds the main state required to run Shepherd Agent """ - def __init__(self): - + def __init__(self, default_config_path, use_custom_config=True, control_enabled=False, + new_device_mode=False): + """ + Load in the Shepherd Agent config and associated plugins. + Args: + default_config_path: The path to the default config file + use_custom_config: Set False to disable the local custom config layer + control_enabled: Set False to disable Shepherd Control remote management + (including any cached Control config layer) + new_device_mode: Set True to clear out any cached state and trigger new generation + of ID, as if it were being run on a fresh system. + """ # Make sure the plugin system uses this instance rather making its own core_interface._plugin_obj = self @@ -75,43 +85,17 @@ class Agent(): self.core_config = None self.interface_functions = None - self.control_enabled = None self.plugin_interfaces = None - self.restart_args = None - - @ plugin.plugin_function - def root_dir(self): - return self.core_config["root_dir"] - - @ plugin.plugin_function - def device_name(self): - return self.core_config["name"] - - def load(self, default_config_path, use_custom_config=True, control_enabled=False, - new_device_mode=False): - """ - Load in the Shepherd Agent config and associated plugins. - Args: - default_config_path: The path to the default config file - use_custom_config: Set False to disable the local custom config layer - control_enabled: Set False to disable Shepherd Control remote management - (including any cached Control config layer) - new_device_mode: Set True to clear out any cached state and trigger new generation - of ID, as if it were being run on a fresh system. - """ - self.restart_args = [default_config_path, use_custom_config, control_enabled, new_device_mode] - self.control_enabled = control_enabled # Compile the config layers - confman = ConfigManager() - # Pre-seed confman with core confspec to bootstrap 'plugin_dir'. This is required even - # though 'load_config_layer_and_plugins()' will get the core confspec from the cached - # interface, as it needs 'plugin_dir' to list the plugins to load first. + # Pre-seed confman with core confspec to bootstrap 'plugin_dir'. + # The plugin load system will get it from the 'shepherd' plugin interface later, but we + # need the 'plugin_dir' before that. confman.add_confspec("shepherd", core_interface.confspec) compile_local_config(confman, default_config_path, use_custom_config) @@ -150,6 +134,14 @@ class Agent(): confman.dump_to_file(self.core_config["compiled_config_path"], message=message) log.info(F"Saved compiled config to {self.core_config['compiled_config_path']}") + @ plugin.plugin_function + def root_dir(self): + return self.core_config["root_dir"] + + @ plugin.plugin_function + def device_name(self): + return self.core_config["name"] + def restart(self): pass @@ -366,7 +358,6 @@ if __name__ == '__main__': args = parser.parse_args() - agent = Agent() - agent.load(args.default_config_path, args.use_custom_config, - args.control_enabled, args.new_device_mode) + agent = Agent(args.default_config_path, args.use_custom_config, + args.control_enabled, args.new_device_mode) agent.start() diff --git a/tests/test_core.py b/tests/test_core.py index ead9368..8821a06 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -9,11 +9,10 @@ from shepherd.agent import core from shepherd.agent import plugin -@pytest.fixture -def local_agent(): +@pytest.fixture(autouse=True) +def fresh_agent_state(): plugin.unload_plugins() importlib.reload(core) - return core.Agent() @pytest.fixture @@ -56,16 +55,12 @@ spec1 = "asdf" return def_conf_file -def test_local_agent(local_agent): - pass - - -def test_local_agent_load(local_agent, basic_config): - local_agent.load(basic_config) +def test_local_agent(basic_config): + core.Agent(basic_config) -def test_local_compiled_conf(local_agent, basic_config): - local_agent.load(basic_config) +def test_local_compiled_conf(basic_config): + core.Agent(basic_config) compiled_conf = (basic_config.parent / "compiled-config.toml").read_text() assert 'name = "shepherd-test"' in compiled_conf # Paths should be resolved to absolute @@ -73,34 +68,34 @@ def test_local_compiled_conf(local_agent, basic_config): assert 'Compiled Shepherd config' in compiled_conf -def test_custom_conf_load(local_agent, custom_config): - local_agent.load(custom_config) - assert local_agent.core_config["name"] == "shepherd-custom" +def test_custom_conf_load(custom_config): + agent = core.Agent(custom_config) + assert agent.core_config["name"] == "shepherd-custom" -def test_new_device_trigger(local_agent, custom_config, caplog): +def test_new_device_trigger(custom_config, caplog): caplog.set_level(logging.INFO) (custom_config.parent / "shepherd.new").touch() - local_agent.load(custom_config) + core.Agent(custom_config) assert "'new device' mode enabled" in caplog.text assert (custom_config.parent / "shepherd.identity").exists() -def test_local_agent_start(local_agent, basic_config): - local_agent.load(basic_config) - local_agent.start() +def test_local_agent_start(basic_config): + agent = core.Agent(basic_config) + agent.start() -def test_local_agent_plugin_start(local_agent, plugin_config): - local_agent.load(plugin_config) - local_agent.start() - assert local_agent.interface_functions["classtestplugin"].instance_method( +def test_local_agent_plugin_start(plugin_config): + agent = core.Agent(plugin_config) + agent.start() + assert agent.interface_functions["classtestplugin"].instance_method( 3) == "instance method 3" -def test_core_interface(local_agent, plugin_config): - local_agent.load(plugin_config) - local_agent.start() - plugin_interface = local_agent.plugin_interfaces["classtestplugin"] +def test_core_interface(plugin_config): + agent = core.Agent(plugin_config) + agent.start() + plugin_interface = agent.plugin_interfaces["classtestplugin"] assert plugin_interface.plugins["shepherd"].device_name() == "shepherd-test" assert plugin_interface.plugins["shepherd"].root_dir() == str(plugin_config.parent)