Skip to content

Commit 6cec55d

Browse files
authored
Enabled blank-out checking for end2end_mocked tests; Fixes (#2120)
Details: * Enabled logging and checking for non-blanked out values using the check_blanked.py script also for end2end_mocked. * Moved the enablement of logging in end2end tests that was done only for the real end2end tests, to a place where it is now done for both real and mocked end2end tests. * Improved the new check_blanked.py script by adding an option --accept-null that accepts 'null', 'None' and None also as valid blanked-out values. This has become an option instead of a default behavior because the future improved blank-out mechanism should use the blank-out string also for values that are None, and then we can stop specifying the option. * Since the check mechanism now reports findings for the end2end_mocked test, these findings are tolerated for the time being. A future PR will resolve them. * Fixed an error on Python 3.9 about f-strings not supporting backslashes. * Fixed an error on Windows for the execution of the check_blanked.py script by invoking it through bash. Signed-off-by: Andreas Maier <maiera@de.ibm.com>
1 parent 72d1386 commit 6cec55d

4 files changed

Lines changed: 55 additions & 37 deletions

File tree

Makefile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ help:
295295
@echo " unittest - Run unit tests (adds to coverage results)"
296296
@echo " functiontest - Run function tests (adds to coverage results)"
297297
@echo " test - Run unit and function tests (adds to coverage results)"
298-
@echo " end2end_mocked - Run end2end tests against example mock environments (adds to coverage results)"
298+
@echo " end2end_mocked - Run end2end tests against example mock environments (adds to coverage results, checks blanked-out properties in log)"
299299
@echo " installtest - Run install tests"
300300
@echo " build - Build the distribution files in: $(dist_dir)"
301301
@echo " builddoc - Build documentation in: $(doc_build_dir)"
@@ -706,14 +706,16 @@ end2end: $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done $(package_py_files) $
706706
-$(call RM_FUNC,end2end.log)
707707
bash -c "PYTHONPATH=. TESTLOGFILE=end2end.log TESTEND2END_LOAD=true coverage run --append -m pytest -v -m 'not check_hmcs' $(pytest_general_opts) $(pytest_test_opts) $(test_dir)/end2end"
708708
coverage html
709-
tools/check_blanked.py end2end.log
709+
bash -c "tools/check_blanked.py --accept-null end2end.log"
710710
@echo "Makefile: $@ done."
711711

712712
# TODO: Enable rc checking again once the remaining issues are resolved
713713
.PHONY: end2end_mocked
714714
end2end_mocked: $(done_dir)/develop_$(pymn)_$(PACKAGE_LEVEL).done $(package_py_files) $(test_end2end_py_files) $(test_common_py_files) $(coverage_config_file) tests/end2end/mocked_inventory.yaml tests/end2end/mocked_vault.yaml tests/end2end/mocked_hmc_z16.yaml
715-
bash -c "PYTHONPATH=. TESTEND2END_LOAD=true TESTINVENTORY=tests/end2end/mocked_inventory.yaml TESTVAULT=tests/end2end/mocked_vault.yaml coverage run --append -m pytest -v -m 'not check_hmcs' $(pytest_general_opts) $(pytest_test_opts) $(test_dir)/end2end"
715+
-$(call RM_FUNC,end2end.log)
716+
bash -c "PYTHONPATH=. TESTLOGFILE=end2end.log TESTEND2END_LOAD=true TESTINVENTORY=tests/end2end/mocked_inventory.yaml TESTVAULT=tests/end2end/mocked_vault.yaml coverage run --append -m pytest -v -m 'not check_hmcs' $(pytest_general_opts) $(pytest_test_opts) $(test_dir)/end2end"
716717
coverage html
718+
-bash -c "tools/check_blanked.py --accept-null end2end.log"
717719
@echo "Makefile: $@ done."
718720

719721
.PHONY: authors

changes/noissue.25.feature.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Test: Added support for checking blanked-out sensitive properties in the
22
zhmcclient log file created during the end2end tests, by adding a new
3-
script tools/check_blanked.py, and running it during "make end2end".
3+
script tools/check_blanked.py, and running it during "make end2end"
4+
and "make end2end_mocked".

tools/check_blanked.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@
2727

2828
# Pattern for matching a single property name and value
2929
PROPERTY_PATTERN = re.compile(
30+
# Property name, including quotes
3031
rf"""(['"])([^'"]*({'|'.join(PROPERTY_NAME_ENDS)}))\1"""
32+
# Separator
3133
r"""\s*:\s*"""
34+
# Property value, including quotes
3235
r"""('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|None|null)"""
3336
)
3437

38+
# On Python 3.9, f-strings must not contain backslashes literally, so we set
39+
# the backslash sequence in a variable.
40+
NL_SEP = "\n "
41+
3542

3643
def parse_args():
3744
"""
@@ -46,7 +53,7 @@ def parse_args():
4653
4754
The properties that are checked are those whose names end with:
4855
49-
{'\n '.join(PROPERTY_NAME_ENDS)}
56+
{NL_SEP.join(PROPERTY_NAME_ENDS)}
5057
5158
The following syntax forms for the properties in the file are supported:
5259
@@ -56,8 +63,12 @@ def parse_args():
5663
"name": "value"
5764
""")
5865

66+
parser.add_argument("--accept-null", action="store_true",
67+
help="accept 'null', 'None' or None as blanked-out "
68+
"values.")
69+
5970
parser.add_argument(dest="file", metavar='FILE',
60-
help="Path name of the zhmcclient log file to be "
71+
help="path name of the zhmcclient log file to be "
6172
"checked")
6273

6374
return parser.parse_args(sys.argv[1:])
@@ -69,6 +80,8 @@ def main():
6980
"""
7081
args = parse_args()
7182
file = args.file
83+
accept_null = args.accept_null
84+
7285
print(f"Checking blanked properties in file: {file}")
7386

7487
checked_pnames = set()
@@ -79,7 +92,9 @@ def main():
7992
pname = match.group(2)
8093
pvalue = match.group(4).strip('"').strip("'")
8194
checked_pnames.add(pname)
82-
if pvalue != BLANKED_OUT_STRING:
95+
if not (pvalue == BLANKED_OUT_STRING or
96+
accept_null and (pvalue is None or pvalue == "None" or
97+
pvalue == "null")):
8398
rc = 1
8499
print(f"{file}({lineno}): Found property {pname!r} with "
85100
f"non-blanked value {pvalue!r}")

zhmcclient/testutils/_hmc_session_functions.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,36 @@ def setup_hmc_session(hd, rt_config=None, skip_on_failure=True):
125125
if skip_msg:
126126
pytest.skip(f"Skip reason from earlier attempt: {skip_msg}")
127127

128+
# Enable debug logging if specified
129+
log_file = os.getenv('TESTLOGFILE', None)
130+
if log_file:
131+
132+
logging.Formatter.converter = LOG_DATETIME_TIMEZONE
133+
log_formatter = logging.Formatter(
134+
LOG_FORMAT_STRING, datefmt=LOG_DATETIME_FORMAT)
135+
log_handler = logging.FileHandler(log_file, encoding='utf-8')
136+
log_handler.setFormatter(log_formatter)
137+
138+
logger = logging.getLogger('zhmcclient.hmc')
139+
if log_handler not in logger.handlers:
140+
logger.addHandler(log_handler)
141+
logger.setLevel(logging.DEBUG)
142+
143+
logger = logging.getLogger('zhmcclient.api')
144+
if log_handler not in logger.handlers:
145+
logger.addHandler(log_handler)
146+
logger.setLevel(logging.DEBUG)
147+
148+
logger = logging.getLogger('zhmcclient.jms')
149+
if log_handler not in logger.handlers:
150+
logger.addHandler(log_handler)
151+
logger.setLevel(logging.DEBUG)
152+
153+
logger = logging.getLogger('zhmcclient.os')
154+
if log_handler not in logger.handlers:
155+
logger.addHandler(log_handler)
156+
logger.setLevel(logging.DEBUG)
157+
128158
if hd.mock_file:
129159
# A mocked HMC
130160

@@ -138,36 +168,6 @@ def setup_hmc_session(hd, rt_config=None, skip_on_failure=True):
138168
else:
139169
# A real HMC
140170

141-
# Enable debug logging if specified
142-
log_file = os.getenv('TESTLOGFILE', None)
143-
if log_file:
144-
145-
logging.Formatter.converter = LOG_DATETIME_TIMEZONE
146-
log_formatter = logging.Formatter(
147-
LOG_FORMAT_STRING, datefmt=LOG_DATETIME_FORMAT)
148-
log_handler = logging.FileHandler(log_file, encoding='utf-8')
149-
log_handler.setFormatter(log_formatter)
150-
151-
logger = logging.getLogger('zhmcclient.hmc')
152-
if log_handler not in logger.handlers:
153-
logger.addHandler(log_handler)
154-
logger.setLevel(logging.DEBUG)
155-
156-
logger = logging.getLogger('zhmcclient.api')
157-
if log_handler not in logger.handlers:
158-
logger.addHandler(log_handler)
159-
logger.setLevel(logging.DEBUG)
160-
161-
logger = logging.getLogger('zhmcclient.jms')
162-
if log_handler not in logger.handlers:
163-
logger.addHandler(log_handler)
164-
logger.setLevel(logging.DEBUG)
165-
166-
logger = logging.getLogger('zhmcclient.os')
167-
if log_handler not in logger.handlers:
168-
logger.addHandler(log_handler)
169-
logger.setLevel(logging.DEBUG)
170-
171171
if hd.password_command is not None:
172172
variables = dict(host=hd.host, userid=hd.userid)
173173
# May raise PasswordCommandFailure:

0 commit comments

Comments
 (0)