summaryrefslogtreecommitdiff
path: root/tests/distro/test_scvmm.py
blob: 2437170340c84a096bd9d4fd053c9751b274fd1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2014 Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires Python 2.4+ and Openssl 1.0+
#
# Implements parts of RFC 2131, 1541, 1497 and
# http://msdn.microsoft.com/en-us/library/cc227282%28PROT.10%29.aspx
# http://msdn.microsoft.com/en-us/library/cc227259%28PROT.13%29.aspx

import mock
from tests.tools import *

import azurelinuxagent.daemon.scvmm as scvmm
from azurelinuxagent.daemon.main import *
from azurelinuxagent.common.osutil.default import DefaultOSUtil

class TestSCVMM(AgentTestCase):
    def test_scvmm_detection_with_file(self):
        # setup
        conf.get_dvd_mount_point = Mock(return_value=self.tmp_dir)
        conf.get_detect_scvmm_env = Mock(return_value=True)
        scvmm_file = os.path.join(self.tmp_dir, scvmm.VMM_CONF_FILE_NAME)
        fileutil.write_file(scvmm_file, "")

        with patch.object(scvmm.ScvmmHandler, 'start_scvmm_agent') as po:
            with patch('os.listdir', return_value=["sr0", "sr1", "sr2"]):
                with patch('time.sleep', return_value=0):
                    # execute
                    failed = False
                    try:
                        scvmm.get_scvmm_handler().run()
                    except:
                        failed = True
                    # assert
                    self.assertTrue(failed)
                    self.assertTrue(po.call_count == 1)
                    # cleanup
                    os.remove(scvmm_file)


    def test_scvmm_detection_with_multiple_cdroms(self):
        # setup
        conf.get_dvd_mount_point = Mock(return_value=self.tmp_dir)
        conf.get_detect_scvmm_env = Mock(return_value=True)

        # execute
        with mock.patch.object(DefaultOSUtil, 'mount_dvd') as patch_mount:
            with patch('os.listdir', return_value=["sr0", "sr1", "sr2"]):
                scvmm.ScvmmHandler().detect_scvmm_env()
                # assert
                assert patch_mount.call_count == 3
                assert patch_mount.call_args_list[0][1]['dvd_device'] == '/dev/sr0'
                assert patch_mount.call_args_list[1][1]['dvd_device'] == '/dev/sr1'
                assert patch_mount.call_args_list[2][1]['dvd_device'] == '/dev/sr2'


    def test_scvmm_detection_without_file(self):
        # setup
        conf.get_dvd_mount_point = Mock(return_value=self.tmp_dir)
        conf.get_detect_scvmm_env = Mock(return_value=True)
        scvmm_file = os.path.join(self.tmp_dir, scvmm.VMM_CONF_FILE_NAME)
        if os.path.exists(scvmm_file):
            os.remove(scvmm_file)

        with mock.patch.object(scvmm.ScvmmHandler, 'start_scvmm_agent') as patch_start:
            # execute
            scvmm.ScvmmHandler().detect_scvmm_env()
            # assert
            patch_start.assert_not_called()


if __name__ == '__main__':
    unittest.main()