def test_get_volume_stats_with_non_zero_reserved_percentage(self): """get_volume_stats must fill the correct values.""" self.configuration.reserved_percentage = 10.0 drv = nfs.NfsDriver(configuration=self.configuration) drv._mounted_shares = [self.TEST_NFS_EXPORT1, self.TEST_NFS_EXPORT2] with mock.patch.object(drv, '_ensure_shares_mounted') as mock_ensure_share: with mock.patch.object( drv, '_get_capacity_info') as mock_get_capacity_info: mock_get_capacity_info.side_effect = [ (10 * units.Gi, 2 * units.Gi, 2 * units.Gi), (20 * units.Gi, 3 * units.Gi, 3 * units.Gi) ] drv._ensure_shares_mounted() drv.get_volume_stats() calls = [ mock.call(self.TEST_NFS_EXPORT1), mock.call(self.TEST_NFS_EXPORT2) ] mock_get_capacity_info.assert_has_calls(calls) self.assertTrue(mock_ensure_share.called) self.assertEqual(30.0, drv._stats['total_capacity_gb']) self.assertEqual(5.0, drv._stats['free_capacity_gb']) self.assertEqual(10.0, drv._stats['reserved_percentage'])
def setUp(self): super(NfsDriverTestCase, self).setUp() self.configuration = mock.Mock(conf.Configuration) self.configuration.append_config_values(mock.ANY) self.configuration.max_over_subscription_ratio = 1.0 self.configuration.reserved_percentage = 5 self.configuration.nfs_shares_config = None self.configuration.nfs_sparsed_volumes = True self.configuration.nfs_reserved_percentage = 5.0 self.configuration.nfs_mount_point_base = self.TEST_MNT_POINT_BASE self.configuration.nfs_mount_options = None self.configuration.nfs_mount_attempts = 3 self.configuration.nfs_qcow2_volumes = False self.configuration.nas_secure_file_permissions = 'false' self.configuration.nas_secure_file_operations = 'false' self.configuration.nas_host = None self.configuration.nas_share_path = None self.configuration.nas_mount_options = None self.configuration.volume_dd_blocksize = '1M' self._driver = nfs.NfsDriver(configuration=self.configuration) self._driver.shares = {} mock_exc = mock.patch.object(self._driver, '_execute') self._execute = mock_exc.start() self.addCleanup(mock_exc.stop) self.context = context.get_admin_context()
def test_get_volume_stats_with_non_zero_reserved_percentage(self): """get_volume_stats must fill the correct values.""" mox = self.mox self.configuration.reserved_percentage = 10.0 drv = nfs.NfsDriver(configuration=self.configuration) drv._mounted_shares = [self.TEST_NFS_EXPORT1, self.TEST_NFS_EXPORT2] mox.StubOutWithMock(drv, '_ensure_shares_mounted') mox.StubOutWithMock(drv, '_get_capacity_info') drv._ensure_shares_mounted() drv._get_capacity_info(self.TEST_NFS_EXPORT1).\ AndReturn((10 * units.Gi, 2 * units.Gi, 2 * units.Gi)) drv._get_capacity_info(self.TEST_NFS_EXPORT2).\ AndReturn((20 * units.Gi, 3 * units.Gi, 3 * units.Gi)) mox.ReplayAll() drv.get_volume_stats() self.assertEqual(30.0, drv._stats['total_capacity_gb']) self.assertEqual(5.0, drv._stats['free_capacity_gb']) self.assertEqual(10.0, drv._stats['reserved_percentage']) mox.VerifyAll()
def setUp(self): super(NfsDriverTestCase, self).setUp() self.mox = mox_lib.Mox() self.stubs = stubout.StubOutForTesting() self.configuration = mox_lib.MockObject(conf.Configuration) self.configuration.append_config_values(mox_lib.IgnoreArg()) self.configuration.nfs_shares_config = None self.configuration.nfs_sparsed_volumes = True self.configuration.nfs_used_ratio = 0.95 self.configuration.nfs_oversub_ratio = 1.0 self.configuration.nfs_mount_point_base = self.TEST_MNT_POINT_BASE self.configuration.nfs_mount_options = None self.configuration.nfs_mount_attempts = 3 self.configuration.nfs_qcow2_volumes = False self.configuration.nas_secure_file_permissions = 'false' self.configuration.nas_secure_file_operations = 'false' self.configuration.nas_ip = None self.configuration.nas_share_path = None self.configuration.nas_mount_options = None self.configuration.reserved_percentage = 0 self.configuration.volume_dd_blocksize = '1M' self._driver = nfs.NfsDriver(configuration=self.configuration) self._driver.shares = {} self.addCleanup(self.stubs.UnsetAll) self.addCleanup(self.mox.UnsetStubs)
def test_init_should_throw_error_if_used_ratio_less_than_zero(self): """__init__ should throw error if nfs_used_ratio is less than 0.""" self.override_config('nfs_used_ratio', -1) with self.assertRaisesRegex(exception.InvalidConfigurationValue, ".*'nfs_used_ratio' invalid.*"): nfs.NfsDriver(configuration=self.configuration)
def test_init_should_throw_error_if_used_ratio_greater_than_one(self): """__init__ should throw error if nfs_used_ratio is greater than 1.""" self.override_config('nfs_used_ratio', 2) with self.assertRaisesRegex(exception.InvalidConfigurationValue, ".*'nfs_used_ratio' invalid.*"): nfs.NfsDriver(configuration=self.configuration)
def setUp(self): self._mox = mox_lib.Mox() self.stubs = stubout.StubOutForTesting() self.configuration = mox_lib.MockObject(conf.Configuration) self.configuration.append_config_values(mox_lib.IgnoreArg()) self.configuration.nfs_shares_config = None self.configuration.nfs_mount_point_base = '$state_path/mnt' self.configuration.nfs_disk_util = 'df' self.configuration.nfs_sparsed_volumes = True self._driver = nfs.NfsDriver(configuration=self.configuration)
def test_update_migrated_volume_is_there(self): """Ensure that driver.update_migrated_volume() is there.""" drv = nfs.NfsDriver(configuration=self.configuration) v1 = DumbVolume() v2 = DumbVolume() self.assertRaises(NotImplementedError, drv.update_migrated_volume, self.context, v1, v2, mock.sentinel)
def test_retype_is_there(self): "Ensure that driver.retype() is there." "" drv = nfs.NfsDriver(configuration=self.configuration) v1 = DumbVolume() ret = drv.retype(self.context, v1, mock.sentinel.new_type, mock.sentinel.diff, mock.sentinel.host) self.assertEqual((False, None), ret)
def _test_update_migrated_volume(self, volume_status, rename_volume, rename_exception=False): drv = nfs.NfsDriver(configuration=self.configuration) fake_volume_id = 'f51b5730-13b7-11e6-a238-fa163e67a298' fake_new_volume_id = '12341234-13b7-11e6-a238-fa163e67a298' fake_provider_source = 'fake_provider_source' fake_provider = 'fake_provider' base_dir = '/dir_base/' volume_name_template = 'volume-%s' original_volume_name = volume_name_template % fake_volume_id current_name = volume_name_template % fake_new_volume_id original_volume_path = base_dir + original_volume_name current_path = base_dir + current_name volume = fake_volume.fake_volume_obj( self.context, id=fake_volume_id, size=1, provider_location=fake_provider_source, _name_id=None) new_volume = fake_volume.fake_volume_obj( self.context, id=fake_new_volume_id, size=1, provider_location=fake_provider, _name_id=None) with mock.patch.object(drv, 'local_path') as local_path: local_path.return_value = base_dir + current_name if volume_status == 'in-use': update = drv.update_migrated_volume(self.context, volume, new_volume, volume_status) self.assertEqual({'_name_id': fake_new_volume_id, 'provider_location': fake_provider}, update) elif rename_exception: rename_volume.side_effect = OSError update = drv.update_migrated_volume(self.context, volume, new_volume, volume_status) rename_volume.assert_called_once_with(current_path, original_volume_path) self.assertEqual({'_name_id': fake_new_volume_id, 'provider_location': fake_provider}, update) else: update = drv.update_migrated_volume(self.context, volume, new_volume, volume_status) rename_volume.assert_called_once_with(current_path, original_volume_path) self.assertEqual({'_name_id': None, 'provider_location': fake_provider}, update)
def test_setup_should_throw_error_if_shares_config_not_configured(self): """do_setup should throw error if shares config is not configured.""" self.override_config('nfs_shares_config', None) drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') with testtools.ExpectedException(exception.NfsException, ".*no NFS config file configured.*"): drv.do_setup(self.context) self.assertEqual(0, mock_os_path_exists.call_count)
def test_setup_should_throw_error_if_shares_file_does_not_exist(self): """do_setup should throw error if shares file does not exist.""" drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') mock_os_path_exists.return_value = False with testtools.ExpectedException(exception.NfsException, "NFS config file.*doesn't exist"): drv.do_setup(self.context) mock_os_path_exists.assert_has_calls( [mock.call(self.configuration.nfs_shares_config)])
def test_get_mount_point_for_share_given_extra_slash_in_state_path(self): """_get_mount_point_for_share should calculate correct value.""" # This test gets called with the extra slash self.configuration.nfs_mount_point_base = ( self.TEST_MNT_POINT_BASE_EXTRA_SLASH) # The driver gets called with the correct configuration and removes # the extra slash drv = nfs.NfsDriver(configuration=self.configuration) self.assertEqual('/opt/stack/data/cinder/mnt', drv.base) self.assertEqual( '/opt/stack/data/cinder/mnt/2f4f60214cf43c595666dd815f0360a4', drv._get_mount_point_for_share(self.TEST_NFS_EXPORT1))
def test_setup_should_throw_error_if_used_ratio_less_than_zero(self): """do_setup should throw error if nfs_used_ratio is less than 0.""" self.override_config('nfs_used_ratio', -1) drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') mock_os_path_exists.return_value = True with testtools.ExpectedException(exception.InvalidConfigurationValue, ".*'nfs_used_ratio' invalid.*"): drv.do_setup(self.context) mock_os_path_exists.assert_has_calls( [mock.call(self.configuration.nfs_shares_config)])
def test_setup_should_throw_error_if_used_ratio_greater_than_one(self): """do_setup should throw error if nfs_used_ratio is greater than 1.""" self.override_config('nfs_used_ratio', 2) drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') mock_os_path_exists.return_value = True with self.assertRaisesRegex(exception.InvalidConfigurationValue, ".*'nfs_used_ratio' invalid.*"): drv.do_setup(self.context) mock_os_path_exists.assert_has_calls( [mock.call(self.configuration.nfs_shares_config)])
def setUp(self): super(NfsDriverTestCase, self).setUp() self._mox = mox_lib.Mox() self.stubs = stubout.StubOutForTesting() self.configuration = mox_lib.MockObject(conf.Configuration) self.configuration.append_config_values(mox_lib.IgnoreArg()) self.configuration.nfs_shares_config = None self.configuration.nfs_sparsed_volumes = True self.configuration.nfs_used_ratio = 0.95 self.configuration.nfs_oversub_ratio = 1.0 self._driver = nfs.NfsDriver(configuration=self.configuration) self._driver.shares = {} self._driver._remotefsclient._mount_options = None self._driver._remotefsclient._mount_base = self.TEST_MNT_POINT_BASE self.addCleanup(self.stubs.UnsetAll) self.addCleanup(self._mox.UnsetStubs)
def test_setup_used_ratio_default_value(self): """do_setup should work with default value for nfs_used_ratio.""" drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') mock_os_path_exists.return_value = True mock_execute = self.mock_object(drv, '_execute') mock_set_security = self.mock_object(drv, 'set_nas_security_options') drv.do_setup(self.context) mock_os_path_exists.assert_has_calls( [mock.call(self.configuration.nfs_shares_config)]) self.assertEqual(1, mock_execute.call_count) self.assertEqual(1, mock_set_security.call_count) self.assertEqual(self.configuration.nfs_used_ratio, 0.95)
def test_setup_should_throw_exception_if_nfs_client_is_not_installed(self): """do_setup should throw error if nfs client is not installed.""" drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') mock_os_path_exists.return_value = True mock_execute = self.mock_object(drv, '_execute') mock_execute.side_effect = OSError(errno.ENOENT, 'No such file or directory.') with self.assertRaisesRegex(exception.NfsException, 'mount.nfs is not installed'): drv.do_setup(self.context) mock_os_path_exists.assert_has_calls( [mock.call(self.configuration.nfs_shares_config)]) mock_execute.assert_has_calls( [mock.call('mount.nfs', check_exit_code=False, run_as_root=False)])
def test_setup_should_throw_exception_if_mount_nfs_command_fails(self): """do_setup should throw error if mount.nfs fails with OSError This test covers the OSError path when mount.nfs is installed. """ drv = nfs.NfsDriver(configuration=self.configuration) mock_os_path_exists = self.mock_object(os.path, 'exists') mock_os_path_exists.return_value = True mock_execute = self.mock_object(drv, '_execute') mock_execute.side_effect = OSError(errno.EPERM, 'Operation... BROKEN') with self.assertRaisesRegex(OSError, '.*Operation... BROKEN'): drv.do_setup(self.context) mock_os_path_exists.assert_has_calls( [mock.call(self.configuration.nfs_shares_config)]) mock_execute.assert_has_calls( [mock.call('mount.nfs', check_exit_code=False, run_as_root=False)])
def _test_update_migrated_volume(self, volume_status, rename_volume, rename_exception=False): drv = nfs.NfsDriver(configuration=self.configuration) fake_volume_id = 'vol1' fake_new_volume_id = 'vol2' fake_provider_source = 'fake_provider_source' fake_provider = 'fake_provider' base_dir = '/dir_base/' volume_name_template = 'volume-%s' original_volume_name = volume_name_template % fake_volume_id current_name = volume_name_template % fake_new_volume_id original_volume_path = base_dir + original_volume_name current_path = base_dir + current_name fake_volume = { 'size': 1, 'id': fake_volume_id, 'provider_location': fake_provider_source, '_name_id': None } fake_new_volume = { 'size': 1, 'id': fake_new_volume_id, 'provider_location': fake_provider, '_name_id': None } with mock.patch.object(drv, 'local_path') as local_path: local_path.return_value = base_dir + current_name if volume_status == 'in-use': update = drv.update_migrated_volume(self.context, fake_volume, fake_new_volume, volume_status) self.assertEqual( { '_name_id': fake_new_volume_id, 'provider_location': fake_provider }, update) elif rename_exception: rename_volume.side_effect = OSError update = drv.update_migrated_volume(self.context, fake_volume, fake_new_volume, volume_status) rename_volume.assert_called_once_with(current_path, original_volume_path) self.assertEqual( { '_name_id': fake_new_volume_id, 'provider_location': fake_provider }, update) else: update = drv.update_migrated_volume(self.context, fake_volume, fake_new_volume, volume_status) rename_volume.assert_called_once_with(current_path, original_volume_path) self.assertEqual( { '_name_id': None, 'provider_location': fake_provider }, update)