def show(self, req, id): context = req.environ['manila.context'] try: instance = db.share_instance_get(context, id) except exception.NotFound: raise exc.HTTPNotFound() return self._view_builder.detail(req, instance)
def setUp(self): super(DataServiceHelperTestCase, self).setUp() self.share = db_utils.create_share() self.share_instance = db_utils.create_share_instance( share_id=self.share['id'], status=constants.STATUS_AVAILABLE) self.context = context.get_admin_context() self.share_instance = db.share_instance_get( self.context, self.share_instance['id'], with_share_data=True) self.access = db_utils.create_access(share_id=self.share['id']) self.helper = data_copy_helper.DataServiceHelper( self.context, db, self.share)
def test__update_access_rules_recursive_driver_exception(self, drv_exc): other = access.ShareInstanceAccess(db, None) share = db_utils.create_share( status=constants.STATUS_AVAILABLE, access_rules_status=constants.SHARE_INSTANCE_RULES_SYNCING) share_instance_id = share['instance']['id'] rule_4 = [] get_and_update_count = [1] drv_count = [1] def _get_and_update_side_effect(*args, **kwargs): # The third call to this method needs to create a new access rule mtd = other.get_and_update_share_instance_access_rules if get_and_update_count[0] == 3: rule_4.append( db_utils.create_access( state=constants.ACCESS_STATE_QUEUED_TO_APPLY, share_id=share['id'])) get_and_update_count[0] += 1 return mtd(*args, **kwargs) def _driver_side_effect(*args, **kwargs): if drv_exc and drv_count[0] == 2: raise exception.ManilaException('fake') drv_count[0] += 1 rule_kwargs = {'share_id': share['id'], 'access_level': 'rw'} rule_1 = db_utils.create_access(state=constants.ACCESS_STATE_APPLYING, **rule_kwargs) rule_2 = db_utils.create_access(state=constants.ACCESS_STATE_ACTIVE, **rule_kwargs) rule_3 = db_utils.create_access(state=constants.ACCESS_STATE_DENYING, **rule_kwargs) self.mock_object(self.access_helper, 'get_and_update_share_instance_access_rules', mock.Mock(side_effect=_get_and_update_side_effect)) self.mock_object(self.access_helper.driver, 'update_access', mock.Mock(side_effect=_driver_side_effect)) if drv_exc: self.assertRaises(exception.ManilaException, self.access_helper._update_access_rules, self.context, share_instance_id) else: retval = self.access_helper._update_access_rules(self.context, share_instance_id) self.assertIsNone(retval) expected_filters_1 = { 'state': (constants.ACCESS_STATE_APPLYING, constants.ACCESS_STATE_ACTIVE, constants.ACCESS_STATE_DENYING), } conditionally_change_2 = { constants.ACCESS_STATE_APPLYING: constants.ACCESS_STATE_ACTIVE, } expected_filters_3 = { 'state': (constants.ACCESS_STATE_QUEUED_TO_APPLY, constants.ACCESS_STATE_QUEUED_TO_DENY), } expected_conditionally_change_3 = { constants.ACCESS_STATE_QUEUED_TO_APPLY: constants.ACCESS_STATE_APPLYING, constants.ACCESS_STATE_QUEUED_TO_DENY: constants.ACCESS_STATE_DENYING, } expected_conditionally_change_4 = { constants.ACCESS_STATE_APPLYING: constants.ACCESS_STATE_ERROR, constants.ACCESS_STATE_DENYING: constants.ACCESS_STATE_ERROR, } expected_get_and_update_calls = [ mock.call(self.context, filters=expected_filters_1, share_instance_id=share_instance_id), mock.call(self.context, share_instance_id=share_instance_id, conditionally_change=conditionally_change_2), mock.call(self.context, filters=expected_filters_3, share_instance_id=share_instance_id, conditionally_change=expected_conditionally_change_3), mock.call(self.context, filters=expected_filters_1, share_instance_id=share_instance_id), ] if drv_exc: expected_get_and_update_calls.append( mock.call( self.context, share_instance_id=share_instance_id, conditionally_change=expected_conditionally_change_4)) else: expected_get_and_update_calls.append( mock.call(self.context, share_instance_id=share_instance_id, conditionally_change=conditionally_change_2)) # Verify rule changes: # 'denying' rule must not exist self.assertRaises(exception.NotFound, db.share_access_get, self.context, rule_3['id']) # 'applying' rule must be set to 'active' rules_that_must_be_active = (rule_1, rule_2) if not drv_exc: rules_that_must_be_active += (rule_4[0], ) for rule in rules_that_must_be_active: rule = db.share_access_get(self.context, rule['id']) self.assertEqual(constants.ACCESS_STATE_ACTIVE, rule['state']) # access_rules_status must be as expected expected_access_rules_status = ( constants.SHARE_INSTANCE_RULES_ERROR if drv_exc else constants.STATUS_ACTIVE) share_instance = db.share_instance_get(self.context, share_instance_id) self.assertEqual( expected_access_rules_status, share_instance['access_rules_status'])
def test__update_access_rules_with_driver_updates( self, driver_returns_updates, access_state): expected_access_rules_status = ( constants.STATUS_ACTIVE if access_state == constants.ACCESS_STATE_ACTIVE else constants.SHARE_INSTANCE_RULES_ERROR ) share = db_utils.create_share( status=constants.STATUS_AVAILABLE, access_rules_status=expected_access_rules_status) share_instance_id = share['instance']['id'] rule_1 = db_utils.create_access( share_id=share['id'], state=access_state) rule_1 = db.share_instance_access_get( self.context, rule_1['id'], share_instance_id) rule_2 = db_utils.create_access( share_id=share['id'], state=constants.ACCESS_STATE_APPLYING) rule_2 = db.share_instance_access_get( self.context, rule_2['id'], share_instance_id) rule_3 = db_utils.create_access( share_id=share['id'], state=constants.ACCESS_STATE_DENYING) rule_3 = db.share_instance_access_get( self.context, rule_3['id'], share_instance_id) if driver_returns_updates: driver_rule_updates = { rule_3['access_id']: {'access_key': 'alic3h4sAcc355'}, rule_2['access_id']: {'state': access_state} } else: driver_rule_updates = None shr_instance_access_rules_status_update_call = self.mock_object( self.access_helper, 'get_and_update_share_instance_access_rules_status', mock.Mock(side_effect=self.access_helper. get_and_update_share_instance_access_rules_status)) all_access_rules_update_call = self.mock_object( self.access_helper, 'get_and_update_share_instance_access_rules', mock.Mock(side_effect=self.access_helper. get_and_update_share_instance_access_rules)) one_access_rule_update_call = self.mock_object( self.access_helper, 'get_and_update_share_instance_access_rule', mock.Mock(side_effect=self.access_helper. get_and_update_share_instance_access_rule)) driver_call = self.mock_object( self.access_helper.driver, 'update_access', mock.Mock(return_value=driver_rule_updates)) self.mock_object(self.access_helper, '_check_needs_refresh', mock.Mock(return_value=False)) retval = self.access_helper._update_access_rules( self.context, share_instance_id, share_server='fake_server') # Expected Values: if access_state != constants.ACCESS_STATE_ERROR: expected_rules_to_be_on_share = [r['id'] for r in (rule_1, rule_2)] else: expected_rules_to_be_on_share = [rule_2['id']] expected_filters_1 = { 'state': (constants.ACCESS_STATE_APPLYING, constants.ACCESS_STATE_ACTIVE, constants.ACCESS_STATE_DENYING), } expected_filters_2 = {'state': constants.STATUS_ERROR} expected_get_and_update_calls = [ mock.call(self.context, filters=expected_filters_1, share_instance_id=share_instance_id), mock.call(self.context, filters=expected_filters_2, share_instance_id=share_instance_id), ] expected_access_rules_status_change_cond1 = { constants.STATUS_ACTIVE: constants.SHARE_INSTANCE_RULES_SYNCING, } if access_state == constants.SHARE_INSTANCE_RULES_ERROR: expected_access_rules_status_change_cond2 = { constants.SHARE_INSTANCE_RULES_SYNCING: constants.SHARE_INSTANCE_RULES_ERROR, } else: expected_access_rules_status_change_cond2 = { constants.SHARE_INSTANCE_RULES_SYNCING: constants.STATUS_ACTIVE, constants.SHARE_INSTANCE_RULES_ERROR: constants.STATUS_ACTIVE, } call_args = driver_call.call_args_list[0][0] call_kwargs = driver_call.call_args_list[0][1] access_rules_to_be_on_share = [r['id'] for r in call_args[2]] # Asserts self.assertIsNone(retval) self.assertEqual(share_instance_id, call_args[1]['id']) self.assertTrue(isinstance(access_rules_to_be_on_share, list)) self.assertEqual(len(expected_rules_to_be_on_share), len(access_rules_to_be_on_share)) for pool in expected_rules_to_be_on_share: self.assertIn(pool, access_rules_to_be_on_share) self.assertEqual(1, len(call_kwargs['add_rules'])) self.assertEqual(rule_2['id'], call_kwargs['add_rules'][0]['id']) self.assertEqual(1, len(call_kwargs['delete_rules'])) self.assertEqual(rule_3['id'], call_kwargs['delete_rules'][0]['id']) self.assertEqual('fake_server', call_kwargs['share_server']) shr_instance_access_rules_status_update_call.assert_has_calls([ mock.call( self.context, share_instance_id=share_instance_id, conditionally_change=expected_access_rules_status_change_cond1 ), mock.call( self.context, share_instance_id=share_instance_id, conditionally_change=expected_access_rules_status_change_cond2 ), ]) if driver_returns_updates: expected_conditional_state_updates = { constants.ACCESS_STATE_APPLYING: access_state, constants.ACCESS_STATE_DENYING: access_state, constants.ACCESS_STATE_ACTIVE: access_state, } expected_access_rule_update_calls = [ mock.call( self.context, rule_3['access_id'], updates={'access_key': 'alic3h4sAcc355'}, share_instance_id=share_instance_id, conditionally_change={}), mock.call( self.context, rule_2['access_id'], updates=mock.ANY, share_instance_id=share_instance_id, conditionally_change=expected_conditional_state_updates) ] one_access_rule_update_call.assert_has_calls( expected_access_rule_update_calls, any_order=True) else: self.assertFalse(one_access_rule_update_call.called) expected_conditionally_change = { constants.ACCESS_STATE_APPLYING: constants.ACCESS_STATE_ACTIVE, } expected_get_and_update_calls.append( mock.call(self.context, share_instance_id=share_instance_id, conditionally_change=expected_conditionally_change)) all_access_rules_update_call.assert_has_calls( expected_get_and_update_calls, any_order=True) share_instance = db.share_instance_get( self.context, share_instance_id) self.assertEqual(expected_access_rules_status, share_instance['access_rules_status'])
def _get(self, *args, **kwargs): return db.share_instance_get(*args, **kwargs)
def test__update_access_rules_recursive_driver_exception(self, drv_exc): other = access.ShareInstanceAccess(db, None) share = db_utils.create_share( status=constants.STATUS_AVAILABLE, access_rules_status=constants.SHARE_INSTANCE_RULES_SYNCING) share_instance_id = share['instance']['id'] rule_4 = [] get_and_update_count = [1] drv_count = [1] def _get_and_update_side_effect(*args, **kwargs): # The third call to this method needs to create a new access rule mtd = other.get_and_update_share_instance_access_rules if get_and_update_count[0] == 3: rule_4.append( db_utils.create_access( state=constants.ACCESS_STATE_QUEUED_TO_APPLY, share_id=share['id'])) get_and_update_count[0] += 1 return mtd(*args, **kwargs) def _driver_side_effect(*args, **kwargs): if drv_exc and drv_count[0] == 2: raise exception.ManilaException('fake') drv_count[0] += 1 rule_kwargs = {'share_id': share['id'], 'access_level': 'rw'} rule_1 = db_utils.create_access(state=constants.ACCESS_STATE_APPLYING, **rule_kwargs) rule_2 = db_utils.create_access(state=constants.ACCESS_STATE_ACTIVE, **rule_kwargs) rule_3 = db_utils.create_access(state=constants.ACCESS_STATE_DENYING, **rule_kwargs) self.mock_object(self.access_helper, 'get_and_update_share_instance_access_rules', mock.Mock(side_effect=_get_and_update_side_effect)) self.mock_object(self.access_helper.driver, 'update_access', mock.Mock(side_effect=_driver_side_effect)) if drv_exc: self.assertRaises(exception.ManilaException, self.access_helper._update_access_rules, self.context, share_instance_id) else: retval = self.access_helper._update_access_rules( self.context, share_instance_id) self.assertIsNone(retval) expected_filters_1 = { 'state': (constants.ACCESS_STATE_APPLYING, constants.ACCESS_STATE_ACTIVE, constants.ACCESS_STATE_DENYING), } conditionally_change_2 = { constants.ACCESS_STATE_APPLYING: constants.ACCESS_STATE_ACTIVE, } expected_filters_3 = { 'state': (constants.ACCESS_STATE_QUEUED_TO_APPLY, constants.ACCESS_STATE_QUEUED_TO_DENY), } expected_conditionally_change_3 = { constants.ACCESS_STATE_QUEUED_TO_APPLY: constants.ACCESS_STATE_APPLYING, constants.ACCESS_STATE_QUEUED_TO_DENY: constants.ACCESS_STATE_DENYING, } expected_conditionally_change_4 = { constants.ACCESS_STATE_APPLYING: constants.ACCESS_STATE_ERROR, constants.ACCESS_STATE_DENYING: constants.ACCESS_STATE_ERROR, } expected_get_and_update_calls = [ mock.call(self.context, filters=expected_filters_1, share_instance_id=share_instance_id), mock.call(self.context, share_instance_id=share_instance_id, conditionally_change=conditionally_change_2), mock.call(self.context, filters=expected_filters_3, share_instance_id=share_instance_id, conditionally_change=expected_conditionally_change_3), mock.call(self.context, filters=expected_filters_1, share_instance_id=share_instance_id), ] if drv_exc: expected_get_and_update_calls.append( mock.call( self.context, share_instance_id=share_instance_id, conditionally_change=expected_conditionally_change_4)) else: expected_get_and_update_calls.append( mock.call(self.context, share_instance_id=share_instance_id, conditionally_change=conditionally_change_2)) # Verify rule changes: # 'denying' rule must not exist self.assertRaises(exception.NotFound, db.share_access_get, self.context, rule_3['id']) # 'applying' rule must be set to 'active' rules_that_must_be_active = (rule_1, rule_2) if not drv_exc: rules_that_must_be_active += (rule_4[0], ) for rule in rules_that_must_be_active: rule = db.share_access_get(self.context, rule['id']) self.assertEqual(constants.ACCESS_STATE_ACTIVE, rule['state']) # access_rules_status must be as expected expected_access_rules_status = (constants.SHARE_INSTANCE_RULES_ERROR if drv_exc else constants.STATUS_ACTIVE) share_instance = db.share_instance_get(self.context, share_instance_id) self.assertEqual(expected_access_rules_status, share_instance['access_rules_status'])
def test__update_access_rules_with_driver_updates(self, driver_returns_updates, access_state): expected_access_rules_status = (constants.STATUS_ACTIVE if access_state == constants.ACCESS_STATE_ACTIVE else constants.SHARE_INSTANCE_RULES_ERROR) share = db_utils.create_share( status=constants.STATUS_AVAILABLE, access_rules_status=expected_access_rules_status) share_instance_id = share['instance']['id'] rule_1 = db_utils.create_access(share_id=share['id'], state=access_state) rule_1 = db.share_instance_access_get(self.context, rule_1['id'], share_instance_id) rule_2 = db_utils.create_access(share_id=share['id'], state=constants.ACCESS_STATE_APPLYING) rule_2 = db.share_instance_access_get(self.context, rule_2['id'], share_instance_id) rule_3 = db_utils.create_access(share_id=share['id'], state=constants.ACCESS_STATE_DENYING) rule_3 = db.share_instance_access_get(self.context, rule_3['id'], share_instance_id) if driver_returns_updates: driver_rule_updates = { rule_3['access_id']: { 'access_key': 'alic3h4sAcc355' }, rule_2['access_id']: { 'state': access_state } } else: driver_rule_updates = None shr_instance_access_rules_status_update_call = self.mock_object( self.access_helper, 'get_and_update_share_instance_access_rules_status', mock.Mock(side_effect=self.access_helper. get_and_update_share_instance_access_rules_status)) all_access_rules_update_call = self.mock_object( self.access_helper, 'get_and_update_share_instance_access_rules', mock.Mock(side_effect=self.access_helper. get_and_update_share_instance_access_rules)) one_access_rule_update_call = self.mock_object( self.access_helper, 'get_and_update_share_instance_access_rule', mock.Mock(side_effect=self.access_helper. get_and_update_share_instance_access_rule)) driver_call = self.mock_object( self.access_helper.driver, 'update_access', mock.Mock(return_value=driver_rule_updates)) self.mock_object(self.access_helper, '_check_needs_refresh', mock.Mock(return_value=False)) retval = self.access_helper._update_access_rules( self.context, share_instance_id, share_server='fake_server') # Expected Values: if access_state != constants.ACCESS_STATE_ERROR: expected_rules_to_be_on_share = [r['id'] for r in (rule_1, rule_2)] else: expected_rules_to_be_on_share = [rule_2['id']] expected_filters_1 = { 'state': (constants.ACCESS_STATE_APPLYING, constants.ACCESS_STATE_ACTIVE, constants.ACCESS_STATE_DENYING), } expected_filters_2 = {'state': constants.STATUS_ERROR} expected_get_and_update_calls = [ mock.call(self.context, filters=expected_filters_1, share_instance_id=share_instance_id), mock.call(self.context, filters=expected_filters_2, share_instance_id=share_instance_id), ] expected_access_rules_status_change_cond1 = { constants.STATUS_ACTIVE: constants.SHARE_INSTANCE_RULES_SYNCING, } if access_state == constants.SHARE_INSTANCE_RULES_ERROR: expected_access_rules_status_change_cond2 = { constants.SHARE_INSTANCE_RULES_SYNCING: constants.SHARE_INSTANCE_RULES_ERROR, } else: expected_access_rules_status_change_cond2 = { constants.SHARE_INSTANCE_RULES_SYNCING: constants.STATUS_ACTIVE, constants.SHARE_INSTANCE_RULES_ERROR: constants.STATUS_ACTIVE, } call_args = driver_call.call_args_list[0][0] call_kwargs = driver_call.call_args_list[0][1] access_rules_to_be_on_share = [r['id'] for r in call_args[2]] # Asserts self.assertIsNone(retval) self.assertEqual(share_instance_id, call_args[1]['id']) self.assertTrue(isinstance(access_rules_to_be_on_share, list)) self.assertEqual(len(expected_rules_to_be_on_share), len(access_rules_to_be_on_share)) for pool in expected_rules_to_be_on_share: self.assertIn(pool, access_rules_to_be_on_share) self.assertEqual(1, len(call_kwargs['add_rules'])) self.assertEqual(rule_2['id'], call_kwargs['add_rules'][0]['id']) self.assertEqual(1, len(call_kwargs['delete_rules'])) self.assertEqual(rule_3['id'], call_kwargs['delete_rules'][0]['id']) self.assertEqual('fake_server', call_kwargs['share_server']) shr_instance_access_rules_status_update_call.assert_has_calls([ mock.call( self.context, share_instance_id=share_instance_id, conditionally_change=expected_access_rules_status_change_cond1 ), mock.call( self.context, share_instance_id=share_instance_id, conditionally_change=expected_access_rules_status_change_cond2 ), ]) if driver_returns_updates: expected_conditional_state_updates = { constants.ACCESS_STATE_APPLYING: access_state, constants.ACCESS_STATE_DENYING: access_state, constants.ACCESS_STATE_ACTIVE: access_state, } expected_access_rule_update_calls = [ mock.call(self.context, rule_3['access_id'], updates={'access_key': 'alic3h4sAcc355'}, share_instance_id=share_instance_id, conditionally_change={}), mock.call( self.context, rule_2['access_id'], updates=mock.ANY, share_instance_id=share_instance_id, conditionally_change=expected_conditional_state_updates) ] one_access_rule_update_call.assert_has_calls( expected_access_rule_update_calls, any_order=True) else: self.assertFalse(one_access_rule_update_call.called) expected_conditionally_change = { constants.ACCESS_STATE_APPLYING: constants.ACCESS_STATE_ACTIVE, } expected_get_and_update_calls.append( mock.call(self.context, share_instance_id=share_instance_id, conditionally_change=expected_conditionally_change)) all_access_rules_update_call.assert_has_calls( expected_get_and_update_calls, any_order=True) share_instance = db.share_instance_get(self.context, share_instance_id) self.assertEqual(expected_access_rules_status, share_instance['access_rules_status'])