def rename_scenario(handler, scenario_name, new_name): """ Renames specified scenario, renames Stubs, reloads cache :param handler: TrackRequest handler :param scenario_name: <string> scenario name :param new_name: <string> new scenario name :return: <tuple> containing status code and message that will be returned """ response = { 'version': version } scenario = Scenario() # getting hostname host = handler.get_argument('host', get_hostname(handler.request)) # full names hostname:scenario_name full_scenario_name = "{0}:{1}".format(host, scenario_name) new_full_scenario_name = "{0}:{1}".format(host, new_name) # getting scenario object scenario_obj = scenario.get(full_scenario_name) # checking if scenario exist, if not - quit if scenario_obj is None: handler.set_status(400) handler.track.scenario = scenario_name response['error'] = "Scenario not found. Name provided: {0}, host checked: {1}.".format(scenario_name, host) log.debug("Scenario not found. Name provided: {0}, host checked: {1}.".format(scenario_name, host)) return response # renaming scenario and all stubs, getting a dict with results try: response = scenario.change_name(full_scenario_name, new_full_scenario_name) except Exception as ex: handler.set_status() log.debug("Failed to change scenario name, got error: %s" % ex) response['error']['database'] = "Failed to change scenario name, got error: %s" % ex try: cache = Cache(host) # change cache scenario_sessions = cache.get_sessions_status(scenario_name) # scenario sessions contains tuples [(u'myscenario_session2_1', u'dormant'), ....] session_info = [] cache.delete_caches(scenario_name) # rebuild cache for session_name, mode in scenario_sessions: cache.create_session_cache(new_name, session_name) session_info.append({'name': session_name}) # sessions after creation go into playback mode, ending them end_session(handler, session_name) response['Remapped sessions'] = session_info except Exception as ex: log.debug("Failed to repopulate cache, got error: %s" % ex) response['error']['cache'] = "Failed to repopulate cache, got error: %s" % ex return response
def begin_session(handler, scenario_name, session_name, mode, system_date=None, warm_cache=False): log.debug('begin_session') response = {'version': version} scenario_col = Scenario() cache = Cache(get_hostname(handler.request)) if cache.blacklisted(): raise exception_response( 400, title="Sorry the host URL '{0}' has been " "blacklisted. Please contact Stub-O-Matic support.".format( cache.host)) scenario_name_key = cache.scenario_key_name(scenario_name) scenario = scenario_col.get(scenario_name_key) cache.assert_valid_session(scenario_name, session_name) if mode == 'record': log.debug('begin_session, mode=record') # precond: delete/stubs?scenario={scenario_name} if scenario: err = exception_response( 400, title='Duplicate scenario found - {0}'.format( scenario_name_key)) raise err if scenario_col.stub_count(scenario_name_key) != 0: raise exception_response( 500, title='stub_count !=0 for scenario: {0}'.format( scenario_name_key)) scenario_id = scenario_col.insert(name=scenario_name_key) log.debug('new scenario: {0}'.format(scenario_id)) session_payload = { 'status': 'record', 'scenario': scenario_name_key, 'scenario_id': str(scenario_id), 'session': str(session_name) } cache.set_session(scenario_name, session_name, session_payload) log.debug('new redis session: {0}:{1}'.format(scenario_name_key, session_name)) response["data"] = { 'message': 'Record mode initiated....', } response["data"].update(session_payload) cache.set_session_map(scenario_name, session_name) log.debug('finish record') elif mode == 'playback': if not scenario: raise exception_response( 400, title='Scenario not found - {0}'.format(scenario_name_key)) recordings = cache.get_sessions_status(scenario_name, status=('record'), local=False) if recordings: raise exception_response(400, title='Scenario recordings taking ' \ 'place - {0}. Found the following record sessions: {1}'.format( scenario_name_key, recordings)) cache.create_session_cache(scenario_name, session_name, system_date) if warm_cache: # iterate over stubs and call get/response for each stub matchers # to build the request & request_index cache # reset request_index to 0 log.debug("warm cache for session '{0}'".format(session_name)) scenario_col = Scenario() for payload in scenario_col.get_stubs(scenario_name_key): stub = Stub(payload['stub'], scenario_name_key) mock_request = " ".join(stub.contains_matchers()) handler.request.body = mock_request get_response(handler, session_name) cache.reset_request_index(scenario_name) response["data"] = {"message": "Playback mode initiated...."} response["data"].update({ "status": "playback", "scenario": scenario_name_key, "session": str(session_name) }) else: raise exception_response(400, title='Mode of playback or record required') return response
def begin_session(handler, scenario_name, session_name, mode, system_date=None, warm_cache=False): log.debug('begin_session') response = { 'version' : version } scenario_col = Scenario() cache = Cache(get_hostname(handler.request)) if cache.blacklisted(): raise exception_response(400, title="Sorry the host URL '{0}' has been " "blacklisted. Please contact Stub-O-Matic support.".format(cache.host)) scenario_name_key = cache.scenario_key_name(scenario_name) scenario = scenario_col.get(scenario_name_key) cache.assert_valid_session(scenario_name, session_name) if mode == 'record': log.debug('begin_session, mode=record') # precond: delete/stubs?scenario={scenario_name} if scenario: err = exception_response(400, title='Duplicate scenario found - {0}'.format(scenario_name_key)) raise err if scenario_col.stub_count(scenario_name_key) != 0: raise exception_response(500, title='stub_count !=0 for scenario: {0}'.format( scenario_name_key)) scenario_id = scenario_col.insert(name=scenario_name_key) log.debug('new scenario: {0}'.format(scenario_id)) session_payload = { 'status' : 'record', 'scenario' : scenario_name_key, 'scenario_id' : str(scenario_id), 'session' : str(session_name) } cache.set_session(scenario_name, session_name, session_payload) log.debug('new redis session: {0}:{1}'.format(scenario_name_key, session_name)) response["data"] = { 'message' : 'Record mode initiated....', } response["data"].update(session_payload) cache.set_session_map(scenario_name, session_name) log.debug('finish record') elif mode == 'playback': if not scenario: raise exception_response(400, title='Scenario not found - {0}'.format(scenario_name_key)) recordings = cache.get_sessions_status(scenario_name, status=('record'), local=False) if recordings: raise exception_response(400, title='Scenario recordings taking ' \ 'place - {0}. Found the following record sessions: {1}'.format( scenario_name_key, recordings)) cache.create_session_cache(scenario_name, session_name, system_date) if warm_cache: # iterate over stubs and call get/response for each stub matchers # to build the request & request_index cache # reset request_index to 0 log.debug("warm cache for session '{0}'".format(session_name)) scenario_col = Scenario() for payload in scenario_col.get_stubs(scenario_name_key): stub = Stub(payload['stub'], scenario_name_key) mock_request = " ".join(stub.contains_matchers()) handler.request.body = mock_request get_response(handler, session_name) cache.reset_request_index(scenario_name) response["data"] = { "message" : "Playback mode initiated...." } response["data"].update({ "status" : "playback", "scenario" : scenario_name_key, "session" : str(session_name) }) else: raise exception_response(400, title='Mode of playback or record required') return response
def begin_session(handler, scenario_name, session_name, mode, system_date=None, warm_cache=False): """ Begins session for given scenario :param handler: request handler class :param scenario_name: scenario name :param session_name: session name :param mode: mode - record, playback :param system_date: :param warm_cache: :return: :raise exception_response: """ log.debug('begin_session') response = { 'version': version } scenario_manager = Scenario() cache = Cache(get_hostname(handler.request)) if cache.blacklisted(): raise exception_response(400, title="Sorry the host URL '{0}' has been " "blacklisted. Please contact Stub-O-Matic support.".format(cache.host)) # checking whether full name (with hostname) was passed, if not - getting full name # scenario_name_key = "localhost:scenario_1" if ":" not in scenario_name: scenario_name_key = cache.scenario_key_name(scenario_name) else: # setting scenario full name scenario_name_key = scenario_name # removing hostname from scenario name scenario_name = scenario_name.split(":")[1] # get scenario document scenario_doc = scenario_manager.get(scenario_name_key) if not scenario_doc: raise exception_response(404, title='Scenario not found - {0}. To begin a' ' session - create a scenario.'.format(scenario_name_key)) cache.assert_valid_session(scenario_name, session_name) if mode == 'record': log.debug('begin_session, mode=record') # check if there are any existing stubs in this scenario if scenario_manager.stub_count(scenario_name_key) > 0: err = exception_response(400, title='Scenario ({0}) has existing stubs, delete them before ' 'recording or create another scenario!'.format(scenario_name_key)) raise err scenario_id = scenario_doc['_id'] log.debug('new scenario: {0}'.format(scenario_id)) session_payload = { 'status': 'record', 'scenario': scenario_name_key, 'scenario_id': str(scenario_id), 'session': str(session_name) } cache.set_session(scenario_name, session_name, session_payload) log.debug('new redis session: {0}:{1}'.format(scenario_name_key, session_name)) response["data"] = { 'message': 'Record mode initiated....', } response["data"].update(session_payload) cache.set_session_map(scenario_name, session_name) log.debug('finish record') elif mode == 'playback': recordings = cache.get_sessions_status(scenario_name, status='record', local=False) if recordings: raise exception_response(400, title='Scenario recordings taking ' 'place - {0}. Found the ' 'following record sessions: {1}'.format(scenario_name_key, recordings)) cache.create_session_cache(scenario_name, session_name, system_date) if warm_cache: # iterate over stubs and call get/response for each stub matchers # to build the request & request_index cache # reset request_index to 0 log.debug("warm cache for session '{0}'".format(session_name)) scenario_manager = Scenario() for payload in scenario_manager.get_stubs(scenario_name_key): stub = Stub(payload['stub'], scenario_name_key) mock_request = " ".join(stub.contains_matchers()) handler.request.body = mock_request get_response(handler, session_name) cache.reset_request_index(scenario_name) response["data"] = { "message": "Playback mode initiated...." } response["data"].update({ "status": "playback", "scenario": scenario_name_key, "session": str(session_name) }) else: raise exception_response(400, title='Mode of playback or record required') return response
def begin_session(handler, scenario_name, session_name, mode, system_date=None, warm_cache=False): """ Begins session for given scenario :param handler: request handler class :param scenario_name: scenario name :param session_name: session name :param mode: mode - record, playback :param system_date: :param warm_cache: :return: :raise exception_response: """ log.debug('begin_session') response = {'version': version} scenario_manager = Scenario() # cache = Cache(get_hostname(handler.request)) # checking whether full name (with hostname) was passed, if not - getting full name # scenario_name_key = "localhost:scenario_1" if ":" not in scenario_name: cache = Cache(get_hostname(handler.request)) scenario_name_key = cache.scenario_key_name(scenario_name) else: # setting scenario full name scenario_name_key = scenario_name # removing hostname from scenario name slices = scenario_name.split(":") scenario_name = slices[1] cache = Cache(slices[0]) # get scenario document scenario_doc = scenario_manager.get(scenario_name_key) if not scenario_doc: raise exception_response( 404, title='Scenario not found - {0}. To begin a' ' session - create a scenario.'.format(scenario_name_key)) cache.assert_valid_session(scenario_name, session_name) if mode == 'record': log.debug('begin_session, mode=record') # check if there are any existing stubs in this scenario if scenario_manager.stub_count(scenario_name_key) > 0: err = exception_response( 400, title='Scenario ({0}) has existing stubs, delete them before ' 'recording or create another scenario!'.format( scenario_name_key)) raise err scenario_id = scenario_doc['_id'] log.debug('new scenario: {0}'.format(scenario_id)) session_payload = { 'status': 'record', 'scenario': scenario_name_key, 'scenario_id': str(scenario_id), 'session': str(session_name) } cache.set_session(scenario_name, session_name, session_payload) log.debug('new redis session: {0}:{1}'.format(scenario_name_key, session_name)) response["data"] = { 'message': 'Record mode initiated....', } response["data"].update(session_payload) cache.set_session_map(scenario_name, session_name) log.debug('finish record') elif mode == 'playback': recordings = cache.get_sessions_status(scenario_name, status='record', local=False) if recordings: raise exception_response(400, title='Scenario recordings taking ' 'place - {0}. Found the ' 'following record sessions: {1}'.format( scenario_name_key, recordings)) cache.create_session_cache(scenario_name, session_name, system_date) if warm_cache: # iterate over stubs and call get/response for each stub matchers # to build the request & request_index cache # reset request_index to 0 log.debug("warm cache for session '{0}'".format(session_name)) scenario_manager = Scenario() for payload in scenario_manager.get_stubs(scenario_name_key): stub = Stub(payload['stub'], scenario_name_key) mock_request = " ".join(stub.contains_matchers()) handler.request.body = mock_request get_response(handler, session_name) cache.reset_request_index(scenario_name) response["data"] = {"message": "Playback mode initiated...."} response["data"].update({ "status": "playback", "scenario": scenario_name_key, "session": str(session_name) }) else: raise exception_response(400, title='Mode of playback or record required') return response