本文整理汇总了Python中stubo.model.db.Scenario.get_stubs方法的典型用法代码示例。如果您正苦于以下问题:Python Scenario.get_stubs方法的具体用法?Python Scenario.get_stubs怎么用?Python Scenario.get_stubs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stubo.model.db.Scenario
的用法示例。
在下文中一共展示了Scenario.get_stubs方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_put_stub
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
def test_put_stub(self):
self.http_client.fetch(self.get_url('/stubo/api/begin/session?scenario=xslt&session=s1&mode=record'), self.stop)
response = self.wait()
self.assertEqual(response.code, 200)
self.http_client.fetch(self.get_url('/stubo/api/put/module?name=/static/cmds/tests/ext/xslt/mangler.py'),
self.stop)
response = self.wait()
self.assertEqual(response.code, 200)
self.http_client.fetch(self.get_url('/stubo/api/put/stub?session=s1&ext_module=mangler'),
callback=self.stop,
method="POST",
body="""||textMatcher||<request><a>ba</a><trans_id>1234</trans_id><b>ba</b><user uuid="xxx">joe</user><dt>2013-11-25</dt></request>||response||<response><a>ba</a><trans_id>1234</trans_id><b>ba</b><user uuid="xxx">joe</user><dt>2013-11-25</dt></response>""")
response = self.wait()
self.assertEqual(response.code, 200)
from stubo.model.db import Scenario
scenario_db = Scenario(db=self.db)
stubs = list(scenario_db.get_stubs('localhost:xslt'))
self.assertEqual(len(stubs), 1)
from stubo.model.stub import Stub
stub = Stub(stubs[0]['stub'], 'localhost:xslt')
self.assertEqual(stub.contains_matchers()[0],
u'<request><a>ba</a><trans_id>***</trans_id><b>ba</b><user uuid="***">***</user><dt>***</dt></request>\n')
self.assertEqual(stub.response_body()[0],
u'<response><a>ba</a><trans_id>***</trans_id><b>ba</b><user uuid="***">***</user><dt>***</dt></response>\n')
from datetime import date
self.assertEqual(stub.module(), {
u'system_date': str(date.today()),
u'recorded_system_date': str(date.today()),
u'name': u'mangler'})
示例2: get_stubs
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
def get_stubs(self, name=None):
if name and isinstance(name, dict) and '$regex' in name:
name = name['$regex'].partition(':')[0]
all_names = list(self.db.scenario_stub.find({}))
result = [x for x in all_names if name in x.get('scenario')]
else:
result = Scenario.get_stubs(self, name=name)
return result
示例3: list_stubs
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
def list_stubs(handler, scenario_name, host=None):
cache = Cache(host or get_hostname(handler.request))
scenario = Scenario()
stubs = scenario.get_stubs(cache.scenario_key_name(scenario_name))
result = dict(version=version, data=dict(scenario=scenario_name))
if stubs:
result['data']['stubs'] = [x['stub'] for x in stubs]
return result
示例4: get_stubs
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
def get_stubs(host, scenario_name=None):
if not scenario_name:
# get all stubs for this host
scenario_name_key = {'$regex': '{0}:.*'.format(host)}
else:
scenario_name_key = ":".join([host, scenario_name])
scenario = Scenario()
return scenario.get_stubs(scenario_name_key)
示例5: begin_session
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
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,
#.........这里部分代码省略.........
示例6: begin_session
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
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
示例7: create_session_cache
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
def create_session_cache(self, scenario_name, session_name,
system_date=None):
scenario_key = self.scenario_key_name(scenario_name)
log.debug("create_session_cache: scenario_key={0}, session_name={1}".format(
scenario_key, session_name))
session = self.get(scenario_key, session_name)
if not session:
# must be using a different session name for playback than record
session = {
'session' : session_name,
'scenario' : scenario_key
}
# add to sessions map
self.set_raw('{0}:sessions'.format(self.host), session_name, scenario_name)
session['status'] = 'playback'
session['system_date'] = system_date or datetime.date.today().strftime(
'%Y-%m-%d')
session['last_used'] = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
cache_info = []
# copy mongo scenario stubs to redis cache
scenario_col = Scenario()
stubs_cursor = scenario_col.get_stubs(scenario_key)
stubs = list(stubs_cursor)
if not stubs:
raise exception_response(500,
title="found no stubs in mongo for {0}".format(scenario_key))
from stubo.ext.module import Module
for scenario_stub in stubs:
stub = Stub(scenario_stub['stub'], scenario_stub['scenario'])
if stub.module():
module_name = stub.module()['name']
# tag this stub with the latest version of the module
module = Module(self.host)
version = module.latest_version(module_name)
if not version:
raise exception_response(500,
title="module '{0}' not found in cache".format(
module.key(module_name)))
stub.module()['version'] = version
response_ids = []
response_bodys = stub.response_body()
# cache each response id -> response (text, status) etc
for response_text in response_bodys:
stub.set_response_body(response_text)
response_id = response_hash(response_text, stub)
self.set_response(scenario_name, session_name, response_id,
stub.response())
response_ids.append(response_id)
# replace response text with response hash ids for session cache
stub.response().pop('body', None)
stub.response()['ids'] = response_ids
delay_policy_name = stub.delay_policy()
if delay_policy_name:
# Note: the delay policy is not really cached with the session.
# The get/response call will just use the name to get the latest
# delay value from the 'delay_policy' key in redis.
delay_policy_key = '{0}:delay_policy'.format(self.host)
delay_policy = self.get(delay_policy_key, delay_policy_name)
if not delay_policy:
log.warn('unable to find delay_policy: {0}'.format(
delay_policy_name))
stub.set_delay_policy(delay_policy)
#_id = ObjectId(scenario_stub['_id'])
#stub['recorded'] = str(_id.generation_time.date())
cache_info.append(stub.payload)
session['stubs'] = cache_info
#log.debug('stubs: {0}'.format(session['stubs']))
self.set(scenario_key, session_name, session)
log.debug('created session cache: {0}:{1}'.format(session['scenario'],
session['session']))
return session
示例8: export_stubs_to_commands_format
# 需要导入模块: from stubo.model.db import Scenario [as 别名]
# 或者: from stubo.model.db.Scenario import get_stubs [as 别名]
def export_stubs_to_commands_format(handler, scenario_name_key, scenario_name, session_id,
runnable, playback_session, static_dir, export_dir):
"""
:param handler:
:param scenario_name_key:
:param scenario_name:
:param session_id:
:param runnable:
:param playback_session:
:param static_dir:
:param export_dir:
:return: :raise exception_response:
"""
# cache = Cache(get_hostname(handler.request))
# scenario_name_key = cache.scenario_key_name(scenario_name)
# use user arg or epoch time
if not session_id:
session_id = int(time.time())
# session_id = handler.get_argument('session_id', int(time.time()))
session = u'{0}_{1}'.format(scenario_name, session_id)
cmds = [
'delete/stubs?scenario={0}'.format(scenario_name),
'begin/session?scenario={0}&session={1}&mode=record'.format(
scenario_name, session)
]
files = []
scenario = Scenario()
# get scenario pre stubs for specified scenario
stubs = list(scenario.get_stubs(scenario_name_key))
pre_stubs = list(scenario.get_pre_stubs(scenario_name_key))
if pre_stubs:
for i in range(len(pre_stubs)):
pre_entry = pre_stubs[i]
pre_stub = Stub(pre_entry['stub'], scenario_name_key)
# if stub is rest - matcher may be None, checking that
if pre_stub.contains_matchers() is None:
cmds.append('# Stub skipped since no matchers were found. Consider using .yaml format for additional '
'capabilities')
# skipping to next stub, this stub is not compatible with .commands format
continue
matchers = [('{0}_{1}_{2}.textMatcher'.format(session, i, x), pre_stub.contains_matchers()[x])
for x in range(len(pre_stub.contains_matchers()))]
matchers_str = ",".join(x[0] for x in matchers)
url_args = pre_stub.args()
url_args['session'] = session
entry = stubs[i] if stubs else None
if entry:
stub = Stub(entry['stub'], scenario_name_key) if entry and "stub" in entry else None
module_info = stub.module() if stub else None
if module_info:
# Note: not including put/module in the export, modules are shared
# by multiple scenarios.
url_args['ext_module'] = module_info['name']
url_args['stub_created_date'] = stub.recorded()
url_args['stubbedSystemDate'] = module_info.get('recorded_system_date')
url_args['system_date'] = module_info.get('system_date')
url_args = urlencode(url_args)
responses = pre_stub.response_body()
assert(len(responses) == 1)
response = responses[0]
response = ('{0}_{1}.response'.format(session, i), response)
cmds.append('put/stub?{0},{1},{2}'.format(url_args, matchers_str,
response[0]))
files.append(response)
files.extend(matchers)
else:
cmds.append('put/stub?session={0},text=a_dummy_matcher,text=a_dummy_response'.format(session))
cmds.append('end/session?session={0}'.format(session))
runnable_info = dict()
# if this scenario is runnable
if runnable:
# playback_session = handler.get_argument('playback_session', None)
if not playback_session:
raise exception_response(400,
title="'playback_session' argument required with 'runnable")
runnable_info['playback_session'] = playback_session
tracker = Tracker()
last_used = tracker.session_last_used(scenario_name_key,
playback_session, 'playback')
if not last_used:
raise exception_response(400,
title="Unable to find playback session")
runnable_info['last_used'] = dict(remote_ip=last_used['remote_ip'],
start_time=str(last_used['start_time']))
playback = tracker.get_last_playback(scenario_name, playback_session,
last_used['start_time'])
playback = list(playback)
if not playback:
raise exception_response(400,
title="Unable to find a playback for scenario='{0}', playback_session='{1}'".format(scenario_name, playback_session))
cmds.append('begin/session?scenario={0}&session={1}&mode=playback'.format(
scenario_name, session))
number_of_requests = len(playback)
#.........这里部分代码省略.........