From 26ec32c7be27643cd34d6b0ce5a71ecded3e7f01 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Sat, 17 Feb 2024 12:08:53 +0900 Subject: [PATCH] Remove six from unit tests (part 2) This is part of the steps to remove usage of six library, which is no longer needed since python 2 support was removed. Change-Id: I2feca51d17d6c63f7984483cc656e9360450a4d0 --- .../unit/common/cert_manager/test_local.py | 8 +++--- magnum/tests/unit/common/test_profiler.py | 4 +-- magnum/tests/unit/common/x509/test_sign.py | 25 ++++++++----------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/magnum/tests/unit/common/cert_manager/test_local.py b/magnum/tests/unit/common/cert_manager/test_local.py index d43e0fc411..e5004608ef 100644 --- a/magnum/tests/unit/common/cert_manager/test_local.py +++ b/magnum/tests/unit/common/cert_manager/test_local.py @@ -106,7 +106,7 @@ class TestLocalManager(base.BaseTestCase): def _store_cert(self): file_mock = mock.mock_open() # Attempt to store the cert - with mock.patch('six.moves.builtins.open', file_mock, create=True): + with mock.patch('builtins.open', file_mock, create=True): cert_id = local_cert_manager.CertManager.store_cert( certificate=self.certificate, intermediates=self.intermediates, @@ -131,7 +131,7 @@ class TestLocalManager(base.BaseTestCase): def _get_cert(self, cert_id): file_mock = mock.mock_open() # Attempt to retrieve the cert - with mock.patch('six.moves.builtins.open', file_mock, create=True): + with mock.patch('builtins.open', file_mock, create=True): data = local_cert_manager.CertManager.get_cert(cert_id) # Verify the correct files were opened @@ -154,7 +154,7 @@ class TestLocalManager(base.BaseTestCase): file_mock = mock.mock_open() file_mock.side_effect = fake_open # Attempt to retrieve the cert - with mock.patch('six.moves.builtins.open', file_mock, create=True): + with mock.patch('builtins.open', file_mock, create=True): self.assertRaises( exception.CertificateStorageException, local_cert_manager.CertManager.get_cert, @@ -187,7 +187,7 @@ class TestLocalManager(base.BaseTestCase): def test_store_cert(self): self._store_cert() - @mock.patch('six.moves.builtins.open', create=True) + @mock.patch('builtins.open', create=True) def test_store_cert_with_io_error(self, file_mock): file_mock.side_effect = IOError diff --git a/magnum/tests/unit/common/test_profiler.py b/magnum/tests/unit/common/test_profiler.py index da292b92b8..18af86a865 100644 --- a/magnum/tests/unit/common/test_profiler.py +++ b/magnum/tests/unit/common/test_profiler.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import importlib import inspect from unittest import mock @@ -20,7 +21,6 @@ from oslo_config import cfg from oslo_utils import importutils from osprofiler import initializer as profiler_init from osprofiler import opts as profiler_opts -import six.moves as six from magnum.common import profiler from magnum import conf @@ -45,7 +45,7 @@ class TestProfiler(base.TestCase): for clsname in classes: # give the metaclass and trace_cls() decorator a chance to patch # methods of the classes above - six.reload_module( + importlib.reload( importutils.import_module(clsname.rsplit('.', 1)[0])) cls = importutils.import_class(clsname) diff --git a/magnum/tests/unit/common/x509/test_sign.py b/magnum/tests/unit/common/x509/test_sign.py index f28f65f4b9..42083ebfe0 100644 --- a/magnum/tests/unit/common/x509/test_sign.py +++ b/magnum/tests/unit/common/x509/test_sign.py @@ -19,8 +19,6 @@ from cryptography import x509 as c_x509 from cryptography.x509.oid import NameOID from unittest import mock -import six - from magnum.common import exception from magnum.common.x509 import operations from magnum.tests import base @@ -30,11 +28,11 @@ class TestX509(base.BaseTestCase): def setUp(self): super(TestX509, self).setUp() - self.issuer_name = six.u("fake-issuer") - self.subject_name = six.u("fake-subject") - self.organization_name = six.u("fake-organization") - self.ca_encryption_password = six.b("fake-ca-password") - self.encryption_password = six.b("fake-password") + self.issuer_name = "fake-issuer" + self.subject_name = "fake-subject" + self.organization_name = "fake-organization" + self.ca_encryption_password = b"fake-ca-password" + self.encryption_password = b"fake-password" def _load_pems(self, keypairs, encryption_password): private_key = serialization.load_pem_private_key( @@ -131,7 +129,7 @@ class TestX509(base.BaseTestCase): self.assertIn(basic_constraints, cert.extensions) def test_generate_ca_certificate_with_bytes_issuer_name(self): - issuer_name = six.b("bytes-issuer-name") + issuer_name = b"bytes-issuer-name" cert, _ = self._generate_ca_certificate(issuer_name) issuer_name = issuer_name.decode('utf-8') @@ -194,16 +192,16 @@ class TestX509(base.BaseTestCase): private_key = self._generate_private_key() private_key = self._private_bytes(private_key) - self.assertIsInstance(private_key, six.binary_type) + self.assertIsInstance(private_key, bytes) private_key = operations._load_pem_private_key(private_key) self.assertIsInstance(private_key, rsa.RSAPrivateKey) def test_load_pem_private_key_with_unicode_private_key(self): private_key = self._generate_private_key() private_key = self._private_bytes(private_key) - private_key = six.text_type(private_key.decode('utf-8')) + private_key = private_key.decode('utf-8') - self.assertIsInstance(private_key, six.text_type) + self.assertIsInstance(private_key, str) private_key = operations._load_pem_private_key(private_key) self.assertIsInstance(private_key, rsa.RSAPrivateKey) @@ -213,7 +211,7 @@ class TestX509(base.BaseTestCase): private_key = self._generate_private_key() csr_obj = self._build_csr(private_key) csr = csr_obj.public_bytes(serialization.Encoding.PEM) - csr = six.text_type(csr.decode('utf-8')) + csr = csr.decode('utf-8') mock_load_pem.return_value = csr_obj operations.sign(csr, self.issuer_name, ca_key, @@ -225,7 +223,7 @@ class TestX509(base.BaseTestCase): private_key = self._generate_private_key() csr_obj = self._build_csr(private_key) csr = csr_obj.public_bytes(serialization.Encoding.PEM) - csr = six.text_type(csr.decode('utf-8')) + csr = csr.decode('utf-8') mock_load_pem.return_value = csr_obj certificate = operations.sign(csr, self.issuer_name, @@ -238,7 +236,6 @@ class TestX509(base.BaseTestCase): def test_sign_with_invalid_csr(self): ca_key = self._generate_private_key() csr = 'test' - csr = six.u(csr) self.assertRaises(exception.InvalidCsr, operations.sign,