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