def test_get_x_user_name_header(self):
        # ensure 'X-User-Name' header is used if available
        with self.__app.test_request_context('/test',
                                             headers={'X-User-Name': 'Test'}):
            owner = UserAuthentication.get_x_user_name_header()
        self.assertEqual(owner, 'Test')

        # ensure 'default-owner' is used if 'X-User-Name' header is not available
        with self.__app.test_request_context('/test'):
            owner = UserAuthentication.get_x_user_name_header()
        self.assertEqual(owner, 'default-owner')
Example #2
0
    def post(self):
        """
        Creates a new simulation which is neither 'initialized' nor 'started'.
        :< json int brainProcesses: Number of brain processes to use (overrides ExD Configuration)
        :< json string experimentConfiguration: Path and name of the experiment configuration file
        :< json string experimentID: The experiment ID of the experiment
        :< json string environmentConfiguration: Path of the custom SDF environment (optional)
        :< json string gzserverHost: The host where gzserver will be run: local for using the same
                                     machine of the backend
        :< json string reservation: the name of the cluster reservation subsequently used to
                                    allocate a job
        :< json string state: The initial state of the simulation
        :< json boolean private: Defines whether the simulation is based on a private experiment
        :< json string playbackPath: The simulation recording to playback (Path to recording root)
        :< json string ctx-id: The context id of the collab if we are running a collab based
                               simulation

        :> json string owner: The simulation owner (Unified Portal user name or 'hbp-default')
        :> json string state: The current state of the simulation (always 'created')
        :> json integer simulationID: The id of the simulation (needed for further REST calls)
        :> json string experimentConfiguration: Path and name of the experiment configuration file
        :> json string environmentConfiguration: Path and name of the environment configuration file
        :> json string creationDate: Date of creation of this simulation
        :> json string gzserverHost: The host where gzserver will be run: local for using the same
                                     machine of the backend, lugano to use a dedicated instance on
                                     the Lugano viz cluster
        :> json string reservation: the name of the cluster reservation subsequently used to
                                    allocate a job
        :> json string experimentID: The experiment ID if the experiment is using the storage
        :> json integer brainProcesses: Number of brain processes for simulation, overrides the
                                        number specified in the experiment configuration file
        :> json string creationUniqueID: The simulation unique creation ID that is used by the
                                         Frontend to identify this simulation
        :>json string playbackPath: Path to simulation recording to play (optional)
        :>json boolean private: Simulation is based on a private experiment

        :status 400: Experiment configuration is not valid
        :status 401: gzserverHost is not valid
        :status 402: Another simulation is already running on the server
        :status 201: Simulation created successfully
        """
        body = request.get_json(force=True)
        sim_id = len(simulations)
        if 'experimentConfiguration' not in body:
            raise NRPServicesClientErrorException('Experiment configuration not given.')

        if ('gzserverHost' in body) and (body.get('gzserverHost') not in ['local', 'lugano']):
            raise NRPServicesClientErrorException('Invalid gazebo server host.', error_code=401)

        if True in [s.state not in ['stopped', 'failed'] for s in simulations]:
            raise NRPServicesClientErrorException(
                'Another simulation is already running on the server.', error_code=409)

        if 'brainProcesses' in body and \
           (not isinstance(body.get('brainProcesses'), int) or body.get('brainProcesses') < 1):
            raise NRPServicesClientErrorException('Invalid number of brain processes.')

        sim_gzserver_host = body.get('gzserverHost', 'local')
        sim_reservation = body.get('reservation', None)
        sim_experiment_id = body.get('experimentID', None)
        sim_state = body.get('state', 'created')
        playback_path = body.get('playbackPath', None)
        sim_owner = UserAuthentication.get_x_user_name_header(request)
        sim_brain_processes = body.get('brainProcesses', 1)
        private = body.get('private', None)
        ctx_id = body.get('ctxId', None)

        sim = Simulation(sim_id,
                         body['experimentConfiguration'],
                         body.get('environmentConfiguration', None),
                         sim_owner,
                         sim_gzserver_host,
                         sim_reservation,
                         sim_brain_processes,
                         sim_experiment_id,
                         sim_state,
                         playback_path=playback_path,
                         private=private,
                         ctx_id=ctx_id)

        sim.creationUniqueID = body.get(
            'creationUniqueID', str(time.time() + random.random()))

        sim.state = "initialized"
        simulations.append(sim)

        return marshal(simulations[sim_id], Simulation.resource_fields), 201, {
            'location': api.url_for(SimulationControl, sim_id=sim_id),
            'gzserverHost': sim_gzserver_host
        }