Merge "Use null backend when cache is disabled"

This commit is contained in:
Jenkins 2015-10-23 14:46:25 +00:00 committed by Gerrit Code Review
commit f0c47663f2
3 changed files with 51 additions and 27 deletions

View File

@ -25,11 +25,12 @@ LOG = logging.getLogger(__name__)
class Builder(object): class Builder(object):
def __init__(self, config): def __init__(self, config):
self.cache = Cache(
config.get('cache', 'cachedir'),
config.getboolean('cache', 'enabled'))
self.grafana = Grafana( self.grafana = Grafana(
config.get('grafana', 'apikey'), config.get('grafana', 'url')) config.get('grafana', 'apikey'), config.get('grafana', 'url'))
self.parser = YamlParser() self.parser = YamlParser()
self.cache_enabled = config.getboolean('cache', 'enabled')
self.cache = Cache(config.get('cache', 'cachedir'))
def delete_dashboard(self, path): def delete_dashboard(self, path):
self.load_files(path) self.load_files(path)
@ -58,7 +59,7 @@ class Builder(object):
LOG.info('Number of dashboards generated: %d', len(dashboards)) LOG.info('Number of dashboards generated: %d', len(dashboards))
for name in dashboards: for name in dashboards:
data, md5 = self.parser.get_dashboard(name) data, md5 = self.parser.get_dashboard(name)
if self.cache.has_changed(name, md5) or not self.cache_enabled: if self.cache.has_changed(name, md5):
self.grafana.dashboard.create(name, data, overwrite=True) self.grafana.dashboard.create(name, data, overwrite=True)
self.cache.set(name, md5) self.cache.set(name, md5)
else: else:

View File

@ -22,16 +22,19 @@ LOG = logging.getLogger(__name__)
class Cache(object): class Cache(object):
def __init__(self, cachedir): def __init__(self, cachedir, enabled=True):
cache_dir = self._get_cache_dir(cachedir) if enabled:
filename = os.path.join(cache_dir, 'cache.dbm') backend = 'dogpile.cache.dbm'
LOG.debug('Using cache: %s' % filename) cache_dir = self._get_cache_dir(cachedir)
self.region = make_region().configure( filename = os.path.join(cache_dir, 'cache.dbm')
'dogpile.cache.dbm', LOG.debug('Using cache: %s' % filename)
arguments={ arguments = {
'filename': filename, 'filename': filename,
} }
) else:
backend = 'dogpile.cache.null'
arguments = {}
self.region = make_region().configure(backend, arguments=arguments)
def get(self, title): def get(self, title):
res = self.region.get(title) res = self.region.get(title)

View File

@ -22,39 +22,59 @@ class TestCaseCache(TestCase):
'hello-world': '2095312189753de6ad47dfe20cbe97ec', 'hello-world': '2095312189753de6ad47dfe20cbe97ec',
} }
def setUp(self):
super(TestCaseCache, self).setUp()
cachedir = self.config.get('cache', 'cachedir')
self.storage = cache.Cache(cachedir)
def test_cache_has_changed(self): def test_cache_has_changed(self):
res = self.storage.has_changed( storage = cache.Cache(self.config.get('cache', 'cachedir'))
res = storage.has_changed(
'hello-world', self.dashboard['hello-world']) 'hello-world', self.dashboard['hello-world'])
self.assertTrue(res) self.assertTrue(res)
self.storage.set('hello-world', self.dashboard['hello-world']) storage.set('hello-world', self.dashboard['hello-world'])
res = self.storage.has_changed( res = storage.has_changed(
'hello-world', self.dashboard['hello-world']) 'hello-world', self.dashboard['hello-world'])
self.assertFalse(res) self.assertFalse(res)
def test_cache_disabled_has_changed(self):
storage = cache.Cache(self.config.get('cache', 'cachedir'), False)
res = storage.has_changed(
'hello-world', self.dashboard['hello-world'])
self.assertTrue(res)
# Set a second time and confirm cache has_changed is True.
storage.set('hello-world', self.dashboard['hello-world'])
res = storage.has_changed(
'hello-world', self.dashboard['hello-world'])
self.assertTrue(res)
def test_cache_get_empty(self): def test_cache_get_empty(self):
self.assertEqual(self.storage.get('empty'), None) storage = cache.Cache(self.config.get('cache', 'cachedir'))
self.assertEqual(storage.get('empty'), None)
def test_cache_disabled_get_empty(self):
storage = cache.Cache(self.config.get('cache', 'cachedir'), False)
self.assertEqual(storage.get('empty'), None)
def test_cache_set_multiple(self): def test_cache_set_multiple(self):
self.storage.set('hello-world', self.dashboard['hello-world']) storage = cache.Cache(self.config.get('cache', 'cachedir'))
storage.set('hello-world', self.dashboard['hello-world'])
self.assertEqual( self.assertEqual(
self.storage.get('hello-world'), self.dashboard['hello-world']) storage.get('hello-world'), self.dashboard['hello-world'])
dashboard = { dashboard = {
'foobar': '14758f1afd44c09b7992073ccf00b43d' 'foobar': '14758f1afd44c09b7992073ccf00b43d'
} }
dashboard['hello-world'] = self.dashboard['hello-world'] dashboard['hello-world'] = self.dashboard['hello-world']
self.storage.set('foobar', dashboard['foobar']) storage.set('foobar', dashboard['foobar'])
self.assertEqual(self.storage.get('foobar'), dashboard['foobar']) self.assertEqual(storage.get('foobar'), dashboard['foobar'])
# Make sure hello-world is still valid. # Make sure hello-world is still valid.
self.assertEqual( self.assertEqual(
self.storage.get('hello-world'), self.dashboard['hello-world']) storage.get('hello-world'), self.dashboard['hello-world'])
def test_cache_set_single(self): def test_cache_set_single(self):
self.storage.set('hello-world', self.dashboard['hello-world']) storage = cache.Cache(self.config.get('cache', 'cachedir'))
storage.set('hello-world', self.dashboard['hello-world'])
self.assertEqual( self.assertEqual(
self.storage.get('hello-world'), self.dashboard['hello-world']) storage.get('hello-world'), self.dashboard['hello-world'])
def test_cache_disabled_set_single(self):
storage = cache.Cache(self.config.get('cache', 'cachedir'), False)
storage.set('hello-world', self.dashboard['hello-world'])
# Make sure cache is empty
self.assertEqual(storage.get('hello-world'), None)