-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Erik-Lamers1/argeparser
Move argeparser to separate file
- Loading branch information
Showing
7 changed files
with
150 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from argparse import Namespace, ArgumentParser | ||
from typing import Sequence | ||
|
||
from vnet_manager.conf import settings | ||
|
||
|
||
def parse_vnet_args(args: Sequence = None) -> Namespace: | ||
parser = ArgumentParser(description="VNet-manager a virtual network manager - manages containers to create virtual networks") | ||
parser.add_argument( | ||
"action", | ||
choices=sorted(settings.VALID_ACTIONS), | ||
help="The action to preform on the virtual network, use '<action> help' for information about that action", | ||
) | ||
parser.add_argument("config", help="The yaml config file to use", nargs="?", default="default") | ||
|
||
# Options | ||
parser.add_argument( | ||
"-m", | ||
"--machines", | ||
nargs="*", | ||
help="Just apply the actions on the following machine names " "(default is all machines defined in the config file)", | ||
) | ||
parser.add_argument("-y", "--yes", action="store_true", help="Answer yes to all questions") | ||
parser.add_argument("-nh", "--no-hosts", action="store_true", help="Disable creation of /etc/hosts") | ||
|
||
start_group = parser.add_argument_group("Start options", "These options can be specified for the start action") | ||
start_group.add_argument("-s", "--sniffer", action="store_true", help="Start a TCPdump sniffer on the VNet interfaces") | ||
|
||
destroy_group = parser.add_argument_group("Destroy options", "These options can be specified for the destroy action") | ||
destroy_group.add_argument("-b", "--base-image", action="store_true", help="Destroy the base image instead of the machines") | ||
|
||
logging_group = parser.add_argument_group("Verbosity options", "Control output verbosity (can be supplied multiple times)") | ||
logging_group.add_argument("-v", "--verbose", action="count", default=0, help="Be more verbose") | ||
logging_group.add_argument("-q", "--quite", action="count", default=0, help="Be more quite") | ||
return validate_argument_sanity(parser.parse_args(args=args), parser) | ||
|
||
|
||
def validate_argument_sanity(args: Namespace, parser: ArgumentParser) -> Namespace: | ||
""" | ||
Validates the passed arguments for sanity | ||
:param args: Namespace, The already processed user arguments | ||
:param parser: ArgumentParser, The parser object | ||
:return: Namespace, The validated arguments | ||
:raises: SystemExit, if arguments are not sane | ||
""" | ||
# User input sanity checks | ||
if args.action == "status": | ||
# For people who are used to status calls | ||
args.action = "show" | ||
if args.config == "default" and args.action in settings.CONFIG_REQUIRED_ACTIONS: | ||
parser.error("This action requires a config file to be passed") | ||
if args.sniffer and not args.action == "start": | ||
parser.error("The sniffer option only makes sense with the 'start' action") | ||
if args.base_image and not args.action == "destroy": | ||
parser.error("The base_image option only makes sense with the 'destroy' action") | ||
if args.no_hosts and not args.action == "create": | ||
parser.error("The no_hosts option only makes sense with the 'create' action") | ||
return args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from io import StringIO | ||
from unittest.mock import patch | ||
|
||
from vnet_manager.argeparser import parse_vnet_args | ||
from vnet_manager.tests import VNetTestCase | ||
|
||
|
||
default_args = ["list", "config"] | ||
|
||
|
||
class TestParseArgs(VNetTestCase): | ||
def test_parse_args_produces_known_args(self): | ||
known_args = ("action", "config", "machines", "yes", "verbose", "sniffer", "base_image", "no_hosts") | ||
args = parse_vnet_args(default_args) | ||
for arg in known_args: | ||
self.assertTrue(hasattr(args, arg), msg="Argument {} not found in parse_args return value".format(arg)) | ||
|
||
@patch("sys.stderr", new_callable=StringIO) | ||
def test_parse_args_exists_when_config_required_action_without_config_is_passed(self, stderr): | ||
with self.assertRaises(SystemExit): | ||
parse_vnet_args(["create"]) | ||
self.assertTrue(stderr.getvalue().strip().endswith("This action requires a config file to be passed")) | ||
|
||
@patch("sys.stderr", new_callable=StringIO) | ||
def test_parse_args_exists_when_sniffer_is_passed_without_start_action(self, stderr): | ||
with self.assertRaises(SystemExit): | ||
parse_vnet_args(["list", "config", "--sniffer"]) | ||
self.assertTrue(stderr.getvalue().strip().endswith("The sniffer option only makes sense with the 'start' action")) | ||
|
||
@patch("sys.stderr", new_callable=StringIO) | ||
def test_parse_args_exists_when_base_image_passed_without_destroy_action(self, stderr): | ||
with self.assertRaises(SystemExit): | ||
parse_vnet_args(["list", "config", "--base-image"]) | ||
self.assertTrue(stderr.getvalue().strip().endswith("The base_image option only makes sense with the 'destroy' action")) | ||
|
||
@patch("sys.stderr", new_callable=StringIO) | ||
def test_parse_args_exists_when_no_hosts_passed_without_create_action(self, stderr): | ||
with self.assertRaises(SystemExit): | ||
parse_vnet_args(["list", "config", "--no-hosts"]) | ||
self.assertTrue(stderr.getvalue().strip().endswith("The no_hosts option only makes sense with the 'create' action")) | ||
|
||
def test_parse_args_sets_show_action_on_status_action(self): | ||
args = parse_vnet_args(["status", "config"]) | ||
self.assertEqual(args.action, "show") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters