Test and refactor argument parsing

This patch does not change functionality of the argument parsing
function, but does make it easier to test. While argparse has its own
tests within the standard library and the `args` function could
theoretically be excluded, including these simple tests provide the
coverage we need without introducing the possibility of skipping over
code that might be added later.

Change-Id: I8ee11480ca6e46748fe19fcc19b92aded8a39c39
This commit is contained in:
Nolan Brubaker 2016-05-04 13:57:55 -04:00
parent 011c72c320
commit 02f8d3d697
2 changed files with 19 additions and 3 deletions

View File

@ -22,6 +22,7 @@ import netaddr
import os
import Queue
import random
import sys
import tarfile
import uuid
import yaml
@ -86,7 +87,7 @@ class MultipleIpForHostError(Exception):
return self.message
def args():
def args(arg_list):
"""Setup argument Parsing."""
parser = argparse.ArgumentParser(
usage='%(prog)s',
@ -105,7 +106,7 @@ def args():
action='store_true'
)
return vars(parser.parse_args())
return vars(parser.parse_args(arg_list))
def get_ip_address(name, ip_q):
@ -1142,6 +1143,6 @@ def main(all_args):
return dynamic_inventory_json
if __name__ == '__main__':
all_args = args()
all_args = args(sys.argv[1:])
output = main(all_args)
print(output)

View File

@ -47,6 +47,21 @@ def get_inventory():
cleanup()
class TestArgParser(unittest.TestCase):
def test_no_args(self):
arg_dict = di.args([])
self.assertEqual(arg_dict['config'], None)
self.assertEqual(arg_dict['list'], False)
def test_list_arg(self):
arg_dict = di.args(['--list'])
self.assertEqual(arg_dict['list'], True)
def test_config_arg(self):
arg_dict = di.args(['--config', '/etc/openstack_deploy'])
self.assertEqual(arg_dict['config'], '/etc/openstack_deploy')
class TestAnsibleInventoryFormatConstraints(unittest.TestCase):
inventory = None