[tor-commits] [nyx/master] Fix unit tests
commit 3ea784a3af26e6772e8ab3531e7185895567a6c7 Author: Damian Johnson Date: Wed Oct 28 15:02:01 2020 -0700 Fix unit tests I don't run Nyx's unit tests very often so a few regressions slipped in. Some due to Nyx adjustments, some from Stem. Stem renamed its is_pep8_available() function to is_pycodestyle_available() because the upstream project renamed itself... Traceback (most recent call last): File "./run_tests.py", line 80, in main() File "/home/atagar/Desktop/nyx/stem/util/conf.py", line 289, in wrapped return func(*args, **kwargs) File "./run_tests.py", line 46, in main if stem.util.test_tools.is_pep8_available(): AttributeError: module 'stem.util.test_tools' has no attribute 'is_pep8_available' --- nyx/__init__.py | 1 + nyx/curses.py| 2 +- nyx/panel/log.py | 2 +- run_tests.py | 2 +- setup.py | 2 +- test/panel/config.py | 2 +- test/panel/connection.py | 12 test/panel/header.py | 2 +- 8 files changed, 15 insertions(+), 10 deletions(-) diff --git a/nyx/__init__.py b/nyx/__init__.py index 67053aa..9fbf2c0 100644 --- a/nyx/__init__.py +++ b/nyx/__init__.py @@ -138,6 +138,7 @@ def conf_handler(key, value): if key == 'redraw_rate': return max(1, value) + CONFIG = stem.util.conf.config_dict('nyx', { 'confirm_quit': True, 'redraw_rate': 5, diff --git a/nyx/curses.py b/nyx/curses.py index 3c3fa69..5ff5cfb 100644 --- a/nyx/curses.py +++ b/nyx/curses.py @@ -115,7 +115,7 @@ RED, GREEN, YELLOW, BLUE, CYAN, MAGENTA, BLACK, WHITE = list(Color) Attr = stem.util.enum.Enum('NORMAL', 'BOLD', 'UNDERLINE', 'HIGHLIGHT') NORMAL, BOLD, UNDERLINE, HIGHLIGHT = list(Attr) -ANSI_RE = re.compile('\x1B\[([0-9;]+)m') +ANSI_RE = re.compile('\x1B\\[([0-9;]+)m') CURSES_COLORS = { Color.RED: curses.COLOR_RED, diff --git a/nyx/panel/log.py b/nyx/panel/log.py index bbb1961..c6587a1 100644 --- a/nyx/panel/log.py +++ b/nyx/panel/log.py @@ -196,7 +196,7 @@ class LogPanel(nyx.panel.DaemonPanel): try: if not os.path.exists(base_dir): os.makedirs(base_dir) -except OSError as exc: +except OSError: raise IOError("unable to make directory '%s'" % base_dir) event_log = list(self._event_log) diff --git a/run_tests.py b/run_tests.py index 4c7f73d..3cab8a6 100755 --- a/run_tests.py +++ b/run_tests.py @@ -43,7 +43,7 @@ def main(): if stem.util.test_tools.is_pyflakes_available(): pyflakes_task = stem.util.system.DaemonTask(stem.util.test_tools.pyflakes_issues, (SRC_PATHS,), start = True) - if stem.util.test_tools.is_pep8_available(): + if stem.util.test_tools.is_pycodestyle_available(): pycodestyle_task = stem.util.system.DaemonTask(stem.util.test_tools.stylistic_issues, (SRC_PATHS,), start = True) tests = unittest.defaultTestLoader.discover('test', pattern = '*.py') diff --git a/setup.py b/setup.py index 7e9b996..a4cd132 100644 --- a/setup.py +++ b/setup.py @@ -51,7 +51,7 @@ global-exclude *~ # installation. As such, just reading our file for the parameters we need. ATTR = {} -ATTR_LINE = re.compile("^__(\S+)__ = '(.+)'") +ATTR_LINE = re.compile("^__(\\S+)__ = '(.+)'") with open('nyx/__init__.py') as init_file: for line in init_file: diff --git a/test/panel/config.py b/test/panel/config.py index 0bf51ab..7b138d0 100644 --- a/test/panel/config.py +++ b/test/panel/config.py @@ -20,7 +20,7 @@ EXPECTED_LINE = 'ControlPort 9051 Port providing access to t EXPECTED_DETAIL_DIALOG = """ +--+ | ControlPort (General Option) | -| Value: 9051 (custom, LineList, usage: PORT|unix:path|auto [flags]) | +| Value: 9051 (custom, LineList, usage: [address:]port|unix:path|auto [flags]) | | Description: If set, Tor will accept connections on this port and allow those| | connections to control the Tor process using the Tor Control Protocol (des-| | cribed in control-spec.txt in torspec). Note: unless you also specify one | diff --git a/test/panel/connection.py b/test/panel/connection.py index b432df5..56a7a66 100644 --- a/test/panel/connection.py +++ b/test/panel/connection.py @@ -201,8 +201,10 @@ class TestConnectionPanel(unittest.TestCase): @require_curses @patch('nyx.panel.connection.tor_controller') def test_draw_line(self, tor_controller_mock): -tor_controller_mock().is_geoip_unavailable.return_value = False -tor_controller_mock().get_info.return_value = '82.121.9.9' +tor_controller_mock().get_info.side_effect = lambda param, default = None: { + 'ip-to-country/ipv4-available': '1', + 'address': '82.121.9.9', +}[param] test_data = (( line(), @@ -228,8 +230,10 @@ class TestConnectionPanel(unittest.TestCase): @require_curses
[tor-commits] [nyx/master] Fix unit tests
commit 314dd59d1d1bc1fdc6aa88763b4c16e5e4284aeb Author: Damian JohnsonDate: Wed Apr 18 11:39:44 2018 -0700 Fix unit tests Just a couple minor regressions from prior changes. --- nyx/panel/graph.py | 2 -- test/arguments.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/nyx/panel/graph.py b/nyx/panel/graph.py index a33aaa4..79b50fa 100644 --- a/nyx/panel/graph.py +++ b/nyx/panel/graph.py @@ -108,8 +108,6 @@ def _bandwidth_title_stats(): stats.append('limit: %s/s' % bw_rate_label) stats.append('burst: %s/s' % bw_burst_label) - my_router_status_entry = nyx.tracker.get_consensus_tracker().my_router_status_entry() - my_server_descriptor = controller.get_server_descriptor(default = None) observed_bw = getattr(my_server_descriptor, 'observed_bandwidth', None) diff --git a/test/arguments.py b/test/arguments.py index da55127..5223af5 100644 --- a/test/arguments.py +++ b/test/arguments.py @@ -83,7 +83,7 @@ class TestArgumentParsing(unittest.TestCase): def test_help(self): self.assertTrue(get_help().startswith('Usage nyx [OPTION]')) -self.assertTrue('change control interface from 127.0.0.1:9051' in get_help()) +self.assertTrue('change control interface from 127.0.0.1:default' in get_help()) def test_version(self): self.assertTrue(get_version().startswith('nyx version')) ___ tor-commits mailing list tor-commits@lists.torproject.org https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits
[tor-commits] [nyx/master] Fix unit tests
commit c4a7093cb3c0e96e5c6ae99e028a68305954e091 Author: Damian JohnsonDate: Sat Nov 4 15:40:42 2017 -0700 Fix unit tests Couple recent changes need to be accounted for. --- test/panel/connection.py | 6 +++--- test/tracker/connection_tracker.py | 11 +++ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/test/panel/connection.py b/test/panel/connection.py index ccc3aa5..b432df5 100644 --- a/test/panel/connection.py +++ b/test/panel/connection.py @@ -206,7 +206,7 @@ class TestConnectionPanel(unittest.TestCase): test_data = (( line(), - ' 75.119.206.243:22 (de) --> 82.121.9.9:3531 15.4s (INBOUND)', + ' 75.119.206.243:22 (de) --> 82.121.9.9:3531 15.4s (INBOUND)', ), ( line(entry = MockEntry(entry_type = Category.CIRCUIT), line_type = LineType.CIRCUIT_HEADER), ' 82.121.9.9 --> 75.119.206.243:22 (de)15.4s (CIRCUIT)', @@ -233,13 +233,13 @@ class TestConnectionPanel(unittest.TestCase): test_data = (( line(), - '75.119.206.243:22 (de) --> 82.121.9.9:3531', + '75.119.206.243:22 (de) --> 82.121.9.9:3531', ), ( line(entry = MockEntry(entry_type = Category.EXIT)), '82.121.9.9:3531--> 75.119.206.243:22 (SSH)', ), ( line(line_type = LineType.CIRCUIT_HEADER, circ = MockCircuit(status = 'EXTENDING')), - 'Building...--> 82.121.9.9', + 'Building... --> 82.121.9.9', ), ( line(line_type = LineType.CIRCUIT), '82.121.9.9', diff --git a/test/tracker/connection_tracker.py b/test/tracker/connection_tracker.py index 20f0095..993d460 100644 --- a/test/tracker/connection_tracker.py +++ b/test/tracker/connection_tracker.py @@ -1,7 +1,7 @@ import time import unittest -from nyx.tracker import CustomResolver, ConnectionTracker +from nyx.tracker import ConnectionTracker from stem.util import connection @@ -22,6 +22,7 @@ class TestConnectionTracker(unittest.TestCase): @patch('nyx.tracker.tor_controller') @patch('nyx.tracker.connection.get_connections') @patch('nyx.tracker.system', Mock(return_value = Mock())) + @patch('stem.util.proc.is_available', Mock(return_value = False)) @patch('nyx.tracker.connection.system_resolvers', Mock(return_value = [connection.Resolver.NETSTAT])) def test_fetching_connections(self, get_value_mock, tor_controller_mock): tor_controller_mock().get_pid.return_value = 12345 @@ -46,6 +47,7 @@ class TestConnectionTracker(unittest.TestCase): @patch('nyx.tracker.tor_controller') @patch('nyx.tracker.connection.get_connections') @patch('nyx.tracker.system', Mock(return_value = Mock())) + @patch('stem.util.proc.is_available', Mock(return_value = False)) @patch('nyx.tracker.connection.system_resolvers', Mock(return_value = [connection.Resolver.NETSTAT, connection.Resolver.LSOF])) def test_resolver_failover(self, get_value_mock, tor_controller_mock): tor_controller_mock().get_pid.return_value = 12345 @@ -55,17 +57,17 @@ class TestConnectionTracker(unittest.TestCase): with ConnectionTracker(0.01) as daemon: time.sleep(0.015) - self.assertEqual([connection.Resolver.NETSTAT, connection.Resolver.LSOF, CustomResolver.INFERENCE], daemon._resolvers) + self.assertEqual([connection.Resolver.NETSTAT, connection.Resolver.LSOF], daemon._resolvers) self.assertEqual([], daemon.get_value()) time.sleep(0.025) - self.assertEqual([connection.Resolver.LSOF, CustomResolver.INFERENCE], daemon._resolvers) + self.assertEqual([connection.Resolver.LSOF], daemon._resolvers) self.assertEqual([], daemon.get_value()) time.sleep(0.035) - self.assertEqual([CustomResolver.INFERENCE], daemon._resolvers) + self.assertEqual([], daemon._resolvers) self.assertEqual([], daemon.get_value()) # Now make connection resolution work. We still shouldn't provide any @@ -86,6 +88,7 @@ class TestConnectionTracker(unittest.TestCase): @patch('nyx.tracker.tor_controller') @patch('nyx.tracker.connection.get_connections') @patch('nyx.tracker.system', Mock(return_value = Mock())) + @patch('stem.util.proc.is_available', Mock(return_value = False)) @patch('nyx.tracker.connection.system_resolvers', Mock(return_value = [connection.Resolver.NETSTAT])) def test_tracking_uptime(self, get_value_mock, tor_controller_mock): tor_controller_mock().get_pid.return_value = 12345 ___ tor-commits mailing list tor-commits@lists.torproject.org https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits
[tor-commits] [nyx/master] Fix unit tests
commit c6c16ebc42908f56d064711492da6787c946c493 Author: Damian JohnsonDate: Tue Oct 24 10:17:43 2017 -0700 Fix unit tests Lots of breakages have slipped in while attempting to improve our performance. Most were timing related due to our switch to static sleeps. --- nyx/__init__.py| 1 + nyx/panel/__init__.py | 5 ++--- nyx/panel/connection.py| 10 +++--- nyx/panel/header.py| 4 ++-- nyx/tracker.py | 11 ++- run_tests.py | 2 +- test/panel/connection.py | 4 ++-- test/panel/header.py | 2 +- test/tracker/connection_tracker.py | 8 test/tracker/port_usage_tracker.py | 2 +- test/tracker/resource_tracker.py | 2 +- 11 files changed, 28 insertions(+), 23 deletions(-) diff --git a/nyx/__init__.py b/nyx/__init__.py index d9a9b52..5947294 100644 --- a/nyx/__init__.py +++ b/nyx/__init__.py @@ -268,6 +268,7 @@ def our_address(default = None): return CACHED_ADDRESS if CACHED_ADDRESS is not None else default + def show_message(message = None, *attr, **kwargs): """ Shows a message in our header. diff --git a/nyx/panel/__init__.py b/nyx/panel/__init__.py index ed32887..b75964c 100644 --- a/nyx/panel/__init__.py +++ b/nyx/panel/__init__.py @@ -31,10 +31,9 @@ import inspect import threading import time +import nyx import nyx.curses -from nyx import PAUSE_TIME, nyx_interface - __all__ = [ 'config', 'connection', @@ -207,7 +206,7 @@ class DaemonPanel(Panel, threading.Thread): sleep_until = last_ran + self._update_rate + 0.1 while not self._halt and time.time() < sleep_until: - time.sleep(PAUSE_TIME) + time.sleep(nyx.PAUSE_TIME) continue # done waiting, try again diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py index 3fe283b..3002d67 100644 --- a/nyx/panel/connection.py +++ b/nyx/panel/connection.py @@ -11,12 +11,13 @@ import itertools import re import time +import nyx import nyx.curses import nyx.panel import nyx.popups import nyx.tracker -from nyx import PAUSE_TIME, nyx_interface, tor_controller, our_address +from nyx import nyx_interface, tor_controller from nyx.curses import WHITE, NORMAL, BOLD, HIGHLIGHT from nyx.menu import MenuItem, Submenu, RadioMenuItem, RadioGroup @@ -505,7 +506,7 @@ class ConnectionPanel(nyx.panel.DaemonPanel): elif self._halt: return else: - time.sleep(PAUSE_TIME) + time.sleep(nyx.PAUSE_TIME) controller = tor_controller() LAST_RETRIEVED_CIRCUITS = controller.get_circuits([]) @@ -607,7 +608,10 @@ def _draw_line(subwindow, x, y, line, is_selected, width, current_time): def _draw_address_column(subwindow, x, y, line, attr): - src = '%s:%s' % (our_address(line.connection.local_address), line.connection.local_port if line.line_type == LineType.CONNECTION else '') + if line.line_type == LineType.CONNECTION: +src = '%s:%s' % (nyx.our_address(line.connection.local_address), line.connection.local_port) + else: +src = nyx.our_address(line.connection.local_address) if line.line_type == LineType.CIRCUIT_HEADER and line.circuit.status != 'BUILT': dst = 'Building...' diff --git a/nyx/panel/header.py b/nyx/panel/header.py index 3751d56..0820a21 100644 --- a/nyx/panel/header.py +++ b/nyx/panel/header.py @@ -23,7 +23,7 @@ import nyx.popups import nyx.tracker from stem.util import conf, log -from nyx import nyx_interface, tor_controller, our_address +from nyx import nyx_interface, tor_controller from nyx.curses import RED, GREEN, YELLOW, CYAN, WHITE, BOLD, HIGHLIGHT @@ -268,7 +268,7 @@ class Sampling(object): 'version': str(controller.get_version('Unknown')).split()[0], 'version_status': controller.get_info('status/version/current', 'Unknown'), - 'address': or_listeners[0][0] if (or_listeners and or_listeners[0][0] != '0.0.0.0') else our_address('Unknown'), + 'address': or_listeners[0][0] if (or_listeners and or_listeners[0][0] != '0.0.0.0') else nyx.our_address('Unknown'), 'or_port': or_listeners[0][1] if or_listeners else '', 'dir_port': controller.get_conf('DirPort', '0'), 'control_port': str(control_listeners[0][1]) if control_listeners else None, diff --git a/nyx/tracker.py b/nyx/tracker.py index 6d6ab64..fb41f9e 100644 --- a/nyx/tracker.py +++ b/nyx/tracker.py @@ -60,7 +60,7 @@ import stem.control import stem.descriptor.router_status_entry import stem.util.log -from nyx import PAUSE_TIME, tor_controller, our_address +from nyx import tor_controller from stem.util import conf, connection, enum, proc, str_tools, system CONFIG = conf.config_dict('nyx', { @@ -387,10 +387,10 @@ class Daemon(threading.Thread): def run(self): while not self._halt: if self._is_paused or time.time() - self._last_ran < self._rate: -sleep_until =
[tor-commits] [nyx/master] Fix unit tests
commit f269fc5846abc952a8d12a135ab94130814b6da9 Author: Damian JohnsonDate: Sat Feb 6 10:15:51 2016 -0800 Fix unit tests Quite a few broken assertions due to recent changes. --- nyx/controller.py | 2 +- nyx/util/tor_config.py | 15 --- setup.py| 2 +- test/arguments.py | 8 test/util/tracker/connection_tracker.py | 9 ++--- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/nyx/controller.py b/nyx/controller.py index b520aa7..aa6da8c 100644 --- a/nyx/controller.py +++ b/nyx/controller.py @@ -25,7 +25,7 @@ from stem.control import State from nyx.util import panel, tor_controller, ui_tools -from stem.util import conf, log, system +from stem.util import conf, log NYX_CONTROLLER = None diff --git a/nyx/util/tor_config.py b/nyx/util/tor_config.py index 22abff7..3ffb435 100644 --- a/nyx/util/tor_config.py +++ b/nyx/util/tor_config.py @@ -50,24 +50,9 @@ TIME_MULT = {'sec': 1, 'min': 60, 'hour': 3600, 'day': 86400, 'week': 604800} ValidationError = enum.Enum('DUPLICATE', 'MISMATCH', 'MISSING', 'IS_DEFAULT') -TORRC = None # singleton torrc instance MULTILINE_PARAM = None # cached multiline parameters (lazily loaded) -def get_torrc(): - """ - Singleton constructor for a Controller. Be aware that this starts as being - unloaded, needing the torrc contents to be loaded before being functional. - """ - - global TORRC - - if TORRC is None: -TORRC = Torrc() - - return TORRC - - def get_config_location(): """ Provides the location of the torrc, raising an IOError with the reason if the diff --git a/setup.py b/setup.py index f5b1c05..13ae6af 100644 --- a/setup.py +++ b/setup.py @@ -102,6 +102,6 @@ setup( packages = ['nyx', 'nyx.menu', 'nyx.util'], keywords = 'tor onion controller', install_requires = ['stem>=1.4.1'], - package_data = {'nyx': ['config/*', 'resources/*']}, + package_data = {'nyx': ['settings/*', 'resources/*']}, cmdclass = {'install': NyxInstaller}, ) diff --git a/test/arguments.py b/test/arguments.py index 2336fdc..d27281c 100644 --- a/test/arguments.py +++ b/test/arguments.py @@ -67,7 +67,7 @@ class TestArgumentParsing(unittest.TestCase): class TestExpandEvents(unittest.TestCase): def test_examples(self): -self.assertEqual(set(['INFO', 'NOTICE', 'UNKNOWN', 'STATUS_CLIENT']), expand_events('inUt')) +self.assertEqual(set(['INFO', 'NOTICE', 'UNKNOWN', 'TRANSPORT_LAUNCHED']), expand_events('inUt')) self.assertEqual(set(['NOTICE', 'WARN', 'ERR', 'NYX_WARN', 'NYX_ERR']), expand_events('N4')) self.assertEqual(set(), expand_events('cfX')) @@ -89,13 +89,13 @@ class TestExpandEvents(unittest.TestCase): # providing results even if there's other invalid options. self.assertEqual(set(), expand_events('z*X*z')) -self.assertEqual(28, len(expand_events('z*A*z'))) +self.assertEqual(39, len(expand_events('z*A*z'))) def test_invalid_flags(self): self._expect_invalid_flags('D1*', '*') self._expect_invalid_flags('*D1', '*') -self._expect_invalid_flags('zzD1zz', 'z') -self._expect_invalid_flags('z*D1*z', 'z*') +self._expect_invalid_flags('zzD1Zz', 'Z') +self._expect_invalid_flags('Z*D1*z', 'Z*') def _expect_invalid_flags(self, argument, expected): try: diff --git a/test/util/tracker/connection_tracker.py b/test/util/tracker/connection_tracker.py index 0f3c93f..797c76b 100644 --- a/test/util/tracker/connection_tracker.py +++ b/test/util/tracker/connection_tracker.py @@ -7,9 +7,9 @@ from stem.util import connection from mock import Mock, patch -STEM_CONNECTION_1 = connection.Connection('127.0.0.1', 3531, '75.119.206.243', 22, 'tcp') -STEM_CONNECTION_2 = connection.Connection('127.0.0.1', 1766, '86.59.30.40', 443, 'tcp') -STEM_CONNECTION_3 = connection.Connection('127.0.0.1', 1059, '74.125.28.106', 80, 'tcp') +STEM_CONNECTION_1 = connection.Connection('127.0.0.1', 3531, '75.119.206.243', 22, 'tcp', False) +STEM_CONNECTION_2 = connection.Connection('127.0.0.1', 1766, '86.59.30.40', 443, 'tcp', False) +STEM_CONNECTION_3 = connection.Connection('127.0.0.1', 1059, '74.125.28.106', 80, 'tcp', False) class TestConnectionTracker(unittest.TestCase): @@ -19,6 +19,7 @@ class TestConnectionTracker(unittest.TestCase): @patch('nyx.util.tracker.connection.system_resolvers', Mock(return_value = [connection.Resolver.NETSTAT])) def test_fetching_connections(self, get_value_mock, tor_controller_mock): tor_controller_mock().get_pid.return_value = 12345 +tor_controller_mock().get_conf.return_value = '0' get_value_mock.return_value = [STEM_CONNECTION_1, STEM_CONNECTION_2, STEM_CONNECTION_3] with ConnectionTracker(0.04) as daemon: @@ -42,6 +43,7 @@ class TestConnectionTracker(unittest.TestCase): @patch('nyx.util.tracker.connection.system_resolvers', Mock(return_value =