Merge "Replace assertIn with assertRaisesRegex"

This commit is contained in:
Zuul
2025-07-02 09:11:15 +00:00
committed by Gerrit Code Review
9 changed files with 80 additions and 124 deletions

View File

@@ -617,11 +617,10 @@ class TestCinderStoreBase(object):
side_effect=fake_open):
mock_cc.return_value = mock.MagicMock(client=fake_client,
volumes=fake_volumes)
try:
self.store.add(expected_image_id, image_file, expected_size,
self.hash_algo, self.context, verifier)
except exceptions.Invalid as e:
self.assertIn(expected_error, e.msg)
self.assertRaisesRegex(
exceptions.Invalid, expected_error,
self.store.add, expected_image_id, image_file,
expected_size, self.hash_algo, self.context, verifier)
fake_volume.delete.assert_called_once()
self.assertEqual(image_file.tell(),

View File

@@ -151,11 +151,10 @@ class TestStore(base.StoreBaseTest,
image_file = io.BytesIO(b'a' * (expected_file_size + 100))
# Call method and assert exception
try:
self.store.add(expected_image_id, image_file,
expected_file_size, self.hash_algo)
except exceptions.Invalid as e:
self.assertIn("Size exceeds: expected", str(e))
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_file,
expected_file_size, self.hash_algo)
# Verify partial data is deleted from backend
self.assertFalse(os.path.exists(path))
@@ -179,11 +178,10 @@ class TestStore(base.StoreBaseTest,
image_file = io.BytesIO(b'b' * actual_data_size)
# Call method and assert exception
try:
self.store.add(expected_image_id, image_file,
declared_size, self.hash_algo)
except exceptions.Invalid as e:
self.assertIn("Size mismatch: expected", str(e))
self.assertRaisesRegex(
exceptions.Invalid, "Size mismatch: expected",
self.store.add, expected_image_id, image_file,
declared_size, self.hash_algo)
# Verify partial data is deleted from backend
self.assertFalse(os.path.exists(path))

View File

@@ -267,10 +267,9 @@ class TestMultiStore(base.MultiStoreBaseTest,
image_file = io.BytesIO(b'a' * (expected_file_size + 100))
# Call method and assert exception
try:
self.store.add(expected_image_id, image_file, expected_file_size)
except exceptions.Invalid as e:
self.assertIn("Size exceeds: expected", str(e))
self.assertRaisesRegex(exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_file,
expected_file_size)
# Verify partial data is deleted from backend
self.assertFalse(os.path.exists(path))
@@ -294,10 +293,9 @@ class TestMultiStore(base.MultiStoreBaseTest,
image_file = io.BytesIO(b'b' * actual_data_size)
# Call method and assert exception
try:
self.store.add(expected_image_id, image_file, declared_size)
except exceptions.Invalid as e:
self.assertIn("Size mismatch: expected", str(e))
self.assertRaisesRegex(exceptions.Invalid, "Size mismatch: expected",
self.store.add, expected_image_id, image_file,
declared_size)
# Verify partial data is deleted from backend
self.assertFalse(os.path.exists(path))

View File

@@ -257,10 +257,10 @@ class TestMultiStore(base.MultiStoreBaseTest,
data = b'a' * (total_bytes + 1024)
image_file = io.BytesIO(data)
with mock.patch.object(rbd_store.rbd.Image, 'write'):
try:
self.store.add('fake_image_id', image_file, image_size)
except exceptions.Invalid as e:
self.assertIn('Size exceeds: expected', e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, 'fake_image_id', image_file,
image_size)
# Confirm that image deletion was called due to size mismatch
mock_delete.assert_called()
@@ -277,10 +277,10 @@ class TestMultiStore(base.MultiStoreBaseTest,
data = b'a' * (total_bytes - 100)
image_file = io.BytesIO(data)
with mock.patch.object(rbd_store.rbd.Image, 'write'):
try:
self.store.add('fake_image_id', image_file, image_size)
except exceptions.Invalid as e:
self.assertIn('Size mismatch: expected', e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size mismatch: expected",
self.store.add, 'fake_image_id', image_file,
image_size)
# Confirm that image deletion was called due to size mismatch
mock_delete.assert_called()

View File

@@ -674,11 +674,10 @@ class TestMultiS3Store(base.MultiStoreBaseTest,
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
try:
self.store.add(expected_image_id, image_s3, expected_s3_size,
self.hash_algo)
except exceptions.Invalid as e:
self.assertIn("Size exceeds: expected", str(e))
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# Verify that the stream's position reflects the number of bytes
# read, which should be exactly at expected_file_size plus the

View File

@@ -357,11 +357,10 @@ class TestStore(base.StoreBaseTest,
data = b'a' * (total_bytes + 1024)
image_file = io.BytesIO(data)
with mock.patch.object(rbd_store.rbd.Image, 'write'):
try:
self.store.add('fake_image_id', image_file, image_size,
self.hash_algo)
except exceptions.Invalid as e:
self.assertIn('Size exceeds: expected', e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, 'fake_image_id', image_file,
image_size, self.hash_algo)
# Confirm that image deletion was called due to size mismatch
mock_delete.assert_called()
@@ -378,11 +377,10 @@ class TestStore(base.StoreBaseTest,
data = b'a' * (total_bytes - 100)
image_file = io.BytesIO(data)
with mock.patch.object(rbd_store.rbd.Image, 'write'):
try:
self.store.add('fake_image_id', image_file, image_size,
self.hash_algo)
except exceptions.Invalid as e:
self.assertIn('Size mismatch: expected', e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size mismatch: expected",
self.store.add, 'fake_image_id', image_file,
image_size, self.hash_algo)
# Confirm that image deletion was called due to size mismatch
mock_delete.assert_called()

View File

@@ -566,11 +566,10 @@ class TestStore(base.StoreBaseTest,
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
try:
self.store.add(expected_image_id, image_s3, expected_s3_size,
self.hash_algo)
except exceptions.Invalid as exc:
self.assertIn("Size exceeds: expected", exc.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# Verify that the stream's position reflects the number of bytes
# read, which should be exactly at expected_file_size plus the
@@ -633,11 +632,10 @@ class TestStore(base.StoreBaseTest,
mock_client.return_value = fake_s3_client
# Expect an exception due to size mismatch
try:
self.store.add(expected_image_id, image_s3, expected_s3_size,
self.hash_algo)
except exceptions.Invalid as exc:
self.assertIn("Size mismatch: expected", exc.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size mismatch: expected",
self.store.add, expected_image_id, image_s3,
expected_s3_size, self.hash_algo)
# The input buffer should be fully read depending on implementation
total_input_size = len(expected_s3_contents)

View File

@@ -27,7 +27,6 @@ import tempfile
import uuid
from oslo_config import cfg
from oslo_utils import encodeutils
from oslo_utils import units
import requests_mock
import swiftclient
@@ -480,11 +479,10 @@ class SwiftTests(object):
with mock.patch.object(self.store,
'_delete_stale_chunks') as mock_delete:
try:
self.store.add(expected_image_id, image_swift,
expected_swift_size, HASH_ALGO)
except exceptions.Invalid as e:
self.assertIn("Size exceeds: expected", e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_swift,
expected_swift_size, HASH_ALGO)
# The position should be equal to total input size
self.assertEqual(image_swift.tell(), len(expected_swift_contents))
@@ -509,11 +507,10 @@ class SwiftTests(object):
with mock.patch.object(self.store,
'_delete_stale_chunks') as mock_delete:
try:
self.store.add(expected_image_id, image_swift,
expected_swift_size, HASH_ALGO)
except exceptions.Invalid as e:
self.assertIn("Size mismatch: expected", e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size mismatch: expected",
self.store.add, expected_image_id, image_swift,
expected_swift_size, HASH_ALGO)
# The position should be equal to actual data size
self.assertEqual(image_swift.tell(), len(expected_swift_contents))
@@ -691,17 +688,10 @@ class SwiftTests(object):
global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0
# We check the exception text to ensure the container
# missing text is found in it, otherwise, we would have
# simply used self.assertRaises here
exception_caught = False
try:
self.store.add(str(uuid.uuid4()), image_swift, 0, HASH_ALGO)
except exceptions.BackendException as e:
exception_caught = True
self.assertIn("container noexist does not exist in Swift",
encodeutils.exception_to_unicode(e))
self.assertTrue(exception_caught)
msg = "container noexist does not exist in Swift"
self.assertRaisesRegex(exceptions.BackendException, msg,
self.store.add, str(uuid.uuid4()),
image_swift, 0, HASH_ALGO)
self.assertEqual(0, SWIFT_PUT_OBJECT_CALLS)
@mock.patch('glance_store._drivers.swift.utils'
@@ -829,18 +819,11 @@ class SwiftTests(object):
global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0
# We check the exception text to ensure the container
# missing text is found in it, otherwise, we would have
# simply used self.assertRaises here
exception_caught = False
try:
self.store.add(expected_image_id, image_swift, 0, HASH_ALGO)
except exceptions.BackendException as e:
exception_caught = True
expected_msg = "container %s does not exist in Swift"
expected_msg = expected_msg % expected_container
self.assertIn(expected_msg, encodeutils.exception_to_unicode(e))
self.assertTrue(exception_caught)
expected_msg = "container %s does not exist in Swift"
expected_msg = expected_msg % expected_container
self.assertRaisesRegex(exceptions.BackendException, expected_msg,
self.store.add, expected_image_id,
image_swift, 0, HASH_ALGO)
self.assertEqual(0, SWIFT_PUT_OBJECT_CALLS)
@mock.patch('glance_store._drivers.swift.utils'

View File

@@ -27,7 +27,6 @@ import tempfile
import uuid
from oslo_config import cfg
from oslo_utils import encodeutils
from oslo_utils import units
import requests_mock
import swiftclient
@@ -453,11 +452,10 @@ class SwiftTests(object):
with mock.patch.object(self.store,
'_delete_stale_chunks') as mock_delete:
try:
self.store.add(expected_image_id, image_swift,
expected_swift_size)
except exceptions.Invalid as e:
self.assertIn("Size exceeds: expected", e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size exceeds: expected",
self.store.add, expected_image_id, image_swift,
expected_swift_size)
# The position should be equal to total input size
self.assertEqual(image_swift.tell(), len(expected_swift_contents))
@@ -482,11 +480,10 @@ class SwiftTests(object):
with mock.patch.object(self.store,
'_delete_stale_chunks') as mock_delete:
try:
self.store.add(expected_image_id, image_swift,
expected_swift_size)
except exceptions.Invalid as e:
self.assertIn("Size mismatch: expected", e.msg)
self.assertRaisesRegex(
exceptions.Invalid, "Size mismatch: expected",
self.store.add, expected_image_id, image_swift,
expected_swift_size)
# The position should be equal to actual data size
self.assertEqual(image_swift.tell(), len(expected_swift_contents))
@@ -641,17 +638,10 @@ class SwiftTests(object):
global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0
# We check the exception text to ensure the container
# missing text is found in it, otherwise, we would have
# simply used self.assertRaises here
exception_caught = False
try:
self.store.add(str(uuid.uuid4()), image_swift, 0)
except exceptions.BackendException as e:
exception_caught = True
self.assertIn("container noexist does not exist in Swift",
encodeutils.exception_to_unicode(e))
self.assertTrue(exception_caught)
msg = "container noexist does not exist in Swift"
self.assertRaisesRegex(exceptions.BackendException, msg,
self.store.add, str(uuid.uuid4()),
image_swift, 0)
self.assertEqual(0, SWIFT_PUT_OBJECT_CALLS)
@mock.patch('glance_store._drivers.swift.utils'
@@ -779,18 +769,11 @@ class SwiftTests(object):
global SWIFT_PUT_OBJECT_CALLS
SWIFT_PUT_OBJECT_CALLS = 0
# We check the exception text to ensure the container
# missing text is found in it, otherwise, we would have
# simply used self.assertRaises here
exception_caught = False
try:
self.store.add(expected_image_id, image_swift, 0)
except exceptions.BackendException as e:
exception_caught = True
expected_msg = "container %s does not exist in Swift"
expected_msg = expected_msg % expected_container
self.assertIn(expected_msg, encodeutils.exception_to_unicode(e))
self.assertTrue(exception_caught)
expected_msg = "container %s does not exist in Swift"
expected_msg = expected_msg % expected_container
self.assertRaisesRegex(exceptions.BackendException, expected_msg,
self.store.add, expected_image_id,
image_swift, 0)
self.assertEqual(0, SWIFT_PUT_OBJECT_CALLS)
@mock.patch('glance_store._drivers.swift.utils'