File: test_session.py

package info (click to toggle)
python-os-xenapi 0.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 1,012 kB
  • sloc: python: 8,137; sh: 2,154; makefile: 45
file content (546 lines) | stat: -rw-r--r-- 25,532 bytes parent folder | download | duplicates (2)
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# -*- coding: utf-8 -*-

# 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.

import errno
import os
import socket

import mock

from os_xenapi.client import exception
from os_xenapi.client import session
from os_xenapi.client import XenAPI
from os_xenapi.tests import base


class SessionTestCase(base.TestCase):
    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, '_get_platform_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(session.XenAPISession, '_get_product_version_and_brand')
    @mock.patch.object(socket, 'gethostbyname')
    def test_session_nova_originator(self,
                                     mock_gethostbyname,
                                     mock_version_and_brand,
                                     mock_create_session,
                                     mock_platform_version,
                                     mock_verify_plugin_version):
        concurrent = 2
        originator = 'os-xenapi-nova'
        version = '2.1'
        timeout = 10
        sess = mock.Mock()
        mock_create_session.return_value = sess
        mock_version_and_brand.return_value = ('6.5', 'XenServer')
        mock_platform_version.return_value = (2, 1, 0)
        sess.xenapi.host.get_uuid.return_value = 'fake_host_uuid'
        sess.xenapi.session.get_this_host.return_value = 'fake_host_ref'
        fake_url = 'http://someserver'
        fake_host_name = 'someserver'

        xenapi_sess = session.XenAPISession(fake_url, 'username',
                                            'password', originator=originator,
                                            concurrent=concurrent,
                                            timeout=timeout)

        sess.login_with_password.assert_called_with('username', 'password',
                                                    version, originator)
        self.assertFalse(xenapi_sess.is_slave)
        mock_gethostbyname.assert_called_with(fake_host_name)
        sess.xenapi.session.get_this_host.assert_called_once_with(sess.handle)
        sess.xenapi.PIF.get_all_records_where.assert_not_called()
        self.assertEqual('fake_host_ref', xenapi_sess.host_ref)
        self.assertEqual('fake_host_uuid', xenapi_sess.host_uuid)
        self.assertEqual(fake_url, xenapi_sess.url)

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, '_get_platform_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(session.XenAPISession, '_get_product_version_and_brand')
    @mock.patch.object(session.XenAPISession, '_create_session_and_login')
    @mock.patch.object(socket, 'gethostbyname')
    def test_session_on_slave_node_using_host_ip(self,
                                                 mock_gethostbyname,
                                                 mock_login,
                                                 mock_version_and_brand,
                                                 mock_create_session,
                                                 mock_platform_version,
                                                 mock_verify_plugin_version):
        sess = mock.Mock()
        fake_records = {'fake_PIF_ref': {'host': 'fake_host_ref'}}
        sess.xenapi.PIF.get_all_records_where.return_value = fake_records
        sess.xenapi.host.get_uuid.return_value = 'fake_host_uuid'
        side_effects = [XenAPI.Failure(['HOST_IS_SLAVE', 'fake_master_url']),
                        sess, sess, sess]
        mock_login.side_effect = side_effects
        concurrent = 2
        originator = 'os-xenapi-nova'
        timeout = 10
        mock_version_and_brand.return_value = ('6.5', 'XenServer')
        mock_platform_version.return_value = (2, 1, 0)
        fake_url = 'http://0.0.0.0'
        fake_ip = '0.0.0.0'

        xenapi_sess = session.XenAPISession(fake_url, 'username',
                                            'password', originator=originator,
                                            concurrent=concurrent,
                                            timeout=timeout)

        self.assertTrue(xenapi_sess.is_slave)
        mock_gethostbyname.assert_called_with(fake_ip)
        self.assertEqual('fake_host_ref', xenapi_sess.host_ref)
        self.assertEqual('fake_host_uuid', xenapi_sess.host_uuid)
        self.assertEqual('http://fake_master_url', xenapi_sess.master_url)
        self.assertEqual(fake_url, xenapi_sess.url)

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, '_get_platform_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(session.XenAPISession, '_get_product_version_and_brand')
    @mock.patch.object(session.XenAPISession, '_create_session_and_login')
    @mock.patch.object(socket, 'gethostbyname')
    def test_session_on_slave_node_using_host_name(self,
                                                   mock_gethostbyname,
                                                   mock_login,
                                                   mock_version_and_brand,
                                                   mock_create_session,
                                                   mock_platform_version,
                                                   mock_verify_plugin_version):
        sess = mock.Mock()
        fake_records = {'fake_PIF_ref': {'host': 'fake_host_ref'}}
        sess.xenapi.PIF.get_all_records_where.return_value = fake_records
        sess.xenapi.host.get_uuid.return_value = 'fake_host_uuid'
        side_effects = [XenAPI.Failure(['HOST_IS_SLAVE', 'fake_master_url']),
                        sess, sess, sess]
        mock_login.side_effect = side_effects
        concurrent = 2
        originator = 'os-xenapi-nova'

        timeout = 10
        mock_version_and_brand.return_value = ('6.5', 'XenServer')
        mock_platform_version.return_value = (2, 1, 0)
        fake_url = 'http://someserver'
        fake_host_name = 'someserver'
        fake_ip = '0.0.0.0'
        mock_gethostbyname.return_value = fake_ip
        xenapi_sess = session.XenAPISession(fake_url, 'username',
                                            'password', originator=originator,
                                            concurrent=concurrent,
                                            timeout=timeout)

        self.assertTrue(xenapi_sess.is_slave)
        mock_gethostbyname.assert_called_with(fake_host_name)
        self.assertEqual('fake_host_ref', xenapi_sess.host_ref)
        self.assertEqual('fake_host_uuid', xenapi_sess.host_uuid)
        self.assertEqual('http://fake_master_url', xenapi_sess.master_url)
        self.assertEqual(fake_url, xenapi_sess.url)

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, '_get_platform_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(session.XenAPISession, '_get_product_version_and_brand')
    @mock.patch.object(session.XenAPISession, '_create_session_and_login')
    @mock.patch.object(socket, 'gethostbyname')
    def test_session_on_slave_node_exc_no_host_ref(self,
                                                   mock_gethostbyname,
                                                   mock_login,
                                                   mock_version_and_brand,
                                                   mock_create_session,
                                                   mock_platform_version,
                                                   mock_verify_plugin_version):
        sess = mock.Mock()
        fake_records = {}
        sess.xenapi.PIF.get_all_records_where.return_value = fake_records
        sess.xenapi.host.get_uuid.return_value = 'fake_host_uuid'
        side_effects = [XenAPI.Failure(['HOST_IS_SLAVE', 'fake_master_url']),
                        sess, sess, sess]
        mock_login.side_effect = side_effects
        concurrent = 2
        originator = 'os-xenapi-nova'

        timeout = 10
        mock_version_and_brand.return_value = ('6.5', 'XenServer')
        mock_platform_version.return_value = (2, 1, 0)
        fake_url = 'http://someserver'
        fake_host_name = 'someserver'
        fake_ip = '0.0.0.0'
        mock_gethostbyname.return_value = fake_ip

        self.assertRaises(
            XenAPI.Failure,
            session.XenAPISession,
            fake_url, 'username', 'password', originator=originator,
            concurrent=concurrent, timeout=timeout)

        mock_gethostbyname.assert_called_with(fake_host_name)

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, '_get_platform_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(session.XenAPISession, '_get_product_version_and_brand')
    @mock.patch.object(session.XenAPISession, '_create_session_and_login')
    @mock.patch.object(socket, 'gethostbyname')
    def test_session_on_slave_node_exc_more_than_one_host_ref(
        self,
        mock_gethostbyname,
        mock_login,
        mock_version_and_brand,
        mock_create_session,
        mock_platform_version,
        mock_verify_plugin_version):
        sess = mock.Mock()
        fake_records = {'fake_PIF_ref_a': {'host': 'fake_host_ref_a'},
                        'fake_PIF_ref_b': {'host': 'fake_host_ref_b'}}
        sess.xenapi.PIF.get_all_records_where.return_value = fake_records
        sess.xenapi.host.get_uuid.return_value = 'fake_host_uuid'
        side_effects = [XenAPI.Failure(['HOST_IS_SLAVE', 'fake_master_url']),
                        sess, sess, sess]
        mock_login.side_effect = side_effects
        concurrent = 2
        originator = 'os-xenapi-nova'

        timeout = 10
        mock_version_and_brand.return_value = ('6.5', 'XenServer')
        mock_platform_version.return_value = (2, 1, 0)
        fake_url = 'http://someserver'
        fake_host_name = 'someserver'
        fake_ip = '0.0.0.0'
        mock_gethostbyname.return_value = fake_ip

        self.assertRaises(
            XenAPI.Failure,
            session.XenAPISession,
            fake_url, 'username', 'password', originator=originator,
            concurrent=concurrent, timeout=timeout)

        mock_gethostbyname.assert_called_with(fake_host_name)

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, '_get_platform_version')
    @mock.patch('eventlet.timeout.Timeout')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(session.XenAPISession, '_get_product_version_and_brand')
    @mock.patch.object(socket, 'gethostbyname')
    @mock.patch.object(session.XenAPISession, '_get_host_ref')
    def test_session_login_with_timeout(self, mock_get_host_ref,
                                        mock_gethostbyname, mock_version,
                                        create_session, mock_timeout,
                                        mock_platform_version,
                                        mock_verify_plugin_version):
        concurrent = 2
        originator = 'os-xenapi-nova'
        sess = mock.Mock()
        create_session.return_value = sess
        mock_version.return_value = ('version', 'brand')
        mock_platform_version.return_value = (2, 1, 0)

        session.XenAPISession('http://someserver', 'username', 'password',
                              originator=originator, concurrent=concurrent)
        self.assertEqual(concurrent, sess.login_with_password.call_count)
        self.assertEqual(concurrent, mock_timeout.call_count)

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, 'call_plugin')
    @mock.patch.object(session.XenAPISession, '_get_software_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(socket, 'gethostbyname')
    @mock.patch.object(session.XenAPISession, '_get_host_ref')
    def test_relax_xsm_sr_check_true(self, mock_get_host_ref,
                                     mock_gethostbyname,
                                     mock_create_session,
                                     mock_get_software_version,
                                     mock_call_plugin,
                                     mock_verify_plugin_version):
        sess = mock.Mock()
        mock_create_session.return_value = sess
        mock_get_software_version.return_value = {'product_version': '6.5.0',
                                                  'product_brand': 'XenServer',
                                                  'platform_version': '1.9.0'}
        # mark relax-xsm-sr-check=True in /etc/xapi.conf
        mock_call_plugin.return_value = "True"
        xenapi_sess = session.XenAPISession(
            'http://someserver', 'username', 'password')
        self.assertTrue(xenapi_sess.is_xsm_sr_check_relaxed())

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, 'call_plugin')
    @mock.patch.object(session.XenAPISession, '_get_software_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(socket, 'gethostbyname')
    @mock.patch.object(session.XenAPISession, '_get_host_ref')
    def test_relax_xsm_sr_check_XS65_missing(self, mock_get_host_ref,
                                             mock_gethostbyname,
                                             mock_create_session,
                                             mock_get_software_version,
                                             mock_call_plugin,
                                             mock_verify_plugin_version):
        sess = mock.Mock()

        mock_create_session.return_value = sess
        mock_get_software_version.return_value = {'product_version': '6.5.0',
                                                  'product_brand': 'XenServer',
                                                  'platform_version': '1.9.0'}
        # mark no relax-xsm-sr-check setting in /etc/xapi.conf
        mock_call_plugin.return_value = ""
        xenapi_sess = session.XenAPISession(
            'http://someserver', 'username', 'password')
        self.assertFalse(xenapi_sess.is_xsm_sr_check_relaxed())

    @mock.patch.object(session.XenAPISession, '_verify_plugin_version')
    @mock.patch.object(session.XenAPISession, 'call_plugin')
    @mock.patch.object(session.XenAPISession, '_get_software_version')
    @mock.patch.object(session.XenAPISession, '_create_session')
    @mock.patch.object(socket, 'gethostbyname')
    @mock.patch.object(session.XenAPISession, '_get_host_ref')
    def test_relax_xsm_sr_check_XS7_missing(self, mock_get_host_ref,
                                            mock_gethostbyname,
                                            mock_create_session,
                                            mock_get_software_version,
                                            mock_call_plugin,
                                            mock_verify_plugin_version):
        sess = mock.Mock()
        mock_create_session.return_value = sess
        mock_get_software_version.return_value = {'product_version': '7.0.0',
                                                  'product_brand': 'XenServer',
                                                  'platform_version': '2.1.0'}
        # mark no relax-xsm-sr-check in /etc/xapi.conf
        mock_call_plugin.return_value = ""
        xenapi_sess = session.XenAPISession(
            'http://someserver', 'username', 'password')
        self.assertTrue(xenapi_sess.is_xsm_sr_check_relaxed())


class ApplySessionHelpersTestCase(base.TestCase):
    def setUp(self):
        super(ApplySessionHelpersTestCase, self).setUp()
        self.session = mock.Mock()
        session.apply_session_helpers(self.session)

    def test_apply_session_helpers_add_VM(self):
        self.session.VM.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("VM.get_X", "ref")

    def test_apply_session_helpers_add_SR(self):
        self.session.SR.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("SR.get_X", "ref")

    def test_apply_session_helpers_add_VDI(self):
        self.session.VDI.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("VDI.get_X", "ref")

    def test_apply_session_helpers_add_VIF(self):
        self.session.VIF.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("VIF.get_X", "ref")

    def test_apply_session_helpers_add_VBD(self):
        self.session.VBD.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("VBD.get_X", "ref")

    def test_apply_session_helpers_add_PBD(self):
        self.session.PBD.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("PBD.get_X", "ref")

    def test_apply_session_helpers_add_PIF(self):
        self.session.PIF.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("PIF.get_X", "ref")

    def test_apply_session_helpers_add_VLAN(self):
        self.session.VLAN.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("VLAN.get_X", "ref")

    def test_apply_session_helpers_add_host(self):
        self.session.host.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("host.get_X", "ref")

    def test_apply_session_helpers_add_network(self):
        self.session.network.get_X("ref")
        self.session.call_xenapi.assert_called_once_with("network.get_X",
                                                         "ref")


class CallPluginTestCase(base.TestCase):
    def _get_fake_xapisession(self):
        class FakeXapiSession(session.XenAPISession):
            def __init__(self, **kwargs):
                "Skip the superclass's dirty init"
                self.XenAPI = mock.MagicMock()

        return FakeXapiSession()

    def setUp(self):
        super(CallPluginTestCase, self).setUp()
        self.session = self._get_fake_xapisession()

    def test_serialized_with_retry_socket_error_conn_reset(self):
        exc = socket.error()
        exc.errno = errno.ECONNRESET
        plugin = 'glance'
        fn = 'download_vhd'
        num_retries = 1
        callback = None
        retry_cb = mock.Mock()
        with mock.patch.object(self.session, 'call_plugin_serialized',
                               spec=True) as call_plugin_serialized:
            call_plugin_serialized.side_effect = exc
            self.assertRaises(
                exception.PluginRetriesExceeded,
                self.session.call_plugin_serialized_with_retry, plugin, fn,
                num_retries, callback, retry_cb)
            call_plugin_serialized.assert_called_with(plugin, fn)
            self.assertEqual(2, call_plugin_serialized.call_count)
            self.assertEqual(2, retry_cb.call_count)

    def test_serialized_with_retry_socket_error_reraised(self):
        exc = socket.error()
        exc.errno = errno.ECONNREFUSED
        plugin = 'glance'
        fn = 'download_vhd'
        num_retries = 1
        callback = None
        retry_cb = mock.Mock()
        with mock.patch.object(
                self.session, 'call_plugin_serialized', spec=True)\
                as call_plugin_serialized:
            call_plugin_serialized.side_effect = exc
            self.assertRaises(
                socket.error, self.session.call_plugin_serialized_with_retry,
                plugin, fn, num_retries, callback, retry_cb)
            call_plugin_serialized.assert_called_once_with(plugin, fn)
            self.assertEqual(0, retry_cb.call_count)

    def test_serialized_with_retry_socket_reset_reraised(self):
        exc = socket.error()
        exc.errno = errno.ECONNRESET
        plugin = 'glance'
        fn = 'download_vhd'
        num_retries = 1
        callback = None
        retry_cb = mock.Mock()
        with mock.patch.object(self.session, 'call_plugin_serialized',
                               spec=True) as call_plugin_serialized:
            call_plugin_serialized.side_effect = exc
            self.assertRaises(
                exception.PluginRetriesExceeded,
                self.session.call_plugin_serialized_with_retry, plugin, fn,
                num_retries, callback, retry_cb)
            call_plugin_serialized.assert_called_with(plugin, fn)
            self.assertEqual(2, call_plugin_serialized.call_count)


class XenAPISessionTestCase(base.TestCase):
    def _get_mock_xapisession(self, software_version):
        class MockXapiSession(session.XenAPISession):
            def __init__(_ignore):
                pass

            def _get_software_version(_ignore):
                return software_version

        return MockXapiSession()

    @mock.patch.object(XenAPI, 'xapi_local')
    def test_local_session(self, mock_xapi_local):
        session = self._get_mock_xapisession({})
        session.is_local_connection = True
        mock_xapi_local.return_value = "local_connection"
        self.assertEqual("local_connection",
                         session._create_session("unix://local"))

    @mock.patch.object(XenAPI, 'Session')
    def test_remote_session(self, mock_session):
        session = self._get_mock_xapisession({})
        session.is_local_connection = False
        mock_session.return_value = "remote_connection"
        self.assertEqual("remote_connection", session._create_session("url"))

    def test_get_product_version_product_brand_does_not_fail(self):
        session = self._get_mock_xapisession(
            {'build_number': '0',
             'date': '2012-08-03',
             'hostname': 'komainu',
             'linux': '3.2.0-27-generic',
             'network_backend': 'bridge',
             'platform_name': 'XCP_Kronos',
             'platform_version': '1.6.0',
             'xapi': '1.3',
             'xen': '4.1.2',
             'xencenter_max': '1.10',
             'xencenter_min': '1.10'})

        self.assertEqual(
            ((1, 6, 0), None),
            session._get_product_version_and_brand()
        )

    def test_get_product_version_product_brand_xs_6(self):
        session = self._get_mock_xapisession(
            {'product_brand': 'XenServer',
             'product_version': '6.0.50',
             'platform_version': '0.0.1'})

        self.assertEqual(
            ((6, 0, 50), 'XenServer'),
            session._get_product_version_and_brand()
        )

    def test_verify_plugin_version_same(self):
        session = self._get_mock_xapisession({})
        session.PLUGIN_REQUIRED_VERSION = '2.4'
        with mock.patch.object(session, 'call_plugin_serialized',
                               spec=True) as call_plugin_serialized:
            call_plugin_serialized.return_value = "2.4"
            session._verify_plugin_version()

    def test_verify_plugin_version_compatible(self):
        session = self._get_mock_xapisession({})
        session.PLUGIN_REQUIRED_VERSION = '2.4'
        with mock.patch.object(session, 'call_plugin_serialized',
                               spec=True) as call_plugin_serialized:
            call_plugin_serialized.return_value = "2.5"
            session._verify_plugin_version()

    def test_verify_plugin_version_bad_maj(self):
        session = self._get_mock_xapisession({})
        session.PLUGIN_REQUIRED_VERSION = '2.4'
        with mock.patch.object(session, 'call_plugin_serialized',
                               spec=True) as call_plugin_serialized:
            call_plugin_serialized.return_value = "3.0"
            self.assertRaises(XenAPI.Failure, session._verify_plugin_version)

    def test_verify_plugin_version_bad_min(self):
        session = self._get_mock_xapisession({})
        session.PLUGIN_REQUIRED_VERSION = '2.4'
        with mock.patch.object(session, 'call_plugin_serialized',
                               spec=True) as call_plugin_serialized:
            call_plugin_serialized.return_value = "2.3"
            self.assertRaises(XenAPI.Failure, session._verify_plugin_version)

    def test_verify_current_version_matches(self):
        session = self._get_mock_xapisession({})

        # Import the plugin to extract its version
        path = os.path.dirname(__file__)
        rel_path_elem = "../../dom0/etc/xapi.d/plugins/dom0_plugin_version.py"
        for elem in rel_path_elem.split('/'):
            path = os.path.join(path, elem)
        path = os.path.realpath(path)

        plugin_version = None
        with open(path) as plugin_file:
            for line in plugin_file:
                if "PLUGIN_VERSION = " in line:
                    plugin_version = line.strip()[17:].strip('"')

        self.assertEqual(session.PLUGIN_REQUIRED_VERSION,
                         plugin_version)