def test_fetch_gluster_volumes(self, sharemark): vol1_qualified = 'root@host1:/manila-share-1-1G' gmgr_vol1 = common.GlusterManager(vol1_qualified) gmgr_vol1.get_vol_option = mock.Mock( return_value=sharemark[vol1_qualified]) vol2_qualified = 'root@host2:/manila-share-2-2G' gmgr_vol2 = common.GlusterManager(vol2_qualified) gmgr_vol2.get_vol_option = mock.Mock( return_value=sharemark[vol2_qualified]) self.mock_object( self.gmgr1, 'gluster_call', mock.Mock(return_value=(self.glusterfs_server1_volumes, ''))) self.mock_object( self.gmgr2, 'gluster_call', mock.Mock(return_value=(self.glusterfs_server2_volumes, ''))) _glustermanager_calls = (self.gmgr1, gmgr_vol1, self.gmgr2, gmgr_vol2) self.mock_object(self._layout, '_glustermanager', mock.Mock(side_effect=_glustermanager_calls)) expected_output = {} for q, d in self.glusterfs_volumes_dict.items(): if sharemark[q] not in (FAKE_UUID1, FAKE_UUID2): expected_output[q] = d ret = self._layout._fetch_gluster_volumes() test_args = ('volume', 'list') self.gmgr1.gluster_call.assert_called_once_with(*test_args, log=mock.ANY) self.gmgr2.gluster_call.assert_called_once_with(*test_args, log=mock.ANY) gmgr_vol1.get_vol_option.assert_called_once_with('user.manila-share') gmgr_vol2.get_vol_option.assert_called_once_with('user.manila-share') self.assertEqual(expected_output, ret)
def test_fetch_gluster_volumes_no_filter_used(self): vol1_qualified = 'root@host1:/manila-share-1-1G' gmgr_vol1 = common.GlusterManager(vol1_qualified) gmgr_vol1.get_vol_option = mock.Mock() vol2_qualified = 'root@host2:/manila-share-2-2G' gmgr_vol2 = common.GlusterManager(vol2_qualified) gmgr_vol2.get_vol_option = mock.Mock() self.mock_object( self.gmgr1, 'gluster_call', mock.Mock(return_value=(self.glusterfs_server1_volumes, ''))) self.mock_object( self.gmgr2, 'gluster_call', mock.Mock(return_value=(self.glusterfs_server2_volumes, ''))) _glustermanager_calls = (self.gmgr1, gmgr_vol1, self.gmgr2, gmgr_vol2) self.mock_object(self._layout, '_glustermanager', mock.Mock(side_effect=_glustermanager_calls)) expected_output = self.glusterfs_volumes_dict ret = self._layout._fetch_gluster_volumes(filter_used=False) test_args = ('volume', 'list') self.gmgr1.gluster_call.assert_called_once_with(*test_args, log=mock.ANY) self.gmgr2.gluster_call.assert_called_once_with(*test_args, log=mock.ANY) self.assertFalse(gmgr_vol1.get_vol_option.called) self.assertFalse(gmgr_vol2.get_vol_option.called) self.assertEqual(expected_output, ret)
def setUp(self): super(GlusterfsNativeShareDriverTestCase, self).setUp() fake_utils.stub_out_utils_execute(self) self._execute = fake_utils.fake_execute self._context = context.get_admin_context() self.glusterfs_target1 = 'root@host1:/gv1' self.glusterfs_target2 = 'root@host2:/gv2' self.glusterfs_server1 = 'root@host1' self.glusterfs_server2 = 'root@host2' self.glusterfs_server1_volumes = 'manila-share-1-1G\nshare1' self.glusterfs_server2_volumes = 'manila-share-2-2G\nshare2' self.share1 = new_share(export_location=self.glusterfs_target1, status=constants.STATUS_AVAILABLE) self.share2 = new_share(export_location=self.glusterfs_target2, status=constants.STATUS_AVAILABLE) self.gmgr1 = common.GlusterManager(self.glusterfs_server1, self._execute, None, None, requires={'volume': False}) self.gmgr2 = common.GlusterManager(self.glusterfs_server2, self._execute, None, None, requires={'volume': False}) self.glusterfs_volumes_dict = ({ 'root@host1:/manila-share-1-1G': { 'size': 1 }, 'root@host2:/manila-share-2-2G': { 'size': 2 } }) self.glusterfs_used_vols = set( ['root@host1:/manila-share-1-1G', 'root@host2:/manila-share-2-2G']) CONF.set_default('glusterfs_volume_pattern', 'manila-share-\d+-#{size}G$') CONF.set_default('driver_handles_share_servers', False) self.fake_conf = config.Configuration(None) self.mock_object(common.GlusterManager, 'make_gluster_call') self._driver = glusterfs_native.GlusterfsNativeShareDriver( execute=self._execute, configuration=self.fake_conf) self.addCleanup(fake_utils.fake_execute_set_repliers, []) self.addCleanup(fake_utils.fake_execute_clear_log)
def test_deny_access_via_manager_no_dyn_auth(self, falseish): self.mock_object(common, '_restart_gluster_vol', mock.Mock()) access = {'access_type': 'cert', 'access_to': 'client.example.com'} gmgr1 = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) def _get_gluster_vol_option(opt): if opt == 'auth.ssl-allow': return('some.common.name,' + access['access_to']) elif opt == 'dynamic-auth': if falseish == '!': raise exception.ProcessExecutionError(exit_code=1) return falseish self.mock_object( gmgr1, 'get_gluster_vol_option', mock.Mock(side_effect=_get_gluster_vol_option)) self._driver.layout.gluster_used_vols = set([self.glusterfs_target1]) self._driver._deny_access_via_manager(gmgr1, self._context, self.share1, access) gmgr1.get_gluster_vol_option.assert_has_calls( [mock.call(a) for a in ('auth.ssl-allow', 'dynamic-auth')]) test_args = ('volume', 'set', 'gv1', 'auth.ssl-allow', 'some.common.name') gmgr1.gluster_call.assert_called_once_with(*test_args) common._restart_gluster_vol.assert_called_once_with(gmgr1)
def test_deny_access_via_manager_dyn_auth_fail(self, trouble, _exception): self.mock_object(common, '_restart_gluster_vol', mock.Mock()) access = {'access_type': 'cert', 'access_to': 'client.example.com'} gmgr1 = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) def _get_gluster_vol_option(opt): if opt == 'auth.ssl-allow': return ('some.common.name,' + access['access_to']) elif opt == 'server.dynamic-auth': raise trouble self.mock_object(gmgr1, 'get_gluster_vol_option', mock.Mock(side_effect=_get_gluster_vol_option)) self._driver.layout.gluster_used_vols = set([self.glusterfs_target1]) self.assertRaises(_exception, self._driver._deny_access_via_manager, gmgr1, self._context, self.share1, access) gmgr1.get_gluster_vol_option.assert_has_calls( [mock.call(a) for a in ('auth.ssl-allow', 'server.dynamic-auth')]) test_args = ('volume', 'set', 'gv1', 'auth.ssl-allow', 'some.common.name') gmgr1.gluster_call.assert_called_once_with(*test_args) self.assertFalse(common._restart_gluster_vol.called)
def test_deny_access_via_manager_excp(self, trouble, _exception): access = {'access_type': 'cert', 'access_to': 'client.example.com'} test_args = ('volume', 'set', 'gv1', 'auth.ssl-allow', 'some.common.name') def raise_exception(*args, **kwargs): if (args == test_args): raise trouble() self.mock_object(common, '_restart_gluster_vol', mock.Mock()) gmgr1 = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) self.mock_object( gmgr1, 'get_gluster_vol_option', mock.Mock(return_value='some.common.name,' + access['access_to'])) self._driver.layout.gluster_used_vols = set([self.glusterfs_target1]) self.mock_object(gmgr1, 'gluster_call', mock.Mock(side_effect=raise_exception)) self.assertRaises(_exception, self._driver._deny_access_via_manager, gmgr1, self._context, self.share1, access) gmgr1.get_gluster_vol_option.assert_called_once_with('auth.ssl-allow') gmgr1.gluster_call.assert_called_once_with(*test_args) self.assertFalse(common._restart_gluster_vol.called)
def test_create_share(self): self._layout._pop_gluster_vol = mock.Mock( return_value=self.glusterfs_target1) gmgr1 = common.GlusterManager(self.glusterfs_target1) gmgr1.set_vol_option = mock.Mock() self.mock_object(self._layout, '_glustermanager', mock.Mock(return_value=gmgr1)) self.mock_object(self.fake_driver, '_setup_via_manager', mock.Mock(return_value='host1:/gv1')) share = new_share() exp_locn = self._layout.create_share(self._context, share) self._layout._pop_gluster_vol.assert_called_once_with(share['size']) self.fake_driver._setup_via_manager.assert_called_once_with({ 'manager': gmgr1, 'share': share }) self._layout.private_storage.update.assert_called_once_with( share['id'], {'volume': self.glusterfs_target1}) gmgr1.set_vol_option.assert_called_once_with('user.manila-share', share['id']) self.assertEqual('host1:/gv1', exp_locn)
def test_gluster_manager_dict_init_no_vol(self, has_volume): test_addr_dict = {'user': '******', 'host': '127.0.0.1'} test_gluster_manager = common.GlusterManager( test_addr_dict, self.fake_execf, requires={'volume': has_volume}) self.assertIsNone(test_gluster_manager.volume)
def test_deny_access_via_manager_no_dyn_auth(self): self.mock_object(common, '_restart_gluster_vol', mock.Mock()) access = {'access_type': 'cert', 'access_to': 'client.example.com'} gmgr1 = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) def _get_vol_option(opt, **kw): if opt == 'auth.ssl-allow': return ('some.common.name,' + access['access_to']) elif opt == 'server.dynamic-auth': return False self.mock_object(gmgr1, 'get_vol_option', mock.Mock(side_effect=_get_vol_option)) self.mock_object(gmgr1, 'set_vol_option') self._driver.layout.gluster_used_vols = set([self.glusterfs_target1]) self._driver._deny_access_via_manager(gmgr1, self._context, self.share1, access) gmgr1.get_vol_option.assert_has_calls([ mock.call(a, **kw) for a, kw in (('auth.ssl-allow', {}), ('server.dynamic-auth', { 'boolean': True })) ]) test_args = ('auth.ssl-allow', 'some.common.name') gmgr1.set_vol_option.assert_called_once_with(*test_args) common._restart_gluster_vol.assert_called_once_with(gmgr1)
def _glustermanager(self, gluster_address): """Create GlusterManager object for gluster_address.""" return common.GlusterManager( gluster_address, self.driver._execute, self.configuration.glusterfs_path_to_private_key, self.configuration.glusterfs_server_password, requires={'volume': True})
def test_gluster_manager_dict_init_has_vol(self, has_volume): test_addr_dict = {'user': '******', 'host': '127.0.0.1', 'volume': 'testvol', 'path': '/testdir'} test_gluster_manager = common.GlusterManager( test_addr_dict, self.fake_execf, requires={'volume': has_volume}) self.assertEqual('testvol', test_gluster_manager.volume)
def test_restart_gluster_vol(self): gmgr = common.GlusterManager(fakeexport, self._execute, None, None) test_args = [('volume', 'stop', fakevol, '--mode=script'), ('volume', 'start', fakevol)] common._restart_gluster_vol(gmgr) self.assertEqual([mock.call(*test_args[0]), mock.call(*test_args[1])], gmgr.gluster_call.call_args_list)
def test_restart_gluster_vol(self): gmgr = common.GlusterManager(fakeexport, self._execute, None, None) test_args = [(('volume', 'stop', fakevol, '--mode=script'), {'log': mock.ANY}), (('volume', 'start', fakevol), {'log': mock.ANY})] common._restart_gluster_vol(gmgr) self.assertEqual( [mock.call(*arg[0], **arg[1]) for arg in test_args], gmgr.gluster_call.call_args_list)
def test_update_access_via_manager_badcn(self, common_name): gluster_mgr = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) self.mock_object(gluster_mgr, 'get_vol_option', mock.Mock( return_value="glusterCN,oldCN")) self.assertRaises(exception.GlusterfsException, self._driver._update_access_via_manager, gluster_mgr, self._context, self.share1, [{'access_to': common_name}], [])
def setUp(self): super(GlusterManagerTestCase, self).setUp() self.fake_execf = mock.Mock() self.fake_executor = mock.Mock(return_value=('', '')) with mock.patch.object(common.GlusterManager, 'make_gluster_call', return_value=self.fake_executor): self._gluster_manager = common.GlusterManager( '[email protected]:/testvol', self.fake_execf, fake_path_to_private_key, fake_remote_server_password)
def test_ensure_share(self): share = self.share1 gmgr1 = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) self.mock_object(self._layout, '_share_manager', mock.Mock(return_value=gmgr1)) self._layout.ensure_share(self._context, share) self._layout._share_manager.assert_called_once_with(share) self.assertIn(self.glusterfs_target1, self._layout.gluster_used_vols)
def test_gluster_manager_make_gluster_call_bad_policy(self): fake_obj = mock.Mock() fake_execute = mock.Mock() with mock.patch.object(common.ganesha_utils, 'RootExecutor', mock.Mock(return_value=fake_obj)): gluster_manager = common.GlusterManager( '127.0.0.1:/testvol', self.fake_execf) self.assertRaises(TypeError, gluster_manager.make_gluster_call(fake_execute), *fake_args, error_policy='foobar')
def test_set_vol_option_error_relaxed(self): fake_obj = mock.Mock( side_effect=exception.ProcessExecutionError(exit_code=1)) with mock.patch.object(common.ganesha_utils, 'RootExecutor', mock.Mock(return_value=fake_obj)): gluster_manager = common.GlusterManager( '127.0.0.1:/testvol', self.fake_execf) gluster_manager.set_vol_option('an_option', "some_value", ignore_failure=True) self.assertTrue(fake_obj.called)
def test_gluster_manager_make_gluster_call_local(self): fake_obj = mock.Mock() fake_execute = mock.Mock() with mock.patch.object(common.ganesha_utils, 'RootExecutor', mock.Mock(return_value=fake_obj)): gluster_manager = common.GlusterManager('127.0.0.1:/testvol', self.fake_execf) gluster_manager.make_gluster_call(fake_execute)(*fake_args, **fake_kwargs) common.ganesha_utils.RootExecutor.assert_called_with(fake_execute) fake_obj.assert_called_once_with(*(('gluster', ) + fake_args), **fake_kwargs)
def test_fetch_gluster_volumes(self): self.mock_object( self.gmgr1, 'gluster_call', mock.Mock(return_value=(self.glusterfs_server1_volumes, ''))) self.mock_object( self.gmgr2, 'gluster_call', mock.Mock(return_value=(self.glusterfs_server2_volumes, ''))) _glustermanager_calls = ( self.gmgr1, common.GlusterManager('root@host1:/manila-share-1-1G'), self.gmgr2, common.GlusterManager('root@host2:/manila-share-2-2G')) self.mock_object(self._layout, '_glustermanager', mock.Mock(side_effect=_glustermanager_calls)) expected_output = self.glusterfs_volumes_dict ret = self._layout._fetch_gluster_volumes() test_args = ('volume', 'list') self.gmgr1.gluster_call.assert_called_once_with(*test_args) self.gmgr2.gluster_call.assert_called_once_with(*test_args) self.assertEqual(expected_output, ret)
def test_set_vol_option_error(self, kwargs): fake_obj = mock.Mock( side_effect=exception.ProcessExecutionError(exit_code=1)) with mock.patch.object(common.ganesha_utils, 'RootExecutor', mock.Mock(return_value=fake_obj)): gluster_manager = common.GlusterManager( '127.0.0.1:/testvol', self.fake_execf) self.assertRaises(exception.GlusterfsException, gluster_manager.set_vol_option, 'an_option', "some_value", **kwargs) self.assertTrue(fake_obj.called)
def test_allow_access_via_manager_with_share_having_access(self): access = {'access_type': 'cert', 'access_to': 'client.example.com'} gmgr1 = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) self.mock_object( gmgr1, 'get_gluster_vol_option', mock.Mock(return_value='some.common.name,' + access['access_to'])) self._driver.layout.gluster_used_vols = set([self.glusterfs_target1]) self._driver._allow_access_via_manager(gmgr1, self._context, self.share1, access) gmgr1.get_gluster_vol_option.assert_called_once_with('auth.ssl-allow') self.assertFalse(gmgr1.gluster_call.called)
def test_restart_gluster_vol_excp1(self): gmgr = common.GlusterManager(fakeexport, self._execute, None, None) test_args = ('volume', 'stop', fakevol, '--mode=script') def raise_exception(*args, **kwargs): if (args == test_args): raise exception.ProcessExecutionError() self.mock_object(gmgr, 'gluster_call', mock.Mock(side_effect=raise_exception)) self.assertRaises(exception.GlusterfsException, common._restart_gluster_vol, gmgr) self.assertEqual([mock.call(*test_args)], gmgr.gluster_call.call_args_list)
def test_update_access_via_manager_restart(self): gluster_mgr = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) self.mock_object( gluster_mgr, 'get_vol_option', mock.Mock( side_effect=lambda a, *x, **kw: { 'auth.ssl-allow': "glusterCN,oldCN", 'server.dynamic-auth': False}[a])) self.mock_object(gluster_mgr, 'set_vol_option') self.mock_object(common, '_restart_gluster_vol') self._driver._update_access_via_manager( gluster_mgr, self._context, self.share1, [], [{'access_to': "oldCN"}]) common._restart_gluster_vol.assert_called_once_with(gluster_mgr)
def test_gluster_manager_make_gluster_call_remote(self): fake_obj = mock.Mock() fake_execute = mock.Mock() with mock.patch.object(common.ganesha_utils, 'SSHExecutor', mock.Mock(return_value=fake_obj)): gluster_manager = common.GlusterManager( '[email protected]:/testvol', self.fake_execf, fake_path_to_private_key, fake_remote_server_password) gluster_manager.make_gluster_call(fake_execute)(*fake_args, **fake_kwargs) common.ganesha_utils.SSHExecutor.assert_called_with( gluster_manager.host, 22, None, gluster_manager.user, password=gluster_manager.remote_server_password, privatekey=gluster_manager.path_to_private_key) fake_obj.assert_called_once_with( *(('gluster',) + fake_args), **fake_kwargs)
def test_gluster_manager_make_gluster_call_error(self, trouble, _exception, xkw): fake_obj = mock.Mock(side_effect=trouble) fake_execute = mock.Mock() kwargs = fake_kwargs.copy() kwargs.update(xkw) with mock.patch.object(common.ganesha_utils, 'RootExecutor', mock.Mock(return_value=fake_obj)): gluster_manager = common.GlusterManager('127.0.0.1:/testvol', self.fake_execf) self.assertRaises(_exception, gluster_manager.make_gluster_call(fake_execute), *fake_args, **kwargs) common.ganesha_utils.RootExecutor.assert_called_with(fake_execute) fake_obj.assert_called_once_with(*(('gluster', ) + fake_args), **fake_kwargs)
def test_fetch_gluster_volumes_no_keymatch(self): self._layout.configuration.glusterfs_servers = [self.glusterfs_server1] self.mock_object( self.gmgr1, 'gluster_call', mock.Mock(return_value=('manila-share-1', ''))) _glustermanager_calls = ( self.gmgr1, common.GlusterManager('root@host1:/manila-share-1')) self.mock_object(self._layout, '_glustermanager', mock.Mock(side_effect=_glustermanager_calls)) self.mock_object(self._layout, 'volume_pattern', re.compile('manila-share-\d+(-(?P<size>\d+)G)?$')) expected_output = {'root@host1:/manila-share-1': {'size': None}} ret = self._layout._fetch_gluster_volumes() test_args = ('volume', 'list') self.gmgr1.gluster_call.assert_called_once_with(*test_args) self.assertEqual(expected_output, ret)
def test_update_access_via_manager(self, delta, expected): gluster_mgr = common.GlusterManager(self.glusterfs_target1, self._execute, None, None) self.mock_object( gluster_mgr, 'get_vol_option', mock.Mock( side_effect=lambda a, *x, **kw: { 'auth.ssl-allow': "glusterCN,oldCN", 'server.dynamic-auth': True}[a])) self.mock_object(gluster_mgr, 'set_vol_option') add_rules, delete_rules = ( map(lambda a: {'access_to': a}, r) for r in delta) self._driver._update_access_via_manager( gluster_mgr, self._context, self.share1, add_rules, delete_rules) argseq = [('auth.ssl-allow', {})] if delete_rules: argseq.append(('server.dynamic-auth', {'boolean': True})) self.assertEqual([mock.call(a[0], **a[1]) for a in argseq], gluster_mgr.get_vol_option.call_args_list) gluster_mgr.set_vol_option.assert_called_once_with('auth.ssl-allow', expected)
def test_gluster_manager_init_no_vol(self, has_volume): test_gluster_manager = common.GlusterManager( '[email protected]', self.fake_execf, requires={'volume': has_volume}) self.assertIsNone(test_gluster_manager.volume)
def test_gluster_manager_init_has_vol(self, has_volume): test_gluster_manager = common.GlusterManager( '[email protected]:/testvol', self.fake_execf, requires={'volume': has_volume}) self.assertEqual('testvol', test_gluster_manager.volume)