def test_origin_db_valid_setup(self): fake_conf = FakeConf(data=['[sos]', 'origin_cdn_host_suffixes = origin_cdn.com']) test_origin = origin.filter_factory( {'sos_conf': fake_conf})(FakeApp()) resp = Request.blank('http://origin_db.com:8080/v1/acc/cont', environ={'REQUEST_METHOD': 'POST'}).get_response(test_origin) self.assertEquals(resp.status_int, 404) fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf})(FakeApp()) resp = Request.blank('http://origin_db.com:8080/v1/acc/cont', environ={'REQUEST_METHOD': 'POST'}).get_response(test_origin) self.assertEquals(resp.status_int, 500) fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 '''.split('\n')) factory = origin.filter_factory( {'sos_conf': fake_conf}) self.assertRaises(origin.InvalidConfiguration, factory, FakeApp())
def test_valid_setup(self): fake_conf = FakeConf(data=['[sos]']) test_origin = origin.filter_factory( {'sos_conf': fake_conf})(FakeApp()) self.assertFalse(test_origin._valid_setup()) fake_conf = FakeConf() test_origin = origin.filter_factory( {'sos_conf': fake_conf})(FakeApp()) self.assertTrue(test_origin._valid_setup())
def test_origin_db_delete_enabled(self): fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf}) test_origin = test_origin(FakeApp(iter([("204 No Content", {}, ""), ("204 No Content", {}, "")]))) req = Request.blank( "http://origin_db.com:8080/v1/acc/cont", environ={"REQUEST_METHOD": "DELETE"}, headers={"x-remove-cdn-container": "true"}, ) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 204) def mock_memcache(env): return FakeMemcache() was_memcache = utils.cache_from_env try: utils.cache_from_env = mock_memcache fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing delete_enabled = true """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf}) test_origin = test_origin(FakeApp(iter([("404 No Content", {}, ""), ("204 No Content", {}, "")]))) req = Request.blank("http://origin_db.com:8080/v1/acc/cont", environ={"REQUEST_METHOD": "DELETE"}) try: resp = req.get_response(test_origin) except Exception, e: self.assertEquals(str(e), "delete called") finally: utils.cache_from_env = was_memcache
def test_origin_db_delete_enabled(self): fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing delete_enabled = true '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf}) test_origin = test_origin(FakeApp(iter([ ('404 No Content', {}, ''), ('204 No Content', {}, '') ]))) req = Request.blank('http://origin_db.com:8080/v1/acc/cont', environ={'REQUEST_METHOD': 'DELETE',}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 204) def mock_memcache(env): return FakeMemcache() was_memcache = utils.cache_from_env try: utils.cache_from_env = mock_memcache fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing delete_enabled = true '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf}) test_origin = test_origin(FakeApp(iter([ ('404 No Content', {}, ''), ('204 No Content', {}, '') ]))) req = Request.blank('http://origin_db.com:8080/v1/acc/cont', environ={'REQUEST_METHOD': 'DELETE',}) try: resp = req.get_response(test_origin) except Exception, e: self.assertEquals(str(e), 'delete called') finally: utils.cache_from_env = was_memcache
def test_cdn_get_fail(self): prev_data = json.dumps({'account': 'acc', 'container': 'cont', 'ttl': 1234, 'logs_enabled': True, 'cdn_enabled': True}) self.test_origin.app = FakeApp(iter([ ('204 No Content', {}, prev_data), # call to _get_cdn_data ('500', {}, 'Failure.')])) #call to get obj req = Request.blank('http://1234.r3.origin_cdn.com:8080/obj1.jpg', environ={'REQUEST_METHOD': 'GET', 'swift.cdn_hash': 'abcd', 'swift.cdn_object_name': 'obj1.jpg'}) resp = req.get_response(self.test_origin) self.assertEquals(resp.status_int, 404) fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing [incoming_url_regex] regex_0 = ^http://origin_cdn\.com.*\/h(?P<cdn_hash>\w+)\/r\d+\/?(?P<object_name>(.+))?$ '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf}) test_origin = test_origin(FakeApp(iter([ ('204 No Content', {}, prev_data), # call to _get_cdn_data ('200 Ok', {'Content-Length': 14}, 'Test obj body.')]))) req = Request.blank('http://1234.r3.origin_cdn.com:8080/obj1.jpg', environ={'REQUEST_METHOD': 'GET', 'swift.cdn_hash': 'abcd', 'swift.cdn_object_name': 'obj1.jpg'}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 400) fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf}) test_origin = test_origin(FakeApp(iter([ ]))) req = Request.blank('http://1234.r3.origin_cdn.com:8080/obj1.jpg', environ={'REQUEST_METHOD': 'GET'}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 500)
def test_origin_db_valid_setup(self): fake_conf = FakeConf(data=["[sos]", "origin_cdn_host_suffixes = origin_cdn.com"]) test_origin = origin.filter_factory({"sos_conf": fake_conf})(FakeApp()) resp = Request.blank("http://origin_db.com:8080/v1/acc/cont", environ={"REQUEST_METHOD": "POST"}).get_response( test_origin ) self.assertEquals(resp.status_int, 404) fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf})(FakeApp()) resp = Request.blank("http://origin_db.com:8080/v1/acc/cont", environ={"REQUEST_METHOD": "POST"}).get_response( test_origin ) self.assertEquals(resp.status_int, 500) fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 """.split( "\n" ) ) factory = origin.filter_factory({"sos_conf": fake_conf}) self.assertRaises(origin.InvalidConfiguration, factory, FakeApp())
def test_origin_db_delete_disabled(self): fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_db_hosts = origin_db.com origin_cdn_host_suffixes = origin_cdn.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing delete_enabled = false '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf}) test_origin = test_origin(FakeApp(iter([]))) req = Request.blank('http://origin_db.com:8080/v1/acc/cont', environ={'REQUEST_METHOD': 'DELETE',}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 405)
def test_origin_db_fail_bad_config(self): fake_conf = FakeConf(data='''[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing '''.split('\n')) test_origin = origin.filter_factory( {'sos_conf': fake_conf}) listing_data = json.dumps([ {'name': 'test1', 'content_type': 'x-cdn/true1234-false'}, {'name': 'test2', 'content_type': 'x-cdn/true-2234-false'}]) test_origin = test_origin(FakeApp(iter([('200 Ok', {}, listing_data)]))) req = Request.blank('http://origin_db.com:8080/v1/acc/cont?format=JSON', environ={'REQUEST_METHOD': 'GET',}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 500)
def test_origin_db_delete_bad_request_second(self): fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing delete_enabled = true """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf}) test_origin = test_origin(FakeApp(iter([("204 No Content", {}, ""), ("500 Internal Server Error", {}, "")]))) req = Request.blank("http://origin_db.com:8080/v1/acc/cont", environ={"REQUEST_METHOD": "DELETE"}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 405)
def test_origin_db_fail_bad_config(self): fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf}) listing_data = json.dumps( [ {"name": "test1", "content_type": "x-cdn/true1234-false"}, {"name": "test2", "content_type": "x-cdn/true-2234-false"}, ] ) test_origin = test_origin(FakeApp(iter([("200 Ok", {}, listing_data)]))) req = Request.blank("http://origin_db.com:8080/v1/acc/cont?format=JSON", environ={"REQUEST_METHOD": "GET"}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 500)
def setUp(self): fake_conf = FakeConf() self.test_origin = origin.filter_factory( {'sos_conf': fake_conf})(FakeApp())
def test_cdn_get_fail(self): prev_data = json.dumps( {"account": "acc", "container": "cont", "ttl": 1234, "logs_enabled": True, "cdn_enabled": True} ) self.test_origin.app = FakeApp( iter([("204 No Content", {}, prev_data), (500, {}, "Failure.")]) # call to _get_cdn_data ) # call to get obj req = Request.blank( "http://1234.r3.origin_cdn.com:8080/obj1.jpg", environ={"REQUEST_METHOD": "GET", "swift.cdn_hash": "abcd", "swift.cdn_object_name": "obj1.jpg"}, ) resp = req.get_response(self.test_origin) self.assertEquals(resp.status_int, 404) fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing [incoming_url_regex] regex_0 = ^http://origin_cdn\.com.*\/h(?P<cdn_hash>\w+)\/r\d+\/?(?P<object_name>(.+))?$ """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf}) test_origin = test_origin( FakeApp( iter( [ ("204 No Content", {}, prev_data), # call to _get_cdn_data ("200 Ok", {"Content-Length": 14}, "Test obj body."), ] ) ) ) req = Request.blank( "http://1234.r3.origin_cdn.com:8080/obj1.jpg", environ={"REQUEST_METHOD": "GET", "swift.cdn_hash": "abcd", "swift.cdn_object_name": "obj1.jpg"}, ) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 400) fake_conf = FakeConf( data="""[sos] origin_admin_key = unittest origin_cdn_host_suffixes = origin_cdn.com origin_db_hosts = origin_db.com origin_account = .origin max_cdn_file_size = 0 hash_path_suffix = testing """.split( "\n" ) ) test_origin = origin.filter_factory({"sos_conf": fake_conf}) test_origin = test_origin(FakeApp(iter([]))) req = Request.blank("http://1234.r3.origin_cdn.com:8080/obj1.jpg", environ={"REQUEST_METHOD": "GET"}) resp = req.get_response(test_origin) self.assertEquals(resp.status_int, 500)
def setUp(self): fake_conf = FakeConf() self.test_origin = origin.filter_factory({"sos_conf": fake_conf, "register_info": False})(FakeApp())