~ubuntu-branches/debian/stretch/waagent/stretch

« back to all changes in this revision

Viewing changes to tests/daemon/test_daemon.py

  • Committer: Package Import Robot
  • Author(s): Bastian Blank
  • Date: 2016-08-24 16:48:22 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20160824164822-vdf8m5xy5gycm1cz
Tags: 2.1.6-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Microsoft Corporation
 
2
#
 
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
 
6
#
 
7
#     http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
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.
 
14
#
 
15
# Requires Python 2.4+ and Openssl 1.0+
 
16
#
 
17
 
 
18
from tests.tools import *
 
19
from azurelinuxagent.common.exception import *
 
20
from azurelinuxagent.daemon import *
 
21
 
 
22
class MockDaemonCall(object):
 
23
    def __init__(self, daemon_handler, count):
 
24
        self.daemon_handler = daemon_handler
 
25
        self.count = count
 
26
 
 
27
    def __call__(self, *args, **kw):
 
28
        self.count = self.count - 1
 
29
        #Stop daemon after restarting for n times
 
30
        if self.count <= 0:
 
31
            self.daemon_handler.running = False
 
32
        raise Exception("Mock unhandled exception")
 
33
 
 
34
@patch("time.sleep")
 
35
class TestDaemon(AgentTestCase):
 
36
    def test_daemon_restart(self, mock_sleep):
 
37
        #Mock daemon function
 
38
        daemon_handler = get_daemon_handler()
 
39
        mock_daemon = Mock(side_effect=MockDaemonCall(daemon_handler, 2))
 
40
        daemon_handler.daemon = mock_daemon
 
41
 
 
42
        daemon_handler.check_pid = Mock()
 
43
 
 
44
        daemon_handler.run()
 
45
 
 
46
        mock_sleep.assert_any_call(15)
 
47
        self.assertEquals(2, daemon_handler.daemon.call_count)
 
48
 
 
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()
 
53
 
 
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)
 
56
 
 
57
        daemon_handler.check_pid()
 
58
        self.assertTrue(os.path.isfile(mock_pid_file))
 
59
 
 
60
        daemon_handler.check_pid()
 
61
        mock_exit.assert_any_call(0)
 
62
   
 
63
if __name__ == '__main__':
 
64
    unittest.main()
 
65