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
1=== modified file 'helpers/python/charmhelpers/__init__.py'
2--- helpers/python/charmhelpers/__init__.py 2012-10-02 15:31:19 +0000
3+++ helpers/python/charmhelpers/__init__.py 2013-02-25 21:46:29 +0000
4@@ -42,12 +42,15 @@
5
6 SLEEP_AMOUNT = 0.1
7 Env = namedtuple('Env', 'uid gid home')
8-log = command('juju-log')
9 # We create a juju_status Command here because it makes testing much,
10 # much easier.
11 juju_status = lambda: command('juju')('status')
12
13
14+def log(message, juju_log=command('juju-log')):
15+ return juju_log('--', message)
16+
17+
18 def log_entry():
19 log("--> Entering {}".format(script_name()))
20
21
22=== modified file 'helpers/python/charmhelpers/tests/test_charmhelpers.py'
23--- helpers/python/charmhelpers/tests/test_charmhelpers.py 2012-10-03 07:57:13 +0000
24+++ helpers/python/charmhelpers/tests/test_charmhelpers.py 2013-02-25 21:46:29 +0000
25@@ -445,6 +445,46 @@
26 RuntimeError, charmhelpers.wait_for_page_contents,
27 'http://example.com', "This will error", timeout=0)
28
29+ def test_log(self):
30+ # The "log" function forwards a string on to the juju-log command.
31+ logged = []
32+ def juju_log(*args):
33+ logged.append(args)
34+ charmhelpers.log('This is a log message', juju_log)
35+ # Since we only logged one message, juju-log was only called once..
36+ self.assertEqual(len(logged), 1)
37+ # The message was included in the arguments passed to juju-log.
38+ self.assertIn('This is a log message', logged[0])
39+
40+ def test_log_escapes_message(self):
41+ # The Go version of juju-log interprets any string begining with two
42+ # hyphens ("--") as a command-line switch, even if the third character
43+ # is non-alphanumeric. This is different behavior than the Python
44+ # version of juju-log. Therefore we signfiy the end of options by
45+ # inserting the string " -- " just before the log message.
46+ logged = []
47+ def juju_log(*args):
48+ logged.append(args)
49+ charmhelpers.log('This is a log message', juju_log)
50+ # The call to juju-log includes the " -- " string before the message.
51+ self.assertEqual([('--', 'This is a log message')], logged)
52+
53+ def test_log_entry(self):
54+ # The log_entry function logs a message about the script starting.
55+ logged = []
56+ self.patch(charmhelpers, 'log', logged.append)
57+ self.patch(charmhelpers, 'script_name', lambda: 'SCRIPT-NAME')
58+ charmhelpers.log_entry()
59+ self.assertEqual(['--> Entering SCRIPT-NAME'], logged)
60+
61+ def test_log_exit(self):
62+ # The log_exit function logs a message about the script ending.
63+ logged = []
64+ self.patch(charmhelpers, 'log', logged.append)
65+ self.patch(charmhelpers, 'script_name', lambda: 'SCRIPT-NAME')
66+ charmhelpers.log_exit()
67+ self.assertEqual(['<-- Exiting SCRIPT-NAME'], logged)
68+
69
70 if __name__ == '__main__':
71 unittest.main()

Subscribers

People subscribed via source and target branches