def test_PUT(self): controller = proxy_server.ContainerController(self.app, 'a', 'c') req = Request.blank('/v1/a/c', {}) with mock.patch( 'osd.proxyService.controllers.container.check_metadata', return_value=HTTPBadRequest(request=req)): resp = controller.PUT(req) self.assertEqual(resp.status_int, 400) controller = proxy_server.ContainerController(self.app, 'a', 'container' * 40) resp = controller.PUT(req) self.assertEqual(resp.status_int, 400) with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=(None, None, None, None)): controller = proxy_server.ContainerController(self.app, 'a', 'c') self.app.account_autocreate = False Controller.autocreate_account = mock.MagicMock() req = Request.blank('/v1/a/c', {}) resp = controller.PUT(req) self.assertEqual(resp.status_int, 404) self.assertFalse(Controller.autocreate_account.called) with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=([{ 'ip': '1', 'port': '1' }], 'fs1', 'd1', 10)): self.app.max_containers_per_account = 5 req = Request.blank('/v1/a/c', {}) resp = controller.PUT(req) self.assertEqual(resp.status_int, 403)
def test_06_put_object_created(self): if not os.path.exists(cont_jour): os.makedirs(cont_jour) if not os.path.exists(trans_jour): os.makedirs(trans_jour) #print "----------6----------------------------------" req = Request.blank('/fs0/accdir/contdir/pramita/container1', environ={'REQUEST_METHOD': 'PUT'}, headers={ 'x-timestamp': '1408505144.00290', 'X-Account-Host': '127.0.0.1:61005', 'Content-Length': '6', 'X-Size': 97, 'x-component-number': 4, 'X-Content-Type': 'text/plain', 'X-etag': 'cxwecuwcbwiu', 'X-Account-Component-Number': 4 }) req.body = 'VERIFY' resp = cont_obj.PUT(req) self.assertEqual(resp.status_int, 201) req1 = Request.blank('/fs0/accdir/contdir/pramita/container1/object1', environ={'REQUEST_METHOD': 'PUT'}, headers={ 'x-timestamp': time.time(), 'X-Account-Host': '127.0.0.1:61005', 'Content-Length': '6', 'X-Size': 97, 'x-component-number': 4, 'X-Content-Type': 'application', 'X-etag': 'cxwecuwcbwiu' }) req1.body = 'VERIFY' resp = cont_obj.PUT(req1) self.assertEqual(resp.status_int, 201)
def __call__(self, env, start_response): """ WSGI entry point. Wraps env in swob.Request object and passes it down. :param env: WSGI environment dictionary :param start_response: WSGI callable """ try: if self.memcache is None: self.memcache = cache_from_env(env) #req = self.update_request(Request(env)) req = Request(env) return self.handle_request(req)(env, start_response) except UnicodeError: err = HTTPPreconditionFailed(request=Request(env), body='Invalid UTF8 or contains NULL') return err(env, start_response) except (Exception, Timeout): start_response('500 Server Error', [('Content-Type', 'text/plain')]) return ['Internal server error.\n'] finally: if (not req.method == "STOP_SERVICE") and ( req in self.ongoing_operation_list): self.ongoing_operation_list.remove(req)
def test_get_account_info_cache(self): # The original test that we prefer to preserve cached = {'status': 404, 'bytes': 3333, 'total_object_count': 10} req = Request.blank("/v1/account/cont", environ={'swift.cache': FakeCache(cached)}) with patch('osd.proxyService.controllers.base.' '_prepare_pre_auth_info_request', FakeRequest): resp = get_account_info(req.environ, 'xxx') self.assertEquals(resp['bytes'], 3333) self.assertEquals(resp['total_object_count'], 10) self.assertEquals(resp['status'], 404) # Here is a more realistic test cached = {'status': 404, 'bytes': '3333', 'container_count': '234', 'total_object_count': '10', 'meta': {}} req = Request.blank("/v1/account/cont", environ={'swift.cache': FakeCache(cached)}) with patch('osd.proxyService.controllers.base.' '_prepare_pre_auth_info_request', FakeRequest): resp = get_account_info(req.environ, 'xxx') self.assertEquals(resp['status'], 404) self.assertEquals(resp['bytes'], '3333') self.assertEquals(resp['container_count'], 234) self.assertEquals(resp['meta'], {}) self.assertEquals(resp['total_object_count'], '10')
def test_GETorHEAD_base(self): base = Controller(self.app) req = Request.blank('/v1/a/c/o/with/slashes') with patch('osd.proxyService.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'object', FakeObjectRing(''), [{'ip': '1.2.3', 'port': '0000'}], \ 'fs0', 'd0', '/a/c/o/with/slashes', 5.0, 0) self.assertTrue('swift.object/a/c/o/with/slashes' in resp.environ) self.assertEqual( resp.environ['swift.object/a/c/o/with/slashes']['status'], 200) req = Request.blank('/v1/a/c/o') with patch('osd.proxyService.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'object', FakeObjectRing(''), [{'ip': '1.2.3', 'port': '0000'}], \ 'fs0', 'd0', '/a/c/o', 5.0, 0) self.assertTrue('swift.object/a/c/o' in resp.environ) self.assertEqual(resp.environ['swift.object/a/c/o']['status'], 200) req = Request.blank('/v1/a/c') with patch('osd.proxyService.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'container', FakeContainerRing(''), [{'ip': '1.2.3', 'port': '0000'}], \ 'fs0', 'd0', '/a/c', 5.0, 0) self.assertTrue('swift.container/a/c' in resp.environ) self.assertEqual(resp.environ['swift.container/a/c']['status'], 200) req = Request.blank('/v1/a') with patch('osd.proxyService.controllers.base.' 'http_connect', fake_http_connect(200)): resp = base.GETorHEAD_base(req, 'account', FakeAccountRing(''), [{'ip': '1.2.3', 'port': '0000'}], \ 'fs0', 'd0', '/a', 5.0, 0) self.assertTrue('swift.account/a' in resp.environ) self.assertEqual(resp.environ['swift.account/a']['status'], 200)
def test_DELETE(self): controller = proxy_server.AccountController(self.app, 'AUTH_bob') req = Request.blank('/v1/AUTH_bob', {}) resp = controller.DELETE(req) self.assertEqual(resp.status_int, 405) self.app.allow_account_management = True with mock.patch('osd.proxyService.controllers.base.http_connect', fake_http_connect(200, body='')): controller = proxy_server.AccountController( self.app, 'AUTH_bob' * 40) req = Request.blank('/v1/AUTH_bob', {}) resp = controller.DELETE(req) self.assertEqual(resp.status_int, 200) controller = proxy_server.AccountController(self.app, 'AUTH_bob' * 40) req = Request.blank('/v1/AUTH_bob', {}, query_string='test') resp = controller.DELETE(req) self.assertEqual(resp.status_int, 400) with mock.patch('osd.proxyService.controllers.base.http_connect', fake_http_connect(404, body='')): req = Request.blank('/v1/AUTH_bob', {'PATH_INFO': '/v1/AUTH_bob'}) resp = controller.DELETE(req) self.assertEqual(resp.status_int, 404)
def test_GETorHEAD(self): controller = proxy_server.ContainerController(self.app, 'a', 'c') req = Request.blank('/v1/a/c', {}) with mock.patch('osd.proxyService.controllers.base.http_connect', fake_http_connect(200)): with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=(None, 'test')): resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 200) with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=(None, None)): req = Request.blank('/v1/a/c', {}) resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 404) with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=(None, 'test')): controller = proxy_server.ContainerController(self.app, 'a', 'c') req = Request.blank('/v1/a/c', {}, query_string='path=2') resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 400) """ self.assertEqual(resp.body, 'Unsupported query parameters path or delimiter or prefix') req = Request.blank('/v1/AUTH_bob', {}, query_string='prefix=a') resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 400) self.assertEqual(resp.body, 'Unsupported query parameters path or delimiter or prefix') req = Request.blank('/v1/AUTH_bob', {}, query_string='delimiter=a') resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 400) self.assertEqual(resp.body, 'Unsupported query parameters path or delimiter or prefix') """ with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=(None, 'test')): controller = proxy_server.ContainerController(self.app, 'a', 'c') req = Request.blank('/v1/a/c', {}, query_string='limit=-1') resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 412) self.assertEqual(resp.body, 'Value of limit must be a positive integer') with mock.patch( 'osd.proxyService.controllers.base.Controller.account_info', return_value=(None, 'test')): controller = proxy_server.ContainerController(self.app, 'a', 'c') req = Request.blank('/v1/a/c', {}, query_string='limit=abc') resp = controller.GETorHEAD(req) self.assertEqual(resp.status_int, 412) self.assertEqual(resp.body, 'Value of limit must be a positive integer')
def test_POST_read_metadata_wrong_timestamp(self): timestamp = normalize_timestamp(time()) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'PUT', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'Content-Type': 'application/x-test', 'X-Object-Meta-1': 'One', 'X-Object-Meta-Two': 'Two', 'X-Container-Host': '1.2.3:0000' }) req.body = 'VERIFY' resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 201) object_server.ObjectController.remove_trans_id = mock.MagicMock() object_server.ObjectController.release_lock = mock.MagicMock() with self.read_meta_manage(): diskfile.DiskFile.read_metadata = mock.MagicMock( return_value={ 'X-Timestamp': str(float(timestamp) + 1), 'Content-Length': 0 }) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'DELETE', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEqual(resp.status_int, 409) with self.read_meta_manage(): diskfile.DiskFile.read_metadata = mock.MagicMock( return_value={ 'X-Timestamp': str(float(timestamp) - 1), 'Content-Length': 0 }) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'DELETE', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEqual(resp.status_int, 204)
def test_POST_old_timestamp(self): ts = time() timestamp = normalize_timestamp(ts) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'PUT', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'Content-Type': 'application/x-test', 'X-Object-Meta-1': 'One', 'X-Object-Meta-Two': 'Two', 'X-Container-Host': '1.2.3:0000' }) req.body = 'VERIFY' resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 201) # Same timestamp should result in 409 req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'POST', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Object-Meta-3': 'Three', 'X-Object-Meta-4': 'Four', 'Content-Encoding': 'gzip', 'Content-Type': 'application/x-test', 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 409) # Earlier timestamp should result in 409 timestamp = normalize_timestamp(ts - 1) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'POST', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Object-Meta-5': 'Five', 'X-Object-Meta-6': 'Six', 'Content-Encoding': 'gzip', 'Content-Type': 'application/x-test', 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 409)
def make_subrequest(env, method=None, path=None, body=None, headers=None, agent='Swift', swift_source=None, make_env=make_env): """ Makes a new swob.Request based on the current env but with the parameters specified. :param env: The WSGI environment to base the new request on. :param method: HTTP method of new request; default is from the original env. :param path: HTTP path of new request; default is from the original env. path should be compatible with what you would send to Request.blank. path should be quoted and it can include a query string. for example: '/a%20space?unicode_str%E8%AA%9E=y%20es' :param body: HTTP body of new request; empty by default. :param headers: Extra HTTP headers of new request; None by default. :param agent: The HTTP user agent to use; default 'Swift'. You can put %(orig)s in the agent to have it replaced with the original env's HTTP_USER_AGENT, such as '%(orig)s StaticWeb'. You also set agent to None to use the original env's HTTP_USER_AGENT or '' to have no HTTP_USER_AGENT. :param swift_source: Used to mark the request as originating out of middleware. Will be logged in proxy logs. :param make_env: make_subrequest calls this make_env to help build the swob.Request. :returns: Fresh swob.Request object. """ query_string = None path = path or '' if path and '?' in path: path, query_string = path.split('?', 1) newenv = make_env(env, method, path=unquote(path), agent=agent, query_string=query_string, swift_source=swift_source) if not headers: headers = {} if body: return Request.blank(path, environ=newenv, body=body, headers=headers) else: return Request.blank(path, environ=newenv, headers=headers)
def test_DELETE_File_Not_Exists(self): with mock.patch('osd.objectService.diskfile.remove_file', side_effect=OSError): object_server.ObjectController.container_update = mock.MagicMock() timestamp = normalize_timestamp(time()) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'DELETE', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 404) self.assertFalse( object_server.ObjectController.container_update.called) timestamp = normalize_timestamp(time()) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'PUT', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'Content-Type': 'application/x-test', 'X-Object-Meta-1': 'One', 'X-Object-Meta-Two': 'Two', 'X-Container-Host': '1.2.3:0000' }) req.body = 'VERIFY' resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 201) timestamp = normalize_timestamp(time()) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'DELETE', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 204) self.assertTrue(object_server.ObjectController.container_update.called)
def test_RECOVER(self): with mock.patch("osd.objectService.server.get_ip_port_by_id", return_value={ 'ip': '1.2.3', 'port': '000' }): # Test osd.obj.server.ObjectController.RECOVER # In memory case osd.objectService.server.RecoveryHandler.triggered_recovery = mock.MagicMock( ) osd.objectService.server.LockManager.is_exists = mock.MagicMock( return_value=True) req = Request.blank('/fs1/a1/d1/a/c', environ={'REQUEST_METHOD': 'RECOVER'}, headers={"x-operation": 'GET', "x-request-trans-id": 'xyz', \ "x-server-id": 'xxx', "x-object-path":'a/c/o'}) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 200) self.assertFalse(osd.objectService.server.RecoveryHandler. triggered_recovery.called) #not in memory case successfulcase osd.objectService.server.LockManager.is_exists = mock.MagicMock( return_value=False) req = Request.blank('/fs1/a1/d1/a/c/o', environ={'REQUEST_METHOD': 'RECOVER'}, headers={"x-operation": 'GET', "x-request-trans-id": 'xyz', \ "x-server-id": 'xxx', "x-object-path":'a/c/o'}) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 200) #not in memory case unsuccessful case osd.objectService.server.TranscationManager.release_lock = mock.MagicMock( side_effect=Exception) req = Request.blank('/fs1/a1/d1/a/c/o', environ={'REQUEST_METHOD': 'RECOVER'}, headers={"x-operation": 'GET', "x-request-trans-id": 'xyz', \ "x-server-id": 'xxx', "x-object-path":'a/c/o'}) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 200) self.assertFalse(osd.objectService.server.RecoveryHandler. triggered_recovery.called) #not in memory DELETE PUT case req = Request.blank('/a/c/o', {'REQUEST_METHOD': 'RECOVER'}, \ headers={"x-operation": 'PUT', "x-request-trans-id": 'xyz', \ "x-server-id": 'xxx', "x-object-path":'a/c/o'}) resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 200) self.assertTrue(osd.objectService.server.RecoveryHandler. triggered_recovery.called) osd.objectService.server.RecoveryHandler.triggered_recovery.assert_called_once_with( 'a/c/o', 'PUT', '1.2.3:000')
def test_get_object_info_swift_source(self): req = Request.blank("/v1/a/c/o", environ={'swift.cache': FakeCache({})}) with patch('osd.proxyService.controllers.base.' '_prepare_pre_auth_info_request', FakeRequest): resp = get_object_info(req.environ, 'app', swift_source='LU') self.assertEquals(resp['meta']['fakerequest-swift-source'], 'LU')
def test_best_response(self): req = Request.blank('/v1/a/c/o') resp = self.base.best_response(req, [], [], [], 'object') self.assertEqual(resp.status, '503 Internal Server Error') req = Request.blank('/v1/a/c/o') resp = self.base.best_response(req, [200], ['Fake'], [''], 'object', {}) self.assertEqual(resp.status, '200 Fake') req = Request.blank('/v1/a/c/o') resp = self.base.best_response(req, [412], ['Fake'], [''], 'object', {}) self.assertEqual(resp.status, '412 Fake') req = Request.blank('/v1/a/c/o') resp = self.base.best_response(req, [500], ['Fake'], [''], 'object', {}) self.assertEqual(resp.status, '503 Internal Server Error')
def test_get_node_returning_empty_value(self): controller = proxy_server.ContainerController(self.app, 'AUTH_bob', 'test') Controller.account_info = mock.MagicMock(return_value=([{ 'ip': '1', 'port': '1' }], 'fs1', 'd1', 1)) req = Request.blank('/v1/a/c', {}) self.app.container_ring.get_node = mock.MagicMock(return_value=([], '', '', 5.0, 0)) resp = controller.GET(req) self.assertEqual(resp.status_int, 500) resp = controller.HEAD(req) self.assertEqual(resp.status_int, 500) resp = controller.PUT(req) self.assertEqual(resp.status_int, 500) resp = controller.DELETE(req) self.assertEqual(resp.status_int, 500) resp = controller.POST(req) self.assertEqual(resp.status_int, 500)
def __call__(self, env, start_response): req = Request(env) if self.check_character(req): return HTTPBadRequest( request=req, body= ("Object/Container/Account name contains forbidden chars from %s" % self.forbidden_chars))(env, start_response) elif self.check_length(req): return HTTPBadRequest( request=req, body= ("Object/Container/Account name longer than the allowed maximum " "%s" % self.maximum_length))(env, start_response) elif self.check_regexp(req): return HTTPBadRequest( request=req, body= ("Object/Container/Account name contains a forbidden substring " "from regular expression %s" % self.forbidden_regexp))( env, start_response) else: # Pass on to downstream WSGI component return self.app(env, start_response)
def test_PUT_client_timeout(self): class FakeTimeout(BaseException): def __enter__(self): raise self def __exit__(self, typ, value, tb): pass # This is just so the test fails when run on older object server code # instead of exploding. if not hasattr(object_server, 'ChunkReadTimeout'): object_server.ChunkReadTimeout = None with mock.patch.object(object_server, 'ChunkReadTimeout', FakeTimeout): timestamp = normalize_timestamp(time()) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={'REQUEST_METHOD': 'PUT'}, headers={ 'X-Timestamp': timestamp, 'Content-Type': 'text/plain', 'Content-Length': '6', 'X-Container-Host': '1.2.3:0000', 'X-GLOBAL-MAP-VERSION': 4.0 }) req.environ['wsgi.input'] = StringIO('VERIFY') resp = req.get_response(self.object_controller) self.assertEquals(resp.status_int, 408)
def test_01_put_container_created(self): if not os.path.exists(cont_jour): os.makedirs(cont_jour) if not os.path.exists(trans_jour): os.makedirs(trans_jour) #print "-------------------------------------------1--------------------------------------------------" req = Request.blank('/fs0/accdir/contdir/pramita/container1', environ={ 'REQUEST_METHOD': 'PUT', 'HTTP_X_COMPONENT_NUMBER': 4 }, headers={ 'x-timestamp': '1408505144.00290', 'X-Account-Host': '127.0.0.1:61005', 'Content-Length': '6', 'X-Size': 97, 'X-GLOBAL-MAP-VERSION': 4.0, 'x-component-number': 4, 'X-Content-Type': 'text/plain', 'X-etag': 'cxwecuwcbwiu', 'X-Account-Component-Number': 4 }) req.body = 'VERIFY' resp = cont_obj.PUT(req) #print "------------------finish---------------" self.assertEqual(resp.status_int, 201)
def test_14_container_post_metadata_acl_sysmeta(self): if not os.path.exists(cont_jour): os.makedirs(cont_jour) if not os.path.exists(trans_jour): os.makedirs(trans_jour) #print "---------------------14---------------------------" req = Request.blank('/fs0/accdir/contdir/pramita/container1', environ={'REQUEST_METHOD': 'POST'}, headers={ 'x-timestamp': time.time(), 'X-Account-Host': '127.0.0.1:61005', 'X-Container-SysMeta-subject': 'abc', 'X-Container-write': 'True', 'Content-Length': '6', 'X-Size': 97, 'x-component-number': 4, 'X-Content-Type': 'application', 'X-etag': 'cxwecuwcbwiu', 'X-Account-Component-Number': 4 }) req.body = 'VERIFY' resp = cont_obj.PUT(req) self.assertEqual(resp.status_int, 201) resp = cont_obj.POST(req) self.assertEqual(resp.status_int, 204)
def test_handle_bw_control_limit_zero(self): req = Request.blank('/a1/c1/o1', environ={'REQUEST_METHOD': 'PUT'}) self.assertFalse(self.controller.handle_bw_control(req, 'a', 'c', 'o')) self.controller.def_max_bandwidth_read_limit = 0 self.assertFalse(self.controller.handle_bw_control(req, 'a', 'c', 'o')) self.controller.def_max_bandwidth_write_limit = 0 self.assertFalse(self.controller.handle_bw_control(req, 'a', 'c', 'o'))
def test_sys_meta_headers_PUT(self): # check that headers in sys meta namespace make it through # the container controller sys_meta_key = '%stest' % get_sys_meta_prefix('container') sys_meta_key = sys_meta_key.title() user_meta_key = 'X-Container-Meta-Test' controller = proxy_server.ContainerController(self.app, 'a', 'c') context = {} callback = self._make_callback_func(context) hdrs_in = { sys_meta_key: 'foo', user_meta_key: 'bar', 'x-timestamp': '1.0' } req = Request.blank('/v1/a/c', headers=hdrs_in) with mock.patch('osd.proxyService.controllers.base.http_connect', fake_http_connect(200, 200, give_connect=callback)): controller.PUT(req) self.assertEqual(context['method'], 'PUT') self.assertTrue(sys_meta_key in context['headers']) self.assertEqual(context['headers'][sys_meta_key], 'foo') self.assertTrue(user_meta_key in context['headers']) self.assertEqual(context['headers'][user_meta_key], 'bar') self.assertNotEqual(context['headers']['x-timestamp'], '1.0')
def test_POST_read_metadata_exception(self): with self.read_meta_manage(): diskfile.DiskFile.read_metadata = mock.MagicMock( side_effect=DiskFileNotExist) object_server.ObjectController.remove_trans_id = mock.MagicMock() object_server.ObjectController.release_lock = mock.MagicMock() timestamp = normalize_timestamp(time()) req = Request.blank('/fs1/a1/d1/o1/a/c/o', environ={ 'REQUEST_METHOD': 'POST ', 'HTTP_X_GLOBAL_MAP_VERSION': 4.0 }, headers={ 'X-Timestamp': timestamp, 'X-Object-Meta-1': 'One', 'X-Object-Meta-2': 'Two', 'Content-Type': 'text/plain', 'X-Container-Host': '1.2.3:0000' }) resp = req.get_response(self.object_controller) self.assertEqual(resp.status_int, 404) object_server.ObjectController.remove_trans_id.assert_called_once_with( "fd2d8dafa0570422b397941531342ca4/6eff33b3ff33852f4cfb514aaa7f5199/d1c91d91068a170a6bcf220efe77711d", "123") self.assertTrue(object_server.ObjectController.release_lock.called) self.assertFalse( self.lock_mgr_obj.is_exists( 'fd2d8dafa0570422b397941531342ca4/6eff33b3ff33852f4cfb514aaa7f5199/d1c91d91068a170a6bcf220efe77711d' ))
def test_stripping_swift_admin_headers(self): # Verify that a GET/HEAD which receives privileged headers from the # account server will strip those headers for non-swift_owners hdrs_ext, hdrs_int = self._make_user_and_sys_acl_headers_data() headers = { 'x-account-meta-harmless': 'hi mom', 'x-account-meta-temp-url-key': 's3kr1t', } controller = proxy_server.AccountController(self.app, 'acct') for verb in ('GET', 'HEAD'): for env in ({'swift_owner': True}, {'swift_owner': False}): req = Request.blank('/v1/acct', environ=env) controller.GETorHEAD_base = lambda *_: Response( headers=headers, environ={ 'PATH_INFO': '/acct', 'REQUEST_METHOD': verb, }) method = getattr(controller, verb) resp = method(req) self.assertEqual(resp.headers.get('x-account-meta-harmless'), 'hi mom') privileged_header_present = ('x-account-meta-temp-url-key' in resp.headers) self.assertEqual(privileged_header_present, env['swift_owner'])
def __call__(self, env, start_response): req = Request(env) try: version, account, container, obj = req.split_path(1, 4, True) except ValueError: return self.app(env, start_response) def iter_response(iterable): key = account iterator = iter(iterable) try: chunk = iterator.next() while not chunk: chunk = iterator.next() except StopIteration: chunk = '' try: while chunk: self.cache.inc_read_bytes(key, delta=len(chunk)) yield chunk current_time = time.time() self.cache.reset_bytes(current_time) if self.cache.get_read_bytes( key) >= self.cache.get_read_limit(key): self.logger.debug('Going to sleep for %s seconds '\ 'Key:%s, read_bytes:%s, read_limit:%s '\ %(self.cache.next_reset_time - current_time,\ key, self.cache.get_read_bytes(key),\ self.cache.get_read_limit(key))) _sleep(self.cache.next_reset_time - current_time) chunk = iterator.next() except GeneratorExit: # generator was closed before we finished raise finally: self.cache.dec_req_count(key) bw_control_resp = self.handle_bw_control(req, account, container, obj) if not bw_control_resp: return self.app(env, start_response) else: try: iterable = self.app(env, start_response) return iter_response(iterable) except Exception: raise return iter_response(iterable)
def test_get_object_info_no_env(self): req = Request.blank("/v1/account/cont/obj", environ={'swift.cache': FakeCache({})}) with patch('osd.proxyService.controllers.base.' '_prepare_pre_auth_info_request', FakeRequest): resp = get_object_info(req.environ, 'xxx') self.assertEquals(resp['length'], 5555) self.assertEquals(resp['type'], 'text/plain')
def test_get_account_info_env(self): cache_key = get_account_memcache_key("account") env_key = 'swift.%s' % cache_key req = Request.blank("/v1/account", environ={env_key: {'bytes': 3867}, 'swift.cache': FakeCache({})}) resp = get_account_info(req.environ, 'xxx') self.assertEquals(resp['bytes'], 3867)
def test_ZeroDivisionError_in_backend_requests(self): controller = proxy_server.ContainerController(self.app, 'AUTH_bob', 'test') Controller.account_info = mock.MagicMock(return_value=([{ 'ip': '1', 'port': '1' }], 'fs1', 'd1', 1)) req = Request.blank('/v1/a/c') controller._backend_requests = mock.MagicMock( side_effect=ZeroDivisionError) resp = controller.DELETE(req) self.assertEqual(resp.status_int, 500) req = Request.blank('/v1/a/c/o', headers={'content-length': '0'}) resp = controller.PUT(req) self.assertEqual(resp.status_int, 500)
def test_PUT(self): controller = proxy_server.AccountController(self.app, 'AUTH_bob') req = Request.blank('/v1/AUTH_bob', {}) resp = controller.PUT(req) self.assertEqual(resp.status_int, 405) self.app.allow_account_management = True req = Request.blank('/v1/AUTH_bob', {}) with mock.patch('osd.proxyService.controllers.account.check_metadata', return_value=HTTPBadRequest(request=req)): resp = controller.PUT(req) self.assertEqual(resp.status_int, 400) controller = proxy_server.AccountController(self.app, 'AUTH_bob' * 40) req = Request.blank('/v1/AUTH_bob', {}) resp = controller.PUT(req) self.assertEqual(resp.status_int, 400)
def test_get_account_info_no_cache(self): req = Request.blank("/v1/AUTH_account", environ={'swift.cache': FakeCache({})}) with patch('osd.proxyService.controllers.base.' '_prepare_pre_auth_info_request', FakeRequest): resp = get_account_info(req.environ, 'xxx') self.assertEquals(resp['bytes'], 6666) self.assertEquals(resp['total_object_count'], 1000)
def test_PUT_success_cases(self): controller = proxy_server.AccountController(self.app, 'AUTH_bob') self.app.allow_account_management = True with mock.patch('osd.proxyService.controllers.base.http_connect', fake_http_connect(200, body='')): req = Request.blank('/v1/AUTH_bob', {'PATH_INFO': '/v1/AUTH_bob'}) resp = controller.PUT(req) self.assertEqual(resp.status_int, 200)