From 4fcbdd77401691dde1c433b6c40e9ddbb506070d Mon Sep 17 00:00:00 2001
From: Will Szumski <will@stackhpc.com>
Date: Thu, 7 May 2020 10:18:37 +0100
Subject: [PATCH] Stop mocking ansible modules globally

This causes non-local side effects that are hard to track down. E.g:

--- import errors ---
Failed to import test module: tests.test_merge_yaml
Traceback (most recent call last):
  File "/home/will/.pyenv/versions/3.7.7/lib/python3.7/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "/home/will/.pyenv/versions/3.7.7/lib/python3.7/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/home/will/code/kolla-ansible/tests/test_merge_yaml.py", line 19, in <module>
    from ansible.errors import AnsibleModuleError
ModuleNotFoundError: No module named 'ansible.errors'; 'ansible' is not a package

This `'ansible' is not a package` message occurs because ansible is a Mock.

Depends-On: https://review.opendev.org/#/c/726768/
Change-Id: Iddbdd3d855daadbf12536cc990559e6b8e123051
---
 lower-constraints.txt      | 1 +
 test-requirements.txt      | 1 +
 tests/test_kolla_docker.py | 5 -----
 tests/test_merge_config.py | 5 -----
 4 files changed, 2 insertions(+), 10 deletions(-)

diff --git a/lower-constraints.txt b/lower-constraints.txt
index de4283fcac..e951d1a753 100644
--- a/lower-constraints.txt
+++ b/lower-constraints.txt
@@ -1,4 +1,5 @@
 alabaster==0.7.10
+ansible==2.8.0
 appdirs==1.4.3
 asn1crypto==0.24.0
 Babel==2.5.3
diff --git a/test-requirements.txt b/test-requirements.txt
index d1e5b433f1..6b327bee8f 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -11,3 +11,4 @@ coverage!=4.4,>=4.0 # Apache-2.0
 docker>=2.4.2 # Apache-2.0
 oslotest>=3.2.0 # Apache-2.0
 stestr>=2.0.0 # Apache-2.0
+ansible>=2.8.0 # GPLv3
diff --git a/tests/test_kolla_docker.py b/tests/test_kolla_docker.py
index 27ef9d4a29..c4c7b21248 100644
--- a/tests/test_kolla_docker.py
+++ b/tests/test_kolla_docker.py
@@ -26,11 +26,6 @@ from docker.types import Ulimit
 from oslotest import base
 
 this_dir = os.path.dirname(sys.modules[__name__].__file__)
-# FIXME(yoctozepto): this modifies global interpreter state and affects tests
-# from other files
-sys.modules['ansible'] = mock.MagicMock()
-sys.modules['ansible.module_utils'] = mock.MagicMock()
-sys.modules['ansible.module_utils.basic'] = mock.MagicMock()
 kolla_docker_file = os.path.join(this_dir, '..', 'ansible',
                                  'library', 'kolla_docker.py')
 kd = imp.load_source('kolla_docker', kolla_docker_file)
diff --git a/tests/test_merge_config.py b/tests/test_merge_config.py
index 1638186f5d..ab5427c1ad 100644
--- a/tests/test_merge_config.py
+++ b/tests/test_merge_config.py
@@ -15,8 +15,6 @@
 
 import imp
 import os
-import sys
-from unittest import mock
 
 from io import StringIO
 from oslotest import base
@@ -26,9 +24,6 @@ PROJECT_DIR = os.path.abspath(os.path.join(os. path.dirname(__file__), '../'))
 MERGE_CONFIG_FILE = os.path.join(PROJECT_DIR,
                                  'ansible/action_plugins/merge_configs.py')
 
-# FIXME(yoctozepto): this modifies global interpreter state and affects tests
-# from other files
-sys.modules['ansible.plugins'] = mock.MagicMock()
 
 merge_configs = imp.load_source('merge_configs', MERGE_CONFIG_FILE)
 
-- 
GitLab