Skip to content

Commit

Permalink
Add hostcfgd tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Fastiuk <yfastiuk@nvidia.com>
  • Loading branch information
fastiuk committed Jun 21, 2023
1 parent 18ebd7b commit 8141e90
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
66 changes: 54 additions & 12 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,23 +318,35 @@ class TesNtpCfgd(TestCase):
Test hostcfd daemon - NtpCfgd
"""
def setUp(self):
MockConfigDb.CONFIG_DB['NTP'] = {'global': {'vrf': 'mgmt', 'src_intf': 'eth0'}}
MockConfigDb.CONFIG_DB['NTP'] = {
'global': {'vrf': 'mgmt', 'src_intf': 'eth0'}
}
MockConfigDb.CONFIG_DB['NTP_SERVER'] = {'0.debian.pool.ntp.org': {}}
MockConfigDb.CONFIG_DB['NTP_KEY'] = {'42': {'value': 'theanswer'}}

def tearDown(self):
MockConfigDb.CONFIG_DB = {}

def test_ntp_global_update_with_no_servers(self):
def test_ntp_update_ntp_keys(self):
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
popen_mock = mock.Mock()
attrs = {'communicate.return_value': ('output', 'error')}
popen_mock.configure_mock(**attrs)
mocked_subprocess.Popen.return_value = popen_mock

ntpcfgd = hostcfgd.NtpCfg()
ntpcfgd.ntp_global_update('global', MockConfigDb.CONFIG_DB['NTP']['global'])

mocked_subprocess.check_call.assert_not_called()
ntpcfgd.ntp_global_update(
'global', MockConfigDb.CONFIG_DB['NTP']['global'])
mocked_subprocess.check_call.assert_has_calls([
call(['service', 'ntp-config', 'restart']),
call(['service', 'ntp', 'restart'])
])

mocked_subprocess.check_call.reset_mock()
ntpcfgd.ntp_srv_key_update({}, MockConfigDb.CONFIG_DB['NTP_KEY'])
mocked_subprocess.check_call.assert_has_calls([
call(['service', 'ntp-config', 'restart'])
])

def test_ntp_global_update_ntp_servers(self):
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
Expand All @@ -344,9 +356,37 @@ def test_ntp_global_update_ntp_servers(self):
mocked_subprocess.Popen.return_value = popen_mock

ntpcfgd = hostcfgd.NtpCfg()
ntpcfgd.ntp_global_update('global', MockConfigDb.CONFIG_DB['NTP']['global'])
ntpcfgd.ntp_server_update('0.debian.pool.ntp.org', 'SET')
mocked_subprocess.check_call.assert_has_calls([call(['systemctl', 'restart', 'ntp-config'])])
ntpcfgd.ntp_global_update(
'global', MockConfigDb.CONFIG_DB['NTP']['global'])
mocked_subprocess.check_call.assert_has_calls([
call(['service', 'ntp-config', 'restart']),
call(['service', 'ntp', 'restart'])
])

mocked_subprocess.check_call.reset_mock()
ntpcfgd.ntp_srv_key_update({'0.debian.pool.ntp.org': {}}, {})
mocked_subprocess.check_call.assert_has_calls([
call(['service', 'ntp-config', 'restart'])
])

def test_ntp_is_caching_config(self):
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
popen_mock = mock.Mock()
attrs = {'communicate.return_value': ('output', 'error')}
popen_mock.configure_mock(**attrs)
mocked_subprocess.Popen.return_value = popen_mock

ntpcfgd = hostcfgd.NtpCfg()
ntpcfgd.cache['global'] = MockConfigDb.CONFIG_DB['NTP']['global']
ntpcfgd.cache['servers'] = MockConfigDb.CONFIG_DB['NTP_SERVER']
ntpcfgd.cache['keys'] = MockConfigDb.CONFIG_DB['NTP_KEY']

ntpcfgd.ntp_global_update(
'global', MockConfigDb.CONFIG_DB['NTP']['global'])
ntpcfgd.ntp_srv_key_update(MockConfigDb.CONFIG_DB['NTP_SERVER'],
MockConfigDb.CONFIG_DB['NTP_KEY'])

mocked_subprocess.check_call.assert_not_called()

def test_loopback_update(self):
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
Expand All @@ -356,11 +396,13 @@ def test_loopback_update(self):
mocked_subprocess.Popen.return_value = popen_mock

ntpcfgd = hostcfgd.NtpCfg()
ntpcfgd.ntp_global = MockConfigDb.CONFIG_DB['NTP']['global']
ntpcfgd.ntp_servers.add('0.debian.pool.ntp.org')
ntpcfgd.cache['global'] = MockConfigDb.CONFIG_DB['NTP']['global']
ntpcfgd.cache['servers'] = {'0.debian.pool.ntp.org': {}}

ntpcfgd.handle_ntp_source_intf_chg('eth0')
mocked_subprocess.check_call.assert_has_calls([call(['systemctl', 'restart', 'ntp-config'])])
mocked_subprocess.check_call.assert_has_calls([
call(['service', 'ntp-config', 'restart'])
])


class TestHostcfgdDaemon(TestCase):
Expand Down Expand Up @@ -541,7 +583,7 @@ def test_loopback_events(self):
daemon.start()
except TimeoutError:
pass
expected = [call(['systemctl', 'restart', 'ntp-config']),
expected = [call(['service', 'ntp-config', 'restart']),
call(['iptables', '-t', 'mangle', '--append', 'PREROUTING', '-p', 'tcp', '--tcp-flags', 'SYN', 'SYN', '-d', '10.184.8.233', '-j', 'TCPMSS', '--set-mss', '1460']),
call(['iptables', '-t', 'mangle', '--append', 'POSTROUTING', '-p', 'tcp', '--tcp-flags', 'SYN', 'SYN', '-s', '10.184.8.233', '-j', 'TCPMSS', '--set-mss', '1460'])]
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)
Expand Down
11 changes: 11 additions & 0 deletions tests/hostcfgd/test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,17 @@
"NTP_SERVER": {
"0.debian.pool.ntp.org": {}
},
"NTP_KEY": {
"1": {
"value": "blahblah",
"type": "md5"
},
"42": {
"value": "theanswer",
"type": "md5",
"trusted": "yes"
}
},
"LOOPBACK_INTERFACE": {
"Loopback0|10.184.8.233/32": {
"scope": "global",
Expand Down

0 comments on commit 8141e90

Please sign in to comment.