From 14432680ded88dcdadf3decaf6d74954d38c7a68 Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Thu, 22 Sep 2016 19:29:51 -0400 Subject: [PATCH] Mock file system when testing duplicate IPs Testing the calculation of unique IP addresses does not need to read/write to the file system in order to provide an accurate test. Removing the IO saves roughly 50% of the previous test runtimes, which is helpful for developers iterating on code locally. Change-Id: I23197390e743b7b5ccb06efd91a71f2d9beaf5c1 --- tests/test_inventory.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/test_inventory.py b/tests/test_inventory.py index b69d8d9d2f..86a3a74285 100644 --- a/tests/test_inventory.py +++ b/tests/test_inventory.py @@ -422,10 +422,16 @@ class TestIps(unittest.TestCase): # Allow custom assertion errors. self.longMessage = True + @mock.patch('dynamic_inventory.load_environment') @mock.patch('dynamic_inventory.load_user_configuration') - def test_duplicates(self, mock_load_config): + def test_duplicates(self, mock_load_config, mock_load_env): """Test that no duplicate IPs are made on any network.""" + + # Grab our values read from the file system just once. mock_load_config.return_value = get_config() + mock_load_env.return_value = di.load_environment(BASE_ENV_DIR, {}) + + mock_open = mock.mock_open() for i in xrange(0, 99): # tearDown is ineffective for this loop, so clean the USED_IPs @@ -433,7 +439,10 @@ class TestIps(unittest.TestCase): inventory = None di.USED_IPS = set() - inventory = get_inventory() + # Mock out the context manager being used to write files. + # We don't need to hit the file system for this test. + with mock.patch('__main__.open', mock_open): + inventory = get_inventory() ips = collections.defaultdict(int) hostvars = inventory['_meta']['hostvars']