Skip to content

Commit c2a05e8

Browse files
committed
Add hostcfgd tests
Signed-off-by: Yevhen Fastiuk <[email protected]>
1 parent 12c01dc commit c2a05e8

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

tests/hostcfgd/hostcfgd_test.py

+54-12
Original file line numberDiff line numberDiff line change
@@ -318,23 +318,35 @@ class TesNtpCfgd(TestCase):
318318
Test hostcfd daemon - NtpCfgd
319319
"""
320320
def setUp(self):
321-
MockConfigDb.CONFIG_DB['NTP'] = {'global': {'vrf': 'mgmt', 'src_intf': 'eth0'}}
321+
MockConfigDb.CONFIG_DB['NTP'] = {
322+
'global': {'vrf': 'mgmt', 'src_intf': 'eth0'}
323+
}
322324
MockConfigDb.CONFIG_DB['NTP_SERVER'] = {'0.debian.pool.ntp.org': {}}
325+
MockConfigDb.CONFIG_DB['NTP_KEY'] = {'42': {'value': 'theanswer'}}
323326

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

327-
def test_ntp_global_update_with_no_servers(self):
330+
def test_ntp_update_ntp_keys(self):
328331
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
329332
popen_mock = mock.Mock()
330333
attrs = {'communicate.return_value': ('output', 'error')}
331334
popen_mock.configure_mock(**attrs)
332335
mocked_subprocess.Popen.return_value = popen_mock
333336

334337
ntpcfgd = hostcfgd.NtpCfg()
335-
ntpcfgd.ntp_global_update('global', MockConfigDb.CONFIG_DB['NTP']['global'])
336-
337-
mocked_subprocess.check_call.assert_not_called()
338+
ntpcfgd.ntp_global_update(
339+
'global', MockConfigDb.CONFIG_DB['NTP']['global'])
340+
mocked_subprocess.check_call.assert_has_calls([
341+
call(['service', 'ntp-config', 'restart']),
342+
call(['service', 'ntp', 'restart'])
343+
])
344+
345+
mocked_subprocess.check_call.reset_mock()
346+
ntpcfgd.ntp_srv_key_update({}, MockConfigDb.CONFIG_DB['NTP_KEY'])
347+
mocked_subprocess.check_call.assert_has_calls([
348+
call(['service', 'ntp-config', 'restart'])
349+
])
338350

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

346358
ntpcfgd = hostcfgd.NtpCfg()
347-
ntpcfgd.ntp_global_update('global', MockConfigDb.CONFIG_DB['NTP']['global'])
348-
ntpcfgd.ntp_server_update('0.debian.pool.ntp.org', 'SET')
349-
mocked_subprocess.check_call.assert_has_calls([call(['systemctl', 'restart', 'ntp-config'])])
359+
ntpcfgd.ntp_global_update(
360+
'global', MockConfigDb.CONFIG_DB['NTP']['global'])
361+
mocked_subprocess.check_call.assert_has_calls([
362+
call(['service', 'ntp-config', 'restart']),
363+
call(['service', 'ntp', 'restart'])
364+
])
365+
366+
mocked_subprocess.check_call.reset_mock()
367+
ntpcfgd.ntp_srv_key_update({'0.debian.pool.ntp.org': {}}, {})
368+
mocked_subprocess.check_call.assert_has_calls([
369+
call(['service', 'ntp-config', 'restart'])
370+
])
371+
372+
def test_ntp_is_caching_config(self):
373+
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
374+
popen_mock = mock.Mock()
375+
attrs = {'communicate.return_value': ('output', 'error')}
376+
popen_mock.configure_mock(**attrs)
377+
mocked_subprocess.Popen.return_value = popen_mock
378+
379+
ntpcfgd = hostcfgd.NtpCfg()
380+
ntpcfgd.cache['global'] = MockConfigDb.CONFIG_DB['NTP']['global']
381+
ntpcfgd.cache['servers'] = MockConfigDb.CONFIG_DB['NTP_SERVER']
382+
ntpcfgd.cache['keys'] = MockConfigDb.CONFIG_DB['NTP_KEY']
383+
384+
ntpcfgd.ntp_global_update(
385+
'global', MockConfigDb.CONFIG_DB['NTP']['global'])
386+
ntpcfgd.ntp_srv_key_update(MockConfigDb.CONFIG_DB['NTP_SERVER'],
387+
MockConfigDb.CONFIG_DB['NTP_KEY'])
388+
389+
mocked_subprocess.check_call.assert_not_called()
350390

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

358398
ntpcfgd = hostcfgd.NtpCfg()
359-
ntpcfgd.ntp_global = MockConfigDb.CONFIG_DB['NTP']['global']
360-
ntpcfgd.ntp_servers.add('0.debian.pool.ntp.org')
399+
ntpcfgd.cache['global'] = MockConfigDb.CONFIG_DB['NTP']['global']
400+
ntpcfgd.cache['servers'] = {'0.debian.pool.ntp.org': {}}
361401

362402
ntpcfgd.handle_ntp_source_intf_chg('eth0')
363-
mocked_subprocess.check_call.assert_has_calls([call(['systemctl', 'restart', 'ntp-config'])])
403+
mocked_subprocess.check_call.assert_has_calls([
404+
call(['service', 'ntp-config', 'restart'])
405+
])
364406

365407

366408
class TestHostcfgdDaemon(TestCase):
@@ -541,7 +583,7 @@ def test_loopback_events(self):
541583
daemon.start()
542584
except TimeoutError:
543585
pass
544-
expected = [call(['systemctl', 'restart', 'ntp-config']),
586+
expected = [call(['service', 'ntp-config', 'restart']),
545587
call(['iptables', '-t', 'mangle', '--append', 'PREROUTING', '-p', 'tcp', '--tcp-flags', 'SYN', 'SYN', '-d', '10.184.8.233', '-j', 'TCPMSS', '--set-mss', '1460']),
546588
call(['iptables', '-t', 'mangle', '--append', 'POSTROUTING', '-p', 'tcp', '--tcp-flags', 'SYN', 'SYN', '-s', '10.184.8.233', '-j', 'TCPMSS', '--set-mss', '1460'])]
547589
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)

tests/hostcfgd/test_vectors.py

+11
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,17 @@
12431243
"NTP_SERVER": {
12441244
"0.debian.pool.ntp.org": {}
12451245
},
1246+
"NTP_KEY": {
1247+
"1": {
1248+
"value": "blahblah",
1249+
"type": "md5"
1250+
},
1251+
"42": {
1252+
"value": "theanswer",
1253+
"type": "md5",
1254+
"trusted": "yes"
1255+
}
1256+
},
12461257
"LOOPBACK_INTERFACE": {
12471258
"Loopback0|10.184.8.233/32": {
12481259
"scope": "global",

0 commit comments

Comments
 (0)