class VarnishTests(unittest.TestCase): def setUp(self): self.manager = VarnishManager((ADDR, )) def test_ping(self): result = self.manager.run('ping')[0][0] self.assertEqual(len(result), 2) self.assertTrue([isinstance(x, float) for x in (True, True)]) def test_purge(self): resp = self.manager.run('purge.url', 'http://%s/myrandomurl/.*' % WEB_ADDR)[0][0] self.assertEqual(resp.status, 200) def test_ban(self): regex = '^/banned/*' self.manager.run('ban.url', regex) self.assertTrue(regex, str(self.manager.run('ban.list'))) def test_multiple(self): result = self.manager.run((('ping', None), ('ping', None))) self.assertEqual(result[0][0], result[0][1]) def tearDown(self): self.manager.close()
class VarnishTests(unittest.TestCase): def setUp(self): self.manager = VarnishManager((ADDR,)) def test_ping(self): result = self.manager.run('ping')[0][0] self.assertEqual(len(result), 2) self.assert_(map(lambda x: isinstance(x, float), (True,True))) def test_purge(self): resp = self.manager.run( 'purge.url', 'http://%s/myrandomurl/.*' % WEB_ADDR)[0][0] self.assertEqual(resp.status, 200) def test_ban(self): regex = '^/banned/*' self.manager.run('ban.url', regex) self.assert_(regex, str(self.manager.run('ban.list'))) def test_multiple(self): result = self.manager.run(( ('ping',None),('ping',None) )) self.assertEqual(result[0][0], result[0][1]) def tearDown(self): self.manager.close()
def setUp(self): self.manager = VarnishManager((ADDR, ))
def setUp(self): self.manager = VarnishManager((ADDR,))
from varnishadm import VarnishManager from urllib.parse import urlparse from http.client import HTTPConnection from sefaria.local_settings import VARNISH_ADM_ADDR, VARNISH_HOST, VARNISH_FRNT_PORT, VARNISH_SECRET, FRONT_END_URL from sefaria.utils.util import graceful_exception import logging logger = logging.getLogger(__name__) with open (VARNISH_SECRET, "r") as sfile: secret=sfile.read().replace('\n', '') manager = VarnishManager((VARNISH_ADM_ADDR,), secret) # PyPi version of python-varnish has broken purge function. We use this instead. # Derived from https://github.com/justquick/python-varnish/blob/master/varnish.py @graceful_exception(logger=logger, return_value=None) def purge_url(url): """ Do an HTTP PURGE of the given asset. The URL is run through urlparse and must point to the varnish instance not the varnishadm """ url = urlparse(url) connection = HTTPConnection(VARNISH_HOST, VARNISH_FRNT_PORT) path = url.path or '/' connection.request('PURGE', '%s?%s' % (path, url.query) if url.query else path, '', {'Host': url.hostname}) response = connection.getresponse() if response.status != 200: