Skip to content

Commit

Permalink
create cache folder if nonexistant (#23)
Browse files Browse the repository at this point in the history
* create cache folder if nonexistant

* removing test_load
  • Loading branch information
fdosani authored and kapilt committed Apr 25, 2016
1 parent 5910ffe commit e672eb5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
24 changes: 16 additions & 8 deletions c7n/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
def factory(config):
if not config:
return NullCache(None)

if not config.cache or not config.cache_period:
log.info("Disabling cache")
log.info("Disabling cache")
return NullCache(config)

return FileCacheManager(config)


Expand All @@ -45,11 +45,11 @@ def load(self):

def get(self, key):
pass

def save(self, key, data):
pass


class FileCacheManager(object):

def __init__(self, config):
Expand All @@ -64,7 +64,7 @@ def __init__(self, config):
def get(self, key):
k = cPickle.dumps(key)
return self.data.get(k)

def load(self):
if os.path.isfile(self.cache_path):
if (time.time() - os.stat(self.cache_path).st_mtime >
Expand All @@ -74,7 +74,7 @@ def load(self):
self.data = cPickle.load(fh)
log.info("Using cache file %s" % self.cache_path)
return True

def save(self, key, data):
try:
with open(self.cache_path, 'w') as fh:
Expand All @@ -83,3 +83,11 @@ def save(self, key, data):
except Exception as e:
log.warning("Could not save cache %s err: %s" % (
self.cache_path, e))
if not os.path.exists(self.cache_path):
directory = os.path.dirname(self.cache_path)
log.info('Generating Cache directory: %s.' % directory)
try:
os.makedirs(directory)
except Exception as e:
log.warning("Could not create directory: %s err: %s" % (
directory, e))
78 changes: 78 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,81 @@

from unittest import TestCase
from c7n import cache
from argparse import Namespace
import cPickle
import tempfile
import mock


class FileCacheManagerTest(TestCase):
def setUp(self):
self.test_config = Namespace(cache_period=60, cache='test-cloud-custodian.cache')
self.test_cache = cache.FileCacheManager(self.test_config)
self.test_key = 'test'
self.bad_key = 'bad'
self.test_value = [1, 2, 3]


def test_get(self):
#mock the pick and set it to the data variable
test_pickle = cPickle.dumps({cPickle.dumps(self.test_key): self.test_value}, protocol=2)
self.test_cache.data = cPickle.loads(test_pickle)

#assert
self.assertEquals(self.test_cache.get(self.test_key), self.test_value)
self.assertEquals(self.test_cache.get(self.bad_key), None)



@mock.patch.object(cache.os, 'makedirs')
@mock.patch.object(cache.os.path, 'exists')
@mock.patch.object(cache.cPickle, 'dump')
@mock.patch.object(cache.cPickle, 'dumps')
def test_save_exists(self, mock_dumps, mock_dump, mock_exists, mock_mkdir):
#path exists then we dont need to create the folder
mock_exists.return_value = True
#tempfile to hold the pickle
temp_cache_file = tempfile.NamedTemporaryFile()
self.test_cache.cache_path = temp_cache_file.name
#make the call
self.test_cache.save(self.test_key, self.test_value)

#assert if file already exists
self.assertFalse(mock_mkdir.called)
self.assertTrue(mock_dumps.called)
self.assertTrue(mock_dump.called)

#mkdir should NOT be called, but pickles should
self.assertEquals(mock_mkdir.call_count,0)
self.assertEquals(mock_dump.call_count,1)
self.assertEquals(mock_dumps.call_count,1)




@mock.patch.object(cache.os, 'makedirs')
@mock.patch.object(cache.os.path, 'exists')
@mock.patch.object(cache.cPickle, 'dump')
@mock.patch.object(cache.cPickle, 'dumps')
def test_save_doesnt_exists(self, mock_dumps, mock_dump, mock_exists, mock_mkdir):

temp_cache_file = tempfile.NamedTemporaryFile()
self.test_cache.cache_path = temp_cache_file.name

#path doesnt exists then we will create the folder
#raise some sort of exception in the try
mock_exists.return_value = False
mock_dump.side_effect = Exception('Error')

#make the call
self.test_cache.save(self.test_key, self.test_value)

#assert if file doesnt exists
self.assertTrue(mock_mkdir.called)
self.assertTrue(mock_dumps.called)
self.assertTrue(mock_dump.called)

#all 3 should be called once
self.assertEquals(mock_mkdir.call_count,1)
self.assertEquals(mock_dump.call_count,1)
self.assertEquals(mock_dumps.call_count,1)

0 comments on commit e672eb5

Please sign in to comment.