Detect cache changes properly.
Also add explicit tests for caching.
This commit is contained in:
parent
48058f5a31
commit
f6de46c259
|
@ -45,33 +45,47 @@ def setup_conf():
|
|||
CONF.register_cli_opts(opts)
|
||||
|
||||
|
||||
def cache_path(name):
|
||||
return os.path.join(CONF.cachedir, '%s.json' % name)
|
||||
|
||||
|
||||
def cache(name, content):
|
||||
if not os.path.exists(CONF.cachedir):
|
||||
os.mkdir(CONF.cachedir)
|
||||
|
||||
changed = False
|
||||
dest_path = os.path.join(CONF.cachedir, '%s.json' % name)
|
||||
dest_path = cache_path(name)
|
||||
orig_path = '%s.orig' % dest_path
|
||||
last_path = '%s.last' % dest_path
|
||||
|
||||
with tempfile.NamedTemporaryFile(dir=CONF.cachedir, delete=False) as new:
|
||||
new.write(json.dumps(content, indent=1))
|
||||
new.flush()
|
||||
if not os.path.exists(orig_path):
|
||||
shutil.copy(new.name, orig_path)
|
||||
shutil.copy(new.name, last_path)
|
||||
changed = True
|
||||
os.rename(new.name, dest_path)
|
||||
|
||||
if not changed:
|
||||
with open(dest_path) as now:
|
||||
with open(orig_path) as then:
|
||||
if os.path.exists(last_path):
|
||||
with open(last_path) as then:
|
||||
for now_line in now:
|
||||
then_line = then.next()
|
||||
if then_line != now_line:
|
||||
changed = True
|
||||
break
|
||||
else:
|
||||
changed = True
|
||||
return (changed, dest_path)
|
||||
|
||||
|
||||
def commit_cache(name):
|
||||
dest_path = cache_path(name)
|
||||
shutil.copy(dest_path, '%s.last' % dest_path)
|
||||
|
||||
|
||||
def __main__():
|
||||
setup_conf()
|
||||
CONF(prog="os-collect-config")
|
||||
|
@ -85,6 +99,7 @@ def __main__():
|
|||
env = dict(os.environ)
|
||||
env["OS_CONFIG_FILES"] = ':'.join(paths)
|
||||
subprocess.call(CONF.command, env=env)
|
||||
commit_cache('ec2')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -75,3 +75,36 @@ class TestConf(testtools.TestCase):
|
|||
def test_setup_conf(self):
|
||||
collect.setup_conf()
|
||||
self.assertEquals('/var/run/os-collect-config', cfg.CONF.cachedir)
|
||||
|
||||
|
||||
class TestCache(testtools.TestCase):
|
||||
def setUp(self):
|
||||
super(TestCache, self).setUp()
|
||||
cfg.CONF.reset()
|
||||
|
||||
def tearDown(self):
|
||||
cfg.CONF.reset()
|
||||
super(TestCache, self).tearDown()
|
||||
|
||||
def test_cache(self):
|
||||
cache_root = self.useFixture(fixtures.TempDir())
|
||||
cache_dir = os.path.join(cache_root.path, 'cache')
|
||||
collect.setup_conf()
|
||||
cfg.CONF(['--cachedir', cache_dir])
|
||||
(changed, path) = collect.cache('foo', {'a': 1})
|
||||
self.assertTrue(changed)
|
||||
self.assertTrue(os.path.exists(cache_dir))
|
||||
self.assertTrue(os.path.exists(path))
|
||||
orig_path = '%s.orig' % path
|
||||
self.assertTrue(os.path.exists(orig_path))
|
||||
|
||||
(changed, path) = collect.cache('foo', {'a': 2})
|
||||
self.assertTrue(changed)
|
||||
orig_path = '%s.orig' % path
|
||||
with open(path) as now:
|
||||
with open(orig_path) as then:
|
||||
self.assertNotEquals(now.read(), then.read())
|
||||
|
||||
collect.commit_cache('foo')
|
||||
(changed, path) = collect.cache('foo', {'a': 2})
|
||||
self.assertFalse(changed)
|
||||
|
|
Loading…
Reference in New Issue