1
# Copyright 2014 Microsoft Corporation
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
15
# Requires Python 2.4+ and Openssl 1.0+
17
# Implements parts of RFC 2131, 1541, 1497 and
18
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
19
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx
21
from tests.tools import *
22
from azurelinuxagent.distro.loader import get_distro
23
from azurelinuxagent.exception import *
24
from azurelinuxagent.distro.default.daemon import *
26
class MockDaemonCall(object):
27
def __init__(self, daemon_handler, count):
28
self.daemon_handler = daemon_handler
31
def __call__(self, *args, **kw):
32
self.count = self.count - 1
33
#Stop daemon after restarting for n times
35
self.daemon_handler.running = False
36
raise Exception("Mock unhandled exception")
39
class TestDaemon(AgentTestCase):
40
def test_daemon_restart(self, mock_sleep):
42
mock_daemon = Mock(side_effect=MockDaemonCall(distro.daemon_handler, 2))
43
distro.daemon_handler.daemon = mock_daemon
44
distro.daemon_handler.check_pid = Mock()
45
distro.daemon_handler.run()
47
mock_sleep.assert_any_call(15)
48
self.assertEquals(2, distro.daemon_handler.daemon.call_count)
50
@patch("azurelinuxagent.distro.default.daemon.conf")
51
@patch("azurelinuxagent.distro.default.daemon.sys.exit")
52
def test_check_pid(self, mock_exit, mock_conf, mock_sleep):
54
mock_pid_file = os.path.join(self.tmp_dir, "pid")
55
mock_conf.get_agent_pid_file_path = Mock(return_value=mock_pid_file)
57
distro.daemon_handler.check_pid()
58
self.assertTrue(os.path.isfile(mock_pid_file))
60
distro.daemon_handler.check_pid()
61
mock_exit.assert_any_call(0)
63
if __name__ == '__main__':