From 8ab3aa06ff3297498760d11af4bdc36708f9c413 Mon Sep 17 00:00:00 2001 From: Rafael Guterres Jeffman <rjeffman@redhat.com> Date: Mon, 4 Jul 2022 12:15:40 -0300 Subject: [PATCH] pytest tests: Enhanced assertion for check_* methods. Checking if some output is present or absent from standard streams was done by simple string searching. Due to recent changes in FreeIPA, this search is not effective due to capitalization differences in boolean values output. Changing the string searching to regular expression searches fixes this behavior for current and previous versions of FreeIPA. This patch also adds more information on the assert tests in case of an error, so that it is easier to understand why the test failed. --- tests/utils.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index db22f973..c7e630a5 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -21,6 +21,7 @@ import os import pytest +import re import subprocess import tempfile import testinfra @@ -314,6 +315,10 @@ class AnsibleFreeIPATestCase(TestCase): expected_msg in result.stderr.decode("utf8") ) + @staticmethod + def __is_text_on_data(text, data): + return re.search(text, data) is not None + def check_details(self, expected_output, cmd, extra_cmds=None): cmd = "ipa " + cmd if extra_cmds: @@ -322,10 +327,16 @@ class AnsibleFreeIPATestCase(TestCase): res = self.master.run(cmd) if res.rc != 0: for output in expected_output: - assert output in res.stderr + assert self.__is_text_on_data(output, res.stderr), ( + f"\n{'='*40}\nExpected: {output}\n{'='*40}\n" + + f"Output:\n{res.stderr}{'='*40}\n" + ) else: for output in expected_output: - assert output in res.stdout + assert self.__is_text_on_data(output, res.stdout), ( + f"\n{'='*40}\nExpected: {output}\n{'='*40}\n" + + f"Output:\n{res.stdout}{'='*40}\n" + ) kdestroy(self.master) def check_notexists(self, members, cmd, extra_cmds=None): @@ -335,7 +346,10 @@ class AnsibleFreeIPATestCase(TestCase): kinit_admin(self.master) res = self.master.run(cmd) for member in members: - assert member not in res.stdout + assert not self.__is_text_on_data(member, res.stdout), ( + f"\n{'='*40}\nExpected: {member}\n{'='*40}\n" + + f"Output:\n{res.stdout}{'='*40}\n" + ) kdestroy(self.master) def mark_xfail_using_ansible_freeipa_version(self, version, reason): -- GitLab