Permalink
Browse files

Merge branch 'feature/basecommand-tests' into develop

  • Loading branch information...
2 parents 7b0ca2d + 9df3b92 commit c0cc49cadb9079af37d324c0e594fefbe0569de9 @ralphbean committed Dec 3, 2012
Showing with 187 additions and 6 deletions.
  1. +1 −3 fedmsg/commands/__init__.py
  2. +2 −1 fedmsg/commands/logger.py
  3. +6 −2 fedmsg/config.py
  4. +176 −0 fedmsg/tests/test_commands.py
  5. +2 −0 setup.py
View
4 fedmsg/commands/__init__.py
@@ -50,9 +50,7 @@ def __init__(self):
#formatter = logging.Formatter("%(asctime)s - %(name)s - %(lineno)s - \
#%(levelname)s - %(message)s")
formatter = logging.Formatter("%(message)s")
- console_log = logging.StreamHandler(
- stream = sys.stdout
- )
+ console_log = logging.StreamHandler(sys.stdout)
console_log.setLevel(logging.DEBUG)
console_log.setFormatter(formatter)
View
3 fedmsg/commands/logger.py
@@ -99,7 +99,8 @@ def __init__(self):
def run(self):
self.config['active'] = True
- fedmsg.init(name='relay_inbound', **self.config)
+ self.config['name'] = 'relay_inbound'
+ fedmsg.init(**self.config)
if self.config.get('logger_message'):
self._log_message(self.config, self.config.get('logger_message'))
View
8 fedmsg/config.py
@@ -66,8 +66,8 @@
__cache = {}
-def load_config(extra_args,
- doc,
+def load_config(extra_args=None,
+ doc=None,
filenames=None,
invalidate_cache=False,
fedmsg_command=False):
@@ -90,6 +90,10 @@ def load_config(extra_args,
if __cache:
return __cache
+ # Coerce defaults if arguments are not supplied.
+ extra_args = extra_args or []
+ doc = doc or ""
+
config = copy.deepcopy(defaults)
config.update(_process_config_file(filenames=filenames))
View
176 fedmsg/tests/test_commands.py
@@ -0,0 +1,176 @@
+import unittest
+from mock import Mock
+from mock import patch
+from datetime import datetime
+import time
+import json
+import os
+
+import fedmsg
+import fedmsg.core
+import fedmsg.config
+import fedmsg.commands
+
+from fedmsg.commands.logger import LoggerCommand
+from fedmsg.commands.tail import TailCommand
+from fedmsg.commands.relay import RelayCommand
+import fedmsg.consumers.relay
+
+from nose.tools import eq_
+
+import threading
+
+import six
+
+
+class TestCommands(unittest.TestCase):
+ def setUp(self):
+ self.local = threading.local()
+
+ def tearDown(self):
+ del self.local
+ self.local = None
+
+ @patch("sys.argv", new_callable=lambda: ["fedmsg-logger"])
+ @patch("sys.stdout", new_callable=six.StringIO)
+ def test_logger_basic(self, stdout, argv):
+
+ test_input = "a message for you"
+
+ if six.PY3:
+ stdin = lambda: six.StringIO(test_input)
+ else:
+ stdin = lambda: six.StringIO(test_input.encode('utf-8'))
+
+ msgs = []
+
+ def mock_publish(context, topic=None, msg=None, modname=None):
+ msgs.append(msg)
+
+ config = {}
+ with patch("fedmsg.__local", self.local):
+ with patch("fedmsg.config.__cache", config):
+ with patch("fedmsg.core.FedMsgContext.publish", mock_publish):
+ with patch("sys.stdin", new_callable=stdin):
+ command = LoggerCommand()
+ command.execute()
+
+ eq_(msgs, [{'log': test_input}])
+
+ @patch("sys.argv", new_callable=lambda: ["fedmsg-logger", "--json-input"])
+ @patch("sys.stdout", new_callable=six.StringIO)
+ def test_logger_json(self, stdout, argv):
+
+ test_input_dict = {"hello": "world"}
+ test_input = json.dumps(test_input_dict)
+
+ if six.PY3:
+ stdin = lambda: six.StringIO(test_input)
+ else:
+ stdin = lambda: six.StringIO(test_input.encode('utf-8'))
+
+ msgs = []
+
+ def mock_publish(context, topic=None, msg=None, modname=None):
+ msgs.append(msg)
+
+ config = {}
+ with patch("fedmsg.__local", self.local):
+ with patch("fedmsg.config.__cache", config):
+ with patch("fedmsg.core.FedMsgContext.publish", mock_publish):
+ with patch("sys.stdin", new_callable=stdin):
+ command = LoggerCommand()
+ command.execute()
+
+ eq_(msgs, [test_input_dict])
+
+ @patch("sys.argv", new_callable=lambda: ["fedmsg-tail"])
+ @patch("sys.stdout", new_callable=six.StringIO)
+ def test_tail_basic(self, stdout, argv):
+ def mock_tail(self, topic="", passive=False, **kw):
+ yield ("name", "endpoint", "topic", "message")
+
+ config = {}
+ with patch("fedmsg.__local", self.local):
+ with patch("fedmsg.config.__cache", config):
+ with patch("fedmsg.core.FedMsgContext.tail_messages",
+ mock_tail):
+ command = fedmsg.commands.tail.TailCommand()
+ command.execute()
+
+ output = stdout.getvalue()
+ eq_(output, "name, endpoint, topic, message\n")
+
+ @patch("sys.argv", new_callable=lambda: ["fedmsg-tail", "--pretty"])
+ @patch("sys.stdout", new_callable=six.StringIO)
+ def test_tail_pretty(self, stdout, argv):
+ msgs = []
+
+ def mock_tail(self, topic="", passive=False, **kw):
+ msg = dict(
+ msg=dict(hello="world"),
+ timestamp=1354563717.472648, # Once upon a time...
+ )
+
+ yield ("name", "endpoint", "topic", msg)
+
+ config = {}
+ with patch("fedmsg.__local", self.local):
+ with patch("fedmsg.config.__cache", config):
+ with patch("fedmsg.core.FedMsgContext.tail_messages",
+ mock_tail):
+ command = fedmsg.commands.tail.TailCommand()
+ command.execute()
+
+ output = stdout.getvalue()
+ expected = """name, endpoint, topic,
+{'msg': {'hello': 'world'}, 'timestamp': 'Mon Dec 3 14:41:57 2012'}
+"""
+ eq_(output, expected)
+
+ @patch("sys.argv", new_callable=lambda: ["fedmsg-tail", "--really-pretty"])
+ @patch("sys.stdout", new_callable=six.StringIO)
+ def test_tail_really_pretty(self, stdout, argv):
+ msgs = []
+
+ def mock_tail(self, topic="", passive=False, **kw):
+ msg = dict(
+ msg=dict(hello="world"),
+ timestamp=1354563717.472648, # Once upon a time...
+ )
+
+ yield ("name", "endpoint", "topic", msg)
+
+ config = {}
+ with patch("fedmsg.__local", self.local):
+ with patch("fedmsg.config.__cache", config):
+ with patch("fedmsg.core.FedMsgContext.tail_messages",
+ mock_tail):
+ command = fedmsg.commands.tail.TailCommand()
+ command.execute()
+
+ output = stdout.getvalue()
+ expected = 'name, endpoint, topic, \n{\x1b[39;49;00m\n \x1b[39;49;00m\x1b[33m"msg"\x1b[39;49;00m:\x1b[39;49;00m \x1b[39;49;00m{\x1b[39;49;00m\n \x1b[39;49;00m\x1b[33m"hello"\x1b[39;49;00m:\x1b[39;49;00m \x1b[39;49;00m\x1b[33m"world"\x1b[39;49;00m\n \x1b[39;49;00m}\x1b[39;49;00m,\x1b[39;49;00m \x1b[39;49;00m\n \x1b[39;49;00m\x1b[33m"timestamp"\x1b[39;49;00m:\x1b[39;49;00m \x1b[39;49;00m\x1b[34m1354563717.472648\x1b[39;49;00m\n}\x1b[39;49;00m\n'
+ eq_(output, expected)
+
+ @patch("sys.argv", new_callable=lambda: ["fedmsg-relay"])
+ def test_relay(self, argv):
+ actual_options = []
+
+ def mock_main(options, consumers):
+ actual_options.append(options)
+
+ config = {}
+ with patch("fedmsg.__local", self.local):
+ with patch("fedmsg.config.__cache", config):
+ with patch("moksha.hub.main", mock_main):
+ command = fedmsg.commands.relay.RelayCommand()
+ command.execute()
+
+ actual_options = actual_options[0]
+ assert(
+ fedmsg.consumers.relay.RelayConsumer.config_key in actual_options
+ )
+ assert(
+ actual_options[fedmsg.consumers.relay.RelayConsumer.config_key]
+ )
View
2 setup.py
@@ -56,6 +56,8 @@
]
tests_require = [
'nose',
+ 'mock',
+ 'six', # In the future, we'll use this across fedmsg proper for py3.
]
if sys.version_info[0] == 2 and sys.version_info[1] <= 6:

0 comments on commit c0cc49c

Please sign in to comment.