From bdb9ba61d9333706a038141ee790b136bf473541 Mon Sep 17 00:00:00 2001 From: Hanne Moa Date: Wed, 18 Oct 2023 13:37:28 +0200 Subject: [PATCH] Move slowest tests to separate file --- tests/test_default.py | 88 +------------------------------------------ tests/test_slow.py | 48 +++++++++++++++++++++++ tests/utils.py | 59 +++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 87 deletions(-) create mode 100644 tests/test_slow.py diff --git a/tests/test_default.py b/tests/test_default.py index 38453e3..3f1f8fe 100644 --- a/tests/test_default.py +++ b/tests/test_default.py @@ -8,68 +8,7 @@ from zinolib.ritz import ritz, ProtocolError, AuthenticationError, caseState, caseType from zinolib.zino_emu import zinoemu - -ritzlog = logging.getLogger("ritz") -ritzlog.setLevel(logging.DEBUG) -ritzlog.addHandler(logging.FileHandler("test1.log")) - - -def executor(client): - d = { - "user testuser 7f53cac4ffa877616b8472d3b33a44cbba1907ad -\r\n": ["200 ok\r\n"], - "user auth-failure 7f53cac4ffa877616b8472d3b33a44cbba1907ad -\r\n": [ - "500 Authentication failure\r\n" - ], - "user illegal-first-response 85970fa23a2f5aa06c22b60f04013fe072319ebd -\r\n": [ - "something-gurba-happened" - ], - "user no_login_response 87101b4944f4200fce90f519ddae5eacffeeadf6 -\r\n": [""], - "caseids\r\n": [ - "304 list of active cases follows, terminated with '.'\r\n", - "32802\r\n34978\r\n.\r\n", - ], - "getattrs 32802\r\n": [ - "303 simple attributes follow, terminated with '.'\r\n", - "state: working\r\nrouter: uninett-gsw2\r\ntype: bgp\r\nopened: 1524465142\r\nremote-addr: 2001:700:0:4515::5:11\r\nid: 32802\r\npeer-uptime: 0\r\nupdated: 1533116751\r\npolladdr: 127.0.0.1\r\npriority: 100\r\nbgpOS: down\r\nbgpAS: halted\r\nremote-AS: 64666\r\nlastevent: peer is admin turned off\r\n.\r\n", - ], - "getattrs 34978\r\n": [ - "303 simple attributes follow, terminated with '.'\r\n", - "router: bergen-sw1\r\nstate: working\r\ntype: alarm\r\nalarm-count: 1\r\nopened: 1529156235\r\nalarm-type: yellow\r\nid: 34978\r\nupdated: 1529156235\r\npolladdr: 127.0.0.1\r\npriority: 100\r\nlastevent: alarms went from 0 to 1\r\n.\r\n", - ], - "getattrs 40959\r\n": [ - "303 simple attributes follow, terminated with '.'\r\n", - "state: open\r\nrouter: oslo-gw1\r\ntype: bgp\r\nopened: 1539480952\r\nremote-addr: 193.108.152.34\r\nid: 40959\r\npeer-uptime: 503\r\nupdated: 1539485757\r\npolladdr: 128.39.0.1\r\npriority: 500\r\nbgpOS: established\r\nremote-AS: 21357\r\nbgpAS: running\r\nlastevent: peer was reset (now up)\r\n.\r\n", - ], - "gethist 40959\r\n": [ - "301 history follows, terminated with '.'\r\n", - "1539480952 state change embryonic -> open (monitor)\r\n1539509123 runarb\r\n Testmelding ifra pyRitz\r\n \r\n.\r\n", - ], - "getlog 40959\r\n": [ - "300 log follows, terminated with '.'\r\n", - "1539480952 oslo-gw1 peer 193.108.152.34 AS 21357 was reset (now up)\r\n1539484557 oslo-gw1 peer 193.108.152.34 AS 21357 was reset (now up)\r\n1539485757 oslo-gw1 peer 193.108.152.34 AS 21357 was reset (now up)\r\n.\r\n", - ], - "addhist 40959 -\r\n": [ - "302 please provide new history entry, termiate with '.'\r\n" - ], - "Testmelding ifra pyRitz\r\n\r\n.\r\n": ["200 ok\r\n"], - "setstate 40959 open\r\n": ["200 ok\r\n"], - "setstate 40959 working\r\n": ["200 ok\r\n"], - "setstate 40959 waiting\r\n": ["200 ok\r\n"], - "setstate 40959 confirm-wait\r\n": ["200 ok\r\n"], - "setstate 40959 ignored\r\n": ["200 ok\r\n"], - "setstate 40959 closed\r\n": ["200 ok\r\n"], - "setstate 40960 open\r\n": ["500 Cannot reopen closed event 40960\r\n"], - "clearflap uninett-tor-sw3 707\r\n": ["200 ok\r\n"], - "pollrtr uninett-tor-sw3": ["200 ok\r\n"], - "pollintf uninett-tor-sw3 707": ["200 ok\r\n"], - "ntie 909e90c2eda89a09819ee7fe9b3f67cadb31449f\r\n": ["200 ok\r\n"], - "ntie xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n": [ - "500 Could not find your notify socket\r\n" - ], - } - - client.send("200 2f88fe9d496b1c1a33a8d69f5c3ff7e8c34a1069 Hello, there\r\n") - client.executor(d) +from .utils import executor class DefaultTest(unittest.TestCase): @@ -107,20 +46,6 @@ def client(client): r.connect() r.close() - def test_C_connect_no_login_response(self): - with zinoemu(executor): - - r = ritz( - "127.0.0.1", username="no_login_response", password="no_login_response" - ) - # with self.assertRaises(ProtocolError): - try: - r.connect() - except ProtocolError: - pass - finally: - r.close() - def test_D_connect_authentication_failed(self): with zinoemu(executor): r = ritz("127.0.0.1", username="auth-failure", password="test") @@ -128,17 +53,6 @@ def test_D_connect_authentication_failed(self): r.connect() r.close() - def test_E_connect_random_data_on_connect(self): - with zinoemu(executor): - r = ritz( - "127.0.0.1", - username="illegal-first-response", - password="illegal-first-response", - ) - with self.assertRaises(ProtocolError): - r.connect() - r.close() - def test_F_with(self): with zinoemu(executor): with ritz("127.0.0.1", username="testuser", password="test") as sess: diff --git a/tests/test_slow.py b/tests/test_slow.py new file mode 100644 index 0000000..3d6d5fc --- /dev/null +++ b/tests/test_slow.py @@ -0,0 +1,48 @@ +import logging +import datetime +import os +import time +import unittest +from ipaddress import ip_address + +from zinolib.ritz import ritz, ProtocolError, AuthenticationError, caseState, caseType +from zinolib.zino_emu import zinoemu + +from .utils import executor + + +class SlowTest(unittest.TestCase): + def setUp(self): + self.old_timezone = os.environ.get('TZ', None) + os.environ['TZ'] = 'Europe/Oslo' + time.tzset() + + def tearDown(self): + if self.old_timezone: + os.environ['TZ'] = self.old_timezone + time.tzset() + + def test_C_connect_no_login_response(self): + with zinoemu(executor): + + r = ritz( + "127.0.0.1", username="no_login_response", password="no_login_response" + ) + # with self.assertRaises(ProtocolError): + try: + r.connect() + except ProtocolError: + pass + finally: + r.close() + + def test_E_connect_random_data_on_connect(self): + with zinoemu(executor): + r = ritz( + "127.0.0.1", + username="illegal-first-response", + password="illegal-first-response", + ) + with self.assertRaises(ProtocolError): + r.connect() + r.close() diff --git a/tests/utils.py b/tests/utils.py index 3b4b22d..6488520 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -7,6 +7,7 @@ 'clean_textfile', 'make_tmptextfile', 'delete_tmpfile', + 'executor', ] @@ -24,3 +25,61 @@ def make_tmptextfile(text, suffix, prefix=None, encoding='ascii'): def delete_tmpfile(filename): Path(filename).unlink(missing_ok=True) + + +def executor(client): + d = { + "user testuser 7f53cac4ffa877616b8472d3b33a44cbba1907ad -\r\n": ["200 ok\r\n"], + "user auth-failure 7f53cac4ffa877616b8472d3b33a44cbba1907ad -\r\n": [ + "500 Authentication failure\r\n" + ], + "user illegal-first-response 85970fa23a2f5aa06c22b60f04013fe072319ebd -\r\n": [ + "something-gurba-happened" + ], + "user no_login_response 87101b4944f4200fce90f519ddae5eacffeeadf6 -\r\n": [""], + "caseids\r\n": [ + "304 list of active cases follows, terminated with '.'\r\n", + "32802\r\n34978\r\n.\r\n", + ], + "getattrs 32802\r\n": [ + "303 simple attributes follow, terminated with '.'\r\n", + "state: working\r\nrouter: uninett-gsw2\r\ntype: bgp\r\nopened: 1524465142\r\nremote-addr: 2001:700:0:4515::5:11\r\nid: 32802\r\npeer-uptime: 0\r\nupdated: 1533116751\r\npolladdr: 127.0.0.1\r\npriority: 100\r\nbgpOS: down\r\nbgpAS: halted\r\nremote-AS: 64666\r\nlastevent: peer is admin turned off\r\n.\r\n", + ], + "getattrs 34978\r\n": [ + "303 simple attributes follow, terminated with '.'\r\n", + "router: bergen-sw1\r\nstate: working\r\ntype: alarm\r\nalarm-count: 1\r\nopened: 1529156235\r\nalarm-type: yellow\r\nid: 34978\r\nupdated: 1529156235\r\npolladdr: 127.0.0.1\r\npriority: 100\r\nlastevent: alarms went from 0 to 1\r\n.\r\n", + ], + "getattrs 40959\r\n": [ + "303 simple attributes follow, terminated with '.'\r\n", + "state: open\r\nrouter: oslo-gw1\r\ntype: bgp\r\nopened: 1539480952\r\nremote-addr: 193.108.152.34\r\nid: 40959\r\npeer-uptime: 503\r\nupdated: 1539485757\r\npolladdr: 128.39.0.1\r\npriority: 500\r\nbgpOS: established\r\nremote-AS: 21357\r\nbgpAS: running\r\nlastevent: peer was reset (now up)\r\n.\r\n", + ], + "gethist 40959\r\n": [ + "301 history follows, terminated with '.'\r\n", + "1539480952 state change embryonic -> open (monitor)\r\n1539509123 runarb\r\n Testmelding ifra pyRitz\r\n \r\n.\r\n", + ], + "getlog 40959\r\n": [ + "300 log follows, terminated with '.'\r\n", + "1539480952 oslo-gw1 peer 193.108.152.34 AS 21357 was reset (now up)\r\n1539484557 oslo-gw1 peer 193.108.152.34 AS 21357 was reset (now up)\r\n1539485757 oslo-gw1 peer 193.108.152.34 AS 21357 was reset (now up)\r\n.\r\n", + ], + "addhist 40959 -\r\n": [ + "302 please provide new history entry, termiate with '.'\r\n" + ], + "Testmelding ifra pyRitz\r\n\r\n.\r\n": ["200 ok\r\n"], + "setstate 40959 open\r\n": ["200 ok\r\n"], + "setstate 40959 working\r\n": ["200 ok\r\n"], + "setstate 40959 waiting\r\n": ["200 ok\r\n"], + "setstate 40959 confirm-wait\r\n": ["200 ok\r\n"], + "setstate 40959 ignored\r\n": ["200 ok\r\n"], + "setstate 40959 closed\r\n": ["200 ok\r\n"], + "setstate 40960 open\r\n": ["500 Cannot reopen closed event 40960\r\n"], + "clearflap uninett-tor-sw3 707\r\n": ["200 ok\r\n"], + "pollrtr uninett-tor-sw3": ["200 ok\r\n"], + "pollintf uninett-tor-sw3 707": ["200 ok\r\n"], + "ntie 909e90c2eda89a09819ee7fe9b3f67cadb31449f\r\n": ["200 ok\r\n"], + "ntie xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n": [ + "500 Could not find your notify socket\r\n" + ], + } + + client.send("200 2f88fe9d496b1c1a33a8d69f5c3ff7e8c34a1069 Hello, there\r\n") + client.executor(d)