def test_new_session_with_state(self): scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub, response_hash stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) stub2 = Stub(create('<test>match this</test>', '<test>BAD</test>'), 'localhost:foo') doc2 = dict(scenario='localhost:foo', stub=stub2) self.scenario.insert_stub(doc2, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) responses = stub.response_ids() self.assertEqual(len(responses), 2) self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(responses, [ response_hash('<test>OK</test>', stub), response_hash('<test>BAD</test>', stub2) ]) self.assertEqual(self.hash.get_raw('localhost:sessions', 'bar'), 'foo')
def test_new_session_with_state(self): scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub, response_hash stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) stub2 = Stub(create('<test>match this</test>', '<test>BAD</test>'), 'localhost:foo') doc2 = dict(scenario='localhost:foo', stub=stub2) self.scenario.insert_stub(doc2, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) responses = stub.response_ids() self.assertEqual(len(responses), 2) self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(responses, [response_hash('<test>OK</test>', stub), response_hash('<test>BAD</test>', stub2)]) self.assertEqual(self.hash.get_raw('localhost:sessions', 'bar'), 'foo')
def test_update_dormant_session_with_stubs(self): self.hash.set('somehost:foo', 'bar', { 'status': 'dormant', 'session': 'bar', 'scenario': 'localhost:foo' }) scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub, response_hash stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(stub.response_ids(), [response_hash('<test>OK</test>', stub)])
def test_update_dormant_session_with_stubs(self): self.hash.set('somehost:foo', 'bar', {'status' : 'dormant', 'session' : 'bar', 'scenario' : 'localhost:foo'}) scenario_name = 'foo' self._make_scenario('localhost:foo') from stubo.model.stub import create, Stub, response_hash stub = Stub(create('<test>match this</test>', '<test>OK</test>'), 'localhost:foo') doc = dict(scenario='localhost:foo', stub=stub) self.scenario.insert_stub(doc, stateful=True) cache = self._get_cache() cache.create_session_cache('foo', 'bar') session = self.hash.get('localhost:foo', 'bar') self.assertEqual(session["status"], "playback") self.assertEqual(session['session'], 'bar') self.assertEqual(session["scenario"], "localhost:foo") self.assertTrue('stubs' in session) stubs = session['stubs'] self.assertEqual(len(stubs), 1) from stubo.model.stub import StubCache stub = StubCache(stubs[0], session["scenario"], session['session']) self.assertEqual(stub.contains_matchers(), ["<test>match this</test>"]) self.assertEqual(stub.response_ids(), [response_hash('<test>OK</test>', stub)])
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( 412, title="Precondition failed: no stubs were" " found in database for scenario: {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
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