Skip to content

Commit

Permalink
Avoid printing message in error level when DEVICE_METADATA|localhost …
Browse files Browse the repository at this point in the history
…updates (#25)

There is error message logged on receiving `DEVICE_METADATA|localhost.hostname` update with the same value `DeviceMetaCfg.hostname`.
However, this can happen when other fields in `DEVICE_METADATA|localhost` are updated after `hostname` is recorded by `DeviceMetaCfg` because it always notifies of updates of all the fields together.
In this case, `INFO` is preferred.
  • Loading branch information
stephenxs authored Dec 1, 2022
1 parent 6c131c4 commit 4a2ef99
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
13 changes: 7 additions & 6 deletions scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,13 @@ class DeviceMetaCfg(object):

# Restart hostname-config service when hostname was changed.
# Empty not allowed
if new_hostname and new_hostname != self.hostname:
if not new_hostname:
syslog.syslog(syslog.LOG_ERR,
'Hostname was not updated: Empty not allowed')
elif new_hostname == self.hostname:
syslog.syslog(syslog.LOG_INFO,
'Hostname was not updated: Already set up with the same name: {}'.format(self.hostname))
else:
syslog.syslog(syslog.LOG_INFO, 'DeviceMetaCfg: Set new hostname: {}'
.format(new_hostname))
self.hostname = new_hostname
Expand All @@ -1367,11 +1373,6 @@ class DeviceMetaCfg(object):
return

run_cmd('sudo monit reload')
else:
msg = 'Hostname was not updated: '
msg += 'Already set up' if new_hostname else 'Empty not allowed'
syslog.syslog(syslog.LOG_ERR, msg)


class MgmtIfaceCfg(object):
"""
Expand Down
31 changes: 31 additions & 0 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,37 @@ def test_devicemeta_event(self):
mocked_subprocess.check_call.assert_has_calls(expected,
any_order=True)

# Mock empty name
HOSTCFG_DAEMON_CFG_DB["DEVICE_METADATA"]["localhost"]["hostname"] = ""
original_syslog = hostcfgd.syslog
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
with mock.patch('hostcfgd.syslog') as mocked_syslog:
mocked_syslog.LOG_ERR = original_syslog.LOG_ERR
try:
daemon.start()
except TimeoutError:
pass

expected = [
call(original_syslog.LOG_ERR, 'Hostname was not updated: Empty not allowed')
]
mocked_syslog.syslog.assert_has_calls(expected)

daemon.devmetacfg.hostname = "SameHostName"
HOSTCFG_DAEMON_CFG_DB["DEVICE_METADATA"]["localhost"]["hostname"] = daemon.devmetacfg.hostname
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
with mock.patch('hostcfgd.syslog') as mocked_syslog:
mocked_syslog.LOG_INFO = original_syslog.LOG_INFO
try:
daemon.start()
except TimeoutError:
pass

expected = [
call(original_syslog.LOG_INFO, 'Hostname was not updated: Already set up with the same name: SameHostName')
]
mocked_syslog.syslog.assert_has_calls(expected)

def test_mgmtiface_event(self):
"""
Test handling mgmt events.
Expand Down

0 comments on commit 4a2ef99

Please # to comment.