-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtest_hyperv.py
More file actions
474 lines (411 loc) · 19 KB
/
Copy pathtest_hyperv.py
File metadata and controls
474 lines (411 loc) · 19 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# -*- coding: utf-8 -*-
from __future__ import print_function
"""
Test of Hyper-V virtualization backend.
Copyright (C) 2016 Radek Novacek <rnovacek@redhat.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
import os
from mock import patch, MagicMock, ANY, Mock
from threading import Event
from queue import Queue
import requests
from base import TestBase
from proxy import Proxy
from virtwho import DefaultInterval
from virtwho.virt.hyperv.hyperv import HyperV, HypervConfigSection, HyperVException
from virtwho.virt import VirtError, Guest, Hypervisor, StatusReport
class HyperVMock(object):
@classmethod
def post(cls, url, data, **kwargs):
if 'uuid:00000000-0000-0000-0000-000000000000' in data:
return HyperVMock.pull(None, {})
if 'Msvm_VirtualSystemSettingData' in data:
return HyperVMock.enumerate(5)
elif 'uuid:00000000-0000-0000-0000-000000000005' in data:
return HyperVMock.pull(0, {
'BIOSGUID': '{78563412-AB90-EFCD-1234-567890ABCDEF}',
'VirtualSystemIdentifier': '',
})
elif 'GetSummaryInformation_INPUT' in data:
return HyperVMock.summary_information()
elif 'select * from CIM_Datafile' in data:
return HyperVMock.enumerate(1)
elif 'uuid:00000000-0000-0000-0000-000000000001' in data:
return HyperVMock.pull(2, {
'p:Path': '\\windows\\system32\\',
'p:Version': '0.1.2345.67890',
})
elif 'uuid:00000000-0000-0000-0000-000000000002' in data:
return HyperVMock.pull(None, {})
elif 'NumberOfProcessors from Win32_ComputerSystem' in data:
return HyperVMock.enumerate(3)
elif 'uuid:00000000-0000-0000-0000-000000000003' in data:
return HyperVMock.pull(0, {
'NumberOfProcessors': '1',
'DNSHostName': 'hostname.domainname',
})
elif 'UUID from Win32_ComputerSystemProduct' in data:
return HyperVMock.enumerate(4)
elif 'uuid:00000000-0000-0000-0000-000000000004' in data:
return HyperVMock.pull(0, {
'UUID': '{78563412-AB90-EFCD-1234-567890ABCDEF}',
})
else:
raise AssertionError("Not implemented")
@classmethod
def envelope(cls, body):
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:w="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd"
xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd"
xmlns:wsen="http://schemas.xmlsoap.org/ws/2004/09/enumeration"
xmlns:vsms="http://schemas.microsoft.com/wbem/wsman/1/wmi/root/virtualization/Msvm_VirtualSystemManagementService">
<s:Body>
{0}
</s:Body>
</s:Envelope>'''.format(body)
return MagicMock(content=xml, status_code=200)
@classmethod
def enumerate(cls, id):
return HyperVMock.envelope('''
<wsen:EnumerateResponse>
<wsen:EnumerationContext>uuid:00000000-0000-0000-0000-{0}</wsen:EnumerationContext>
</wsen:EnumerateResponse>'''.format(str(id).rjust(12, '0')))
@classmethod
def pull(cls, msg_id, data):
print("PULL", msg_id, data)
if msg_id is not None:
s = []
for key, value in data.items():
s.append("<{0}>{1}</{0}>".format(key, value))
return HyperVMock.envelope('''
<wsen:PullResponse>
<wsen:EnumerationContext>uuid:00000000-0000-0000-0000-{0}</wsen:EnumerationContext>
<wsen:Items>
<p:CIM_DataFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/CIM_DataFile" xmlns:cim="http://schemas.dmtf.org/wbem/wscim/1/common" xsi:type="p:CIM_DataFile_Type">
{1}
</p:CIM_DataFile>
</wsen:Items>
</wsen:PullResponse>
'''.format(str(msg_id).rjust(12, '0'), "\n".join(s)))
else:
print("NONE")
return HyperVMock.envelope('''
<wsen:PullResponse>
<wsen:Items></wsen:Items>
<wsen:EndOfSequence/>
</wsen:PullResponse>''')
@classmethod
def method(cls, body):
return HyperVMock.envelope(body)
@classmethod
def summary_information(cls):
return HyperVMock.method('''
<vsms:GetSummaryInformation_OUTPUT>
<vsms:ReturnValue>0</vsms:ReturnValue>
</vsms:GetSummaryInformation_OUTPUT>''')
@classmethod
def datafile(cls):
pass
class TestHyperV(TestBase):
def setUp(self):
config_values = {
'type': 'hyperv',
'server': 'localhost',
'username': 'username',
'password': u'1€345678',
'owner': 'owner',
}
config = HypervConfigSection('test', None)
config.update(**config_values)
config.validate()
self.hyperv = HyperV(self.logger, config, None, interval=DefaultInterval)
def run_once(self, queue=None):
''' Run Hyper-V in oneshot mode '''
self.hyperv._oneshot = True
self.hyperv.dest = queue or Queue()
self.hyperv._terminate_event = Event()
self.hyperv._interval = 0
self.hyperv._run()
@patch('requests.Session')
def test_connect(self, session):
session.return_value.post.side_effect = HyperVMock.post
self.run_once()
session.assert_called_with()
session.return_value.post.assert_called_with('http://localhost:5985/wsman', ANY, headers=ANY)
@patch('requests.Session')
def test_status(self, session):
session.return_value.post.side_effect = HyperVMock.post
self.hyperv.status = True
self.hyperv._send_data = Mock()
self.run_once()
self.hyperv._send_data.assert_called_once_with(data_to_send=ANY)
self.assertTrue(isinstance(self.hyperv._send_data.mock_calls[0].kwargs['data_to_send'], StatusReport))
self.assertEqual(
self.hyperv._send_data.mock_calls[0].kwargs['data_to_send'].data['source']['server'],
self.hyperv.config['server']
)
@patch('requests.Session')
def test_status_bad_source_credentials(self, session):
session.return_value.post.side_effect = HyperVMock.post
self.hyperv.status = True
self.hyperv._send_data = Mock()
self.hyperv.statusConfirmConnection = Mock()
self.hyperv.statusConfirmConnection.side_effect = VirtError("Incorrect domain/username/password")
self.hyperv._oneshot = True
self.hyperv.dest = Queue()
self.hyperv._terminate_event = Event()
self.hyperv._interval = 0
self.hyperv.run()
self.hyperv._send_data.assert_called_once_with(data_to_send=ANY)
self.assertTrue(isinstance(self.hyperv._send_data.mock_calls[0].kwargs['data_to_send'], StatusReport))
self.assertEqual(
self.hyperv._send_data.mock_calls[0].kwargs['data_to_send'].data['source']['server'],
self.hyperv.config['server']
)
self.assertEqual(
self.hyperv._send_data.mock_calls[0].kwargs['data_to_send'].data['source']['message'],
"Incorrect domain/username/password."
)
@patch('requests.Session')
def test_connection_refused(self, session):
session.return_value.post.side_effect = requests.ConnectionError
self.assertRaises(VirtError, self.run_once)
@patch('requests.Session')
def test_invalid_login(self, session):
session.return_value.post.return_value.status_code = 401
self.assertRaises(VirtError, self.run_once)
@patch('requests.Session')
def test_404(self, session):
session.return_value.post.return_value.text = ''
session.return_value.post.return_value.status_code = 404
self.assertRaises(VirtError, self.run_once)
@patch('requests.Session')
def test_500(self, session):
session.return_value.post.return_value.text = ''
session.return_value.post.return_value.status_code = 500
self.assertRaises(VirtError, self.run_once)
@patch('requests.Session')
def test_wrong_namespace(self, session):
session.return_value.post.return_value.text = '''
<s:Envelope xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Body>
<s:Fault>
<s:Detail>
<p:MSFT_WmiError xmlns:p="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/MSFT_WmiError">
<p:error_Code>2150858778</p:error_Code>
</p:MSFT_WmiError>
</s:Detail>
</s:Fault>
</s:Body>
</s:Envelope>'''
session.return_value.post.return_value.status_code = 500
self.assertRaises(VirtError, self.run_once)
@patch('requests.Session')
def test_fault(self, session):
session.return_value.post.return_value.text = '''
<s:Envelope xml:lang="en-US" xmlns:s="http://www.w3.org/2003/05/soap-envelope"></s:Envelope>'''
session.return_value.post.return_value.status_code = 500
self.assertRaises(VirtError, self.run_once)
@patch('requests.Session')
def test_getHostGuestMapping(self, session):
expected_hostname = 'hostname.domainname'
expected_hypervisorId = '12345678-90AB-CDEF-1234-567890ABCDEF'
expected_guestId = '12345678-90AB-CDEF-1234-567890ABCDEF'
expected_guest_state = Guest.STATE_UNKNOWN
session.return_value.post.side_effect = HyperVMock.post
expected_result = Hypervisor(
hypervisorId=expected_hypervisorId,
name=expected_hostname,
guestIds=[
Guest(
expected_guestId,
self.hyperv.CONFIG_TYPE,
expected_guest_state,
)
],
facts={
Hypervisor.CPU_SOCKET_FACT: '1',
Hypervisor.HYPERVISOR_TYPE_FACT: 'hyperv',
Hypervisor.HYPERVISOR_VERSION_FACT: '0.1.2345.67890',
Hypervisor.SYSTEM_UUID_FACT: expected_hypervisorId
}
)
result = self.hyperv.getHostGuestMapping()['hypervisors'][0]
assert expected_result.toDict() == result.toDict()
def test_proxy(self):
proxy = Proxy()
self.addCleanup(proxy.terminate)
proxy.start()
oldenv = os.environ.copy()
self.addCleanup(lambda: setattr(os, 'environ', oldenv))
os.environ['http_proxy'] = proxy.address
self.assertRaises(VirtError, self.run_once)
self.assertIsNotNone(proxy.last_path, "Proxy was not called")
self.assertEqual(proxy.last_path, 'http://localhost:5985/wsman')
@patch('requests.Session')
@patch('logging.Logger.debug')
def test_proxy_if_html_response_only_status_code_and_title_is_logged(self, logger_debug, session):
proxy_response = '''
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ERROR: The requested URL could not be retrieved</title>
<style type="text/css"></style>"
</head>
<body>
</body>
</html>'''
session.return_value.post.return_value.content = proxy_response
session.return_value.post.return_value.status_code = 403
self.assertRaises(VirtError, self.run_once)
logger_debug.assert_called_with('Invalid response (403) from Hyper-V: '
'ERROR: The requested URL could not be retrieved')
@patch('requests.Session')
@patch('logging.Logger.debug')
def test_proxy_if_html_parse_error_only_status_code_is_logged(self, logger_debug, session):
proxy_response = '''
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>ERROR: The requested URL could not be retrieved</title>
<style type="text/css"></style>"
</head>
<body <!-- the incomplete tag will cause parse error -->
</body>
</html>'''
session.return_value.post.return_value.content = proxy_response
session.return_value.post.return_value.status_code = 403
self.assertRaises(VirtError, self.run_once)
logger_debug.assert_called_with(
'Invalid response (403) from Hyper-V (error: not well-formed (invalid token): line 8, column 18)'
)
class TestHyperVKerberosConnect(TestBase):
"""Tests for the kerberos auth wiring in HyperV.connect()."""
def _make_hyperv(self, auth_method='kerberos', keytab=None, principal=None):
config_values = {
'type': 'hyperv',
'server': 'hyperv.example.com',
'auth_method': auth_method,
'owner': 'owner',
}
if keytab:
config_values['kerberos_keytab'] = keytab
if principal:
config_values['kerberos_principal'] = principal
config = HypervConfigSection('test', None)
config.update(**config_values)
config.validate()
return HyperV(self.logger, config, None, interval=DefaultInterval)
@patch('requests.Session')
def test_basic_auth_uses_hypervauth(self, session):
"""auth_method=basic uses the HyperVAuth handler."""
from virtwho.virt.hyperv.hyperv import HyperVAuth
config_values = {
'type': 'hyperv',
'server': 'hyperv.example.com',
'auth_method': 'basic',
'username': 'user',
'password': 'pass',
'owner': 'owner',
}
config = HypervConfigSection('test', None)
config.update(**config_values)
config.validate()
hyperv = HyperV(self.logger, config, None, interval=DefaultInterval)
s = hyperv.connect()
self.assertIsInstance(s.auth, HyperVAuth)
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_auth_uses_httpspnegoauth(self, session, mock_credentials):
"""auth_method=kerberos uses HTTPSPNEGOAuth."""
from requests_gssapi import HTTPSPNEGOAuth
hyperv = self._make_hyperv()
s = hyperv.connect()
self.assertIsInstance(s.auth, HTTPSPNEGOAuth)
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_sets_principal(self, session, mock_credentials):
"""kerberos_principal is used to acquire gssapi.Credentials passed to HTTPSPNEGOAuth."""
sentinel_creds = object()
mock_credentials.return_value = sentinel_creds
hyperv = self._make_hyperv(principal='virtwho@EXAMPLE.COM')
s = hyperv.connect()
called_name = mock_credentials.call_args.kwargs['name']
self.assertEqual(str(called_name), 'virtwho@EXAMPLE.COM')
self.assertEqual(mock_credentials.call_args.kwargs['usage'], 'initiate')
self.assertIs(s.auth.creds, sentinel_creds)
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_principal_credential_failure_raises_clean_error(self, session, mock_credentials):
"""A GSSError while acquiring credentials for the principal surfaces as HyperVException."""
import gssapi
class FakeGSSError(gssapi.exceptions.GSSError):
def __str__(self):
return 'no matching credentials in cache collection'
error = FakeGSSError.__new__(FakeGSSError)
BaseException.__init__(error, 'no matching credentials in cache collection')
mock_credentials.side_effect = error
hyperv = self._make_hyperv(principal='virtwho@EXAMPLE.COM')
self.assertRaises(HyperVException, hyperv.connect)
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_sets_keytab_store(self, session, mock_credentials):
"""kerberos_keytab is passed to gssapi.Credentials via the 'store' argument."""
import tempfile
with tempfile.NamedTemporaryFile(delete=False) as f:
keytab_path = f.name
try:
hyperv = self._make_hyperv(keytab=keytab_path)
hyperv.connect()
called_store = mock_credentials.call_args.kwargs['store']
self.assertEqual(called_store, {"client_keytab": "FILE:%s" % keytab_path})
finally:
os.unlink(keytab_path)
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_no_principal_or_keytab_uses_defaults(self, session, mock_credentials):
"""Without kerberos_principal/kerberos_keytab, gssapi.Credentials is still called,
with name and store left as None so the default credential cache is used."""
sentinel_creds = Mock(name='resolved-creds')
sentinel_creds.name = 'defaulted-user@EXAMPLE.COM'
mock_credentials.return_value = sentinel_creds
hyperv = self._make_hyperv()
s = hyperv.connect()
mock_credentials.assert_called_once_with(name=None, store=None, usage='initiate')
self.assertIs(s.auth.creds, sentinel_creds)
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_connect_runs_oneshot(self, session, mock_credentials):
"""Kerberos auth path can complete a full oneshot run with mocked responses."""
session.return_value.post.side_effect = HyperVMock.post
hyperv = self._make_hyperv()
hyperv._oneshot = True
hyperv.dest = Queue()
hyperv._terminate_event = Event()
hyperv._interval = 0
hyperv._run()
session.return_value.post.assert_called()
@patch('virtwho.virt.hyperv.hyperv.gssapi.Credentials')
@patch('requests.Session')
def test_kerberos_401_raises_auth_failed(self, session, mock_credentials):
"""A 401 after kerberos negotiate raises HyperVAuthFailed."""
session.return_value.post.return_value.status_code = 401
hyperv = self._make_hyperv()
hyperv._oneshot = True
hyperv.dest = Queue()
hyperv._terminate_event = Event()
hyperv._interval = 0
self.assertRaises(VirtError, hyperv._run)