Skip to content

Commit

Permalink
Merge pull request #49 from Erik-Lamers1/action-manager-re
Browse files Browse the repository at this point in the history
Remove deprecated list to show action conversion
  • Loading branch information
Erik-Lamers1 authored Oct 29, 2022
2 parents 3bbb816 + 19ba3d1 commit a7b9210
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 61 deletions.
51 changes: 21 additions & 30 deletions vnet_manager/actions/manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from logging import getLogger
from os import EX_OK, EX_USAGE
from os.path import isdir, isfile
from warnings import warn
from os import EX_OK, EX_USAGE, EX_OSERR
from os.path import isdir
from typing import Optional, Tuple, List

import vnet_manager.operations.machine as machine_op
Expand Down Expand Up @@ -62,21 +61,20 @@ def execute(self, action: str) -> int:
"""
Execute an action
:param str action: The name of the action to execute
:raises NotImplementedError: If the action is unknown
:raises RuntimeError
:return: int: returncode of the action
"""
# First do a sanity check on the action
action_func = action.replace("-", "_")
if not hasattr(self, f"preform_{action_func}_action"):
raise NotImplementedError(f"{action} is not a valid action")
logger.critical(f"{action} is not a valid action")
return EX_USAGE
if self.config_path and action not in ("connect", "list") and not self.parse_config():
logger.critical("Config NOT OK, can't proceed")
return EX_USAGE
# Preform the action
logger.info(f"Initiating {action} action")
getattr(self, f"preform_{action_func}_action")()
return EX_OK
# Return the exit code provided by the execute function or exit EX_OK if no exit code is provided
return getattr(self, f"preform_{action_func}_action")() or EX_OK

def parse_config(self) -> bool:
"""
Expand Down Expand Up @@ -128,11 +126,11 @@ def preform_stop_action(self):
def preform_connect_action(self):
# Make the provider exists
if self.provider not in settings.PROVIDERS.keys():
msg = f"Provider {self.provider} not supported"
logger.error(msg)
raise NotImplementedError(msg)
logger.error(f"Provider {self.provider} not supported")
return EX_USAGE
# Connect to it
getattr(machine_op, f"connect_to_{self.provider}_machine")(self.config_path)
return EX_OK

def preform_create_action(self):
# Make sure the provider environments are correct
Expand Down Expand Up @@ -165,25 +163,18 @@ def preform_destroy_action(self):
delete_vnet_interfaces(self.config)

def preform_list_action(self):
# First check if we have been passed a file or directory
if isfile(self.config_path):
dep_msg = "List action with a regular config file is deprecated, use the 'show' action instead"
logger.warning(dep_msg)
warn(dep_msg)
# Execute the show action instead
self.execute("show")
elif isdir(self.config_path):
# We exclude the default.yaml config file because it is not a valid user config
yaml_files = get_yaml_files_from_disk_path(self.config_path)
for path in yaml_files:
self.config_path = path
if not self.parse_config():
logger.error(f"Config {path} does not seem to be a valid config, skipping")
continue
logger.info(f"Showing machine status for {path}")
machine_op.show_status(self.config)
else:
logger.error(f"Path {self.config_path} does not seem to be a file or a directory, did you forget to pass a config directory?")
if not isdir(self.config_path):
logger.error(f"Provided path {self.config_path} does not seem to be a directory")
return EX_OSERR
yaml_files = get_yaml_files_from_disk_path(self.config_path)
for path in yaml_files:
self.config_path = path
if not self.parse_config():
logger.error(f"Config {path} does not seem to be a valid config, skipping")
continue
logger.info(f"Showing machine status for {path}")
machine_op.show_status(self.config)
return EX_OK

@staticmethod
def preform_version_action():
Expand Down
53 changes: 22 additions & 31 deletions vnet_manager/tests/actions/test_action_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from os import EX_OK, EX_USAGE
from os import EX_OK, EX_USAGE, EX_OSERR
from unittest.mock import MagicMock

from vnet_manager.tests import VNetTestCase
Expand Down Expand Up @@ -27,17 +27,14 @@ def setUp(self) -> None:
self.destroy_lxc_image = self.set_up_patch("vnet_manager.actions.manager.destroy_lxc_image")
self.delete_vnet_interfaces = self.set_up_patch("vnet_manager.actions.manager.delete_vnet_interfaces")
self.cleanup_vnet_lxc_environment = self.set_up_patch("vnet_manager.actions.manager.cleanup_vnet_lxc_environment")
self.isfile = self.set_up_patch("vnet_manager.actions.manager.isfile")
self.isdir = self.set_up_patch("vnet_manager.actions.manager.isdir")
self.isdir = self.set_up_patch("vnet_manager.actions.manager.isdir")
self.write_file = self.set_up_patch("vnet_manager.actions.manager.write_file_to_disk")
self.get_yaml_file_from_disk_path = self.set_up_patch("vnet_manager.actions.manager.get_yaml_files_from_disk_path")
self.get_yaml_file_from_disk_path.return_value = ["file1"]

def test_action_raises_not_implemented_error_if_unsupported_action(self):
manager = ActionManager()
with self.assertRaises(NotImplementedError):
manager.execute("not_working")
def test_action_manager_returns_usage_exit_code_if_action_does_not_exist(self):
ret = ActionManager().execute("blaap")
self.assertEqual(ret, EX_USAGE)

def test_action_manager_calls_show_version_with_version_action(self):
manager = ActionManager()
Expand All @@ -48,7 +45,7 @@ def test_action_manager_calls_show_version_with_version_action(self):
def test_action_manager_calls_get_config(self):
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.get_config.assert_called_once_with("blaap")
self.get_config.assert_called_once_with("file1")

def test_action_manager_calls_validate_config(self):
manager = ActionManager(config_path="blaap")
Expand All @@ -60,6 +57,13 @@ def test_action_manager_called_validator_function(self):
manager.execute("list")
self.validator.validate.assert_called_once()

def test_action_manager_does_not_show_status_of_non_valid_config_files(self):
self.validator.config_validation_successful = False
self.get_yaml_file_from_disk_path.return_value = ["file1", "file2"]
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.assertFalse(self.machine_op.show_status.called)

def test_action_manager_returns_usage_exit_code_if_validator_unsuccessful(self):
self.validator.config_validation_successful = False
manager = ActionManager(config_path="blaap")
Expand All @@ -72,43 +76,30 @@ def test_action_manager_calls_show_status_with_list_action(self):
manager.execute("list")
self.machine_op.show_status.assert_called_once_with(self.validator.updated_config)

def test_action_manager_calls_show_vnet_interface_status_with_list_action(self):
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.show_status_interfaces.assert_called_once_with(self.validator.updated_config)

def test_action_manager_does_not_call_vnet_veth_interface_status_with_list_action_if_veths_config_not_present(self):
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.assertFalse(self.show_status_interfaces_veth.called)

def test_action_manager_calls_show_vnet_veth_interface_status_with_list_action(self):
self.validator.updated_config["veths"] = "jajaja"
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.show_status_interfaces_veth.assert_called_once_with(self.validator.updated_config)

def test_action_manager_calls_get_yaml_file_from_disk_path_with_list_action(self):
self.isfile.return_value = False
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.get_yaml_file_from_disk_path.assert_called_once_with("blaap")

def test_action_manager_calls_show_status_with_return_values_of_get_yaml_files(self):
self.get_yaml_file_from_disk_path.return_value = ["file1", "file2", "file3"]
self.isfile.return_value = False
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.assertEqual(3, self.machine_op.show_status.call_count)

def test_action_manager_does_nothing_when_not_file_and_not_dir_with_list_action(self):
self.isfile.return_value = False
self.isdir.return_value = False
manager = ActionManager(config_path="blaap")
manager.execute("list")
self.assertFalse(self.get_yaml_file_from_disk_path.called)
self.assertFalse(self.machine_op.show_status.called)

def test_action_manager_returns_os_error_exit_code_when_not_dir_with_list_action(self):
self.isdir.return_value = False
manager = ActionManager(config_path="blaap")
ret = manager.execute("list")
self.assertEqual(ret, EX_OSERR)

def test_action_manager_calls_show_status_with_show_action(self):
manager = ActionManager(config_path="blaap")
manager.execute("show")
Expand Down Expand Up @@ -299,10 +290,10 @@ def test_action_manager_calls_connect_to_lxc_machine(self):
manager.execute("connect")
self.machine_op.connect_to_lxc_machine.assert_called_once_with("machine1")

def test_action_manager_raises_not_implemented_error_on_non_supported_provider(self):
manager = ActionManager(config_path="machine1", provider="test123")
with self.assertRaises(NotImplementedError):
manager.execute("connect")
def test_action_manager_returns_usage_exit_code_with_non_supported_provider(self):
manager = ActionManager(config_path="machine1", provider="blaap133")
ret = manager.execute("connect")
self.assertEqual(ret, EX_USAGE)

def test_action_manager_calls_cleanup_lxc_environment_with_purge_action_on_destroy(self):
manager = ActionManager(purge=True)
Expand Down

0 comments on commit a7b9210

Please sign in to comment.