Browse Source

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>
Lukas Bezdicka 2 years ago
parent
commit
8566ad549c
2 changed files with 19 additions and 1 deletions
  1. 5
    1
      os_collect_config/ec2.py
  2. 14
    0
      os_collect_config/tests/test_ec2.py

+ 5
- 1
os_collect_config/ec2.py View File

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

+ 14
- 0
os_collect_config/tests/test_ec2.py View File

@@ -133,6 +133,20 @@ class TestEc2(testtools.TestCase):
133 133
         self.assertRaises(exc.Ec2MetadataNotAvailable, collect_ec2.collect)
134 134
         self.assertIn('Forbidden', self.log.output)
135 135
 
136
+    @mock.patch.object(config_drive, 'config_drive')
137
+    def test_collect_ec2_invalid_cache(self, cd):
138
+        cd.return_value = None
139
+        collect.setup_conf()
140
+        cache_dir = self.useFixture(fixtures.TempDir())
141
+        self.addCleanup(cfg.CONF.reset)
142
+        cfg.CONF.set_override('cachedir', cache_dir.path)
143
+        ec2_path = os.path.join(cache_dir.path, 'ec2.json')
144
+        with open(ec2_path, 'w') as f:
145
+            f.write('')
146
+
147
+        ec2_md = ec2.Collector(requests_impl=FakeRequests).collect()
148
+        self.assertEqual([('ec2', META_DATA_RESOLVED)], ec2_md)
149
+
136 150
     @mock.patch.object(config_drive, 'config_drive')
137 151
     def test_collect_ec2_collected(self, cd):
138 152
         cd.return_value = None

Loading…
Cancel
Save