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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 Cloudbase Solutions Srl
#
# Author: Alessandro Pilotti <apilotti@cloudbasesolutions.com>
#
# 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.
"""
Tests for Hyper-V utilsv2.
"""
import mock
from ceilometer.compute.virt import inspector
from ceilometer.compute.virt.hyperv import utilsv2 as utilsv2
from ceilometer.tests import base as test_base
class TestUtilsV2(test_base.TestCase):
_FAKE_RETURN_CLASS = 'fake_return_class'
def setUp(self):
self._utils = utilsv2.UtilsV2()
self._utils._conn = mock.MagicMock()
self._utils._conn_cimv2 = mock.MagicMock()
super(TestUtilsV2, self).setUp()
def test_get_host_cpu_info(self):
_fake_clock_speed = 1000
_fake_cpu_count = 2
mock_cpu = mock.MagicMock()
mock_cpu.MaxClockSpeed = _fake_clock_speed
self._utils._conn_cimv2.Win32_Processor.return_value = [mock_cpu,
mock_cpu]
cpu_info = self._utils.get_host_cpu_info()
self.assertEqual(_fake_clock_speed, cpu_info[0])
self.assertEqual(_fake_cpu_count, cpu_info[1])
def test_get_all_vms(self):
fake_vm_element_name = "fake_vm_element_name"
fake_vm_name = "fake_vm_name"
mock_vm = mock.MagicMock()
mock_vm.ElementName = fake_vm_element_name
mock_vm.Name = fake_vm_name
self._utils._conn.Msvm_ComputerSystem.return_value = [mock_vm]
vms = self._utils.get_all_vms()
self.assertEqual((fake_vm_element_name, fake_vm_name), vms[0])
def test_get_cpu_metrics(self):
fake_vm_element_name = "fake_vm_element_name"
fake_cpu_count = 2
fake_uptime = 1000
fake_cpu_metric_val = 2000
self._utils._lookup_vm = mock.MagicMock()
self._utils._lookup_vm().OnTimeInMilliseconds = fake_uptime
self._utils._get_vm_resources = mock.MagicMock()
mock_res = self._utils._get_vm_resources()[0]
mock_res.VirtualQuantity = fake_cpu_count
self._utils._get_metrics = mock.MagicMock()
self._utils._get_metrics()[0].MetricValue = fake_cpu_metric_val
cpu_metrics = self._utils.get_cpu_metrics(fake_vm_element_name)
self.assertEqual(3, len(cpu_metrics))
self.assertEqual(fake_cpu_metric_val, cpu_metrics[0])
self.assertEqual(fake_cpu_count, cpu_metrics[1])
self.assertEqual(fake_uptime, cpu_metrics[2])
@mock.patch('ceilometer.compute.virt.hyperv.utilsv2.UtilsV2'
'._sum_metric_values_by_defs')
@mock.patch('ceilometer.compute.virt.hyperv.utilsv2.UtilsV2'
'._get_metric_value_instances')
def test_get_vnic_metrics(self, mock_get_instances, mock_get_by_defs):
fake_vm_element_name = "fake_vm_element_name"
fake_vnic_element_name = "fake_vnic_name"
fake_vnic_address = "fake_vnic_address"
fake_vnic_path = "fake_vnic_path"
fake_rx_mb = 1000
fake_tx_mb = 2000
self._utils._lookup_vm = mock.MagicMock()
self._utils._get_vm_resources = mock.MagicMock()
mock_port = mock.MagicMock()
mock_port.Parent = fake_vnic_path
mock_vnic = mock.MagicMock()
mock_vnic.path_.return_value = fake_vnic_path
mock_vnic.ElementName = fake_vnic_element_name
mock_vnic.Address = fake_vnic_address
self._utils._get_vm_resources.side_effect = [[mock_port], [mock_vnic]]
self._utils._get_metric_def = mock.MagicMock()
mock_get_by_defs.return_value = [fake_rx_mb, fake_tx_mb]
vnic_metrics = list(self._utils.get_vnic_metrics(fake_vm_element_name))
self.assertEqual(1, len(vnic_metrics))
self.assertEqual(fake_rx_mb, vnic_metrics[0]['rx_mb'])
self.assertEqual(fake_tx_mb, vnic_metrics[0]['tx_mb'])
self.assertEqual(fake_vnic_element_name,
vnic_metrics[0]['element_name'])
self.assertEqual(fake_vnic_address, vnic_metrics[0]['address'])
def test_get_disk_metrics(self):
fake_vm_element_name = "fake_vm_element_name"
fake_host_resource = "fake_host_resource"
fake_instance_id = "fake_instance_id"
fake_read_mb = 1000
fake_write_mb = 2000
self._utils._lookup_vm = mock.MagicMock()
mock_disk = mock.MagicMock()
mock_disk.HostResource = [fake_host_resource]
mock_disk.InstanceID = fake_instance_id
self._utils._get_vm_resources = mock.MagicMock(
return_value=[mock_disk])
self._utils._get_metric_def = mock.MagicMock()
self._utils._get_metric_values = mock.MagicMock()
self._utils._get_metric_values.return_value = [fake_read_mb,
fake_write_mb]
disk_metrics = list(self._utils.get_disk_metrics(fake_vm_element_name))
self.assertEqual(1, len(disk_metrics))
self.assertEqual(fake_read_mb, disk_metrics[0]['read_mb'])
self.assertEqual(fake_write_mb, disk_metrics[0]['write_mb'])
self.assertEqual(fake_instance_id, disk_metrics[0]['instance_id'])
self.assertEqual(fake_host_resource, disk_metrics[0]['host_resource'])
def test_get_metric_value_instances(self):
mock_el1 = mock.MagicMock()
mock_associator = mock.MagicMock()
mock_el1.associators.return_value = [mock_associator]
mock_el2 = mock.MagicMock()
mock_el2.associators.return_value = []
returned = self._utils._get_metric_value_instances(
[mock_el1, mock_el2], self._FAKE_RETURN_CLASS)
self.assertEqual([mock_associator], returned)
def test_lookup_vm(self):
fake_vm_element_name = "fake_vm_element_name"
fake_vm = "fake_vm"
self._utils._conn.Msvm_ComputerSystem.return_value = [fake_vm]
vm = self._utils._lookup_vm(fake_vm_element_name)
self.assertEqual(fake_vm, vm)
def test_lookup_vm_not_found(self):
fake_vm_element_name = "fake_vm_element_name"
self._utils._conn.Msvm_ComputerSystem.return_value = []
self.assertRaises(inspector.InstanceNotFoundException,
self._utils._lookup_vm, fake_vm_element_name)
def test_lookup_vm_duplicate_found(self):
fake_vm_element_name = "fake_vm_element_name"
fake_vm = "fake_vm"
self._utils._conn.Msvm_ComputerSystem.return_value = [fake_vm, fake_vm]
self.assertRaises(utilsv2.HyperVException,
self._utils._lookup_vm, fake_vm_element_name)
def test_get_metric_values(self):
fake_metric_def_id = "fake_metric_def_id"
fake_metric_value = "1000"
mock_metric = mock.MagicMock()
mock_metric.MetricDefinitionId = fake_metric_def_id
mock_metric.MetricValue = fake_metric_value
mock_element = mock.MagicMock()
mock_element.associators.return_value = [mock_metric]
mock_metric_def = mock.MagicMock()
mock_metric_def.Id = fake_metric_def_id
metric_values = self._utils._get_metric_values(mock_element,
[mock_metric_def])
self.assertEqual(1, len(metric_values))
self.assertEqual(long(fake_metric_value), metric_values[0])
def test_get_vm_setting_data(self):
mock_vm_s = mock.MagicMock()
mock_vm_s.VirtualSystemType = self._utils._VIRTUAL_SYSTEM_TYPE_REALIZED
mock_vm = mock.MagicMock()
mock_vm.associators.return_value = [mock_vm_s]
vm_setting_data = self._utils._get_vm_setting_data(mock_vm)
self.assertEqual(mock_vm_s, vm_setting_data)
|