Repository: apache/libcloud (language: python)

Issue:
use unittest.mock instead of mock
Could you please consider to use unittest.mock, as a fallback at least?
use unittest.mock instead of mock
Could you please consider to use unittest.mock, as a fallback at least?


Discussion / hints:
Can you please provide some reasoning on this feature request? ``mock`` is a tests only dependency so it shouldn't be a problem if we depend on it (since we already depend on other libraries for tests as well).
Yes, forgot to add:
https://fedoraproject.org/wiki/Changes/DeprecatePythonMock
https://trello.com/b/WsskhdXA/opensuse-python

You seem to not support Python 2 anymore and as far as I tested:
https://build.opensuse.org/package/show/home:pgajdos:python/python-apache-libcloud
the testsuite run is successful at least with python 3.6 and later (see [python-apache-libcloud-no-mock.patch](https://build.opensuse.org/package/view_file/home:pgajdos:python/python-apache-libcloud/python-apache-libcloud-no-mock.patch)).

The root of this is sake of simplicity, why not use built-in unittest.mock instead of another library. That is of course your choice and feel free to close this in case you feel it is better to stay with python-mock.

Thank you for consideration
Thanks for the additional context.

If it simplifies the packaging and we don't loose any functionality, I'm fine with replacing it with stdlib mock module.

Not sure how much it really simplifies the packaging though, since we still have a lot of other test dependencies.
Can you please provide some reasoning on this feature request? ``mock`` is a tests only dependency so it shouldn't be a problem if we depend on it (since we already depend on other libraries for tests as well).
Yes, forgot to add:
https://fedoraproject.org/wiki/Changes/DeprecatePythonMock
https://trello.com/b/WsskhdXA/opensuse-python

You seem to not support Python 2 anymore and as far as I tested:
https://build.opensuse.org/package/show/home:pgajdos:python/python-apache-libcloud
the testsuite run is successful at least with python 3.6 and later (see [python-apache-libcloud-no-mock.patch](https://build.opensuse.org/package/view_file/home:pgajdos:python/python-apache-libcloud/python-apache-libcloud-no-mock.patch)).

The root of this is sake of simplicity, why not use built-in unittest.mock instead of another library. That is of course your choice and feel free to close this in case you feel it is better to stay with python-mock.

Thank you for consideration
Thanks for the additional context.

If it simplifies the packaging and we don't loose any functionality, I'm fine with replacing it with stdlib mock module.

Not sure how much it really simplifies the packaging though, since we still have a lot of other test dependencies.

Fix patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 341c7e1c12..c7ea740a8d 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -26,7 +26,7 @@ Compute
 
   (GITHUB-1672)
   [Miguel Caballer - @micafer]
-  
+
 Storage
 ~~~~~~~
 
@@ -46,6 +46,15 @@ DNS
   (GITHUB-1681)
   [Dave Grenier - @livegrenier]
 
+Other
+~~~~~
+
+- Test code has been updated to utilize stdlib ``unittest.mock`` module instead
+  of 3rd party PyPi ``mock`` package.
+
+  (GITHUG-1684)
+  Reported by @pgajdos.
+
 Changes in Apache Libcloud 3.5.1
 --------------------------------
 
@@ -7507,4 +7516,4 @@ Changes with Apache Libcloud 0.3.0 [Tagged May 6, 2010, not released]
 Changes with Apache Libcloud 0.2.0 [Tagged February 2, 2010]
 ------------------------------------------------------------
 
-- First public release.
\ No newline at end of file
+- First public release.
diff --git a/setup.py b/setup.py
index d96ae99095..374b92bf40 100644
--- a/setup.py
+++ b/setup.py
@@ -208,7 +208,6 @@ def get_data_files(dname, ignore=None, parent=None):
         raise RuntimeError(msg)
 
 TEST_REQUIREMENTS = [
-    "mock",
     "requests_mock",
     "pytest",
     "pytest-runner",


Test patch:
diff --git a/docs/testing.rst b/docs/testing.rst
index 2587048b07..0c9c7e8ca4 100644
--- a/docs/testing.rst
+++ b/docs/testing.rst
@@ -17,7 +17,6 @@ dependencies installed:
 
 * ``tox`` (``pip install tox``) - you only need this library if you want to
   use tox to run the tests with all the supported Python versions
-* ``mock`` (``pip install mock``)
 * ``fasteners`` (``pip install fasteners``) - only used in the local storage
   driver
 * ``coverage`` (``pip install coverage``) - you only need this library if you
diff --git a/libcloud/test/benchmarks/test_list_objects_filtering_performance.py b/libcloud/test/benchmarks/test_list_objects_filtering_performance.py
index 4850e0f313..a8240b3dec 100644
--- a/libcloud/test/benchmarks/test_list_objects_filtering_performance.py
+++ b/libcloud/test/benchmarks/test_list_objects_filtering_performance.py
@@ -22,7 +22,7 @@
 import tempfile
 
 import pytest
-import mock
+from unittest import mock
 
 from libcloud.storage.drivers.local import LocalStorageDriver
 
diff --git a/libcloud/test/common/test_aws.py b/libcloud/test/common/test_aws.py
index 994edf5e3b..94bcfaa08c 100644
--- a/libcloud/test/common/test_aws.py
+++ b/libcloud/test/common/test_aws.py
@@ -17,7 +17,7 @@
 import unittest
 from datetime import datetime
 
-import mock
+from unittest import mock
 
 from libcloud.common.aws import AWSRequestSignerAlgorithmV4
 from libcloud.common.aws import SignedAWSConnection
diff --git a/libcloud/test/common/test_base.py b/libcloud/test/common/test_base.py
index c522bf61be..8c4c4825f0 100644
--- a/libcloud/test/common/test_base.py
+++ b/libcloud/test/common/test_base.py
@@ -16,7 +16,7 @@
 import unittest
 import sys
 
-import mock
+from unittest import mock
 
 from libcloud.common.base import LazyObject, Response
 from libcloud.common.exceptions import BaseHTTPError, RateLimitReachedError
diff --git a/libcloud/test/common/test_base_driver.py b/libcloud/test/common/test_base_driver.py
index 8e5224cf20..ef5246d7da 100644
--- a/libcloud/test/common/test_base_driver.py
+++ b/libcloud/test/common/test_base_driver.py
@@ -15,7 +15,7 @@
 
 import sys
 
-from mock import Mock
+from unittest.mock import Mock
 
 from libcloud.common.base import BaseDriver
 
diff --git a/libcloud/test/common/test_google.py b/libcloud/test/common/test_google.py
index b61306701c..03ee38aa16 100644
--- a/libcloud/test/common/test_google.py
+++ b/libcloud/test/common/test_google.py
@@ -16,7 +16,7 @@
 Tests for Google Connection classes.
 """
 import datetime
-import mock
+from unittest import mock
 import os
 import sys
 import unittest
diff --git a/libcloud/test/common/test_nfsn.py b/libcloud/test/common/test_nfsn.py
index 8437279504..7b391f0f98 100644
--- a/libcloud/test/common/test_nfsn.py
+++ b/libcloud/test/common/test_nfsn.py
@@ -17,7 +17,7 @@
 import sys
 import unittest
 
-from mock import Mock, patch
+from unittest.mock import Mock, patch
 
 from libcloud.common.nfsn import NFSNConnection
 from libcloud.test import LibcloudTestCase, MockHttp
diff --git a/libcloud/test/common/test_openstack.py b/libcloud/test/common/test_openstack.py
index c30b6464f0..5e7727ce5d 100644
--- a/libcloud/test/common/test_openstack.py
+++ b/libcloud/test/common/test_openstack.py
@@ -17,7 +17,7 @@
 import unittest
 from unittest.mock import patch
 
-from mock import Mock
+from unittest.mock import Mock
 from libcloud.common.base import LibcloudConnection
 from libcloud.common.openstack import OpenStackBaseConnection
 
diff --git a/libcloud/test/common/test_openstack_identity.py b/libcloud/test/common/test_openstack_identity.py
index e707c43e88..7e077c9586 100644
--- a/libcloud/test/common/test_openstack_identity.py
+++ b/libcloud/test/common/test_openstack_identity.py
@@ -21,7 +21,7 @@
 except ImportError:
     import json
 
-from mock import Mock
+from unittest.mock import Mock
 
 from libcloud.utils.py3 import httplib
 from libcloud.utils.py3 import assertRaisesRegex
diff --git a/libcloud/test/common/test_retry_limit.py b/libcloud/test/common/test_retry_limit.py
index 853b7b401e..bbb7b4eeba 100644
--- a/libcloud/test/common/test_retry_limit.py
+++ b/libcloud/test/common/test_retry_limit.py
@@ -16,7 +16,7 @@
 import socket
 import ssl
 
-from mock import Mock, patch, MagicMock
+from unittest.mock import Mock, patch, MagicMock
 
 from libcloud.utils.retry import TRANSIENT_SSL_ERROR
 from libcloud.common.base import Connection
diff --git a/libcloud/test/common/test_upcloud.py b/libcloud/test/common/test_upcloud.py
index 0b8db69083..97b7ebde8f 100644
--- a/libcloud/test/common/test_upcloud.py
+++ b/libcloud/test/common/test_upcloud.py
@@ -15,7 +15,7 @@
 import sys
 import json
 
-from mock import Mock, call
+from unittest.mock import Mock, call
 
 from libcloud.common.upcloud import (
     UpcloudCreateNodeRequestBody,
diff --git a/libcloud/test/compute/test_azure_arm.py b/libcloud/test/compute/test_azure_arm.py
index 72bd69e81e..edb655e36f 100644
--- a/libcloud/test/compute/test_azure_arm.py
+++ b/libcloud/test/compute/test_azure_arm.py
@@ -18,7 +18,7 @@
 import functools
 from datetime import datetime
 
-import mock
+from unittest import mock
 
 from libcloud.common.exceptions import BaseHTTPError
 from libcloud.common.types import LibcloudError
diff --git a/libcloud/test/compute/test_deployment.py b/libcloud/test/compute/test_deployment.py
index afdbf01c4b..ef1f25db02 100644
--- a/libcloud/test/compute/test_deployment.py
+++ b/libcloud/test/compute/test_deployment.py
@@ -39,7 +39,7 @@
 
 from libcloud.test import MockHttp, XML_HEADERS
 from libcloud.test.file_fixtures import ComputeFileFixtures
-from mock import Mock, patch
+from unittest.mock import Mock, patch
 
 from libcloud.test.secrets import RACKSPACE_PARAMS
 
diff --git a/libcloud/test/compute/test_gce.py b/libcloud/test/compute/test_gce.py
index 0062b92e9a..bad0bc488c 100644
--- a/libcloud/test/compute/test_gce.py
+++ b/libcloud/test/compute/test_gce.py
@@ -17,7 +17,7 @@
 """
 
 import datetime
-import mock
+from unittest import mock
 import sys
 import unittest
 
diff --git a/libcloud/test/compute/test_gig_g8.py b/libcloud/test/compute/test_gig_g8.py
index a89cae1b0e..ea3d3e91bc 100644
--- a/libcloud/test/compute/test_gig_g8.py
+++ b/libcloud/test/compute/test_gig_g8.py
@@ -18,7 +18,7 @@
 import json
 import time
 
-import mock
+from unittest import mock
 
 from libcloud.utils.py3 import httplib
 from libcloud.test import MockHttp
diff --git a/libcloud/test/compute/test_libvirt_driver.py b/libcloud/test/compute/test_libvirt_driver.py
index 322d3a34fe..b2ac00946e 100644
--- a/libcloud/test/compute/test_libvirt_driver.py
+++ b/libcloud/test/compute/test_libvirt_driver.py
@@ -15,7 +15,7 @@
 
 import sys
 
-import mock
+from unittest import mock
 
 from libcloud.compute.drivers.libvirt_driver import LibvirtNodeDriver
 from libcloud.compute.drivers.libvirt_driver import have_libvirt
diff --git a/libcloud/test/compute/test_openstack.py b/libcloud/test/compute/test_openstack.py
index 7339ed2380..5de3177ef6 100644
--- a/libcloud/test/compute/test_openstack.py
+++ b/libcloud/test/compute/test_openstack.py
@@ -19,7 +19,7 @@
 import sys
 import unittest
 import datetime
-import mock
+from unittest import mock
 import pytest
 
 from libcloud.utils.iso8601 import UTC
@@ -29,7 +29,7 @@
 except ImportError:
     import json
 
-from mock import Mock, patch
+from unittest.mock import Mock, patch
 import requests_mock
 
 from libcloud.utils.py3 import httplib
diff --git a/libcloud/test/compute/test_ovh.py b/libcloud/test/compute/test_ovh.py
index 01bd9930d7..b6971724ac 100644
--- a/libcloud/test/compute/test_ovh.py
+++ b/libcloud/test/compute/test_ovh.py
@@ -14,7 +14,7 @@
 # limitations under the License.
 import sys
 import unittest
-from mock import patch
+from unittest.mock import patch
 
 from libcloud.utils.py3 import httplib
 from libcloud.common.exceptions import BaseHTTPError
diff --git a/libcloud/test/compute/test_ssh_client.py b/libcloud/test/compute/test_ssh_client.py
index 13b3775a41..04c40406cc 100644
--- a/libcloud/test/compute/test_ssh_client.py
+++ b/libcloud/test/compute/test_ssh_client.py
@@ -32,7 +32,7 @@
 from libcloud.utils.py3 import u
 from libcloud.utils.py3 import assertRaisesRegex
 
-from mock import patch, Mock, MagicMock, call
+from unittest.mock import patch, Mock, MagicMock, call
 
 if not have_paramiko:
     ParamikoSSHClient = None  # NOQA
diff --git a/libcloud/test/compute/test_vcloud.py b/libcloud/test/compute/test_vcloud.py
index 835cf23e2c..0444578f2a 100644
--- a/libcloud/test/compute/test_vcloud.py
+++ b/libcloud/test/compute/test_vcloud.py
@@ -45,7 +45,7 @@
 from libcloud.test import MockHttp
 from libcloud.test.compute import TestCaseMixin
 from libcloud.test.file_fixtures import ComputeFileFixtures
-from mock import patch, mock_open
+from unittest.mock import patch, mock_open
 
 from libcloud.test.secrets import VCLOUD_PARAMS
 
diff --git a/libcloud/test/dns/test_base.py b/libcloud/test/dns/test_base.py
index 27aea9cacd..b0f89d6670 100644
--- a/libcloud/test/dns/test_base.py
+++ b/libcloud/test/dns/test_base.py
@@ -18,7 +18,7 @@
 import tempfile
 import datetime
 
-from mock import Mock
+from unittest.mock import Mock
 
 from libcloud import __version__
 from libcloud.test import unittest
diff --git a/libcloud/test/dns/test_durabledns.py b/libcloud/test/dns/test_durabledns.py
index af89055ba5..c9dd135068 100644
--- a/libcloud/test/dns/test_durabledns.py
+++ b/libcloud/test/dns/test_durabledns.py
@@ -15,7 +15,7 @@
 import sys
 import unittest
 
-from mock import MagicMock
+from unittest.mock import MagicMock
 
 from libcloud.dns.base import Record, Zone
 from libcloud.dns.types import RecordType
diff --git a/libcloud/test/dns/test_zonomi.py b/libcloud/test/dns/test_zonomi.py
index e054506008..cb5be27d03 100644
--- a/libcloud/test/dns/test_zonomi.py
+++ b/libcloud/test/dns/test_zonomi.py
@@ -13,7 +13,7 @@
 # limitations under the License.
 import sys
 import unittest
-from mock import MagicMock
+from unittest.mock import MagicMock
 
 
 from libcloud.test import MockHttp
diff --git a/libcloud/test/storage/test_backblaze_b2.py b/libcloud/test/storage/test_backblaze_b2.py
index c2e0bc0ba4..4588ed11b5 100644
--- a/libcloud/test/storage/test_backblaze_b2.py
+++ b/libcloud/test/storage/test_backblaze_b2.py
@@ -17,7 +17,7 @@
 import sys
 import tempfile
 
-import mock
+from unittest import mock
 import json
 
 from libcloud.storage.drivers.backblaze_b2 import BackblazeB2StorageDriver
diff --git a/libcloud/test/storage/test_base.py b/libcloud/test/storage/test_base.py
index 5042508319..1c4dd01810 100644
--- a/libcloud/test/storage/test_base.py
+++ b/libcloud/test/storage/test_base.py
@@ -18,8 +18,8 @@
 import sys
 from io import BytesIO
 
-import mock
-from mock import Mock
+from unittest import mock
+from unittest.mock import Mock
 
 from libcloud.common.exceptions import RateLimitReachedError
 from libcloud.storage.base import DEFAULT_CONTENT_TYPE
diff --git a/libcloud/test/storage/test_cloudfiles.py b/libcloud/test/storage/test_cloudfiles.py
index 821e41ea4c..e5cd7b5694 100644
--- a/libcloud/test/storage/test_cloudfiles.py
+++ b/libcloud/test/storage/test_cloudfiles.py
@@ -24,9 +24,9 @@
 import hashlib
 from hashlib import sha1
 
-import mock
-from mock import Mock
-from mock import PropertyMock
+from unittest import mock
+from unittest.mock import Mock
+from unittest.mock import PropertyMock
 
 import libcloud.utils.files
 
diff --git a/libcloud/test/storage/test_google_storage.py b/libcloud/test/storage/test_google_storage.py
index 90586cb216..96e88f6100 100644
--- a/libcloud/test/storage/test_google_storage.py
+++ b/libcloud/test/storage/test_google_storage.py
@@ -15,7 +15,7 @@
 
 import copy
 import json
-import mock
+from unittest import mock
 import re
 import sys
 import unittest
@@ -24,8 +24,8 @@
 
 import email.utils
 import pytest
-from mock import Mock
-from mock import PropertyMock
+from unittest.mock import Mock
+from unittest.mock import PropertyMock
 
 from libcloud.common.google import GoogleAuthType
 from libcloud.common.types import InvalidCredsError
diff --git a/libcloud/test/storage/test_oss.py b/libcloud/test/storage/test_oss.py
index 6bc7e876e3..f9839d5989 100644
--- a/libcloud/test/storage/test_oss.py
+++ b/libcloud/test/storage/test_oss.py
@@ -19,10 +19,7 @@
 import sys
 import unittest
 
-try:
-    import mock
-except ImportError:
-    from unittest import mock
+from unittest import mock
 
 from libcloud.utils.py3 import b
 from libcloud.utils.py3 import httplib
diff --git a/libcloud/test/storage/test_s3.py b/libcloud/test/storage/test_s3.py
index 287ccc4d61..ee3af96b21 100644
--- a/libcloud/test/storage/test_s3.py
+++ b/libcloud/test/storage/test_s3.py
@@ -21,9 +21,9 @@
 from io import BytesIO
 from hashlib import sha1
 
-import mock
-from mock import Mock
-from mock import PropertyMock
+from unittest import mock
+from unittest.mock import Mock
+from unittest.mock import PropertyMock
 import libcloud.utils.files  # NOQA: F401
 
 from libcloud.utils.py3 import ET
diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index 154a1e4dd2..d3423e20ac 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -21,7 +21,7 @@
 from unittest import mock
 
 import requests_mock
-from mock import Mock, patch
+from unittest.mock import Mock, patch
 from requests.exceptions import ConnectTimeout
 
 import libcloud.common.base
diff --git a/libcloud/test/test_init.py b/libcloud/test/test_init.py
index d99fec2151..7a714043c0 100644
--- a/libcloud/test/test_init.py
+++ b/libcloud/test/test_init.py
@@ -26,7 +26,7 @@
 except ImportError:
     have_paramiko = False
 
-from mock import patch
+from unittest.mock import patch
 
 import libcloud
 from libcloud import _init_once
diff --git a/libcloud/test/test_logging_connection.py b/libcloud/test/test_logging_connection.py
index 7fc7ba9cef..82ec2954a6 100644
--- a/libcloud/test/test_logging_connection.py
+++ b/libcloud/test/test_logging_connection.py
@@ -19,7 +19,7 @@
 import zlib
 import requests_mock
 
-import mock
+from unittest import mock
 
 import libcloud
 from libcloud.test import unittest
diff --git a/requirements-tests.txt b/requirements-tests.txt
index 6c2eb3918c..0b23cea554 100644
--- a/requirements-tests.txt
+++ b/requirements-tests.txt
@@ -2,7 +2,6 @@ pep8==1.7.1
 flake8==4.0.1
 astroid==2.11.4
 pylint==2.13.8
-mock==4.0.3
 codecov==2.1.12
 coverage==4.5.4
 requests>=2.27.1