Merge lp:~david-goetz/swift/sos into lp:~hudson-openstack/swift/trunk

Proposed by David Goetz
Status: Merged
Approved by: gholt
Approved revision: 349
Merged at revision: 349
Proposed branch: lp:~david-goetz/swift/sos
Merge into: lp:~hudson-openstack/swift/trunk
Diff against target: 143 lines (+72/-3)
4 files modified
swift/common/utils.py (+7/-3)
swift/common/wsgi.py (+30/-0)
test/unit/common/test_utils.py (+21/-0)
test/unit/common/test_wsgi.py (+14/-0)
To merge this branch: bzr merge lp:~david-goetz/swift/sos
Reviewer Review Type Date Requested Status
gholt (community) Approve
John Dickinson Approve
Review via email: mp+73280@code.launchpad.net

Description of the change

Changes needed for SOS

To post a comment you must log in.
lp:~david-goetz/swift/sos updated
347. By David Goetz

fix for trans id

348. By David Goetz

fix for trans id

Revision history for this message
John Dickinson (notmyname) wrote :

lgtm

review: Approve
Revision history for this message
gholt (gholt) wrote :

No tests?

review: Needs Information
lp:~david-goetz/swift/sos updated
349. By David Goetz

adding unit tests

Revision history for this message
David Goetz (david-goetz) wrote :

added some unit tests

Revision history for this message
gholt (gholt) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'swift/common/utils.py'
2--- swift/common/utils.py 2011-08-11 19:36:02 +0000
3+++ swift/common/utils.py 2011-08-30 19:07:31 +0000
4@@ -30,7 +30,8 @@
5 import ctypes
6 import ctypes.util
7 import struct
8-from ConfigParser import ConfigParser, NoSectionError, NoOptionError
9+from ConfigParser import ConfigParser, NoSectionError, NoOptionError, \
10+ RawConfigParser
11 from optparse import OptionParser
12 from tempfile import mkstemp
13 import cPickle as pickle
14@@ -749,7 +750,7 @@
15 return item_from_env(env, 'swift.cache')
16
17
18-def readconf(conf, section_name=None, log_name=None, defaults=None):
19+def readconf(conf, section_name=None, log_name=None, defaults=None, raw=False):
20 """
21 Read config file and return config items as a dict
22
23@@ -763,7 +764,10 @@
24 """
25 if defaults is None:
26 defaults = {}
27- c = ConfigParser(defaults)
28+ if raw:
29+ c = RawConfigParser(defaults)
30+ else:
31+ c = ConfigParser(defaults)
32 if hasattr(conf, 'readline'):
33 c.readfp(conf)
34 else:
35
36=== modified file 'swift/common/wsgi.py'
37--- swift/common/wsgi.py 2011-04-16 00:17:52 +0000
38+++ swift/common/wsgi.py 2011-08-30 19:07:31 +0000
39@@ -26,6 +26,7 @@
40 from eventlet import greenio, GreenPool, sleep, wsgi, listen
41 from paste.deploy import loadapp, appconfig
42 from eventlet.green import socket, ssl
43+from webob import Request
44
45 from swift.common.utils import get_logger, drop_privileges, \
46 validate_configuration, capture_stdio, NullLogger
47@@ -188,3 +189,32 @@
48 greenio.shutdown_safe(sock)
49 sock.close()
50 logger.notice('Exited')
51+
52+
53+def make_pre_authed_request(env, method, path, body=None, headers=None,
54+ agent='Swift'):
55+ """
56+ Makes a new webob.Request based on the current env but with the
57+ parameters specified. Note that this request will be preauthorized.
58+
59+ :param env: Current WSGI environment dictionary
60+ :param method: HTTP method of new request
61+ :param path: HTTP path of new request
62+ :param body: HTTP body of new request; None by default
63+ :param headers: Extra HTTP headers of new request; None by default
64+
65+ :returns: webob.Request object
66+ (Stolen from Swauth: https://github.com/gholt/swauth)
67+ """
68+ newenv = {'REQUEST_METHOD': method, 'HTTP_USER_AGENT': agent}
69+ for name in ('swift.cache', 'HTTP_X_TRANS_ID'):
70+ if name in env:
71+ newenv[name] = env[name]
72+ newenv['swift.authorize'] = lambda req: None
73+ if not headers:
74+ headers = {}
75+ if body:
76+ return Request.blank(path, environ=newenv, body=body,
77+ headers=headers)
78+ else:
79+ return Request.blank(path, environ=newenv, headers=headers)
80
81=== modified file 'test/unit/common/test_utils.py'
82--- test/unit/common/test_utils.py 2011-08-11 19:36:02 +0000
83+++ test/unit/common/test_utils.py 2011-08-30 19:07:31 +0000
84@@ -536,6 +536,27 @@
85 os.unlink('/tmp/test')
86 self.assertRaises(SystemExit, utils.readconf, '/tmp/test')
87
88+ def test_readconf_raw(self):
89+ conf = '''[section1]
90+foo = bar
91+
92+[section2]
93+log_name = %(yarr)s'''
94+ # setup a real file
95+ with open('/tmp/test', 'wb') as f:
96+ f.write(conf)
97+ make_filename = lambda: '/tmp/test'
98+ # setup a file stream
99+ make_fp = lambda: StringIO(conf)
100+ for conf_object_maker in (make_filename, make_fp):
101+ result = utils.readconf(conf_object_maker(), raw=True)
102+ expected = {'log_name': None,
103+ 'section1': {'foo': 'bar'},
104+ 'section2': {'log_name': '%(yarr)s'}}
105+ self.assertEquals(result, expected)
106+ os.unlink('/tmp/test')
107+ self.assertRaises(SystemExit, utils.readconf, '/tmp/test')
108+
109 def test_drop_privileges(self):
110 user = getuser()
111 # over-ride os with mock
112
113=== modified file 'test/unit/common/test_wsgi.py'
114--- test/unit/common/test_wsgi.py 2010-11-19 18:15:41 +0000
115+++ test/unit/common/test_wsgi.py 2011-08-30 19:07:31 +0000
116@@ -28,6 +28,7 @@
117 from collections import defaultdict
118
119 from eventlet import sleep
120+from webob import Request
121
122 from swift.common import wsgi
123
124@@ -173,6 +174,19 @@
125 wsgi.sleep = old_sleep
126 wsgi.time = old_time
127
128+ def test_pre_auth_req(self):
129+ class FakeReq(object):
130+ @classmethod
131+ def fake_blank(cls, path, environ={}, body='', headers={}):
132+ self.assertEquals(environ['swift.authorize']('test'), None)
133+ self.assertEquals(environ['HTTP_X_TRANS_ID'], '1234')
134+ was_blank = Request.blank
135+ Request.blank = FakeReq.fake_blank
136+ wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'},
137+ 'PUT', '/', body='tester', headers={})
138+ wsgi.make_pre_authed_request({'HTTP_X_TRANS_ID': '1234'},
139+ 'PUT', '/', headers={})
140+ Request.blank = was_blank
141
142 if __name__ == '__main__':
143 unittest.main()