Merge lp:~benji/charm-tools/bug-1130793-add-log-option-escaping into lp:~charmers/charm-tools/trunk

Proposed by Benji York
Status: Merged
Approved by: Marco Ceppi
Approved revision: 170
Merged at revision: 170
Proposed branch: lp:~benji/charm-tools/bug-1130793-add-log-option-escaping
Merge into: lp:~charmers/charm-tools/trunk
Diff against target: 71 lines (+44/-1)
2 files modified
helpers/python/charmhelpers/__init__.py (+4/-1)
helpers/python/charmhelpers/tests/test_charmhelpers.py (+40/-0)
To merge this branch: bzr merge lp:~benji/charm-tools/bug-1130793-add-log-option-escaping
Reviewer Review Type Date Requested Status
Marco Ceppi (community) Approve
Review via email: mp+150442@code.launchpad.net

Description of the change

This branch fixes bug 1130793 by prefixing messages sent to juju-log with a end-of-options marker ("--").

To post a comment you must log in.
Revision history for this message
Marco Ceppi (marcoceppi) wrote :

LGTM!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'helpers/python/charmhelpers/__init__.py'
--- helpers/python/charmhelpers/__init__.py 2012-10-02 15:31:19 +0000
+++ helpers/python/charmhelpers/__init__.py 2013-02-25 21:46:29 +0000
@@ -42,12 +42,15 @@
4242
43SLEEP_AMOUNT = 0.143SLEEP_AMOUNT = 0.1
44Env = namedtuple('Env', 'uid gid home')44Env = namedtuple('Env', 'uid gid home')
45log = command('juju-log')
46# We create a juju_status Command here because it makes testing much,45# We create a juju_status Command here because it makes testing much,
47# much easier.46# much easier.
48juju_status = lambda: command('juju')('status')47juju_status = lambda: command('juju')('status')
4948
5049
50def log(message, juju_log=command('juju-log')):
51 return juju_log('--', message)
52
53
51def log_entry():54def log_entry():
52 log("--> Entering {}".format(script_name()))55 log("--> Entering {}".format(script_name()))
5356
5457
=== modified file 'helpers/python/charmhelpers/tests/test_charmhelpers.py'
--- helpers/python/charmhelpers/tests/test_charmhelpers.py 2012-10-03 07:57:13 +0000
+++ helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-02-25 21:46:29 +0000
@@ -445,6 +445,46 @@
445 RuntimeError, charmhelpers.wait_for_page_contents,445 RuntimeError, charmhelpers.wait_for_page_contents,
446 'http://example.com', "This will error", timeout=0)446 'http://example.com', "This will error", timeout=0)
447447
448 def test_log(self):
449 # The "log" function forwards a string on to the juju-log command.
450 logged = []
451 def juju_log(*args):
452 logged.append(args)
453 charmhelpers.log('This is a log message', juju_log)
454 # Since we only logged one message, juju-log was only called once..
455 self.assertEqual(len(logged), 1)
456 # The message was included in the arguments passed to juju-log.
457 self.assertIn('This is a log message', logged[0])
458
459 def test_log_escapes_message(self):
460 # The Go version of juju-log interprets any string begining with two
461 # hyphens ("--") as a command-line switch, even if the third character
462 # is non-alphanumeric. This is different behavior than the Python
463 # version of juju-log. Therefore we signfiy the end of options by
464 # inserting the string " -- " just before the log message.
465 logged = []
466 def juju_log(*args):
467 logged.append(args)
468 charmhelpers.log('This is a log message', juju_log)
469 # The call to juju-log includes the " -- " string before the message.
470 self.assertEqual([('--', 'This is a log message')], logged)
471
472 def test_log_entry(self):
473 # The log_entry function logs a message about the script starting.
474 logged = []
475 self.patch(charmhelpers, 'log', logged.append)
476 self.patch(charmhelpers, 'script_name', lambda: 'SCRIPT-NAME')
477 charmhelpers.log_entry()
478 self.assertEqual(['--> Entering SCRIPT-NAME'], logged)
479
480 def test_log_exit(self):
481 # The log_exit function logs a message about the script ending.
482 logged = []
483 self.patch(charmhelpers, 'log', logged.append)
484 self.patch(charmhelpers, 'script_name', lambda: 'SCRIPT-NAME')
485 charmhelpers.log_exit()
486 self.assertEqual(['<-- Exiting SCRIPT-NAME'], logged)
487
448488
449if __name__ == '__main__':489if __name__ == '__main__':
450 unittest.main()490 unittest.main()

Subscribers

People subscribed via source and target branches