From 02f8d3d697da37dc0386864a2946a28c47f4d517 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Wed, 4 May 2016 13:57:55 -0400 Subject: [PATCH] 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 --- playbooks/inventory/dynamic_inventory.py | 7 ++++--- tests/test_inventory.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index 92e34d3b64..914160d085 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -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) diff --git a/tests/test_inventory.py b/tests/test_inventory.py index 7fecb0d81c..de205b6830 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -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