Esempio n. 1
0
    def delete(self, scenario_name):
        """

        Deletes scenario object from database. Fails if there are existing stubs
        in the database
        :param scenario_name: <string> scenario name
        """
        scenario_name = _get_scenario_full_name(self, scenario_name)

        query = {'name': scenario_name}
        # query MongoDB
        document = yield self.db.scenario.find_one(query)

        if document is not None:
            # delete document
            yield self.db.scenario.remove(query)
            try:
                # scenario name contains host, splitting it and using this value to clear cache
                host, name = scenario_name.split(":")

                cache = Cache(host)
                # change cache
                cache.delete_caches(name)
            except Exception as ex:
                log.warn("Failed to delete caches for scenario: %s. Got error: %s" % (scenario_name, ex))

            self.set_status(200)
        else:
            self.send_error(412, reason="Precondition failed - scenario (%s) does not exist." % scenario_name)
Esempio n. 2
0
    def delete(self, scenario_name):
        """

        Deletes scenario object from database. Fails if there are existing stubs
        in the database
        :param scenario_name: <string> scenario name
        """
        scenario_name = _get_scenario_full_name(self, scenario_name)

        query = {'name': scenario_name}
        # query MongoDB
        document = yield self.db.scenario.find_one(query)

        if document is not None:
            # delete document
            yield self.db.scenario.remove(query)
            try:
                # scenario name contains host, splitting it and using this value to clear cache
                host, name = scenario_name.split(":")

                cache = Cache(host)
                # change cache
                cache.delete_caches(name)
            except Exception as ex:
                log.warn(
                    "Failed to delete caches for scenario: %s. Got error: %s" %
                    (scenario_name, ex))

            self.set_status(200)
        else:
            self.send_error(
                412,
                reason="Precondition failed - scenario (%s) does not exist." %
                scenario_name)
Esempio n. 3
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
Esempio n. 4
0
 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)
Esempio n. 5
0
    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='E016: Sessons in playback/record, can'
                    'not delete. Found the following active sessions: {0} for '
                    'scenario: {1}'.format(active_sessions, scenario_name))

        scenario_db.remove_all(scenario_name_key)
        cache.delete_caches(scenario_name)
Esempio n. 6
0
    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)
Esempio n. 7
0
    def delete(self, scenario_name):
        """
        Deletes specified scenario. If force is not supplied - checks for active sessions and if there are any - stops
        deletion. If force is supplied or there are no active sessions - deletes all stubs for scenario
        :param scenario_name:
        :return:
        """
        response = {
            'version': version
        }
        # checking for supplied headers
        host = None
        force = False
        if 'target_host' in self.request.headers:
            host = self.request.headers['target_host']

        if 'force' in self.request.headers:
            force = asbool(self.request.headers['force'])

        # getting full scenario database
        scenario_name = _get_scenario_full_name(self, scenario_name, host)

        host, scenario = scenario_name.split(':')
        cache = Cache(host)
        # if force is False or absent - checking for active sessions and if there are any - aborting deletion
        if not force:
            active_sessions = cache.get_active_sessions(scenario,
                                                        local=False)
            if active_sessions:
                self.set_status(409)
                error = 'Sessons in playback/record, can not delete. Found the ' \
                        'following active sessions: {0} for scenario: {1}'.format(active_sessions, scenario_name)
                response['error'] = error
                self.write(response)
                return
        # asynchronous deletion of stubs, returns two params - "ok" and "n" (deleted items count)
        result = yield self.db.scenario_stub.remove({'scenario': scenario_name})
        # deleting scenario from cache
        cache.delete_caches(scenario)
        response['data'] = "Deleted stubs count: %s" % result['n']
        self.write(response)
Esempio n. 8
0
    def delete(self, scenario_name):
        """
        Deletes specified scenario. If force is not supplied - checks for active sessions and if there are any - stops
        deletion. If force is supplied or there are no active sessions - deletes all stubs for scenario
        :param scenario_name:
        :return:
        """
        response = {'version': version}
        # checking for supplied headers
        host = None
        force = False
        if 'target_host' in self.request.headers:
            host = self.request.headers['target_host']

        if 'force' in self.request.headers:
            force = asbool(self.request.headers['force'])

        # getting full scenario database
        scenario_name = _get_scenario_full_name(self, scenario_name, host)

        host, scenario = scenario_name.split(':')
        cache = Cache(host)
        # if force is False or absent - checking for active sessions and if there are any - aborting deletion
        if not force:
            active_sessions = cache.get_active_sessions(scenario, local=False)
            if active_sessions:
                self.set_status(409)
                error = 'Sessons in playback/record, can not delete. Found the ' \
                        'following active sessions: {0} for scenario: {1}'.format(active_sessions, scenario_name)
                response['error'] = error
                self.write(response)
                return
        # asynchronous deletion of stubs, returns two params - "ok" and "n" (deleted items count)
        result = yield self.db.scenario_stub.remove(
            {'scenario': scenario_name})
        # deleting scenario from cache
        cache.delete_caches(scenario)
        response['data'] = "Deleted stubs count: %s" % result['n']
        self.write(response)