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

« back to all changes in this revision

Viewing changes to tests/distro/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
 
# 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
20
 
 
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 *
25
 
 
26
 
class MockDaemonCall(object):
27
 
    def __init__(self, daemon_handler, count):
28
 
        self.daemon_handler = daemon_handler
29
 
        self.count = count
30
 
 
31
 
    def __call__(self, *args, **kw):
32
 
        self.count = self.count - 1
33
 
        #Stop daemon after restarting for n times
34
 
        if self.count <= 0:
35
 
            self.daemon_handler.running = False
36
 
        raise Exception("Mock unhandled exception")
37
 
 
38
 
@patch("time.sleep")
39
 
class TestDaemon(AgentTestCase):
40
 
    def test_daemon_restart(self, mock_sleep):
41
 
        distro = get_distro()
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()
46
 
 
47
 
        mock_sleep.assert_any_call(15)
48
 
        self.assertEquals(2, distro.daemon_handler.daemon.call_count)
49
 
 
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):
53
 
        distro = get_distro()
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
 
        distro.daemon_handler.check_pid()
58
 
        self.assertTrue(os.path.isfile(mock_pid_file))
59
 
 
60
 
        distro.daemon_handler.check_pid()
61
 
        mock_exit.assert_any_call(0)
62
 
   
63
 
if __name__ == '__main__':
64
 
    unittest.main()
65