-
Notifications
You must be signed in to change notification settings - Fork 23.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VMware: Fix module usages in module_utils
Added unit tests for vmware.py Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
bugfixes: | ||
- Fix VMware module utils for self usage. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright: (c) 2018, Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
import sys | ||
|
||
from units.compat import unittest | ||
from units.compat.mock import patch, MagicMock | ||
|
||
from ansible.module_utils.six.moves import builtins | ||
from ansible.module_utils._text import to_native | ||
|
||
realimport = builtins.__import__ | ||
|
||
|
||
class TestVmware(unittest.TestCase): | ||
def clear_modules(self, mods): | ||
for mod in mods: | ||
if mod in sys.modules: | ||
del sys.modules[mod] | ||
|
||
@patch.object(builtins, '__import__') | ||
def test_vmware_missing_ensure_libs(self, mock_import): | ||
def _mock_import(name, *args, **kwargs): | ||
if name in ('pyVmomi', 'requests'): | ||
raise ImportError | ||
return realimport(name, *args, **kwargs) | ||
|
||
self.clear_modules(['pyVmomi', 'ansible.module_utils.vmware', 'requests']) | ||
mock_import.side_effect = _mock_import | ||
mod = builtins.__import__('ansible.module_utils.vmware') | ||
|
||
self.assertFalse(mod.module_utils.vmware.HAS_PYVMOMI) | ||
self.assertFalse(mod.module_utils.vmware.HAS_REQUESTS) | ||
|
||
with self.assertRaises(NameError) as context: | ||
mod.module_utils.vmware.PyVmomi(MagicMock()) | ||
|
||
self.assertIn("name 'vim' is not defined", to_native(context.exception)) | ||
|
||
@patch.object(builtins, '__import__') | ||
def test_vmware_found_ensure_libs(self, mock_import): | ||
def _mock_import(name, *args, **kwargs): | ||
if 'pyVmomi' in name: | ||
return MagicMock() | ||
return realimport(name, *args, **kwargs) | ||
|
||
self.clear_modules(['pyVmomi', 'ansible.module_utils.vmware', 'requests']) | ||
mock_import.side_effect = _mock_import | ||
mod = builtins.__import__('ansible.module_utils.vmware') | ||
|
||
self.assertTrue(mod.module_utils.vmware.HAS_PYVMOMI) | ||
self.assertTrue(mod.module_utils.vmware.HAS_REQUESTS) |