commit 79e0200834ec28483b829275cfc53d7a7baae9c5 Author: Damian Johnson atagar@torproject.org Date: Sat Dec 28 11:11:05 2013 -0800
Unit tests for expand_events()
Oh, and fixes. Quite a few bugs for such a simple function. :P --- arm/arguments.py | 61 ++++++++++++++++++++++++------------------------ arm/config/strings.cfg | 2 +- test/arguments.py | 45 ++++++++++++++++++++++++++++++++--- 3 files changed, 74 insertions(+), 34 deletions(-)
diff --git a/arm/arguments.py b/arm/arguments.py index 17e2958..95069e1 100644 --- a/arm/arguments.py +++ b/arm/arguments.py @@ -9,7 +9,6 @@ import os import arm
import stem.connection -import stem.util.log
from arm.util import msg
@@ -26,25 +25,25 @@ DEFAULT_ARGS = { 'print_help': False, }
-OPT = "i:s:c:d:bl:vh" +OPT = 'i:s:c:d:bl:vh'
OPT_EXPANDED = [ - "interface=", - "socket=", - "config=", - "debug=", - "log=", - "version", - "help", + 'interface=', + 'socket=', + 'config=', + 'debug=', + 'log=', + 'version', + 'help', ]
TOR_EVENT_TYPES = { - "d": "DEBUG", "a": "ADDRMAP", "k": "DESCCHANGED", "s": "STREAM", - "i": "INFO", "f": "AUTHDIR_NEWDESCS", "g": "GUARD", "r": "STREAM_BW", - "n": "NOTICE", "h": "BUILDTIMEOUT_SET", "l": "NEWCONSENSUS", "t": "STATUS_CLIENT", - "w": "WARN", "b": "BW", "m": "NEWDESC", "u": "STATUS_GENERAL", - "e": "ERR", "c": "CIRC", "p": "NS", "v": "STATUS_SERVER", - "j": "CLIENTS_SEEN", "q": "ORCONN"} + 'd': 'DEBUG', 'a': 'ADDRMAP', 'k': 'DESCCHANGED', 's': 'STREAM', + 'i': 'INFO', 'f': 'AUTHDIR_NEWDESCS', 'g': 'GUARD', 'r': 'STREAM_BW', + 'n': 'NOTICE', 'h': 'BUILDTIMEOUT_SET', 'l': 'NEWCONSENSUS', 't': 'STATUS_CLIENT', + 'w': 'WARN', 'b': 'BW', 'm': 'NEWDESC', 'u': 'STATUS_GENERAL', + 'e': 'ERR', 'c': 'CIRC', 'p': 'NS', 'v': 'STATUS_SERVER', + 'j': 'CLIENTS_SEEN', 'q': 'ORCONN'}
def parse(argv): @@ -151,45 +150,47 @@ def expand_events(flags): ::
>>> expand_events('inUt') - ["INFO", "NOTICE", "UNKNOWN", "STREAM_BW"] + set(['INFO', 'NOTICE', 'UNKNOWN', 'STATUS_CLIENT'])
>>> expand_events('N4') - ["NOTICE", "WARN", "ERR", "ARM_WARN", "ARM_ERR"] + set(['NOTICE', 'WARN', 'ERR', 'ARM_WARN', 'ARM_ERR'])
>>> expand_events('cfX') - [] + set([])
:param str flags: character flags to be expanded
+ :returns: **set** of the expanded event types + :raises: **ValueError** with invalid input if any flags are unrecognized """
expanded_events, invalid_flags = set(), '' - arm_runlevels = ['ARM_' + runlevel for runlevel in stem.util.log.Runlevel] + + tor_runlevels = ['DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERR'] + arm_runlevels = ['ARM_' + runlevel for runlevel in tor_runlevels]
for flag in flags: if flag == 'A': - expanded_events = set(list(TOR_EVENT_TYPES) + arm_runlevels + ['UNKNOWN']) - break + return set(list(TOR_EVENT_TYPES) + arm_runlevels + ['UNKNOWN']) elif flag == 'X': - expanded_events = set() - break + return set() elif flag in 'DINWE12345': # all events for a runlevel and higher
if flag in 'D1': - runlevel_index = 1 + runlevel_index = 0 elif flag in 'I2': - runlevel_index = 2 + runlevel_index = 1 elif flag in 'N3': - runlevel_index = 3 + runlevel_index = 2 elif flag in 'W4': - runlevel_index = 4 + runlevel_index = 3 elif flag in 'E5': - runlevel_index = 5 + runlevel_index = 4
if flag in 'DINWE': - runlevels = list(stem.util.log.Runlevel)[runlevel_index:] + runlevels = tor_runlevels[runlevel_index:] elif flag in '12345': runlevels = arm_runlevels[runlevel_index:]
@@ -202,6 +203,6 @@ def expand_events(flags): invalid_flags += flag
if invalid_flags: - raise ValueError(invalid_flags) + raise ValueError(''.join(set(invalid_flags))) else: return expanded_events diff --git a/arm/config/strings.cfg b/arm/config/strings.cfg index cc204c1..85c23f4 100644 --- a/arm/config/strings.cfg +++ b/arm/config/strings.cfg @@ -117,7 +117,7 @@ msg.usage.help_output | |Example: |arm -i 1643 attach to control port 1643 -|arm -e we -c /tmp/cfg use this configuration file with 'WARN'/'ERR' events +|arm -l we -c /tmp/cfg use this configuration file with 'WARN'/'ERR' events
msg.usage.version_output |arm version {version} (released {date}) diff --git a/test/arguments.py b/test/arguments.py index a8fae78..f0fcdb9 100644 --- a/test/arguments.py +++ b/test/arguments.py @@ -1,7 +1,7 @@ import getopt import unittest
-from arm.arguments import parse, DEFAULT_ARGS +from arm.arguments import DEFAULT_ARGS, parse, expand_events
class TestArgumentParsing(unittest.TestCase): @@ -27,7 +27,7 @@ class TestArgumentParsing(unittest.TestCase): args = parse(['--debug', '/tmp/dump']) self.assertEqual('/tmp/dump', args.debug_path)
- args = parse(['--event', 'D1']) + args = parse(['--log', 'D1']) self.assertEqual('D1', args.logged_events)
args = parse(['--version']) @@ -40,7 +40,7 @@ class TestArgumentParsing(unittest.TestCase): args = parse(['-i', '1643']) self.assertEqual(1643, args.control_port)
- args = parse(['-e', 'we', '-c', '/tmp/cfg']) + args = parse(['-l', 'we', '-c', '/tmp/cfg']) self.assertEqual('we', args.logged_events) self.assertEqual('/tmp/cfg', args.config)
@@ -62,3 +62,42 @@ class TestArgumentParsing(unittest.TestCase):
for invalid_input in invalid_inputs: self.assertRaises(ValueError, parse, ['--interface', invalid_input]) + +class TestExpandEvents(unittest.TestCase): + def test_examples(self): + self.assertEqual(set(['INFO', 'NOTICE', 'UNKNOWN', 'STATUS_CLIENT']), expand_events('inUt')) + self.assertEqual(set(['NOTICE', 'WARN', 'ERR', 'ARM_WARN', 'ARM_ERR']), expand_events('N4')) + self.assertEqual(set(), expand_events('cfX')) + + def test_runlevel_expansion(self): + self.assertEqual(set(['DEBUG', 'INFO', 'NOTICE', 'WARN', 'ERR']), expand_events('D')) + self.assertEqual(set(['INFO', 'NOTICE', 'WARN', 'ERR']), expand_events('I')) + self.assertEqual(set(['NOTICE', 'WARN', 'ERR']), expand_events('N')) + self.assertEqual(set(['WARN', 'ERR']), expand_events('W')) + self.assertEqual(set(['ERR']), expand_events('E')) + + self.assertEqual(set(['ARM_DEBUG', 'ARM_INFO', 'ARM_NOTICE', 'ARM_WARN', 'ARM_ERR']), expand_events('1')) + self.assertEqual(set(['ARM_INFO', 'ARM_NOTICE', 'ARM_WARN', 'ARM_ERR']), expand_events('2')) + self.assertEqual(set(['ARM_NOTICE', 'ARM_WARN', 'ARM_ERR']), expand_events('3')) + self.assertEqual(set(['ARM_WARN', 'ARM_ERR']), expand_events('4')) + self.assertEqual(set(['ARM_ERR']), expand_events('5')) + + def test_short_circuit_options(self): + # Check that the 'A' and 'X' options short circuit normal parsing, + # 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'))) + + 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*') + + def _expect_invalid_flags(self, argument, expected): + try: + expand_events(argument) + self.fail() + except ValueError as exc: + self.assertEqual(set(expected), set(str(exc)))
tor-commits@lists.torproject.org