From 7ebe0853bb1bdc8ae42999568d89d95c14703be4 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Wed, 25 May 2016 14:25:39 -0400 Subject: [PATCH] Reduce reliance on global state for testing In writing other tests, the INVENTORY_SKEL dictionary was mutated globally and affecting tests. This change makes sure that it is copied on each run, instead of re-used. During normal usage, this behavior is not dramatically different, because the INVENTORY_SKEL dictionary is destroyed when the script exits. However, when running tests, the module and thus the variable stay loaded and are not implicitly destroyed. This changed revealed that USED_IPS was also not being properly cleaned on the duplicate IP test. In order to minimize the scope of this commit, the affected test is fixed rather than all instances of USED_IPS access. Change-Id: I6d1ea3447079e2f61d2d4a99ffdbb7fe311e4406 --- playbooks/inventory/dynamic_inventory.py | 3 ++- tests/test_inventory.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/playbooks/inventory/dynamic_inventory.py b/playbooks/inventory/dynamic_inventory.py index b0b7f48912..0d81cf2b8a 100755 --- a/playbooks/inventory/dynamic_inventory.py +++ b/playbooks/inventory/dynamic_inventory.py @@ -16,6 +16,7 @@ # (c) 2014, Kevin Carter import argparse +import copy import datetime import json import netaddr @@ -1079,7 +1080,7 @@ def main(all_args): backup_name = '%s-%s.json' % (basename, utctime) tar.add(dynamic_inventory_file, arcname=backup_name) else: - dynamic_inventory = INVENTORY_SKEL + dynamic_inventory = copy.deepcopy(INVENTORY_SKEL) # Save the users container cidr as a group variable cidr_networks = user_defined_config.get('cidr_networks') diff --git a/tests/test_inventory.py b/tests/test_inventory.py index f6ed904698..4364940793 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -316,6 +316,10 @@ class TestIps(unittest.TestCase): """Test that no duplicate IPs are made on any network.""" for i in xrange(0, 99): + # tearDown is ineffective for this loop, so clean the USED_IPs + # on each run + inventory = None + di.USED_IPS = [] inventory = get_inventory() ips = collections.defaultdict(int) hostvars = inventory['_meta']['hostvars']