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+
18
from tests.tools import *
19
from azurelinuxagent.common.exception import *
20
from azurelinuxagent.daemon import *
22
class MockDaemonCall(object):
23
def __init__(self, daemon_handler, count):
24
self.daemon_handler = daemon_handler
27
def __call__(self, *args, **kw):
28
self.count = self.count - 1
29
#Stop daemon after restarting for n times
31
self.daemon_handler.running = False
32
raise Exception("Mock unhandled exception")
35
class TestDaemon(AgentTestCase):
36
def test_daemon_restart(self, mock_sleep):
38
daemon_handler = get_daemon_handler()
39
mock_daemon = Mock(side_effect=MockDaemonCall(daemon_handler, 2))
40
daemon_handler.daemon = mock_daemon
42
daemon_handler.check_pid = Mock()
46
mock_sleep.assert_any_call(15)
47
self.assertEquals(2, daemon_handler.daemon.call_count)
49
@patch("azurelinuxagent.daemon.main.conf")
50
@patch("azurelinuxagent.daemon.main.sys.exit")
51
def test_check_pid(self, mock_exit, mock_conf, mock_sleep):
52
daemon_handler = get_daemon_handler()
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
daemon_handler.check_pid()
58
self.assertTrue(os.path.isfile(mock_pid_file))
60
daemon_handler.check_pid()
61
mock_exit.assert_any_call(0)
63
if __name__ == '__main__':