From 16a0457da5694b504b65580b5440202a6b3df62d Mon Sep 17 00:00:00 2001
From: Benjamin Drung <benjamin.drung@profitbricks.com>
Date: Wed, 7 Mar 2018 12:20:13 +0100
Subject: Support unittest.mock from Python >= 3.6 again

Python 3.6 contains unittest.mock which provide all needed functions
(including MagicMock.assert_called). Therefore reverts commit
9d004f6512d0619df895e0aca56143490a34337c partially to only require
mock >= 2.0.0 for Python < 3.6.

Bug: https://github.com/saltstack/salt/issues/46281
Forwarded: https://github.com/saltstack/salt/pull/46415
Signed-off-by: Benjamin Drung <benjamin.drung@profitbricks.com>
---
 tests/support/mock.py                         | 26 ++++++++++++-------
 tests/unit/cloud/clouds/test_dimensiondata.py |  4 +--
 tests/unit/cloud/clouds/test_gce.py           |  4 +--
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/tests/support/mock.py b/tests/support/mock.py
index 7ef02e0701..a8fe24db3a 100644
--- a/tests/support/mock.py
+++ b/tests/support/mock.py
@@ -5,12 +5,12 @@
     tests.support.mock
     ~~~~~~~~~~~~~~~~~~
 
-    Helper module that wraps `mock` and provides some fake objects in order to
-    properly set the function/class decorators and yet skip the test case's
-    execution.
+    Helper module that wraps :mod:`mock <python3:unittest.mock>` or `mock` and
+    provides some fake objects in order to properly set the function/class
+    decorators and yet skip the test case's execution.
 
-    Note: mock >= 2.0.0 required since unittest.mock does not have
-    MagicMock.assert_called in Python < 3.6.
+    Note: mock >= 2.0.0 is required for Python < 3.6 since unittest.mock in
+    older Python versions does not have MagicMock.assert_called.
 """
 # pylint: disable=unused-import,function-redefined,blacklisted-module,blacklisted-external-module
 
@@ -22,6 +22,12 @@ import errno
 import fnmatch
 import sys
 
+if sys.version_info >= (3, 6):  # noqa
+    import unittest.mock
+
+    sys.modules["mock"] = unittest.mock  # noqa
+
+# isort:imports-thirdparty
 # By these days, we should blowup if mock is not available
 import mock  # pylint: disable=blacklisted-external-import
 import salt.utils.stringutils
@@ -36,7 +42,6 @@ from mock import (
     NonCallableMagicMock,
     NonCallableMock,
     PropertyMock,
-    __version__,
     call,
     create_autospec,
     patch,
@@ -44,15 +49,18 @@ from mock import (
 )
 
 # Import salt libs
+# isort:imports-firstparty
 from salt.ext import six
 
 # pylint: disable=no-name-in-module,no-member
 
 
-__mock_version = tuple(
-    [int(part) for part in mock.__version__.split(".") if part.isdigit()]
+__mock_version = (
+    tuple([int(part) for part in mock.__version__.split(".") if part.isdigit()])
+    if hasattr(mock, "__version__")
+    else None
 )  # pylint: disable=no-member
-if sys.version_info < (3, 6) and __mock_version < (2,):
+if sys.version_info < (3, 6) and (__mock_version is None or __mock_version < (2,)):
     # We need mock >= 2.0.0 before Py3.6
     raise ImportError("Please install mock>=2.0.0")
 
diff --git a/tests/unit/cloud/clouds/test_dimensiondata.py b/tests/unit/cloud/clouds/test_dimensiondata.py
index 10310b9d9a..1b0c27c115 100644
--- a/tests/unit/cloud/clouds/test_dimensiondata.py
+++ b/tests/unit/cloud/clouds/test_dimensiondata.py
@@ -10,7 +10,6 @@ from salt.exceptions import SaltCloudSystemExit
 from salt.utils.versions import LooseVersion
 from tests.support.mixins import LoaderModuleMockMixin
 from tests.support.mock import MagicMock
-from tests.support.mock import __version__ as mock_version
 from tests.support.mock import patch
 from tests.support.unit import TestCase, skipIf
 from tests.unit.cloud.clouds import _preferred_ip
@@ -138,8 +137,7 @@ class DimensionDataTestCase(ExtendedTestCase, LoaderModuleMockMixin):
         with patch("salt.config.check_driver_dependencies", return_value=True) as p:
             get_deps = dimensiondata.get_dependencies()
             self.assertEqual(get_deps, True)
-            if LooseVersion(mock_version) >= LooseVersion("2.0.0"):
-                self.assertTrue(p.call_count >= 1)
+            self.assertTrue(p.call_count >= 1)
 
     def test_provider_matches(self):
         """
diff --git a/tests/unit/cloud/clouds/test_gce.py b/tests/unit/cloud/clouds/test_gce.py
index 414f2dcd98..0c66597f15 100644
--- a/tests/unit/cloud/clouds/test_gce.py
+++ b/tests/unit/cloud/clouds/test_gce.py
@@ -19,7 +19,6 @@ from salt.utils.versions import LooseVersion
 # Import Salt Testing Libs
 from tests.support.mixins import LoaderModuleMockMixin
 from tests.support.mock import MagicMock
-from tests.support.mock import __version__ as mock_version
 from tests.support.mock import patch
 from tests.support.unit import TestCase
 
@@ -145,8 +144,7 @@ class GCETestCase(TestCase, LoaderModuleMockMixin):
         with patch("salt.config.check_driver_dependencies", return_value=True) as p:
             get_deps = gce.get_dependencies()
             self.assertEqual(get_deps, True)
-            if LooseVersion(mock_version) >= LooseVersion("2.0.0"):
-                self.assert_called_once(p)
+            self.assert_called_once(p)
 
     def test_provider_matches(self):
         """
-- 
2.27.0

