This file is indexed.

/usr/lib/python3/dist-packages/os_brick/tests/windows/test_iscsi.py is in python3-os-brick 2.3.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# Copyright 2016 Cloudbase Solutions Srl
# 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 ddt
import mock
from os_win import exceptions as os_win_exc

from os_brick import exception
from os_brick.initiator.windows import iscsi
from os_brick.tests.windows import test_base


@ddt.ddt
class WindowsISCSIConnectorTestCase(test_base.WindowsConnectorTestBase):
    @mock.patch.object(iscsi.WindowsISCSIConnector, 'validate_initiators')
    def setUp(self, mock_validate_connectors):
        super(WindowsISCSIConnectorTestCase, self).setUp()

        self._diskutils = mock.Mock()
        self._iscsi_utils = mock.Mock()

        self._connector = iscsi.WindowsISCSIConnector(
            device_scan_interval=mock.sentinel.rescan_interval)
        self._connector._diskutils = self._diskutils
        self._connector._iscsi_utils = self._iscsi_utils

    @ddt.data({'requested_initiators': [mock.sentinel.initiator_0],
               'available_initiators': [mock.sentinel.initiator_0,
                                        mock.sentinel.initiator_1]},
              {'requested_initiators': [mock.sentinel.initiator_0],
               'available_initiators': [mock.sentinel.initiator_1]},
              {'requested_initiators': [],
               'available_initiators': [mock.sentinel.software_initiator]})
    @ddt.unpack
    def test_validate_initiators(self, requested_initiators,
                                 available_initiators):
        self._iscsi_utils.get_iscsi_initiators.return_value = (
            available_initiators)
        self._connector.initiator_list = requested_initiators

        expected_valid_initiator = not (
            set(requested_initiators).difference(set(available_initiators)))
        valid_initiator = self._connector.validate_initiators()

        self.assertEqual(expected_valid_initiator, valid_initiator)

    def test_get_initiator(self):
        initiator = self._connector.get_initiator()
        self.assertEqual(self._iscsi_utils.get_iscsi_initiator.return_value,
                         initiator)

    @mock.patch.object(iscsi, 'utilsfactory')
    def test_get_connector_properties(self, mock_utilsfactory):
        mock_iscsi_utils = (
            mock_utilsfactory.get_iscsi_initiator_utils.return_value)

        props = self._connector.get_connector_properties()
        expected_props = dict(
            initiator=mock_iscsi_utils.get_iscsi_initiator.return_value)

        self.assertEqual(expected_props, props)

    @mock.patch.object(iscsi.WindowsISCSIConnector, '_get_all_targets')
    def test_get_all_paths(self, mock_get_all_targets):
        initiators = [mock.sentinel.initiator_0, mock.sentinel.initiator_1]
        all_targets = [(mock.sentinel.portal_0, mock.sentinel.target_0,
                        mock.sentinel.lun_0),
                       (mock.sentinel.portal_1, mock.sentinel.target_1,
                        mock.sentinel.lun_1)]

        self._connector.initiator_list = initiators
        mock_get_all_targets.return_value = all_targets

        expected_paths = [
            (initiator_name, target_portal, target_iqn, target_lun)
            for target_portal, target_iqn, target_lun in all_targets
            for initiator_name in initiators]
        all_paths = self._connector._get_all_paths(mock.sentinel.conn_props)

        self.assertEqual(expected_paths, all_paths)
        mock_get_all_targets.assert_called_once_with(mock.sentinel.conn_props)

    @ddt.data(True, False)
    @mock.patch.object(iscsi.WindowsISCSIConnector, '_get_scsi_wwn')
    @mock.patch.object(iscsi.WindowsISCSIConnector, '_get_all_paths')
    def test_connect_volume(self, use_multipath,
                            mock_get_all_paths, mock_get_scsi_wwn):
        fake_paths = [(mock.sentinel.initiator_name,
                       mock.sentinel.target_portal,
                       mock.sentinel.target_iqn,
                       mock.sentinel.target_lun)] * 3
        fake_conn_props = dict(auth_username=mock.sentinel.auth_username,
                               auth_password=mock.sentinel.auth_password)

        mock_get_all_paths.return_value = fake_paths
        self._iscsi_utils.login_storage_target.side_effect = [
            os_win_exc.OSWinException, None, None]
        self._iscsi_utils.get_device_number_and_path.return_value = (
            mock.sentinel.device_number, mock.sentinel.device_path)
        self._connector.use_multipath = use_multipath

        device_info = self._connector.connect_volume(fake_conn_props)
        expected_device_info = dict(type='block',
                                    path=mock.sentinel.device_path,
                                    number=mock.sentinel.device_number,
                                    scsi_wwn=mock_get_scsi_wwn.return_value)

        self.assertEqual(expected_device_info, device_info)

        mock_get_all_paths.assert_called_once_with(fake_conn_props)
        expected_login_attempts = 3 if use_multipath else 2
        self._iscsi_utils.login_storage_target.assert_has_calls(
            [mock.call(target_lun=mock.sentinel.target_lun,
                       target_iqn=mock.sentinel.target_iqn,
                       target_portal=mock.sentinel.target_portal,
                       auth_username=mock.sentinel.auth_username,
                       auth_password=mock.sentinel.auth_password,
                       mpio_enabled=use_multipath,
                       initiator_name=mock.sentinel.initiator_name,
                       ensure_lun_available=False)] *
            expected_login_attempts)
        self._iscsi_utils.ensure_lun_available.assert_has_calls(
            [mock.call(target_iqn=mock.sentinel.target_iqn,
                       target_lun=mock.sentinel.target_lun,
                       rescan_attempts=(
                           self._connector.device_scan_attempts),
                       retry_interval=mock.sentinel.rescan_interval)] *
            (expected_login_attempts - 1))

        self._iscsi_utils.get_device_number_and_path.assert_called_once_with(
            mock.sentinel.target_iqn, mock.sentinel.target_lun)
        mock_get_scsi_wwn.assert_called_once_with(mock.sentinel.device_number)

    @mock.patch.object(iscsi.WindowsISCSIConnector, '_get_all_paths')
    def test_connect_volume_exc(self, mock_get_all_paths):
        fake_paths = [(mock.sentinel.initiator_name,
                       mock.sentinel.target_portal,
                       mock.sentinel.target_iqn,
                       mock.sentinel.target_lun)] * 3

        mock_get_all_paths.return_value = fake_paths
        self._iscsi_utils.login_storage_target.side_effect = (
            os_win_exc.OSWinException)
        self._connector.use_multipath = True

        self.assertRaises(exception.BrickException,
                          self._connector.connect_volume,
                          connection_properties={})

    @mock.patch.object(iscsi.WindowsISCSIConnector, '_get_all_targets')
    def test_disconnect_volume(self, mock_get_all_targets):
        targets = [
            (mock.sentinel.portal_0, mock.sentinel.tg_0, mock.sentinel.lun_0),
            (mock.sentinel.portal_1, mock.sentinel.tg_1, mock.sentinel.lun_1)]

        mock_get_all_targets.return_value = targets
        self._iscsi_utils.get_target_luns.return_value = [mock.sentinel.lun_0]

        self._connector.disconnect_volume(mock.sentinel.conn_props)

        self._diskutils.rescan_disks.assert_called_once_with()
        mock_get_all_targets.assert_called_once_with(mock.sentinel.conn_props)
        self._iscsi_utils.logout_storage_target.assert_called_once_with(
            mock.sentinel.tg_0)
        self._iscsi_utils.get_target_luns.assert_has_calls(
            [mock.call(mock.sentinel.tg_0), mock.call(mock.sentinel.tg_1)])

    @mock.patch.object(iscsi.WindowsISCSIConnector, '_get_all_targets')
    @mock.patch.object(iscsi.WindowsISCSIConnector, '_check_device_paths')
    def test_get_volume_paths(self, mock_check_dev_paths,
                              mock_get_all_targets):
        targets = [
            (mock.sentinel.portal_0, mock.sentinel.tg_0, mock.sentinel.lun_0),
            (mock.sentinel.portal_1, mock.sentinel.tg_1, mock.sentinel.lun_1)]

        mock_get_all_targets.return_value = targets
        self._iscsi_utils.get_device_number_and_path.return_value = [
            mock.sentinel.dev_num, mock.sentinel.dev_path]

        volume_paths = self._connector.get_volume_paths(
            mock.sentinel.conn_props)
        expected_paths = [mock.sentinel.dev_path]

        self.assertEqual(expected_paths, volume_paths)
        mock_check_dev_paths.assert_called_once_with(set(expected_paths))