summaryrefslogtreecommitdiff
path: root/nova/tests/virt/xenapi/test_volume_utils.py
blob: 2acbaac102f2f23bf8e412e4f5d854863568610e (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
85
86
87
# Copyright 2013 OpenStack Foundation
# All Rights Reserved.
#
#    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 mock

from nova.tests.virt.xenapi import stubs
from nova import utils
from nova.virt.xenapi import volume_utils


class CallXenAPIHelpersTestCase(stubs.XenAPITestBaseNoDB):
    def test_vbd_plug(self):
        session = mock.Mock()
        volume_utils.vbd_plug(session, "vbd_ref", "vm_ref:123")
        session.call_xenapi.assert_called_once_with("VBD.plug", "vbd_ref")

    @mock.patch.object(utils, 'synchronized')
    def test_vbd_plug_check_synchronized(self, mock_synchronized):
        session = mock.Mock()
        volume_utils.vbd_plug(session, "vbd_ref", "vm_ref:123")
        mock_synchronized.assert_called_once_with("xenapi-events-vm_ref:123")

    def test_vbd_unplug(self):
        session = mock.Mock()
        volume_utils.vbd_unplug(session, "vbd_ref", "vm_ref:123")
        session.call_xenapi.assert_called_once_with("VBD.unplug", "vbd_ref")

    @mock.patch.object(utils, 'synchronized')
    def test_vbd_unplug_check_synchronized(self, mock_synchronized):
        session = mock.Mock()
        volume_utils.vbd_unplug(session, "vbd_ref", "vm_ref:123")
        mock_synchronized.assert_called_once_with("xenapi-events-vm_ref:123")


class SROps(stubs.XenAPITestBaseNoDB):
    def test_find_sr_valid_uuid(self):
        self.session = mock.Mock()
        self.session.call_xenapi.return_value = 'sr_ref'
        self.assertEqual(volume_utils.find_sr_by_uuid(self.session,
                                                      'sr_uuid'),
                         'sr_ref')

    def test_find_sr_invalid_uuid(self):
        class UUIDException(Exception):
            details = ["UUID_INVALID", "", "", ""]

        self.session = mock.Mock()
        self.session.XenAPI.Failure = UUIDException
        self.session.call_xenapi.side_effect = UUIDException
        self.assertEqual(volume_utils.find_sr_by_uuid(self.session,
                                                      'sr_uuid'),
                         None)


class ISCSIParametersTestCase(stubs.XenAPITestBaseNoDB):
    def test_target_host(self):
        self.assertEqual(volume_utils._get_target_host('host:port'),
                         'host')

        self.assertEqual(volume_utils._get_target_host('host'),
                         'host')

        # There is no default value
        self.assertEqual(volume_utils._get_target_host(':port'),
                         None)

        self.assertEqual(volume_utils._get_target_host(None),
                         None)

    def test_target_port(self):
        self.assertEqual(volume_utils._get_target_port('host:port'),
                         'port')

        self.assertEqual(volume_utils._get_target_port('host'),
                         '3260')