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
This commit is contained in:
Takashi Kajinami 2024-02-17 12:08:53 +09:00
parent 766089b7be
commit 26ec32c7be
3 changed files with 17 additions and 20 deletions

View File

@ -106,7 +106,7 @@ class TestLocalManager(base.BaseTestCase):
def _store_cert(self): def _store_cert(self):
file_mock = mock.mock_open() file_mock = mock.mock_open()
# Attempt to store the cert # 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( cert_id = local_cert_manager.CertManager.store_cert(
certificate=self.certificate, certificate=self.certificate,
intermediates=self.intermediates, intermediates=self.intermediates,
@ -131,7 +131,7 @@ class TestLocalManager(base.BaseTestCase):
def _get_cert(self, cert_id): def _get_cert(self, cert_id):
file_mock = mock.mock_open() file_mock = mock.mock_open()
# Attempt to retrieve the cert # 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) data = local_cert_manager.CertManager.get_cert(cert_id)
# Verify the correct files were opened # Verify the correct files were opened
@ -154,7 +154,7 @@ class TestLocalManager(base.BaseTestCase):
file_mock = mock.mock_open() file_mock = mock.mock_open()
file_mock.side_effect = fake_open file_mock.side_effect = fake_open
# Attempt to retrieve the cert # 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( self.assertRaises(
exception.CertificateStorageException, exception.CertificateStorageException,
local_cert_manager.CertManager.get_cert, local_cert_manager.CertManager.get_cert,
@ -187,7 +187,7 @@ class TestLocalManager(base.BaseTestCase):
def test_store_cert(self): def test_store_cert(self):
self._store_cert() 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): def test_store_cert_with_io_error(self, file_mock):
file_mock.side_effect = IOError file_mock.side_effect = IOError

View File

@ -13,6 +13,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import importlib
import inspect import inspect
from unittest import mock from unittest import mock
@ -20,7 +21,6 @@ from oslo_config import cfg
from oslo_utils import importutils from oslo_utils import importutils
from osprofiler import initializer as profiler_init from osprofiler import initializer as profiler_init
from osprofiler import opts as profiler_opts from osprofiler import opts as profiler_opts
import six.moves as six
from magnum.common import profiler from magnum.common import profiler
from magnum import conf from magnum import conf
@ -45,7 +45,7 @@ class TestProfiler(base.TestCase):
for clsname in classes: for clsname in classes:
# give the metaclass and trace_cls() decorator a chance to patch # give the metaclass and trace_cls() decorator a chance to patch
# methods of the classes above # methods of the classes above
six.reload_module( importlib.reload(
importutils.import_module(clsname.rsplit('.', 1)[0])) importutils.import_module(clsname.rsplit('.', 1)[0]))
cls = importutils.import_class(clsname) cls = importutils.import_class(clsname)

View File

@ -19,8 +19,6 @@ from cryptography import x509 as c_x509
from cryptography.x509.oid import NameOID from cryptography.x509.oid import NameOID
from unittest import mock from unittest import mock
import six
from magnum.common import exception from magnum.common import exception
from magnum.common.x509 import operations from magnum.common.x509 import operations
from magnum.tests import base from magnum.tests import base
@ -30,11 +28,11 @@ class TestX509(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestX509, self).setUp() super(TestX509, self).setUp()
self.issuer_name = six.u("fake-issuer") self.issuer_name = "fake-issuer"
self.subject_name = six.u("fake-subject") self.subject_name = "fake-subject"
self.organization_name = six.u("fake-organization") self.organization_name = "fake-organization"
self.ca_encryption_password = six.b("fake-ca-password") self.ca_encryption_password = b"fake-ca-password"
self.encryption_password = six.b("fake-password") self.encryption_password = b"fake-password"
def _load_pems(self, keypairs, encryption_password): def _load_pems(self, keypairs, encryption_password):
private_key = serialization.load_pem_private_key( private_key = serialization.load_pem_private_key(
@ -131,7 +129,7 @@ class TestX509(base.BaseTestCase):
self.assertIn(basic_constraints, cert.extensions) self.assertIn(basic_constraints, cert.extensions)
def test_generate_ca_certificate_with_bytes_issuer_name(self): 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) cert, _ = self._generate_ca_certificate(issuer_name)
issuer_name = issuer_name.decode('utf-8') issuer_name = issuer_name.decode('utf-8')
@ -194,16 +192,16 @@ class TestX509(base.BaseTestCase):
private_key = self._generate_private_key() private_key = self._generate_private_key()
private_key = self._private_bytes(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) private_key = operations._load_pem_private_key(private_key)
self.assertIsInstance(private_key, rsa.RSAPrivateKey) self.assertIsInstance(private_key, rsa.RSAPrivateKey)
def test_load_pem_private_key_with_unicode_private_key(self): def test_load_pem_private_key_with_unicode_private_key(self):
private_key = self._generate_private_key() private_key = self._generate_private_key()
private_key = self._private_bytes(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) private_key = operations._load_pem_private_key(private_key)
self.assertIsInstance(private_key, rsa.RSAPrivateKey) self.assertIsInstance(private_key, rsa.RSAPrivateKey)
@ -213,7 +211,7 @@ class TestX509(base.BaseTestCase):
private_key = self._generate_private_key() private_key = self._generate_private_key()
csr_obj = self._build_csr(private_key) csr_obj = self._build_csr(private_key)
csr = csr_obj.public_bytes(serialization.Encoding.PEM) 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 mock_load_pem.return_value = csr_obj
operations.sign(csr, self.issuer_name, ca_key, operations.sign(csr, self.issuer_name, ca_key,
@ -225,7 +223,7 @@ class TestX509(base.BaseTestCase):
private_key = self._generate_private_key() private_key = self._generate_private_key()
csr_obj = self._build_csr(private_key) csr_obj = self._build_csr(private_key)
csr = csr_obj.public_bytes(serialization.Encoding.PEM) 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 mock_load_pem.return_value = csr_obj
certificate = operations.sign(csr, self.issuer_name, certificate = operations.sign(csr, self.issuer_name,
@ -238,7 +236,6 @@ class TestX509(base.BaseTestCase):
def test_sign_with_invalid_csr(self): def test_sign_with_invalid_csr(self):
ca_key = self._generate_private_key() ca_key = self._generate_private_key()
csr = 'test' csr = 'test'
csr = six.u(csr)
self.assertRaises(exception.InvalidCsr, self.assertRaises(exception.InvalidCsr,
operations.sign, operations.sign,