Don't fail on non-valid json in cache files

Resolves: rhbz#1438096
Closes-Bug: #1678328

Change-Id: Iee1bb4a7769568cfd3bad8b23c9dff440679ac16
Co-Authored-By: Alex Schultz <aschultz@redhat.com>
This commit is contained in:
Lukas Bezdicka 2017-03-13 14:20:39 +01:00 committed by Alex Schultz
parent 0762fcb0b5
commit 8566ad549c
2 changed files with 19 additions and 1 deletions

View File

@ -68,7 +68,11 @@ class Collector(object):
cache_path = cache.get_path('ec2')
if os.path.exists(cache_path):
with open(cache_path) as f:
metadata = json.load(f)
try:
metadata = json.load(f)
except ValueError as e:
log.getLogger(__name__).warn(e)
metadata = None
if metadata:
return [('ec2', metadata)]

View File

@ -133,6 +133,20 @@ class TestEc2(testtools.TestCase):
self.assertRaises(exc.Ec2MetadataNotAvailable, collect_ec2.collect)
self.assertIn('Forbidden', self.log.output)
@mock.patch.object(config_drive, 'config_drive')
def test_collect_ec2_invalid_cache(self, cd):
cd.return_value = None
collect.setup_conf()
cache_dir = self.useFixture(fixtures.TempDir())
self.addCleanup(cfg.CONF.reset)
cfg.CONF.set_override('cachedir', cache_dir.path)
ec2_path = os.path.join(cache_dir.path, 'ec2.json')
with open(ec2_path, 'w') as f:
f.write('')
ec2_md = ec2.Collector(requests_impl=FakeRequests).collect()
self.assertEqual([('ec2', META_DATA_RESOLVED)], ec2_md)
@mock.patch.object(config_drive, 'config_drive')
def test_collect_ec2_collected(self, cd):
cd.return_value = None