From c9bc471e063f2865d6423e4f1c9b81e73a45e43f Mon Sep 17 00:00:00 2001 From: Stanislav Levin Date: Wed, 4 Aug 2021 18:38:16 +0300 Subject: [PATCH] ipatests: Fix TestAJPSecretUpgrade tests on systems without pkiuser Tests in `test_ipaserver.test_secure_ajp_connector' assume that there is pkiuser in OS, but this is not always true (for example, in systems having minimum installed dependencies, in particular, without pki-server RPM package). Since the tests already use the mock and pkiuser entity is not the subject of testing the pwd.getpwnam has been mocked. Fixes: https://pagure.io/freeipa/issue/8942 Signed-off-by: Stanislav Levin Reviewed-By: Rob Crittenden --- .../test_secure_ajp_connector.py | 40 ++++++++++++++++--- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/ipatests/test_ipaserver/test_secure_ajp_connector.py b/ipatests/test_ipaserver/test_secure_ajp_connector.py index 2719dbc48..35ef7407a 100644 --- a/ipatests/test_ipaserver/test_secure_ajp_connector.py +++ b/ipatests/test_ipaserver/test_secure_ajp_connector.py @@ -1,5 +1,6 @@ # Copyright (C) 2021 FreeIPA Project Contributors - see LICENSE file +from collections import namedtuple from io import BytesIO from lxml.etree import parse as myparse # pylint: disable=no-name-in-module import pytest @@ -32,6 +33,32 @@ def mock_etree_parse(data): return myparse(f) +def mock_pkiuser_entity(): + """Return struct_passwd for mocked pkiuser""" + StructPasswd = namedtuple( + "StructPasswd", + [ + "pw_name", + "pw_passwd", + "pw_uid", + "pw_gid", + "pw_gecos", + "pw_dir", + "pw_shell", + ] + ) + pkiuser_entity = StructPasswd( + constants.PKI_USER, + pw_passwd="x", + pw_uid=-1, + pw_gid=-1, + pw_gecos="", + pw_dir="/dev/null", + pw_shell="/sbin/nologin", + ) + return pkiuser_entity + + # Format of test_data is: # ( # is_newer_tomcat (boolean), @@ -148,14 +175,15 @@ test_data = ( class TestAJPSecretUpgrade: - @patch('os.chown') - @patch('lxml.etree.parse') - @pytest.mark.parametrize('is_newer, data, secret, expect, rewrite', - test_data) - def test_connecter(self, mock_parse, mock_chown, is_newer, data, secret, - expect, rewrite): + @patch("ipaplatform.base.constants.pwd.getpwnam") + @patch("ipaplatform.base.constants.os.chown") + @patch("ipaserver.install.dogtaginstance.lxml.etree.parse") + @pytest.mark.parametrize("test_data", test_data) + def test_connecter(self, mock_parse, mock_chown, mock_getpwnam, test_data): + is_newer, data, secret, expect, rewrite = test_data mock_chown.return_value = None mock_parse.return_value = mock_etree_parse(data) + mock_getpwnam.return_value = mock_pkiuser_entity() dogtag = MyDogtagInstance(is_newer) with patch('ipaserver.install.dogtaginstance.open', mock_open()) \ -- 2.31.1