diff --git a/neutron/db/agents_db.py b/neutron/db/agents_db.py index 2e44da67775..70c7ab0680d 100644 --- a/neutron/db/agents_db.py +++ b/neutron/db/agents_db.py @@ -220,15 +220,18 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): def get_configuration_dict(self, agent_db): return self._get_dict(agent_db, 'configurations') - def _get_dict(self, agent_db, dict_name): + def _get_dict(self, agent_db, dict_name, ignore_missing=False): + json_value = None try: - conf = jsonutils.loads(getattr(agent_db, dict_name)) + json_value = getattr(agent_db, dict_name) + conf = jsonutils.loads(json_value) except Exception: - msg = _LW('Dictionary %(dict_name)s for agent %(agent_type)s on ' - 'host %(host)s is invalid.') - LOG.warning(msg, {'dict_name': dict_name, - 'agent_type': agent_db.agent_type, - 'host': agent_db.host}) + if json_value or not ignore_missing: + msg = _LW('Dictionary %(dict_name)s for agent %(agent_type)s ' + 'on host %(host)s is invalid.') + LOG.warning(msg, {'dict_name': dict_name, + 'agent_type': agent_db.agent_type, + 'host': agent_db.host}) conf = {} return conf @@ -249,7 +252,8 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): if k not in ['alive', 'configurations']) res['alive'] = not self.is_agent_down(res['heartbeat_timestamp']) res['configurations'] = self._get_dict(agent, 'configurations') - res['resource_versions'] = self._get_dict(agent, 'resource_versions') + res['resource_versions'] = self._get_dict(agent, 'resource_versions', + ignore_missing=True) res['availability_zone'] = agent['availability_zone'] return self._fields(res, fields) @@ -371,7 +375,7 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): # _update_local_agent_resource_versions() will call # version_manager and bring it up to date agent_state['resource_versions'] = self._get_dict( - agent_db, 'resource_versions') + agent_db, 'resource_versions', ignore_missing=True) res['heartbeat_timestamp'] = current_time if agent_state.get('start_flag'): res['started_at'] = current_time diff --git a/neutron/tests/unit/db/test_agents_db.py b/neutron/tests/unit/db/test_agents_db.py index 3e30c0eae0f..f6f3cba99d5 100644 --- a/neutron/tests/unit/db/test_agents_db.py +++ b/neutron/tests/unit/db/test_agents_db.py @@ -196,12 +196,33 @@ class TestAgentsDbMixin(TestAgentsDbBase): " DHCP Agent 2015-05-06 22:40:40.432295 some.node"} ) - def test_get_dict(self): + def test__get_dict(self): db_obj = mock.Mock(conf1='{"test": "1234"}') conf1 = self.plugin._get_dict(db_obj, 'conf1') self.assertIn('test', conf1) self.assertEqual("1234", conf1['test']) + def test__get_dict_missing(self): + with mock.patch.object(agents_db.LOG, 'warning') as warn: + db_obj = mock.Mock(spec=['agent_type', 'host']) + self.plugin._get_dict(db_obj, 'missing_conf') + self.assertTrue(warn.called) + + def test__get_dict_ignore_missing(self): + with mock.patch.object(agents_db.LOG, 'warning') as warn: + db_obj = mock.Mock(spec=['agent_type', 'host']) + missing_conf = self.plugin._get_dict(db_obj, 'missing_conf', + ignore_missing=True) + self.assertEqual({}, missing_conf) + warn.assert_not_called() + + def test__get_dict_broken(self): + with mock.patch.object(agents_db.LOG, 'warning') as warn: + db_obj = mock.Mock(conf1='{"test": BROKEN') + conf1 = self.plugin._get_dict(db_obj, 'conf1', ignore_missing=True) + self.assertEqual({}, conf1) + self.assertTrue(warn.called) + def get_configurations_dict(self): db_obj = mock.Mock(configurations='{"cfg1": "val1"}') cfg = self.plugin.get_configuration_dict(db_obj)