"Fossies" - the Fresh Open Source Software Archive

Member "cinder-13.0.7/cinder/tests/unit/targets/test_nvmet_driver.py" (4 Oct 2019, 10630 Bytes) of package /linux/misc/openstack/cinder-13.0.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "test_nvmet_driver.py": 14.0.2_vs_15.0.0.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 import mock
   14 
   15 from oslo_utils import timeutils
   16 
   17 from cinder import context
   18 from cinder import exception
   19 from cinder.tests.unit.targets import targets_fixture as tf
   20 from cinder import utils
   21 from cinder.volume.targets import nvmet
   22 
   23 
   24 class TestNVMETDriver(tf.TargetDriverFixture):
   25 
   26     def setUp(self):
   27         super(TestNVMETDriver, self).setUp()
   28 
   29         self.configuration.target_protocol = 'nvmet_rdma'
   30         self.target = nvmet.NVMET(root_helper=utils.get_root_helper(),
   31                                   configuration=self.configuration)
   32 
   33         self.target_ip = '192.168.0.1'
   34         self.target_port = '1234'
   35         self.nvmet_subsystem_name = self.configuration.target_prefix
   36         self.nvmet_ns_id = self.configuration.nvmet_ns_id
   37         self.nvmet_port_id = self.configuration.nvmet_port_id
   38         self.nvme_transport_type = 'rdma'
   39 
   40         self.fake_volume_id = 'c446b9a2-c968-4260-b95f-a18a7b41c004'
   41         self.testvol_path = (
   42             '/dev/stack-volumes-lvmdriver-1/volume-%s' % self.fake_volume_id)
   43         self.fake_project_id = 'ed2c1fd4-5555-1111-aa15-123b93f75cba'
   44         self.testvol = (
   45             {'project_id': self.fake_project_id,
   46              'name': 'testvol',
   47              'size': 1,
   48              'id': self.fake_volume_id,
   49              'volume_type_id': None,
   50              'provider_location': self.target.get_nvmeof_location(
   51                  "nqn.%s-%s" % (self.nvmet_subsystem_name,
   52                                 self.fake_volume_id),
   53                  self.target_ip, self.target_port, self.nvme_transport_type,
   54                  self.nvmet_ns_id),
   55              'provider_auth': None,
   56              'provider_geometry': None,
   57              'created_at': timeutils.utcnow(),
   58              'host': 'fake_host@lvm#lvm'})
   59 
   60     @mock.patch.object(nvmet.NVMET, '_get_nvmf_subsystem')
   61     @mock.patch.object(nvmet.NVMET, '_get_available_nvmf_subsystems')
   62     @mock.patch.object(nvmet.NVMET, '_add_nvmf_subsystem')
   63     def test_create_export(self, mock_add_nvmf_subsystem,
   64                            mock_get_available_nvmf_subsystems,
   65                            mock_get_nvmf_subsystem):
   66 
   67         mock_testvol = self.testvol
   68         mock_testvol_path = self.testvol_path
   69         ctxt = context.get_admin_context()
   70         mock_get_available_nvmf_subsystems.return_value = {
   71             "subsystems": [],
   72             "hosts": [],
   73             "ports": [
   74                 {"subsystems": [],
   75                  "referrals": [],
   76                  "portid": 1,
   77                  "addr":
   78                  {"treq": "not specified",
   79                           "trtype": "rdma",
   80                           "adrfam": "ipv4",
   81                           "trsvcid": self.target_port,
   82                           "traddr":
   83                               self.target_ip
   84                   }
   85                  }]
   86         }
   87         mock_get_nvmf_subsystem.return_value = (
   88             "nqn.%s-%s" % (self.nvmet_subsystem_name,
   89                            mock_testvol['id']))
   90 
   91         mock_add_nvmf_subsystem.return_value = (
   92             "nqn.%s-%s" % (self.nvmet_subsystem_name,
   93                            mock_testvol['id']))
   94 
   95         expected_return = {
   96             'location': self.target.get_nvmeof_location(
   97                 mock_add_nvmf_subsystem.return_value, self.target_ip,
   98                 self.target_port, self.nvme_transport_type, self.nvmet_ns_id),
   99             'auth': ''
  100         }
  101 
  102         self.target.target_ip = self.target_ip
  103         self.target.target_port = self.target_port
  104         self.assertEqual(expected_return,
  105                          self.target.create_export(
  106                              ctxt, mock_testvol,
  107                              mock_testvol_path))
  108 
  109     @mock.patch.object(nvmet.NVMET, '_get_nvmf_subsystem')
  110     @mock.patch.object(nvmet.NVMET, '_get_available_nvmf_subsystems')
  111     @mock.patch.object(nvmet.NVMET, '_add_nvmf_subsystem')
  112     def test_create_export_with_error_add_nvmf_subsystem(
  113             self,
  114             mock_add_nvmf_subsystem,
  115             mock_get_available_nvmf_subsystems,
  116             mock_get_nvmf_subsystem):
  117 
  118         mock_testvol = self.testvol
  119         mock_testvol_path = self.testvol_path
  120         ctxt = context.get_admin_context()
  121         mock_get_available_nvmf_subsystems.return_value = {
  122             "subsystems": [],
  123             "hosts": [],
  124             "ports": [
  125                 {"subsystems": [],
  126                  "referrals": [],
  127                  "portid": 1,
  128                  "addr":
  129                  {"treq": "not specified",
  130                           "trtype": "rdma",
  131                           "adrfam": "ipv4",
  132                           "trsvcid": self.target_port,
  133                           "traddr":
  134                               self.target_ip
  135                   }
  136                  }]
  137         }
  138         mock_get_nvmf_subsystem.return_value = None
  139 
  140         mock_add_nvmf_subsystem.return_value = None
  141 
  142         self.target.target_ip = self.target_ip
  143         self.target.target_port = self.target_port
  144         self.assertRaises(exception.NVMETTargetAddError,
  145                           self.target.create_export,
  146                           ctxt,
  147                           mock_testvol,
  148                           mock_testvol_path)
  149 
  150     @mock.patch.object(nvmet.NVMET, '_get_nvmf_subsystem')
  151     @mock.patch.object(nvmet.NVMET, '_get_available_nvmf_subsystems')
  152     @mock.patch.object(nvmet.NVMET, '_delete_nvmf_subsystem')
  153     def test_remove_export(self, mock_delete_nvmf_subsystem,
  154                            mock_get_available_nvmf_subsystems,
  155                            mock_get_nvmf_subsystem):
  156         mock_testvol = self.testvol
  157         mock_testvol_path = self.testvol_path
  158         ctxt = context.get_admin_context()
  159         mock_get_available_nvmf_subsystems.return_value = {
  160             "subsystems": [
  161                 {"allowed_hosts": [],
  162                  "nqn": "nqn.%s-%s" % (
  163                      self.nvmet_subsystem_name,
  164                      mock_testvol['id']),
  165                  "attr": {"allow_any_host": "1"},
  166                  "namespaces": [
  167                  {"device":
  168                   {"path": mock_testvol_path,
  169                    "nguid":
  170                    "86fab0e0-825d-4f25-a449-28b93c5e8dd6"
  171                    },
  172                   "enable": 1, "nsid":
  173                   self.nvmet_ns_id,
  174                   }]}],
  175             "hosts": [],
  176             "ports": [
  177                 {"subsystems": [
  178                     "nqn.%s-%s" % (self.nvmet_subsystem_name,
  179                                    mock_testvol['id'])],
  180                  "referrals": [],
  181                  "portid": self.nvmet_port_id,
  182                  "addr":
  183                  {"treq": "not specified",
  184                   "trtype": "rdma",
  185                   "adrfam": "ipv4",
  186                   "trsvcid": self.target_port,
  187                   "traddr": self.target_ip}}
  188             ]
  189         }
  190 
  191         mock_get_nvmf_subsystem.return_value = (
  192             "nqn.%s-%s" % (self.nvmet_subsystem_name,
  193                            mock_testvol['id']))
  194         mock_delete_nvmf_subsystem.return_value = (
  195             "nqn.%s-%s" % (self.nvmet_subsystem_name,
  196                            mock_testvol['id']))
  197         expected_return = mock_delete_nvmf_subsystem.return_value
  198         self.assertEqual(expected_return,
  199                          self.target.remove_export(ctxt, mock_testvol))
  200 
  201     @mock.patch.object(nvmet.NVMET, '_get_nvmf_subsystem')
  202     @mock.patch.object(nvmet.NVMET, '_get_available_nvmf_subsystems')
  203     def test_remove_export_with_empty_subsystems(
  204             self,
  205             mock_get_available_nvmf_subsystems,
  206             mock_get_nvmf_subsystem):
  207         mock_testvol = self.testvol
  208         ctxt = context.get_admin_context()
  209         mock_get_available_nvmf_subsystems.return_value = {
  210             "subsystems": [],
  211             "hosts": [],
  212             "ports": []
  213         }
  214         mock_get_nvmf_subsystem.return_value = None
  215         self.assertIsNone(self.target.remove_export(ctxt, mock_testvol))
  216 
  217     @mock.patch.object(nvmet.NVMET, '_get_nvmf_subsystem')
  218     @mock.patch.object(nvmet.NVMET, '_get_available_nvmf_subsystems')
  219     @mock.patch.object(nvmet.NVMET, '_delete_nvmf_subsystem')
  220     def test_remove_export_with_delete_nvmf_subsystem_fails(
  221             self,
  222             moc_delete_nvmf_subsystem,
  223             mock_get_available_nvmf_subsystems,
  224             mock_get_nvmf_subsystem):
  225         mock_testvol = self.testvol
  226         mock_testvol_path = self.testvol_path
  227         ctxt = context.get_admin_context()
  228         mock_get_available_nvmf_subsystems.return_value = {
  229             "subsystems": [
  230                 {"allowed_hosts": [],
  231                  "nqn": "nqn.%s-%s" % (
  232                      self.nvmet_subsystem_name,
  233                      mock_testvol['id']),
  234                  "attr": {"allow_any_host": "1"},
  235                  "namespaces": [
  236                  {"device":
  237                   {"path": mock_testvol_path,
  238                    "nguid":
  239                    "86fab0e0-825d-4f25-a449-28b93c5e8dd6"
  240                    },
  241                   "enable": 1, "nsid":
  242                   self.nvmet_ns_id,
  243                   }]}],
  244             "hosts": [],
  245             "ports": [
  246                 {"subsystems": [
  247                     "nqn.%s-%s" % (self.nvmet_subsystem_name,
  248                                    mock_testvol['id'])],
  249                  "referrals": [],
  250                  "portid": self.nvmet_port_id,
  251                  "addr":
  252                  {"treq": "not specified",
  253                   "trtype": "rdma",
  254                   "adrfam": "ipv4",
  255                   "trsvcid": self.target_port,
  256                   "traddr": self.target_ip}}
  257             ]
  258         }
  259         mock_get_nvmf_subsystem.return_value = (
  260             "nqn.%s-%s" % (self.nvmet_subsystem_name,
  261                            mock_testvol['id']))
  262         moc_delete_nvmf_subsystem.return_value = None
  263         self.assertRaises(exception.NVMETTargetDeleteError,
  264                           self.target.remove_export,
  265                           ctxt,
  266                           mock_testvol)