Commit c4af7dce authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[2380] some option handling

parent 974e63e7
......@@ -42,8 +42,16 @@ def set_cmd_options(parser):
'''Helper function to set command-line options.
'''
parser.add_option("-c", "--datasrc-conf", dest="conf", action="store",
help="""(Mandatory) configuration of datasrc to load
the zone in. Example:
'{"database_file": "/path/to/dbfile/db.sqlite3"}'""",
metavar='CONFIG')
parser.add_option("-t", "--datasrc-type", dest="datasrc_type",
action="store", default='sqlite3',
help="type of data source (e.g., 'sqlite3')")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
help="display more about what is going on")
help="display more about what is going on")
class LoadZoneRunner:
'''TBD
......@@ -57,12 +65,20 @@ class LoadZoneRunner:
self._zone_class = None
self._zone_name = None
self._zone_file = None
self._datasrc_config = None
self._datasrc_type = None
def _parse_args(self):
usage_txt = 'usage: %prog [options] zonename zonefile'
parser = OptionParser(usage=usage_txt)
set_cmd_options(parser)
(options, args) = parser.parse_args(args=self.__command_args)
if options.conf is None:
raise BadArgument('data source config option cannot be omitted')
self._datasrc_config = options.conf
self._datasrc_type = options.datasrc_type
if len(args) != 2:
raise BadArgument('Unexpected number of arguments: %d (must be 2)'
% (len(args)))
......@@ -71,6 +87,7 @@ class LoadZoneRunner:
except Exception as ex: # too broad, but there's no better granurality
raise BadArgument("Invalid zone name '" + args[0] + "': " +
str(ex))
self._zone_file = args[1]
def __cancel_create(self):
'''sqlite3-only hack: delete the zone just created on load failure.
......
......@@ -49,7 +49,7 @@ class TestLoadZoneRunner(unittest.TestCase):
shutil.copyfile(READ_ZONE_DB_FILE, WRITE_ZONE_DB_FILE)
# default command line arguments
self.__args = ['example.org', 'example.zone']
self.__args = ['-c', DATASRC_CONFIG, 'example.org', 'example.zone']
self.__runner = LoadZoneRunner(self.__args)
def tearDown(self):
......@@ -69,17 +69,27 @@ class TestLoadZoneRunner(unittest.TestCase):
def test_parse_args(self):
self.__runner._parse_args()
self.assertEqual(TEST_ZONE_NAME, self.__runner._zone_name)
self.assertEqual('example.zone', self.__runner._zone_file)
self.assertEqual(DATASRC_CONFIG, self.__runner._datasrc_config)
self.assertEqual('sqlite3', self.__runner._datasrc_type) # default
def test_parse_bad_args(self):
# -c cannot be omitted (right now)
self.assertRaises(BadArgument,
LoadZoneRunner(['example', 'example.zone']).
_parse_args)
# There must be exactly 2 non-option arguments: zone name and zone file
self.assertRaises(BadArgument, LoadZoneRunner([])._parse_args)
self.assertRaises(BadArgument, LoadZoneRunner(['example'])._parse_args)
copt = ['-c', '0']
self.assertRaises(BadArgument, LoadZoneRunner(copt)._parse_args)
self.assertRaises(BadArgument, LoadZoneRunner(copt + ['example']).
_parse_args)
self.assertRaises(BadArgument, LoadZoneRunner(self.__args + ['0']).
_parse_args)
# Bad zone name
self.assertRaises(BadArgument,
LoadZoneRunner(['bad..name', 'example.zone']).
LoadZoneRunner(copt + ['bad..name', 'example.zone']).
_parse_args)
def __common_load_setup(self):
......
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