PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/test_sos/unit/test_origin.py

https://github.com/smballe/sos
Python | 213 lines | 191 code | 6 blank | 16 comment | 5 complexity | 8df65fd6fc77c55a3655584f26ff6bb8 MD5 | raw file
  1. # Copyright (c) 2010-2011 OpenStack, LLC.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. try:
  16. import simplejson as json
  17. except ImportError:
  18. import json
  19. import unittest
  20. #from contextlib import contextmanager
  21. #from time import time
  22. from webob import Request, Response
  23. from sos import origin
  24. class FakeConf(object):
  25. def __init__(self, data=None):
  26. if data:
  27. self.data = data
  28. else:
  29. self.data = '''[sos]
  30. origin_admin_key = unittest
  31. origin_cdn_hosts = origin_cdn.com
  32. origin_db_hosts = origin_db.com
  33. origin_account = .origin
  34. outgoing_cdn_uri_format = http://cdn.com:8080/h%(hash)s/r%(hash_mod)d
  35. outgoing_ssl_cdn_uri_format = https://ssl.cdn.com/h%(hash)s'''.split('\n')
  36. def readline(self):
  37. if self.data:
  38. return self.data.pop(0)
  39. return ''
  40. class FakeApp(object):
  41. def __init__(self, status_headers_body_iter=None):
  42. self.calls = 0
  43. self.status_headers_body_iter = status_headers_body_iter
  44. if not self.status_headers_body_iter:
  45. self.status_headers_body_iter = iter([('404 Not Found', {}, '')])
  46. def __call__(self, env, start_response):
  47. self.calls += 1
  48. self.request = Request.blank('', environ=env)
  49. # if 'swift.authorize' in env:
  50. # resp = env['swift.authorize'](self.request)
  51. # if resp:
  52. # return resp(env, start_response)
  53. status, headers, body = self.status_headers_body_iter.next()
  54. return Response(status=status, headers=headers,
  55. body=body)(env, start_response)
  56. class FakeConn(object):
  57. def __init__(self, status_headers_body_iter=None):
  58. self.calls = 0
  59. self.status_headers_body_iter = status_headers_body_iter
  60. if not self.status_headers_body_iter:
  61. self.status_headers_body_iter = iter([('404 Not Found', {}, '')])
  62. def request(self, method, path, headers):
  63. self.calls += 1
  64. self.request_path = path
  65. self.status, self.headers, self.body = \
  66. self.status_headers_body_iter.next()
  67. self.status, self.reason = self.status.split(' ', 1)
  68. self.status = int(self.status)
  69. def getresponse(self):
  70. return self
  71. def read(self):
  72. body = self.body
  73. self.body = ''
  74. return body
  75. class TestOrigin(unittest.TestCase):
  76. def setUp(self):
  77. fake_conf = FakeConf()
  78. self.test_origin = origin.filter_factory(
  79. {'sos_conf': fake_conf})(FakeApp())
  80. def test_valid_setup(self):
  81. fake_conf = FakeConf(data=['[sos]'])
  82. test_origin = origin.filter_factory(
  83. {'sos_conf': fake_conf})(FakeApp())
  84. self.assertFalse(test_origin._valid_setup())
  85. fake_conf = FakeConf()
  86. test_origin = origin.filter_factory(
  87. {'sos_conf': fake_conf})(FakeApp())
  88. self.assertTrue(test_origin._valid_setup())
  89. def test_admin_setup_failures(self):
  90. resp = Request.blank('/origin/.prep',
  91. environ={'REQUEST_METHOD': 'PUT'}).get_response(self.test_origin)
  92. self.assertEquals(resp.status_int, 403)
  93. resp = Request.blank('/origin/.prep_not_there',
  94. environ={'REQUEST_METHOD': 'PUT'},
  95. headers={'X-Origin-Admin-User': '.origin_admin',
  96. 'X-Origin-Admin-Key': 'unittest'}
  97. ).get_response(self.test_origin)
  98. self.assertEquals(resp.status_int, 404)
  99. self.test_origin.app = FakeApp(iter([('404 Not Found', {}, '')]))
  100. try:
  101. resp = Request.blank('/origin/.prep',
  102. environ={'REQUEST_METHOD': 'PUT'},
  103. headers={'X-Origin-Admin-User': '.origin_admin',
  104. 'X-Origin-Admin-Key': 'unittest'}
  105. ).get_response(self.test_origin)
  106. except Exception:
  107. pass
  108. else:
  109. self.assertTrue(False)
  110. def test_admin_setup(self):
  111. # PUTs for account, 16 .hash's, and .hash_to_legacy
  112. self.test_origin.app = FakeApp(iter(
  113. [('204 No Content', {}, '') for i in xrange(18)]))
  114. resp = Request.blank('/origin/.prep',
  115. environ={'REQUEST_METHOD': 'PUT'},
  116. headers={'X-Origin-Admin-User': '.origin_admin',
  117. 'X-Origin-Admin-Key': 'unittest'}
  118. ).get_response(self.test_origin)
  119. self.assertEquals(resp.status_int, 204)
  120. self.assertEquals(self.test_origin.app.calls, 18)
  121. def test_origin_db_post_fail(self):
  122. self.test_origin.app = FakeApp(iter(
  123. [('204 No Content', {}, '')]))
  124. resp = Request.blank('http://origin_db.com:8080/v1/acc/cont',
  125. environ={'REQUEST_METHOD': 'POST'},
  126. ).get_response(self.test_origin)
  127. self.assertEquals(resp.status_int, 404)
  128. self.test_origin.app = FakeApp(iter(
  129. [('404 Not Found', {}, '')]))
  130. resp = Request.blank('http://origin_db.com:8080/v1/acc/cont',
  131. environ={'REQUEST_METHOD': 'POST'},
  132. ).get_response(self.test_origin)
  133. self.assertEquals(resp.status_int, 404)
  134. def test_origin_db_post_ttl(self):
  135. data = {'account': 'acc', 'container': 'cont',
  136. 'ttl': 29500, 'logs_enabled': 'false',
  137. 'cdn_enabled': 'true'}
  138. self.test_origin.app = FakeApp(iter(
  139. [('200 Ok', {}, json.dumps(data)),
  140. ]))
  141. resp = Request.blank('http://origin_db.com:8080/v1/acc/cont',
  142. environ={'REQUEST_METHOD': 'POST'}, headers={'X-TTL': 'foo'},
  143. ).get_response(self.test_origin)
  144. self.assertEquals(resp.status_int, 400)
  145. self.assertTrue('Invalid X-TTL, must be integer' in resp.body)
  146. self.test_origin.app = FakeApp(iter(
  147. [('200 Ok', {}, json.dumps(data)),
  148. ]))
  149. resp = Request.blank('http://origin_db.com:8080/v1/acc/cont',
  150. environ={'REQUEST_METHOD': 'POST'}, headers={'X-TTL': '1'},
  151. ).get_response(self.test_origin)
  152. self.assertEquals(resp.status_int, 400)
  153. self.assertTrue('Invalid X-TTL, must be between' in resp.body)
  154. def test_origin_db_put(self):
  155. data = {'account': 'acc', 'container': 'cont',
  156. 'ttl': 29500, 'logs_enabled': 'false',
  157. 'cdn_enabled': 'true'}
  158. self.test_origin.app = FakeApp(iter([
  159. ('404 Not Found', {}, ''), # call to get current values
  160. ('204 Not Found', {}, ''), # call to get current values
  161. ]))
  162. req = Request.blank('http://origin_db.com:8080/v1/acc/cont',
  163. environ={'REQUEST_METHOD': 'PUT'},
  164. )
  165. resp = req.get_response(self.test_origin)
  166. self.assertEquals(resp.status_int, 204)
  167. def test_origin_db_put_update(self):
  168. data = {'account': 'acc', 'container': 'cont',
  169. 'ttl': 29500, 'logs_enabled': 'false',
  170. 'cdn_enabled': 'true'}
  171. self.test_origin.app = FakeApp(iter(
  172. [('200 Ok', {}, json.dumps(data)),
  173. ]))
  174. req = Request.blank('http://origin_db.com:8080/v1/acc/cont',
  175. environ={'REQUEST_METHOD': 'PUT'},
  176. )
  177. resp = req.get_response(self.test_origin)
  178. self.assertEquals(resp.status_int, 204)
  179. if __name__ == '__main__':
  180. unittest.main()