Exemplo n.º 1
0
    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'})
Exemplo n.º 2
0
 def test_put_stub(self):
     self.http_client.fetch(self.get_url('/stubo/api/put/module?name=/static/cmds/tests/ext/cache/text/example.py'),
                            self.stop)
     response = self.wait()
     self.assertEqual(response.code, 200)
     self.http_client.fetch(self.get_url('/stubo/api/begin/session?scenario=cache&session=cache_1&mode=record'),
                            self.stop)
     response = self.wait()
     self.assertEqual(response.code, 200)
     self.http_client.fetch(self.get_url('/stubo/api/put/stub?session=cache_1&ext_module=example'),
                            callback=self.stop,
                            method="POST",
                            body="""||textMatcher||<request>hello</request>||response||<response>0</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:cache'))
     self.assertEqual(len(stubs), 1)
     from stubo.model.stub import Stub
     stub = Stub(stubs[0]['stub'], 'localhost:cache')
     self.assertEqual(stub.contains_matchers(), ['<request>hello</request>'])
     self.assertEqual(stub.response_body()[0], u'<response>0</response>')
     from datetime import date
     self.assertEqual(stub.module(), {u'system_date': str(date.today()),
                                      u'recorded_system_date': str(date.today()),
                                      u'name': u'example'})
Exemplo n.º 3
0
    def test_put_stub(self):
        self.http_client.fetch(self.get_url('/stubo/api/put/module?name=/static/cmds/tests/ext/split/splitter.py'),
                               self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(self.get_url('/stubo/api/begin/session?scenario=split&session=split_1&mode=record'),
                               self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(self.get_url('/stubo/api/put/stub?session=split_1&ext_module=splitter'),
                               callback=self.stop,
                               method="POST",
                               body="""||textMatcher||<a>
<pre>hello</pre>
<id>xxx</id>
<post>goodbye</post>
</a>||response||Hello {{1+1}} World""")
        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:split'))
        self.assertEqual(len(stubs), 1)
        from stubo.model.stub import Stub
        stub = Stub(stubs[0]['stub'], 'localhost:split')
        self.assertEqual(stub.contains_matchers(),
                        [u'<a>\n<pre>hello</pre>\n', '\n<post>goodbye</post>\n</a>'])
        self.assertEqual(stub.response_body()[0],
                         u'Hello {{1+1}} World')
        from datetime import date
        self.assertEqual(stub.module(), {
            u'system_date': str(date.today()),
            u'recorded_system_date': str(date.today()),
            u'name': u'splitter'})
Exemplo n.º 4
0
def store_source_recording(scenario_name_key, record_session):
    host, scenario_name = scenario_name_key.split(':')
    # use original put/stub payload logged in tracker
    tracker = Tracker()
    last_used = tracker.session_last_used(scenario_name_key,
                                          record_session, 'record')
    if not last_used:
        # empty recordings are currently supported!
        log.debug('Unable to find a recording for session={0}, scenario={1}'.format(record_session, scenario_name_key))
        return

    recording = tracker.get_last_recording(scenario_name, record_session,
                                           last_used['start_time'])
    recording = list(recording)
    if not recording:
        raise exception_response(400,
                                 title="Unable to find a recording for scenario='{0}', record_session='{1}'".format(
                                     scenario_name, record_session))

    number_of_requests = len(recording)
    scenario_db = Scenario()
    for nrequest in range(number_of_requests):
        track = recording[nrequest]
        request_text = track.get('request_text')
        if not request_text:
            raise exception_response(400, title='Unable to obtain recording details, was full tracking enabled?')

        priority = int(track['request_params'].get('priority', nrequest + 1))
        stub = parse_stub(request_text, scenario_name_key,
                          track['request_params'])
        stub.set_priority(priority)
        scenario_db.insert_pre_stub(scenario_name_key, stub)
Exemplo n.º 5
0
def store_source_recording(scenario_name_key, record_session):
    host, scenario_name = scenario_name_key.split(':')
    # use original put/stub payload logged in tracker
    tracker = Tracker()    
    last_used = tracker.session_last_used(scenario_name_key, 
                                          record_session, 'record')
    if not last_used:
        # empty recordings are currently supported!
        log.debug('Unable to find a recording for session={0}, scenario={1}'.format(record_session, scenario_name_key))
        return 
    
    recording = tracker.get_last_recording(scenario_name, record_session,
                                           last_used['start_time']) 
    recording = list(recording)
    if not recording:
        raise exception_response(400, 
          title="Unable to find a recording for scenario='{0}', record_session='{1}'".format(scenario_name, record_session))
    
    number_of_requests = len(recording)
    scenario_db = Scenario()
    for nrequest in range(number_of_requests):
        track = recording[nrequest]
        request_text = track.get('request_text')
        if not request_text:
            raise exception_response(400, title='Unable to obtain recording details, was full tracking enabled?')
        
        priority = int(track['request_params'].get('priority', nrequest+1))
        stub = parse_stub(request_text, scenario_name_key, 
                          track['request_params'])
        stub.set_priority(priority)
        scenario_db.insert_pre_stub(scenario_name_key, stub)   
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
 def get_all(self, name=None):
     if name and isinstance(name, dict) and '$regex' in name:
         # {'$regex': 'localhost:.*'}
         name = name['$regex'].partition(':')[0]
         all_names =  list(Scenario.get_all(self))
         return [self.get(x.get('name')) for x in all_names if name in x.get('name')]
     else:
         return Scenario.get_all(self, name=name)  
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
    def get(self):
        """

        Returns a list with all scenarios (and URL paths to these resources),
        stub count
        """
        # getting all scenarios
        cursor = self.db.scenario.find()
        # sorting based on name
        cursor.sort([('name', pymongo.ASCENDING)])

        # get size
        scenario_cl = Scenario()
        scenarios_sizes = scenario_cl.size()
        scenarios_recorded = scenario_cl.recorded()
        scenarios_stub_counts = scenario_cl.stub_counts()

        # start mapping data
        scenarios = []
        result_dict = {}
        while (yield cursor.fetch_next):
            document = cursor.next_object()
            try:
                # getting information about recorded, sizes and stub counts
                scenario_recorded = scenarios_recorded.get(
                    document['name'], '-')
                scenario_size = int(scenarios_sizes.get(document['name'], 0))
                scenario_stub_count = scenarios_stub_counts.get(
                    document['name'], 0)
                scenario_name = document['name']
                host, scenario = scenario_name.split(':')
                # getting session data
                sessions = []
                cache = Cache(host)
                for session_info in cache.get_scenario_sessions_information(
                        scenario):
                    sessions.append(session_info)

                scenarios.append({
                    'name':
                    scenario_name,
                    'recorded':
                    scenario_recorded,
                    'space_used_kb':
                    scenario_size,
                    'stub_count':
                    scenario_stub_count,
                    'sessions':
                    sessions,
                    'scenarioRef':
                    '/stubo/api/v2/scenarios/objects/%s' % document['name']
                })
            except KeyError:
                log.warn('Scenario name not found for object: %s' %
                         document['_id'])
        result_dict['data'] = scenarios
        self.set_status(200)
        self.write(result_dict)
Exemplo n.º 12
0
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
Exemplo n.º 13
0
    def test_put_stub(self):
        self.http_client.fetch(
            self.get_url(
                '/stubo/api/put/module?name=/static/cmds/tests/ext/cache/text/example.py'
            ), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(
            self.get_url(
                '/stubo/api/begin/session?scenario=cache&session=cache_1&mode=record'
            ), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)

        stub = {
            "priority": 1,
            "args": {
                "priority": "1",
                "ext_module": "example"
            },
            "request": {
                "bodyPatterns": {
                    "contains": ["<request>hello</request>\n"]
                },
                "method": "POST"
            },
            "response": {
                "body": "<response>0</response>\n",
                "status": 200
            }
        }
        import json
        self.http_client.fetch(self.get_url(
            '/stubo/api/put/stub?session=cache_1&ext_module=example'),
                               callback=self.stop,
                               method="POST",
                               body=json.dumps(stub),
                               headers={'Content-Type': 'application/json'})
        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:cache'))
        self.assertEqual(len(stubs), 1)
        from stubo.model.stub import Stub
        stub = Stub(stubs[0]['stub'], 'localhost:cache')
        self.assertEqual(stub.contains_matchers(),
                         ['<request>hello</request>\n'])
        self.assertEqual(stub.response_body()[0], u'<response>0</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'example'
            })
Exemplo n.º 14
0
def get_session_status(handler, all_hosts=True):
    scenario = Scenario()
    host_scenarios = defaultdict()

    # getting a dictionary with sizes for all scenarios
    scenario_sizes = scenario.size()
    scenarios_recorded = scenario.recorded()

    for s in scenario.get_all():
        host, scenario_name = s['name'].split(':')
        if not all_hosts and get_hostname(handler.request)  != host:
            continue
        if host not in host_scenarios:
            host_scenarios[host] = {}
            
        # getting session data
        sessions = []
        cache = Cache(host)

        for session_name, session in cache.get_sessions(scenario_name):
            # try and get the last_used from the last tracker get/response
            # else when the begin/session playback was called
            last_used = session_last_used(s['name'], session_name, 'playback')
            if last_used:
                last_used = last_used['start_time'].strftime('%Y-%m-%d %H:%M:%S')
            else:
                # session has never been used for playback 
                last_used = session.get('last_used', '-')
            session['last_used'] = last_used
            # removing stub information since we aren't using it anyway and it can consume a lot of memory
            session.pop('stubs', None)
            # creating sessions list
            sessions.append(session)
        # getting stub count
        stub_counts = stub_count(host, scenario_name)['data']['count']
        recorded = '-'

        # adding session information
        if sessions:
            if stub_counts:
                # getting scenario size and recorded values
                scenario_size = 0
                try:
                    scenario_size = scenario_sizes[s['name']]
                    recorded = scenarios_recorded[s['name']]
                except KeyError:
                    log.debug("Could not get scenario size for: %s" % s['name'])
                except Exception as ex:
                    log.warn("Failed to get scenario size for: %s, got error: %s" % (s['name'], ex))
                # creating a dict with scenario information
                host_scenarios[host][scenario_name] = (sessions, stub_counts, recorded, round(scenario_size, 0))

            else:
                host_scenarios[host][scenario_name] = (sessions, 0, '-', 0)

    return host_scenarios
Exemplo n.º 15
0
def list_scenarios(host):
    response = {'version': version}
    scenario_db = Scenario()
    if host == 'all':
        scenarios = [x['name'] for x in scenario_db.get_all()]
    else:
        # get all scenarios for host
        scenarios = [x['name'] for x in scenario_db.get_all(
            {'$regex': '{0}:.*'.format(host)})]
    response['data'] = dict(host=host, scenarios=scenarios)
    return response
Exemplo n.º 16
0
def list_scenarios(host):
    response = {'version' : version}
    scenario_db = Scenario()
    if host == 'all':
        scenarios = [x['name'] for x in scenario_db.get_all()]
    else:
        # get all scenarios for host
        scenarios = [x['name'] for x in scenario_db.get_all(
                                            {'$regex': '{0}:.*'.format(host)})]
    response['data'] = dict(host=host, scenarios=scenarios)    
    return response        
Exemplo n.º 17
0
    def get(self, scenario_name):
        """

        Returns scenario name, current sessions and their states,
        stub count, total, document size. Also, provides direct URL link to stub list.
        :param scenario_name: <string> scenario name

        Response JSON:
        {
            "stubs": 32,
             "space_used_kb": 840,
             "recorded": "2015-07-15",
             "name": "localhost:scenario_16",
             "scenarioRef": "/stubo/api/v2/scenarios/objects/localhost:scenario_16"
         }
        """

        # check if hostname is supplied - if not, override scenario name with new value
        scenario_name = _get_scenario_full_name(self, scenario_name)
        # query MongoDB
        document = yield self.db.scenario.find_one({'name': scenario_name})

        # form a result dictionary
        if document is not None:
            # get stub count
            stub_count = yield self.db.scenario_stub.find({'scenario': scenario_name}).count()
            # get size
            scenario_cl = Scenario()
            size = scenario_cl.size(scenario_name)
            # check if size is None
            if size is None:
                size = 0
            recorded = scenario_cl.recorded(scenario_name)
            if recorded is None:
                recorded = '-'

            host, scenario = scenario_name.split(':')
            # getting session data
            sessions = []
            cache = Cache(host)
            for session_info in cache.get_scenario_sessions_information(scenario):
                sessions.append(session_info)

            result_dict = {'name': scenario_name,
                           'stub_count': stub_count,
                           'recorded': recorded,
                           'space_used_kb': int(size),
                           'scenarioRef': '/stubo/api/v2/scenarios/objects/{0}'.format(scenario_name),
                           'sessions': sessions}
            self.set_status(200)
            self.write(result_dict)
        else:
            self.send_error(404)
Exemplo n.º 18
0
def delete_stubs(handler, scenario_name=None, host=None, force=False):
    """delete all data relating to one named scenario or host/s."""
    log.debug('delete_stubs')
    response = {
        'version': version
    }
    scenario_db = Scenario()
    static_dir = handler.settings['static_path']

    def delete_scenario(sce_name_key, frc):
        log.debug(u'delete_scenario: {0}'.format(sce_name_key))
        # getting host and scenario names
        hst, sce_name = sce_name_key.split(':')
        cache = Cache(hst)
        if not frc:
            active_sessions = cache.get_active_sessions(sce_name,
                                                        local=False)
            if active_sessions:
                raise exception_response(400,
                                         title='Sessons in playback/record, can not delete. '
                                               'Found the following active sessions: {0} '
                                               'for scenario: {1}'.format(active_sessions, sce_name))

        scenario_db.remove_all(sce_name_key)
        cache.delete_caches(sce_name)

    scenarios = []
    if scenario_name:
        # if scenario_name exists it takes priority 
        handler.track.scenario = scenario_name
        hostname = host or get_hostname(handler.request)
        scenarios.append(':'.join([hostname, scenario_name]))
    elif host:
        if host == 'all':
            scenarios = [x['name'] for x in scenario_db.get_all()]
            export_dir = os.path.join(static_dir, 'exports')
            if os.path.exists(export_dir):
                log.info('delete export dir')
                shutil.rmtree(export_dir)
        else:
            # get all scenarios for host
            scenarios = [x['name'] for x in scenario_db.get_all(
                {'$regex': '{0}:.*'.format(host)})]
    else:
        raise exception_response(400,
                                 title='scenario or host argument required')
    for scenario_name_key in scenarios:
        delete_scenario(scenario_name_key, force)

    response['data'] = dict(message='stubs deleted.', scenarios=scenarios)
    return response
Exemplo n.º 19
0
def delete_stubs(handler, scenario_name=None, host=None, force=False):
    """delete all data relating to one named scenario or host/s."""
    log.debug('delete_stubs')
    response = {'version': version}
    scenario_db = Scenario()
    static_dir = handler.settings['static_path']

    def delete_scenario(scenario_name_key, force):
        log.debug(u'delete_scenario: {0}'.format(scenario_name_key))
        host, scenario_name = scenario_name_key.split(':')
        cache = Cache(host)
        if not force:
            active_sessions = cache.get_active_sessions(scenario_name,
                                                        local=False)
            if active_sessions:
                raise exception_response(
                    400,
                    title='Sessons in playback/record, can not delete. Found th'
                    'e following active sessions: {0} for scenario: {1}'.
                    format(active_sessions, scenario_name))

        scenario_db.remove_all(scenario_name_key)
        cache.delete_caches(scenario_name)

    scenarios = []
    if scenario_name:
        # if scenario_name exists it takes priority
        handler.track.scenario = scenario_name
        hostname = host or get_hostname(handler.request)
        scenarios.append(':'.join([hostname, scenario_name]))
    elif host:
        if host == 'all':
            scenarios = [x['name'] for x in scenario_db.get_all()]
            export_dir = os.path.join(static_dir, 'exports')
            if os.path.exists(export_dir):
                log.info('delete export dir')
                shutil.rmtree(export_dir)
        else:
            # get all scenarios for host
            scenarios = [
                x['name']
                for x in scenario_db.get_all({'$regex': '{0}:.*'.format(host)})
            ]
    else:
        raise exception_response(400,
                                 title='scenario or host argument required')
    for scenario_name_key in scenarios:
        delete_scenario(scenario_name_key, force)

    response['data'] = dict(message='stubs deleted.', scenarios=scenarios)
    return response
Exemplo n.º 20
0
def stub_count(host, scenario_name=None):
    if host == 'all':
        scenario_name_key = None
    else:
        if not scenario_name:
            # get all stubs for this host
            value = '{0}:.*'.format(host)
            scenario_name_key = {'$regex': value}
        else:
            scenario_name_key = ":".join([host, scenario_name])
    scenario = Scenario()
    result = {'version': version}
    count = scenario.stub_count(scenario_name_key)
    result['data'] = {'count': count,
                      'scenario': scenario_name or 'all',
                      'host': host}
    return result
Exemplo n.º 21
0
def stub_count(host, scenario_name=None):
    if host == 'all':
        scenario_name_key = None
    else:    
        if not scenario_name:
            # get all stubs for this host
            value = '{0}:.*'.format(host)
            scenario_name_key = {'$regex': value}   
        else: 
            scenario_name_key = ":".join([host, scenario_name])   
    scenario = Scenario()
    result = {'version' : version}
    count = scenario.stub_count(scenario_name_key)
    result['data'] = {'count' : count, 
                      'scenario' : scenario_name or 'all',
                      'host' : host}
    return result    
Exemplo n.º 22
0
 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
Exemplo n.º 23
0
    def test_put_stub(self):
        self.http_client.fetch(self.get_url('/stubo/api/put/module?name=/static/cmds/tests/ext/cache/text/example.py'), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(self.get_url('/stubo/api/begin/session?scenario=cache&session=cache_1&mode=record'), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)

        stub = {
           "priority": 1,
           "args": {
              "priority": "1",
              "ext_module": "example"
           },
           "request": {
              "bodyPatterns": {
                 "contains": [
                    "<request>hello</request>\n"
                 ]
              },
              "method": "POST"
           },
           "response": {
              "body": "<response>0</response>\n",
              "status": 200
           }
        }
        import json
        self.http_client.fetch(self.get_url('/stubo/api/put/stub?session=cache_1&ext_module=example'),
                               callback=self.stop,
                               method="POST", body=json.dumps(stub),
                               headers={'Content-Type': 'application/json'})
        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:cache'))
        self.assertEqual(len(stubs), 1)
        from stubo.model.stub import Stub
        stub = Stub(stubs[0]['stub'], 'localhost:cache')
        self.assertEqual(stub.contains_matchers(), ['<request>hello</request>\n'])
        self.assertEqual(stub.response_body()[0], u'<response>0</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'example'})
Exemplo n.º 24
0
    def get(self):
        """

        Returns a list with all scenarios (and URL paths to these resources),
        stub count
        """
        # getting all scenarios
        cursor = self.db.scenario.find()
        # sorting based on name
        cursor.sort([('name', pymongo.ASCENDING)])

        # get size
        scenario_cl = Scenario()
        scenarios_sizes = scenario_cl.size()
        scenarios_recorded = scenario_cl.recorded()
        scenarios_stub_counts = scenario_cl.stub_counts()

        # start mapping data
        scenarios = []
        result_dict = {}
        while (yield cursor.fetch_next):
            document = cursor.next_object()
            try:
                # getting information about recorded, sizes and stub counts
                scenario_recorded = scenarios_recorded.get(document['name'], '-')
                scenario_size = int(scenarios_sizes.get(document['name'], 0))
                scenario_stub_count = scenarios_stub_counts.get(document['name'], 0)
                scenario_name = document['name']
                host, scenario = scenario_name.split(':')
                # getting session data
                sessions = []
                cache = Cache(host)
                for session_info in cache.get_scenario_sessions_information(scenario):
                    sessions.append(session_info)

                scenarios.append({'name': scenario_name,
                                  'recorded': scenario_recorded,
                                  'space_used_kb': scenario_size,
                                  'stub_count': scenario_stub_count,
                                  'sessions': sessions,
                                  'scenarioRef': '/stubo/api/v2/scenarios/objects/%s' % document['name']})
            except KeyError:
                log.warn('Scenario name not found for object: %s' % document['_id'])
        result_dict['data'] = scenarios
        self.set_status(200)
        self.write(result_dict)
Exemplo n.º 25
0
 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       
Exemplo n.º 26
0
    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'
            })
Exemplo n.º 27
0
    def test_put_stub(self):
        self.http_client.fetch(
            self.get_url(
                '/stubo/api/put/module?name=/static/cmds/tests/ext/split/splitter.py'
            ), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(
            self.get_url(
                '/stubo/api/begin/session?scenario=split&session=split_1&mode=record'
            ), self.stop)
        response = self.wait()
        self.assertEqual(response.code, 200)
        self.http_client.fetch(self.get_url(
            '/stubo/api/put/stub?session=split_1&ext_module=splitter'),
                               callback=self.stop,
                               method="POST",
                               body="""||textMatcher||<a>
<pre>hello</pre>
<id>xxx</id>
<post>goodbye</post>
</a>||response||Hello {{1+1}} World""")
        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:split'))
        self.assertEqual(len(stubs), 1)
        from stubo.model.stub import Stub
        stub = Stub(stubs[0]['stub'], 'localhost:split')
        self.assertEqual(
            stub.contains_matchers(),
            [u'<a>\n<pre>hello</pre>\n', '\n<post>goodbye</post>\n</a>'])
        self.assertEqual(stub.response_body()[0], u'Hello {{1+1}} World')
        from datetime import date
        self.assertEqual(
            stub.module(), {
                u'system_date': str(date.today()),
                u'recorded_system_date': str(date.today()),
                u'name': u'splitter'
            })
Exemplo n.º 28
0
def get_session_status(handler, all_hosts=True):
    scenario = Scenario()
    host_scenarios = {}
    for s in scenario.get_all():
        host, scenario_name = s['name'].split(':')
        if not all_hosts and get_hostname(handler.request) != host:
            continue
        if host not in host_scenarios:
            host_scenarios[host] = {}
        sessions = []
        cache = Cache(host)
        for session_name, session in cache.get_sessions(scenario_name):
            # try and get the last_used from the last tracker get/response
            # else when the begin/session playback was called
            last_used = session_last_used(s['name'], session_name)
            if last_used:
                last_used = last_used['start_time'].strftime(
                    '%Y-%m-%d %H:%M:%S')
            else:
                # session has never been used for playback
                last_used = session.get('last_used', '-')
            session['last_used'] = last_used
            sessions.append(session)
        stub_counts = stub_count(host, scenario_name)['data']['count']
        recorded = '-'
        space_used = 0
        if sessions:
            if stub_counts:
                stubs = list(get_stubs(host, scenario_name))
                recorded = max(x['stub'].get('recorded') for x in stubs)
                for stub in stubs:
                    stub = Stub(stub['stub'], s['name'])
                    for matcher in stub.contains_matchers():
                        space_used += len(matcher)
                    space_used += stub.space_used()
                host_scenarios[host][scenario_name] = (sessions, stub_counts,
                                                       recorded,
                                                       human_size(space_used))
            else:
                host_scenarios[host][scenario_name] = (sessions, 0, '-', 0)
    return host_scenarios
Exemplo n.º 29
0
 def test_put_stub(self):
     self.http_client.fetch(
         self.get_url(
             '/stubo/api/put/module?name=/static/cmds/tests/ext/cache/text/example.py'
         ), self.stop)
     response = self.wait()
     self.assertEqual(response.code, 200)
     self.http_client.fetch(
         self.get_url(
             '/stubo/api/begin/session?scenario=cache&session=cache_1&mode=record'
         ), self.stop)
     response = self.wait()
     self.assertEqual(response.code, 200)
     self.http_client.fetch(
         self.get_url(
             '/stubo/api/put/stub?session=cache_1&ext_module=example'),
         callback=self.stop,
         method="POST",
         body=
         """||textMatcher||<request>hello</request>||response||<response>0</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:cache'))
     self.assertEqual(len(stubs), 1)
     from stubo.model.stub import Stub
     stub = Stub(stubs[0]['stub'], 'localhost:cache')
     self.assertEqual(stub.contains_matchers(),
                      ['<request>hello</request>'])
     self.assertEqual(stub.response_body()[0], u'<response>0</response>')
     from datetime import date
     self.assertEqual(
         stub.module(), {
             u'system_date': str(date.today()),
             u'recorded_system_date': str(date.today()),
             u'name': u'example'
         })
Exemplo n.º 30
0
def get_session_status(handler, all_hosts=True):
    scenario = Scenario()
    host_scenarios = {}
    for s in scenario.get_all():
        host, scenario_name = s['name'].split(':')
        if not all_hosts and get_hostname(handler.request)  != host:
            continue
        if host not in host_scenarios:
            host_scenarios[host] = {}
        sessions = []
        cache = Cache(host)
        for session_name, session in cache.get_sessions(scenario_name):
            # try and get the last_used from the last tracker get/response
            # else when the begin/session playback was called
            last_used = session_last_used(s['name'], session_name, 'playback')
            if last_used:
                last_used = last_used['start_time'].strftime('%Y-%m-%d %H:%M:%S')
            else:
                # session has never been used for playback 
                last_used = session.get('last_used', '-')
            session['last_used'] =  last_used  
            sessions.append(session)   
        stub_counts =  stub_count(host, scenario_name)['data']['count']
        recorded = '-'
        space_used = 0
        if sessions:
            if stub_counts:
                stubs = list(get_stubs(host, scenario_name))
                recorded =  max(x['stub'].get('recorded') for x in stubs)   
                for stub in stubs:
                    stub = Stub(stub['stub'], s['name']) 
                    space_used += stub.space_used()             
                host_scenarios[host][scenario_name] = (sessions, stub_counts, 
                                            recorded, human_size(space_used)) 
            else:
                host_scenarios[host][scenario_name] = (sessions, 0, '-', 0)        
    return host_scenarios  
Exemplo n.º 31
0
def get_session_status(handler, all_hosts=True):
    scenario = Scenario()
    host_scenarios = defaultdict()

    # getting a dictionary with sizes for all scenarios
    scenario_sizes = scenario.size()
    scenarios_recorded = scenario.recorded()

    for s in scenario.get_all():
        host, scenario_name = s['name'].split(':')
        if not all_hosts and get_hostname(handler.request) != host:
            continue
        if host not in host_scenarios:
            host_scenarios[host] = {}

        # getting session data
        sessions = []
        cache = Cache(host)

        for session_name, session in cache.get_sessions(scenario_name):
            # try and get the last_used from the last tracker get/response
            # else when the begin/session playback was called
            last_used = session_last_used(s['name'], session_name, 'playback')
            if last_used:
                last_used = last_used['start_time'].strftime(
                    '%Y-%m-%d %H:%M:%S')
            else:
                # session has never been used for playback
                last_used = session.get('last_used', '-')
            session['last_used'] = last_used
            # removing stub information since we aren't using it anyway and it can consume a lot of memory
            session.pop('stubs', None)
            # creating sessions list
            sessions.append(session)
        # getting stub count
        stub_counts = stub_count(host, scenario_name)['data']['count']
        recorded = '-'

        # adding session information
        if sessions:
            if stub_counts:
                # getting scenario size and recorded values
                scenario_size = 0
                try:
                    scenario_size = scenario_sizes[s['name']]
                    recorded = scenarios_recorded[s['name']]
                except KeyError:
                    log.debug("Could not get scenario size for: %s" %
                              s['name'])
                except Exception as ex:
                    log.warn(
                        "Failed to get scenario size for: %s, got error: %s" %
                        (s['name'], ex))
                # creating a dict with scenario information
                host_scenarios[host][scenario_name] = (sessions, stub_counts,
                                                       recorded,
                                                       round(scenario_size, 0))

            else:
                host_scenarios[host][scenario_name] = (sessions, 0, '-', 0)

    return host_scenarios
Exemplo n.º 32
0
 def __init__(self):
     dbname = testdb_name()
     client = getattr(mg.conn, dbname)
     Scenario.__init__(self, db=client)
Exemplo n.º 33
0
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
Exemplo n.º 34
0
def export_stubs(handler, scenario_name):
    cache = Cache(get_hostname(handler.request))
    scenario_name_key = cache.scenario_key_name(scenario_name)
    scenario = Scenario()
    stubs = list(scenario.get_stubs(scenario_name_key))
    """
    [{   u'_id': ObjectId('537c8f1cac5f7303ad704d85'),
    u'scenario': u'localhost:first',
    u'stub': {   u'recorded': u'2014-05-21',
                 u'request': {   u'bodyPatterns': [   {   u'contains': [   u'get my stub\n']}],
                                 u'method': u'POST'},
                 u'response': {   u'body': u'Hello {{1+1}} World\n',
                                  u'delayPolicy': u'slow',
                                  u'status': 200}}}]
    """
    # use user arg or epoch 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 = []
    if len(stubs) > 0:
        for i in range(len(stubs)):
            entry = stubs[i]
            stub = Stub(entry['stub'], scenario_name_key)
            matchers = [('{0}_{1}_{2}.textMatcher'.format(session, i, x),
                         stub.contains_matchers()[x])
                        for x in range(len(stub.contains_matchers()))]
            matchers_str = ",".join(x[0] for x in matchers)
            url_args = 'session={0}'.format(session)
            module_info = stub.module()
            if module_info:
                # Note: not including put/module in the export, modules are shared
                # by multiple scenarios.
                url_args += '&ext_module={0}&stub_created_date={1}&stubbedSystem'\
                    'Date={2}&system_date={3}'.format(module_info['name'],
                    stub.recorded(), module_info.get('recorded_system_date'),
                    module_info.get('system_date'))
            responses = stub.response_body()
            for ii in range(len(responses)):
                response = responses[ii]
                response = ('{0}_{1}.response.{2}'.format(session, i,
                                                          ii), responses[ii])
                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))
    bookmarks = cache.get_all_saved_request_index_data()
    if bookmarks:
        cmds.append('import/bookmarks?location=bookmarks')
        files.append(('bookmarks', json.dumps(bookmarks)))

    files.append(('{0}.commands'.format(scenario_name), b"\r\n".join(cmds)))

    static_dir = handler.settings['static_path']
    scenario_dir = os.path.join(static_dir, 'exports',
                                scenario_name_key.replace(':', '_'))

    if os.path.exists(scenario_dir):
        shutil.rmtree(scenario_dir)
    os.makedirs(scenario_dir)

    archive_name = os.path.join(scenario_dir, scenario_name)
    zout = zipfile.ZipFile(archive_name + '.zip', "w")
    tar = tarfile.open(archive_name + ".tar.gz", "w:gz")
    for finfo in files:
        fname, contents = finfo
        file_path = os.path.join(scenario_dir, fname)
        with codecs.open(file_path, mode='wb', encoding='utf-8') as f:
            f.write(contents)
        tar.add(file_path, fname)
        zout.write(file_path, fname)
    tar.close()
    zout.close()
    shutil.copy(archive_name + '.zip', archive_name + '.jar')

    files.extend([(scenario_name + '.zip', ), (scenario_name + '.tar.gz', ),
                  (scenario_name + '.jar', )])
    links = get_export_links(handler, scenario_name_key, files)
    payload = dict(scenario=scenario_name,
                   scenario_dir=scenario_dir,
                   links=links)
    return dict(version=version, data=payload)
Exemplo n.º 35
0
 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_pairs = [(compute_hash(x), x) for x in stub.response_body()]
         # cache each response id -> text
         for response in response_pairs:
             response_id, response_text = response
             self.set_response_text(scenario_name, session_name, response_id,
                                     response_text)
         # replace response text with response hash ids for session cache 
         # stub.pop('response', None)
         stub.response().pop('body', None)
         stub.response()['ids'] = [x[0] for x in response_pairs]
         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']))
Exemplo n.º 36
0
def export_stubs_to_commands_format(handler, scenario_name):
    """
    Exports scenario to .commands file format.
    :param handler:
    :param scenario_name: <string> Scenario name
    :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
    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_pre_stubs(scenario_name_key))
    if stubs:
        for i in range(len(stubs)):
            entry = stubs[i]
            stub = Stub(entry['stub'], scenario_name_key)
            # if stub is rest - matcher may be None, checking that
            if 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),
                         stub.contains_matchers()[x])
                        for x in range(len(stub.contains_matchers()))]
            matchers_str = ",".join(x[0] for x in matchers)
            url_args = stub.args()
            url_args['session'] = session
            module_info = stub.module()
            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 = 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 = asbool(handler.get_argument('runnable', False))
    runnable_info = dict()

    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)
        runnable_info['number_of_playback_requests'] = number_of_requests
        for nrequest in range(number_of_requests):
            track = playback[nrequest]
            request_text = track.get('request_text')
            if not request_text:
                raise exception_response(
                    400,
                    title=
                    'Unable to obtain playback details, was full tracking enabled?'
                )

            request_file_name = '{0}_{1}.request'.format(session, nrequest)
            files.append((request_file_name, request_text))
            stubo_response_text = track['stubo_response']
            if not isinstance(stubo_response_text, basestring):
                stubo_response_text = unicode(stubo_response_text)
            stubo_response_file_name = '{0}_{1}.stubo_response'.format(
                session, nrequest)
            files.append((stubo_response_file_name, stubo_response_text))
            url_args = track['request_params']
            url_args['session'] = session
            url_args = urlencode(url_args)
            cmds.append(u'get/response?{0},{1}'.format(url_args,
                                                       request_file_name))
        cmds.append('end/session?session={0}'.format(session))

    files.append(('{0}.commands'.format(scenario_name), b"\r\n".join(cmds)))

    static_dir = handler.settings['static_path']
    export_dir = handler.get_argument('export_dir',
                                      scenario_name_key).replace(':', '_')
    export_dir_path = os.path.join(static_dir, 'exports', export_dir)

    if os.path.exists(export_dir_path):
        shutil.rmtree(export_dir_path)
    os.makedirs(export_dir_path)

    archive_name = os.path.join(export_dir_path, scenario_name)
    zout = zipfile.ZipFile(archive_name + '.zip', "w")
    tar = tarfile.open(archive_name + ".tar.gz", "w:gz")
    for finfo in files:
        fname, contents = finfo
        file_path = os.path.join(export_dir_path, fname)
        with codecs.open(file_path, mode='wb', encoding='utf-8') as f:
            f.write(contents)
        f.close()
        tar.add(file_path, fname)
        zout.write(file_path, fname)
    tar.close()
    zout.close()
    shutil.copy(archive_name + '.zip', archive_name + '.jar')

    files.extend([(scenario_name + '.zip', ), (scenario_name + '.tar.gz', ),
                  (scenario_name + '.jar', )])
    # getting links
    links = get_export_links(handler, scenario_name_key, files)

    return links
Exemplo n.º 37
0
                           stub.payload)
                trace.info('stub was transformed into', stub.payload)
        except UserExitModuleNotFound, e:
            # ignore legacy stubbedSystem param
            stub.payload.pop('module')

    scenario_name = session['scenario']
    handler.track.scenario = scenario_name.partition(':')[2]
    session_status = session['status']
    if session_status != 'record':
        raise exception_response(400,
                                 title='Scenario not in record '
                                 'mode - {0} in {1} mode.'.format(
                                     scenario_name, session_status))
    doc = dict(scenario=scenario_name, stub=stub)
    scenario_col = Scenario()
    result = scenario_col.insert_stub(doc, stateful)
    response = {'version': version}
    response['data'] = {'message': result}
    return response


def calculate_delay(policy):
    delay = 0
    delay_type = policy.get('delay_type')
    if delay_type == 'fixed':
        delay = policy['milliseconds']
    elif delay_type == 'normalvariate':
        # Calculate from the normal distribution, but set minimum at zero
        delay = max(
            0.0,
Exemplo n.º 38
0
    def get(self, scenario_name):
        """

        Returns scenario name, current sessions and their states,
        stub count, total, document size. Also, provides direct URL link to stub list.
        :param scenario_name: <string> scenario name

        Response JSON:
        {
            "stubs": 32,
             "space_used_kb": 840,
             "recorded": "2015-07-15",
             "name": "localhost:scenario_16",
             "scenarioRef": "/stubo/api/v2/scenarios/objects/localhost:scenario_16"
         }
        """

        # check if hostname is supplied - if not, override scenario name with new value
        scenario_name = _get_scenario_full_name(self, scenario_name)
        # query MongoDB
        document = yield self.db.scenario.find_one({'name': scenario_name})

        # form a result dictionary
        if document is not None:
            # get stub count
            stub_count = yield self.db.scenario_stub.find({
                'scenario':
                scenario_name
            }).count()
            # get size
            scenario_cl = Scenario()
            size = scenario_cl.size(scenario_name)
            # check if size is None
            if size is None:
                size = 0
            recorded = scenario_cl.recorded(scenario_name)
            if recorded is None:
                recorded = '-'

            host, scenario = scenario_name.split(':')
            # getting session data
            sessions = []
            cache = Cache(host)
            for session_info in cache.get_scenario_sessions_information(
                    scenario):
                sessions.append(session_info)

            result_dict = {
                'name':
                scenario_name,
                'stub_count':
                stub_count,
                'recorded':
                recorded,
                'space_used_kb':
                int(size),
                'scenarioRef':
                '/stubo/api/v2/scenarios/objects/{0}'.format(scenario_name),
                'sessions':
                sessions
            }
            self.set_status(200)
            self.write(result_dict)
        else:
            self.send_error(404)
Exemplo n.º 39
0
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
Exemplo n.º 40
0
 def __init__(self):
     dbname = testdb_name()
     client = getattr(mg.conn, dbname)
     Scenario.__init__(self, db=client)