python-common: Remove duplicated DriveGroupSpec.__repr__ and __eq__

Signed-off-by: Sebastian Wagner <sewagner@redhat.com>
This commit is contained in:
Sebastian Wagner 2021-08-24 14:31:56 +02:00
parent 38b52f715f
commit b91f81801a
No known key found for this signature in database
GPG Key ID: 8D2442807E6979F8
3 changed files with 13 additions and 17 deletions

View File

@ -330,21 +330,5 @@ class DriveGroupSpec(ServiceSpec):
if self.method == 'raw' and self.objectstore == 'filestore':
raise DriveGroupValidationError('method raw only supports bluestore')
def __repr__(self) -> str:
keys = [
key for key in self._supported_features if getattr(self, key) is not None
]
if 'encrypted' in keys and not self.encrypted:
keys.remove('encrypted')
if 'objectstore' in keys and self.objectstore == 'bluestore':
keys.remove('objectstore')
return "DriveGroupSpec(name={}->{})".format(
self.service_id,
', '.join('{}={}'.format(key, repr(getattr(self, key))) for key in keys)
)
def __eq__(self, other: Any) -> bool:
return repr(self) == repr(other)
yaml.add_representer(DriveGroupSpec, DriveGroupSpec.yaml_representer)

View File

@ -668,7 +668,8 @@ class ServiceSpec(object):
)
def __repr__(self) -> str:
return "{}({!r})".format(self.__class__.__name__, self.__dict__)
y = yaml.dump(cast(dict, self), default_flow_style=False)
return f"{self.__class__.__name__}.from_json(yaml.safe_load('''{y}'''))"
def __eq__(self, other: Any) -> bool:
return (self.__class__ == other.__class__

View File

@ -342,6 +342,17 @@ def test_alertmanager_spec_2():
assert 'default_webhook_urls' in spec.user_data.keys()
def test_repr():
val = """ServiceSpec.from_json(yaml.safe_load('''service_type: crash
service_name: crash
placement:
count: 42
'''))"""
obj = eval(val)
assert obj.service_type == 'crash'
assert val == repr(obj)
@pytest.mark.parametrize("spec1, spec2, eq",
[
(