From 090eadbe4e4d0ab9fe387c1966b51cdf28ed9b17 Mon Sep 17 00:00:00 2001 From: Christian Heimes Date: Tue, 30 May 2017 10:36:59 +0200 Subject: [PATCH] Reimplement yield tests are parametrized tests https://pagure.io/freeipa/issue/6591 Signed-off-by: Christian Heimes Reviewed-By: Martin Basti --- ipatests/test_ipapython/test_ipautil.py | 85 ++++++++++------------ ipatests/test_ipapython/test_ssh.py | 96 ++++++++++++------------- 2 files changed, 82 insertions(+), 99 deletions(-) diff --git a/ipatests/test_ipapython/test_ipautil.py b/ipatests/test_ipapython/test_ipautil.py index 6427935b1..409cf2ddc 100644 --- a/ipatests/test_ipapython/test_ipautil.py +++ b/ipatests/test_ipapython/test_ipautil.py @@ -31,53 +31,44 @@ from ipapython import ipautil pytestmark = pytest.mark.tier0 -def make_ipaddress_checker(addr, words=None, prefixlen=None): - def check_ipaddress(): - try: - ip = ipautil.CheckedIPAddress(addr, match_local=False) - assert ip.words == words and ip.prefixlen == prefixlen - except Exception: - assert words is None and prefixlen is None - check_ipaddress.description = "Test IP address parsing and verification (%s)" % addr - return check_ipaddress - - -def test_ip_address(): - addrs = [ - ('0.0.0.0/0',), - ('10.11.12.13', (10, 11, 12, 13), 8), - ('10.11.12.13/14', (10, 11, 12, 13), 14), - ('10.11.12.13%zoneid',), - ('10.11.12.13%zoneid/14',), - ('10.11.12.1337',), - ('10.11.12.13/33',), - ('127.0.0.1',), - ('241.1.2.3',), - ('169.254.1.2',), - ('10.11.12.0/24', (10, 11, 12, 0), 24), - ('10.0.0.255', (10, 0, 0, 255), 8), - ('224.5.6.7',), - ('10.11.12.255/24', (10, 11, 12, 255), 24), - ('255.255.255.255',), - - ('::/0',), - ('2001::1', (0x2001, 0, 0, 0, 0, 0, 0, 1), 64), - ('2001::1/72', (0x2001, 0, 0, 0, 0, 0, 0, 1), 72), - ('2001::1%zoneid', (0x2001, 0, 0, 0, 0, 0, 0, 1), 64), - ('2001::1%zoneid/72',), - ('2001::1beef',), - ('2001::1/129',), - ('::1',), - ('6789::1',), - ('fe89::1',), - ('2001::/64', (0x2001, 0, 0, 0, 0, 0, 0, 0), 64), - ('ff01::1',), - - ('junk',) - ] - - for addr in addrs: - yield make_ipaddress_checker(*addr) +@pytest.mark.parametrize("addr,words,prefixlen", [ + ('0.0.0.0/0', None, None), + ('10.11.12.13', (10, 11, 12, 13), 8), + ('10.11.12.13/14', (10, 11, 12, 13), 14), + ('10.11.12.13%zoneid', None, None), + ('10.11.12.13%zoneid/14', None, None), + ('10.11.12.1337', None, None), + ('10.11.12.13/33', None, None), + ('127.0.0.1', None, None), + ('241.1.2.3', None, None), + ('169.254.1.2', None, None), + ('10.11.12.0/24', (10, 11, 12, 0), 24), + ('10.0.0.255', (10, 0, 0, 255), 8), + ('224.5.6.7', None, None), + ('10.11.12.255/24', (10, 11, 12, 255), 24), + ('255.255.255.255', None, None), + ('::/0', None, None), + ('2001::1', (0x2001, 0, 0, 0, 0, 0, 0, 1), 64), + ('2001::1/72', (0x2001, 0, 0, 0, 0, 0, 0, 1), 72), + ('2001::1%zoneid', (0x2001, 0, 0, 0, 0, 0, 0, 1), 64), + ('2001::1%zoneid/72', None, None), + ('2001::1beef', None, None), + ('2001::1/129', None, None), + ('::1', None, None), + ('6789::1', None, None), + ('fe89::1', None, None), + ('2001::/64', (0x2001, 0, 0, 0, 0, 0, 0, 0), 64), + ('ff01::1', None, None), + ('junk', None, None) +]) +def test_ip_address(addr, words, prefixlen): + if words is None: + pytest.raises( + ValueError, ipautil.CheckedIPAddress, addr, match_local=False) + else: + ip = ipautil.CheckedIPAddress(addr, match_local=False) + assert ip.words == words + assert ip.prefixlen == prefixlen class TestCIDict(object): diff --git a/ipatests/test_ipapython/test_ssh.py b/ipatests/test_ipapython/test_ssh.py index 817ef057e..848c14cad 100644 --- a/ipatests/test_ipapython/test_ssh.py +++ b/ipatests/test_ipapython/test_ssh.py @@ -32,61 +32,53 @@ if six.PY3: pytestmark = pytest.mark.tier0 +b64 = 'AAAAB3NzaC1yc2EAAAADAQABAAABAQDGAX3xAeLeaJggwTqMjxNwa6XHBUAikXPGMzEpVrlLDCZtv00djsFTBi38PkgxBJVkgRWMrcBsr/35lq7P6w8KGIwA8GI48Z0qBS2NBMJ2u9WQ2hjLN6GdMlo77O0uJY3251p12pCVIS/bHRSq8kHO2No8g7KA9fGGcagPfQH+ee3t7HUkpbQkFTmbPPN++r3V8oVUk5LxbryB3UIIVzNmcSIn3JrXynlvui4MixvrtX6zx+O/bBo68o8/eZD26QrahVbA09fivrn/4h3TM019Eu/c2jOdckfU3cHUV/3Tno5d6JicibyaoDDK7S/yjdn5jhaz8MSEayQvFkZkiF0L' +raw = base64.b64decode(b64) +openssh = 'ssh-rsa %s' % b64 -def make_public_key_checker(pk, out): - def check_public_key(): - try: - parsed = ssh.SSHPublicKey(pk) - assert parsed.openssh() == out - except Exception as e: - assert type(e) is out - check_public_key.description = "Test SSH public key parsing (%s)" % repr(pk) - return check_public_key -def test_public_key_parsing(): - b64 = 'AAAAB3NzaC1yc2EAAAADAQABAAABAQDGAX3xAeLeaJggwTqMjxNwa6XHBUAikXPGMzEpVrlLDCZtv00djsFTBi38PkgxBJVkgRWMrcBsr/35lq7P6w8KGIwA8GI48Z0qBS2NBMJ2u9WQ2hjLN6GdMlo77O0uJY3251p12pCVIS/bHRSq8kHO2No8g7KA9fGGcagPfQH+ee3t7HUkpbQkFTmbPPN++r3V8oVUk5LxbryB3UIIVzNmcSIn3JrXynlvui4MixvrtX6zx+O/bBo68o8/eZD26QrahVbA09fivrn/4h3TM019Eu/c2jOdckfU3cHUV/3Tno5d6JicibyaoDDK7S/yjdn5jhaz8MSEayQvFkZkiF0L' - raw = base64.b64decode(b64) - openssh = 'ssh-rsa %s' % b64 +@pytest.mark.parametrize("pk,out", [ + (b'\xff', UnicodeDecodeError), + (u'\xff', ValueError), - pks = [ - (b'\xff', UnicodeDecodeError), - (u'\xff', ValueError), + (raw, openssh), + (b'\0\0\0\x04none', u'none AAAABG5vbmU='), + (b'\0\0\0', ValueError), + (b'\0\0\0\0', ValueError), + (b'\0\0\0\x01', ValueError), + (b'\0\0\0\x01\xff', ValueError), - (raw, openssh), - (b'\0\0\0\x04none', u'none AAAABG5vbmU='), - (b'\0\0\0', ValueError), - (b'\0\0\0\0', ValueError), - (b'\0\0\0\x01', ValueError), - (b'\0\0\0\x01\xff', ValueError), + (u'\0\0\0\x04none', ValueError), + (u'\0\0\0', ValueError), + (u'\0\0\0\0', ValueError), + (u'\0\0\0\x01', ValueError), + (u'\0\0\0\x01\xff', ValueError), - (u'\0\0\0\x04none', ValueError), - (u'\0\0\0', ValueError), - (u'\0\0\0\0', ValueError), - (u'\0\0\0\x01', ValueError), - (u'\0\0\0\x01\xff', ValueError), + (b64, openssh), + (unicode(b64), openssh), + (b64.encode('ascii'), openssh), + (u'\n%s\n\n' % b64, openssh), + (u'AAAABG5vbmU=', u'none AAAABG5vbmU='), + (u'AAAAB', ValueError), - (b64, openssh), - (unicode(b64), openssh), - (b64.encode('ascii'), openssh), - (u'\n%s\n\n' % b64, openssh), - (u'AAAABG5vbmU=', u'none AAAABG5vbmU='), - (u'AAAAB', ValueError), - - (openssh, openssh), - (unicode(openssh), openssh), - (openssh.encode('ascii'), openssh), - (u'none AAAABG5vbmU=', u'none AAAABG5vbmU='), - (u'\t \t ssh-rsa \t \t%s\t \tthis is a comment\t \t ' % b64, - u'%s this is a comment' % openssh), - (u'opt3,opt2="\tx ",opt1,opt2="\\"x " %s comment ' % openssh, - u'opt1,opt2="\\"x ",opt3 %s comment' % openssh), - (u'ssh-rsa\n%s' % b64, ValueError), - (u'ssh-rsa\t%s' % b64, ValueError), - (u'vanitas %s' % b64, ValueError), - (u'@opt %s' % openssh, ValueError), - (u'opt=val %s' % openssh, ValueError), - (u'opt, %s' % openssh, ValueError), - ] - - for pk in pks: - yield make_public_key_checker(*pk) + (openssh, openssh), + (unicode(openssh), openssh), + (openssh.encode('ascii'), openssh), + (u'none AAAABG5vbmU=', u'none AAAABG5vbmU='), + (u'\t \t ssh-rsa \t \t%s\t \tthis is a comment\t \t ' % b64, + u'%s this is a comment' % openssh), + (u'opt3,opt2="\tx ",opt1,opt2="\\"x " %s comment ' % openssh, + u'opt1,opt2="\\"x ",opt3 %s comment' % openssh), + (u'ssh-rsa\n%s' % b64, ValueError), + (u'ssh-rsa\t%s' % b64, ValueError), + (u'vanitas %s' % b64, ValueError), + (u'@opt %s' % openssh, ValueError), + (u'opt=val %s' % openssh, ValueError), + (u'opt, %s' % openssh, ValueError), +]) +def test_public_key_parsing(pk, out): + if isinstance(out, type) and issubclass(out, Exception): + pytest.raises(out, ssh.SSHPublicKey, pk) + else: + parsed = ssh.SSHPublicKey(pk) + assert parsed.openssh() == out