1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2013 Cloudbase Solutions Srl
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
21
from nova.virt.hyperv import constants
22
from nova.virt.hyperv import vhdutils
23
from nova.virt.hyperv import vmutils
26
class VHDUtilsTestCase(test.TestCase):
27
"""Unit tests for the Hyper-V VHDUtils class."""
29
_FAKE_VHD_PATH = "C:\\fake_path.vhdx"
31
_FAKE_MAK_INTERNAL_SIZE = 1000
32
_FAKE_JOB_PATH = 'fake_job_path'
36
self._vhdutils = vhdutils.VHDUtils()
37
self._vhdutils._conn = mock.MagicMock()
38
self._vhdutils._vmutils = mock.MagicMock()
39
super(VHDUtilsTestCase, self).setUp()
41
def test_create_dynamic_vhd(self):
42
self._vhdutils.get_vhd_info = mock.MagicMock(
43
return_value={'Format': self._FAKE_FORMAT})
45
mock_img_svc = self._vhdutils._conn.Msvm_ImageManagementService()[0]
46
mock_img_svc.CreateDynamicVirtualHardDisk.return_value = (
47
self._FAKE_JOB_PATH, self._FAKE_RET_VAL)
49
self._vhdutils.create_dynamic_vhd(self._FAKE_VHD_PATH,
50
self._FAKE_MAK_INTERNAL_SIZE,
51
constants.DISK_FORMAT_VHD)
53
mock_img_svc.CreateDynamicVirtualHardDisk.assert_called_once_with(
54
Path=self._FAKE_VHD_PATH,
55
MaxInternalSize=self._FAKE_MAK_INTERNAL_SIZE)
57
def test_get_internal_vhd_size_by_file_size_fixed(self):
58
vhdutil = vhdutils.VHDUtils()
59
root_vhd_size = 1 * 1024 ** 3
60
vhdutil.get_vhd_info = mock.MagicMock()
61
vhdutil.get_vhd_info.return_value = {'Type': constants.VHD_TYPE_FIXED}
63
real_size = vhdutil._get_internal_vhd_size_by_file_size(None,
65
expected_vhd_size = 1 * 1024 ** 3 - 512
66
self.assertEqual(expected_vhd_size, real_size)
68
def test_get_internal_vhd_size_by_file_size_dynamic(self):
69
vhdutil = vhdutils.VHDUtils()
70
root_vhd_size = 20 * 1024 ** 3
71
vhdutil.get_vhd_info = mock.MagicMock()
72
vhdutil.get_vhd_info.return_value = {'Type':
73
constants.VHD_TYPE_DYNAMIC}
74
vhdutil._get_vhd_dynamic_blk_size = mock.MagicMock()
75
vhdutil._get_vhd_dynamic_blk_size.return_value = 2097152
77
real_size = vhdutil._get_internal_vhd_size_by_file_size(None,
79
expected_vhd_size = 20 * 1024 ** 3 - 43008
80
self.assertEqual(expected_vhd_size, real_size)
82
def test_get_internal_vhd_size_by_file_size_unsupported(self):
83
vhdutil = vhdutils.VHDUtils()
84
root_vhd_size = 20 * 1024 ** 3
85
vhdutil.get_vhd_info = mock.MagicMock()
86
vhdutil.get_vhd_info.return_value = {'Type': 5}
88
self.assertRaises(vmutils.HyperVException,
89
vhdutil._get_internal_vhd_size_by_file_size,