def test_ntp_common(self, logger, wait, time): remote = Remote() ntp_init = ntp.NtpInitscript(remote) remote.reset_mock() result = ntp_init.set_actual_time() self.assertTrue(result) self.assertTrue(ntp_init.is_synchronized) wait.assert_called_once() remote.execute.assert_called_once_with("hwclock -w") wait.reset_mock() logger.reset_mock() debug = mock.Mock() logger.attach_mock(debug, 'debug') wait.side_effect = error.TimeoutError('E') result = ntp_init.set_actual_time() self.assertFalse(result) self.assertFalse(ntp_init.is_synchronized) debug.assert_called_once_with('Time sync failed with E') result = ntp_init.wait_peer(timeout=-1) self.assertFalse(result) self.assertFalse(ntp_init.is_connected) time.assert_has_calls((mock.call(), mock.call()))
def test_wait_peer(self): ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') ntp_init.wait_peer() self.wait_mock.assert_called_once_with( ntp_init._get_sync_complete, interval=8, timeout=600, timeout_msg="Failed to wait peer on node 'node'")
def test_set_actual_time(self): self.remote_mock.execute.side_effect = ( self.make_exec_result('/etc/init.d/ntp'), self.make_exec_result('server1.com'), self.make_exec_result(''), ) ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') ntp_init.set_actual_time() self.wait_mock.assert_called_once_with( mock.ANY, timeout=600, timeout_msg="Failed to set actual time on node 'node'") waiter = self.wait_mock.call_args[0][0] assert waiter() is True self.remote_mock.execute.assert_has_calls(( mock.call( "find /etc/init.d/ -regex '/etc/init.d/ntp.?' -executable"), mock.call("awk '/^server/ && $2 !~ /^127\./ {print $2}' " "/etc/ntp.conf"), mock.call('ntpdate -p 4 -t 0.2 -bu server1.com'), )) self.remote_mock.check_call.assert_called_once_with('hwclock -w')
def test_stop(self): self.remote_mock.check_call.return_value = self.make_exec_result('') ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') ntp_init.stop() self.remote_mock.check_call.assert_called_once_with( '/etc/init.d/ntp stop')
def test_init(self): ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') assert ntp_init.remote is self.remote_mock assert ntp_init.node_name == 'node' assert repr(ntp_init) == \ "NtpInitscript(remote=<SSHClient()>, node_name='node')" self.remote_mock.execute.assert_called_once_with( "find /etc/init.d/ -regex '/etc/init.d/ntp.?' -executable")
def test_date(self): self.remote_mock.execute.side_effect = ( self.make_exec_result('/etc/init.d/ntp'), self.make_exec_result('Thu May 26 13:35:43 MSK 2016'), ) ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') date = ntp_init.date self.remote_mock.execute.assert_has_calls(( mock.call( "find /etc/init.d/ -regex '/etc/init.d/ntp.?' -executable"), mock.call('date'), )) assert date == 'Thu May 26 13:35:43 MSK 2016'
def test_get_ntpq(self): self.remote_mock.execute.side_effect = ( self.make_exec_result('/etc/init.d/ntp'), self.make_exec_result('Line1\nLine2\nLine3\nLine4\n'), ) ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') peers = ntp_init._get_ntpq() self.remote_mock.execute.assert_has_calls(( mock.call( "find /etc/init.d/ -regex '/etc/init.d/ntp.?' -executable"), mock.call('ntpq -pn 127.0.0.1'), )) assert peers == ['Line3\n', 'Line4\n']
def test_get_sync_complete_false(self): self.remote_mock.execute.side_effect = ( self.make_exec_result('/etc/init.d/ntp'), self.make_exec_result("""\ remote refid st t when poll reach delay offset jitter ============================================================================== +95.213.132.250 195.210.189.106 2 u 8 64 377 40.263 -1.379 532.46 -87.229.205.75 212.51.144.44 2 u 16 64 377 31.288 -1.919 9.969 *31.131.249.26 46.46.152.214 2 u 34 64 1 40.522 -0.988 7.747 -217.65.8.75 195.3.254.2 3 u 26 64 377 28.758 -4.249 44.240 +91.189.94.4 138.96.64.10 2 u 24 64 377 83.284 -1.810 14.550 """)) ntp_init = ntp.NtpInitscript(self.remote_mock, 'node') assert ntp_init._get_sync_complete() is False
def test_ntp_init(self): remote = Remote() ntp_init = ntp.NtpInitscript(remote) self.check_shared(ntp_obj=ntp_init, remote=remote, pacemaker=False) remote.execute.assert_has_calls(( mock.call( "awk '/^server/ && $2 !~ /127.*/ {print $2}' /etc/ntp.conf"), mock.call("find /etc/init.d/ -regex '/etc/init.d/ntp.?'") )) self.assertEqual( str(ntp_init), 'NtpInitscript(remote=Remote, node_name=node, admin_ip=None)') remote.reset_mock() peers = ntp_init.peers self.assertEqual(peers, ['2', '3']) remote.execute.assert_called_once_with('ntpq -pn 127.0.0.1') remote.reset_mock() date = ntp_init.date self.assertEqual(date, return_value['stdout']) remote.execute.assert_called_once_with('date') remote.reset_mock() ntp_init.start() self.assertFalse(ntp_init.is_connected) remote.execute.assert_called_once_with('0 2 4 start') remote.reset_mock() ntp_init.stop() self.assertFalse(ntp_init.is_connected) remote.execute.assert_called_once_with('0 2 4 stop')