Commit fc26c739 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[trac615] Parsing of port for cmdctl

parent 9dcb5326
......@@ -657,6 +657,7 @@ AC_CONFIG_FILES([Makefile
src/lib/python/isc/net/tests/Makefile
src/lib/python/isc/notify/Makefile
src/lib/python/isc/notify/tests/Makefile
src/lib/python/isc/testutils/Makefile
src/lib/config/Makefile
src/lib/config/tests/Makefile
src/lib/config/tests/testdata/Makefile
......
......@@ -795,12 +795,12 @@ def process_rename(option, opt_str, value, parser):
"""Function that renames the process if it is requested by a option."""
isc.util.process.rename(value)
def parse_args(args=sys.argv[1:]):
def parse_args(args=sys.argv[1:], Parser=OptionParser):
"""
Function for parsing command line arguments. Returns the
options object from OptionParser.
"""
parser = OptionParser(version=VERSION)
parser = Parser(version=VERSION)
parser.add_option("-m", "--msgq-socket-file", dest="msgq_socket_file",
type="string", default=None,
help="UNIX domain socket file the b10-msgq daemon will use")
......@@ -819,7 +819,14 @@ def parse_args(args=sys.argv[1:]):
parser.add_option("-p", "--data-path", dest="data_path",
help="Directory to search for configuration files",
default=None)
parser.add_option("--cmdctl-port", dest="cmdctl_port", type="int",
default=None, help="Port of command control")
(options, args) = parser.parse_args(args)
if options.cmdctl_port is not None:
try:
isc.net.parse.port_parse(options.cmdctl_port)
except ValueError as e:
parser.error(e)
if args:
parser.print_help()
sys.exit(1)
......
......@@ -9,6 +9,7 @@ import os
import signal
import socket
from isc.net.addr import IPAddr
from isc.testutils.parse_args import TestOptParser, OptsError
class TestProcessInfo(unittest.TestCase):
def setUp(self):
......@@ -421,26 +422,37 @@ class TestParseArgs(unittest.TestCase):
"""
Test correct default values when no options are passed.
"""
options = parse_args([])
options = parse_args([], TestOptParser)
self.assertEqual(None, options.data_path)
self.assertEqual(None, options.config_file)
self.assertEqual(None, options.cmdctl_port)
def test_data_path(self):
"""
Test it can parse the data path.
"""
options = parse_args(['-p', '/data/path'])
options = parse_args(['-p', '/data/path'], TestOptParser)
self.assertEqual('/data/path', options.data_path)
options = parse_args(['--data-path=/data/path'])
options = parse_args(['--data-path=/data/path'], TestOptParser)
self.assertEqual('/data/path', options.data_path)
def test_config_filename(self):
"""
Test it can parse the config switch.
"""
options = parse_args(['-c', 'config-file'])
options = parse_args(['-c', 'config-file'], TestOptParser)
self.assertEqual('config-file', options.config_file)
options = parse_args(['--config-file=config-file'])
options = parse_args(['--config-file=config-file'], TestOptParser)
self.assertEqual('config-file', options.config_file)
def test_cmdctl_port(self):
"""
Test it can parse the command control port.
"""
self.assertRaises(OptsError, parse_args, ['--cmdctl-port=abc'],
TestOptParser)
self.assertRaises(OptsError, parse_args, ['--cmdctl-port=100000000'],
TestOptParser)
options = parse_args(['--cmdctl-port=1234'], TestOptParser)
self.assertEqual(1234, options.cmdctl_port)
if __name__ == '__main__':
unittest.main()
......@@ -20,7 +20,7 @@
import unittest
import os
import sys
from optparse import OptionParser
from isc.testutils.parse_args import OptsError, TestOptParser
class MyConfigManager:
def __init__(self, path, filename):
......@@ -89,20 +89,6 @@ class TestConfigManagerStartup(unittest.TestCase):
sys.modules.pop("b10-cfgmgr")
class OptsError(Exception):
"""To know when OptionParser would exit"""
pass
class TestOptParser(OptionParser):
"""
We define our own option parser to push into the parsing routine.
This one does not exit the whole application on error, it just raises
exception. It doesn't change anything else. The application uses the
stock one.
"""
def error(self, message):
raise OptsError(message)
class TestParseArgs(unittest.TestCase):
"""
Test for the parsing of command line arguments. We provide a different
......
SUBDIRS = datasrc cc config log net notify util
SUBDIRS = datasrc cc config log net notify util testutils
python_PYTHON = __init__.py
......
EXTRA_DIST = __init__.py parse_args.py
This contains some shared test code for other modules and python processes.
That's why it doesn't have it's own test subdirectory and why it isn't
installed.
# Copyright (C) 2011 Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Nothing here, really, it's just to tell python this directory is in
# module hierarchy
# Copyright (C) 2011 Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from optparse import OptionParser
class OptsError(Exception):
"""To know when OptionParser would exit"""
pass
class TestOptParser(OptionParser):
"""
We define our own option parser to push into the parsing routine.
This one does not exit the whole application on error, it just raises
exception. It doesn't change anything else. The application uses the
stock one.
"""
def error(self, message):
raise OptsError(message)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment