@@ -318,23 +318,35 @@ class TesNtpCfgd(TestCase):
318
318
Test hostcfd daemon - NtpCfgd
319
319
"""
320
320
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
+ }
322
324
MockConfigDb .CONFIG_DB ['NTP_SERVER' ] = {'0.debian.pool.ntp.org' : {}}
325
+ MockConfigDb .CONFIG_DB ['NTP_KEY' ] = {'42' : {'value' : 'theanswer' }}
323
326
324
327
def tearDown (self ):
325
328
MockConfigDb .CONFIG_DB = {}
326
329
327
- def test_ntp_global_update_with_no_servers (self ):
330
+ def test_ntp_update_ntp_keys (self ):
328
331
with mock .patch ('hostcfgd.subprocess' ) as mocked_subprocess :
329
332
popen_mock = mock .Mock ()
330
333
attrs = {'communicate.return_value' : ('output' , 'error' )}
331
334
popen_mock .configure_mock (** attrs )
332
335
mocked_subprocess .Popen .return_value = popen_mock
333
336
334
337
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
+ ])
338
350
339
351
def test_ntp_global_update_ntp_servers (self ):
340
352
with mock .patch ('hostcfgd.subprocess' ) as mocked_subprocess :
@@ -344,9 +356,37 @@ def test_ntp_global_update_ntp_servers(self):
344
356
mocked_subprocess .Popen .return_value = popen_mock
345
357
346
358
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 ()
350
390
351
391
def test_loopback_update (self ):
352
392
with mock .patch ('hostcfgd.subprocess' ) as mocked_subprocess :
@@ -356,11 +396,13 @@ def test_loopback_update(self):
356
396
mocked_subprocess .Popen .return_value = popen_mock
357
397
358
398
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': {}}
361
401
362
402
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
+ ])
364
406
365
407
366
408
class TestHostcfgdDaemon (TestCase ):
@@ -541,7 +583,7 @@ def test_loopback_events(self):
541
583
daemon .start ()
542
584
except TimeoutError :
543
585
pass
544
- expected = [call (['systemctl ' , 'restart ' , 'ntp-config ' ]),
586
+ expected = [call (['service ' , 'ntp-config ' , 'restart ' ]),
545
587
call (['iptables' , '-t' , 'mangle' , '--append' , 'PREROUTING' , '-p' , 'tcp' , '--tcp-flags' , 'SYN' , 'SYN' , '-d' , '10.184.8.233' , '-j' , 'TCPMSS' , '--set-mss' , '1460' ]),
546
588
call (['iptables' , '-t' , 'mangle' , '--append' , 'POSTROUTING' , '-p' , 'tcp' , '--tcp-flags' , 'SYN' , 'SYN' , '-s' , '10.184.8.233' , '-j' , 'TCPMSS' , '--set-mss' , '1460' ])]
547
589
mocked_subprocess .check_call .assert_has_calls (expected , any_order = True )
0 commit comments