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>
(cherry picked from commit 8566ad549c
)
This commit is contained in:
parent
15c86a7bd9
commit
17b2b244e1
@ -68,7 +68,11 @@ class Collector(object):
|
|||||||
cache_path = cache.get_path('ec2')
|
cache_path = cache.get_path('ec2')
|
||||||
if os.path.exists(cache_path):
|
if os.path.exists(cache_path):
|
||||||
with open(cache_path) as f:
|
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:
|
if metadata:
|
||||||
return [('ec2', metadata)]
|
return [('ec2', metadata)]
|
||||||
|
|
||||||
|
@ -133,6 +133,20 @@ class TestEc2(testtools.TestCase):
|
|||||||
self.assertRaises(exc.Ec2MetadataNotAvailable, collect_ec2.collect)
|
self.assertRaises(exc.Ec2MetadataNotAvailable, collect_ec2.collect)
|
||||||
self.assertIn('Forbidden', self.log.output)
|
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')
|
@mock.patch.object(config_drive, 'config_drive')
|
||||||
def test_collect_ec2_collected(self, cd):
|
def test_collect_ec2_collected(self, cd):
|
||||||
cd.return_value = None
|
cd.return_value = None
|
||||||
|
Loading…
Reference in New Issue
Block a user