Skip to content

Commit 00ef77a

Browse files
committed
Fix backward compatibility for renamed exception classes
Resolves GitHub issue #220 by adding backward compatibility aliases for exception classes that were renamed in commit fdd69b1 to follow PEP8 naming conventions. Changes: - Add RTSLibALUANotSupported alias for RTSLibALUANotSupportedError - Add RTSLibNotInCFS alias for RTSLibNotInCFSError - Export both old and new names in __all__ - Add comprehensive unit tests for backward compatibility This restores compatibility with external projects like targetd that depend on the original exception names while maintaining the new PEP8-compliant names.
1 parent 03ec9c1 commit 00ef77a

File tree

4 files changed

+133
-14
lines changed

4 files changed

+133
-14
lines changed

rtslib/__init__.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,36 @@
3838
UserBackedStorageObject,
3939
)
4040
from .utils import (
41+
RTSLibALUANotSupported, # Backward compatibility alias
4142
RTSLibALUANotSupportedError,
4243
RTSLibBrokenLink,
4344
RTSLibError,
45+
RTSLibNotInCFS, # Backward compatibility alias
4446
RTSLibNotInCFSError,
4547
)
4648

4749
__all__ = [
48-
"RTSRoot",
49-
"RTSLibError",
50-
"RTSLibBrokenLink",
51-
"RTSLibNotInCFSError",
52-
"RTSLibALUANotSupportedError",
5350
"LUN",
54-
"MappedLUN",
55-
"NodeACL",
56-
"NetworkPortal",
5751
"TPG",
58-
"Target",
59-
"NodeACLGroup",
60-
"MappedLUNGroup",
52+
"ALUATargetPortGroup",
53+
"BlockStorageObject",
6154
"FabricModule",
6255
"FileIOStorageObject",
63-
"BlockStorageObject",
56+
"MappedLUN",
57+
"MappedLUNGroup",
58+
"NetworkPortal",
59+
"NodeACL",
60+
"NodeACLGroup",
6461
"PSCSIStorageObject",
6562
"RDMCPStorageObject",
66-
"UserBackedStorageObject",
63+
"RTSLibALUANotSupported", # Backward compatibility alias
64+
"RTSLibALUANotSupportedError",
65+
"RTSLibBrokenLink",
66+
"RTSLibError",
67+
"RTSLibNotInCFS", # Backward compatibility alias
68+
"RTSLibNotInCFSError",
69+
"RTSRoot",
6770
"StorageObjectFactory",
68-
"ALUATargetPortGroup",
71+
"Target",
72+
"UserBackedStorageObject",
6973
]

rtslib/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class RTSLibNotInCFSError(RTSLibError):
5353
object that does not exist.
5454
'''
5555

56+
57+
# Backward compatibility aliases for the old exception names
58+
# These were renamed in commit fdd69b1 to follow PEP8 naming conventions
59+
# but broke backward compatibility. Keep the old names as aliases.
60+
RTSLibALUANotSupported = RTSLibALUANotSupportedError
61+
RTSLibNotInCFS = RTSLibNotInCFSError
62+
5663
def fwrite(path, string):
5764
'''
5865
This function writes a string to a file, and takes care of

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Test package for rtslib-fb
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
Unit tests for backward compatibility of renamed exception classes.
3+
4+
Tests that the old exception names from before commit fdd69b1 still work
5+
to maintain compatibility with external projects like targetd.
6+
"""
7+
8+
import pytest
9+
10+
11+
class TestBackwardCompatibility:
12+
"""Test backward compatibility for renamed exception classes."""
13+
14+
def test_old_exception_names_exist(self):
15+
"""Test that old exception names are still available in rtslib module."""
16+
import rtslib
17+
18+
# Test that old names exist
19+
assert hasattr(rtslib, 'RTSLibALUANotSupported')
20+
assert hasattr(rtslib, 'RTSLibNotInCFS')
21+
22+
def test_new_exception_names_exist(self):
23+
"""Test that new PEP8-compliant exception names exist."""
24+
import rtslib
25+
26+
# Test that new names exist
27+
assert hasattr(rtslib, 'RTSLibALUANotSupportedError')
28+
assert hasattr(rtslib, 'RTSLibNotInCFSError')
29+
30+
def test_exception_aliases_point_to_same_class(self):
31+
"""Test that old and new names refer to the same classes."""
32+
import rtslib
33+
34+
assert rtslib.RTSLibALUANotSupported is rtslib.RTSLibALUANotSupportedError
35+
assert rtslib.RTSLibNotInCFS is rtslib.RTSLibNotInCFSError
36+
37+
def test_aliases_in_module_all(self):
38+
"""Test that both old and new names are exported in __all__."""
39+
import rtslib
40+
41+
assert 'RTSLibALUANotSupported' in rtslib.__all__
42+
assert 'RTSLibNotInCFS' in rtslib.__all__
43+
assert 'RTSLibALUANotSupportedError' in rtslib.__all__
44+
assert 'RTSLibNotInCFSError' in rtslib.__all__
45+
46+
def test_exception_inheritance(self):
47+
"""Test that all exceptions inherit from RTSLibError."""
48+
import rtslib
49+
50+
assert issubclass(rtslib.RTSLibALUANotSupported, rtslib.RTSLibError)
51+
assert issubclass(rtslib.RTSLibNotInCFS, rtslib.RTSLibError)
52+
assert issubclass(rtslib.RTSLibALUANotSupportedError, rtslib.RTSLibError)
53+
assert issubclass(rtslib.RTSLibNotInCFSError, rtslib.RTSLibError)
54+
55+
def test_raise_and_catch_old_alua_exception(self):
56+
"""Test raising and catching RTSLibALUANotSupported."""
57+
import rtslib
58+
59+
with pytest.raises(rtslib.RTSLibALUANotSupported):
60+
raise rtslib.RTSLibALUANotSupported("Test ALUA exception")
61+
62+
def test_raise_and_catch_old_cfs_exception(self):
63+
"""Test raising and catching RTSLibNotInCFS."""
64+
import rtslib
65+
66+
with pytest.raises(rtslib.RTSLibNotInCFS):
67+
raise rtslib.RTSLibNotInCFS("Test CFS exception")
68+
69+
def test_cross_compatibility_alua_exception(self):
70+
"""Test that new exception can be caught with old name."""
71+
import rtslib
72+
73+
with pytest.raises(rtslib.RTSLibALUANotSupported):
74+
raise rtslib.RTSLibALUANotSupportedError("Test exception")
75+
76+
def test_cross_compatibility_cfs_exception(self):
77+
"""Test that new exception can be caught with old name."""
78+
import rtslib
79+
80+
with pytest.raises(rtslib.RTSLibNotInCFS):
81+
raise rtslib.RTSLibNotInCFSError("Test exception")
82+
83+
def test_old_exception_can_be_caught_with_new_name(self):
84+
"""Test that old exception name can be caught with new name."""
85+
import rtslib
86+
87+
with pytest.raises(rtslib.RTSLibALUANotSupportedError):
88+
raise rtslib.RTSLibALUANotSupported("Test exception")
89+
90+
with pytest.raises(rtslib.RTSLibNotInCFSError):
91+
raise rtslib.RTSLibNotInCFS("Test exception")
92+
93+
def test_exception_messages_preserved(self):
94+
"""Test that exception messages work correctly with both names."""
95+
import rtslib
96+
97+
test_message = "Custom error message"
98+
99+
try:
100+
raise rtslib.RTSLibALUANotSupported(test_message)
101+
except rtslib.RTSLibALUANotSupportedError as e:
102+
assert str(e) == test_message
103+
104+
try:
105+
raise rtslib.RTSLibNotInCFS(test_message)
106+
except rtslib.RTSLibNotInCFSError as e:
107+
assert str(e) == test_message

0 commit comments

Comments
 (0)