Skip to content

Commit

Permalink
Removed code duplication in the new blank-properties tests
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Maier <[email protected]>
  • Loading branch information
andy-maier committed Nov 28, 2024
1 parent a269ab8 commit 5958432
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 46 deletions.
18 changes: 18 additions & 0 deletions tests/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,21 @@ def timestamp_aware(dt):
if dt.tzinfo is None:
dt = dt.replace(tzinfo=tz.tzlocal()) # new object
return dt


def assert_blanked_in_message(message, properties, blanked_properties):
"""
Assert that a message containing a string representation of a properties
dict has the desired properties blanked out.
Parameters:
message (str): The message to be checked.
properties (dict): Properties that can possibly be in the message
(with hyphened names).
blanked_properties (list of str): The names of the properties that
need to be blanked out in the message (with hyphened names).
"""
for pname in blanked_properties:
if pname in properties:
exp_str = f"'{pname}': '{zhmcclient.BLANKED_OUT_STRING}'"
assert message.find(exp_str) > 0
13 changes: 5 additions & 8 deletions tests/unit/zhmcclient/test_activation_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import logging
import pytest

from zhmcclient import Client, ActivationProfile, BLANKED_OUT_STRING
from zhmcclient import Client, ActivationProfile
from zhmcclient_mock import FakedSession
from tests.common.utils import assert_resources
from tests.common.utils import assert_resources, assert_blanked_in_message


class TestActivationProfile:
Expand Down Expand Up @@ -386,9 +386,6 @@ def test_profile_update_properties(self, caplog, input_props, profile_type):
assert prop_value == exp_prop_value

# Verify the API call log record for blanked-out properties.
if 'ssc-master-pw' in input_props:
exp_str = f"'ssc-master-pw': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
if 'zaware-master-pw' in input_props:
exp_str = f"'zaware-master-pw': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['ssc-master-pw', 'zaware-master-pw'])
17 changes: 8 additions & 9 deletions tests/unit/zhmcclient/test_ldap_server_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import logging
import pytest

from zhmcclient import Client, HTTPError, NotFound, LdapServerDefinition, \
BLANKED_OUT_STRING
from zhmcclient import Client, HTTPError, NotFound, LdapServerDefinition
from zhmcclient_mock import FakedSession
from tests.common.utils import assert_resources
from tests.common.utils import assert_resources, assert_blanked_in_message


class TestLdapServerDefinition:
Expand Down Expand Up @@ -206,9 +205,9 @@ def test_ldap_srv_def_manager_create(
assert value == exp_value

# Verify the API call log record for blanked-out properties.
if 'bind-password' in input_props:
exp_str = f"'bind-password': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['bind-password'])

def test_ldap_srv_def_repr(self):
"""Test LdapServerDefinition.__repr__()."""
Expand Down Expand Up @@ -357,6 +356,6 @@ def test_ldap_srv_def_update_properties(self, caplog, input_props):
assert prop_value == exp_prop_value

# Verify the API call log record for blanked-out properties.
if 'bind-password' in input_props:
exp_str = f"'bind-password': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['bind-password'])
14 changes: 5 additions & 9 deletions tests/unit/zhmcclient/test_lpar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
import pytest
import requests_mock

from zhmcclient import Client, Lpar, HTTPError, StatusTimeout, Job, \
BLANKED_OUT_STRING
from zhmcclient import Client, Lpar, HTTPError, StatusTimeout, Job
from zhmcclient_mock import FakedSession, LparActivateHandler, \
LparDeactivateHandler, LparLoadHandler
from tests.common.utils import assert_resources
from tests.common.utils import assert_resources, assert_blanked_in_message

# pylint: disable=unused-import,line-too-long
from tests.common.http_mocked_fixtures import http_mocked_session # noqa: F401
Expand Down Expand Up @@ -373,12 +372,9 @@ def test_lpar_update_properties(self, caplog, input_props, lpar_name):
assert prop_value == exp_prop_value

# Verify the API call log record for blanked-out properties.
if 'ssc-master-pw' in input_props:
exp_str = f"'ssc-master-pw': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
if 'zaware-master-pw' in input_props:
exp_str = f"'zaware-master-pw': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['ssc-master-pw', 'zaware-master-pw'])

@pytest.mark.parametrize(
"initial_profile, profile_kwargs, exp_profile, exp_profile_exc", [
Expand Down
23 changes: 8 additions & 15 deletions tests/unit/zhmcclient/test_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import logging
import pytest

from zhmcclient import Client, Partition, HTTPError, NotFound, \
BLANKED_OUT_STRING
from zhmcclient import Client, Partition, HTTPError, NotFound
from zhmcclient_mock import FakedSession
from tests.common.utils import assert_resources
from tests.common.utils import assert_resources, assert_blanked_in_message

# Object IDs and names of our faked partitions:
PART1_OID = 'part1-oid'
Expand Down Expand Up @@ -388,12 +387,9 @@ def test_pm_create(self, caplog, input_props, exp_prop_names, exp_exc):
assert value == exp_value

# Verify the API call log record for blanked-out properties.
if 'boot-ftp-password' in input_props:
exp_str = f"'boot-ftp-password': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
if 'ssc-master-pw' in input_props:
exp_str = f"'ssc-master-pw': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['boot-ftp-password', 'ssc-master-pw'])

def test_pm_resource_object(self):
"""
Expand Down Expand Up @@ -753,12 +749,9 @@ def test_partition_update_properties(
assert prop_value == exp_prop_value

# Verify the API call log record for blanked-out properties.
if 'boot-ftp-password' in input_props:
exp_str = f"'boot-ftp-password': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
if 'ssc-master-pw' in input_props:
exp_str = f"'ssc-master-pw': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['boot-ftp-password', 'ssc-master-pw'])

def test_partition_update_name(self):
"""
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/zhmcclient/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import logging
import pytest

from zhmcclient import Client, HTTPError, NotFound, User, BLANKED_OUT_STRING
from zhmcclient import Client, HTTPError, NotFound, User
from zhmcclient_mock import FakedSession
from tests.common.utils import assert_resources
from tests.common.utils import assert_resources, assert_blanked_in_message


class TestUser:
Expand Down Expand Up @@ -228,9 +228,9 @@ def test_user_manager_create(
assert value == exp_value

# Verify the API call log record for blanked-out properties.
if 'password' in input_props:
exp_str = f"'password': '{BLANKED_OUT_STRING}'"
assert call_record.message.find(exp_str) > 0
assert_blanked_in_message(
call_record.message, input_props,
['password'])

def test_user_repr(self):
"""Test User.__repr__()."""
Expand Down

0 comments on commit 5958432

Please sign in to comment.