Exemple #1
0
    def setUp(self):
        self.patch_state = patch(
            'hbp_nrp_backend.simulation_control.__Simulation.Simulation.state')
        self.mock_state = self.patch_state.start()

        #monkey patch StateMachineManager.create_state_machine
        self.original_sm_manager_create_state_machine = sm_manager_module.StateMachineManager.create_state_machine
        sm_manager_module.StateMachineManager.create_state_machine = self.create_sm_mock

        sim_id = 2
        experiment_conf = 'some_exp_id'
        owner = 'some_owner'
        sim_gzserver_host = 'some_gzserver_host'
        self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                       sim_gzserver_host)
        sm_path = path.join(path.split(__file__)[0], "sm_mock.py")

        self.assertIsNotNone(self.__simulation.kill_datetime)
        self.assertIsNotNone(self.__simulation.lifecycle)
        self.assertIsNotNone(self.__simulation.creation_datetime)

        self.assertGreater(self.__simulation.kill_datetime,
                           self.__simulation.creation_datetime)

        self.create_sm_mock('SM1', 0, sm_path)
        self.create_sm_mock('SM2', 0)
        self.create_sm_mock('SM3', 0, sm_path)

        with open(sm_path, "r") as sm_file:
            self.valid_sm_code = sm_file.read()
 def setUp(self):
     del simulations[:]
     simulations.append(
         Simulation(0, 'experiments/experiment_data/test_1.exc', None,
                    'default-owner', 'created'))
     simulations.append(
         Simulation(1, 'experiments/experiment_data/test_1.exc', None,
                    'im-not-the-owner', 'created'))
 def setUp(self):
     del simulations[:]
     simulations.append(Simulation(0, 'experiment_0', None,
                                   'default-owner', 'local', 'created'))
     simulations.append(Simulation(1, 'experiment_1', None,
                                   'untrusted-owner', 'local', 'created'))
     self.sim = simulations[0]
     self.sim.cle = MagicMock()
     self.sim.cle.edit_simulation_transfer_function = MagicMock(
         return_value='')
    def setUp(self):

        del simulations[:]
        simulations.append(
            Simulation(0, 'experiment1', None, 'default-owner', 'created'))
        simulations.append(
            Simulation(1, 'experiment2', None, 'other-owner', 'created'))

        for s in simulations:
            s.cle = MagicMock()
            s.cle.command_simulation_recorder = MagicMock(
                return_value=srv.SimulationRecorderResponse())
Exemple #5
0
    def setUp(self):
        del simulations[:]
        simulations.append(
            Simulation(0, 'experiment_0', None, 'default-owner', 'local',
                       'created'))
        simulations.append(
            Simulation(1, 'experiment_1', None, 'untrusted-owner', 'local',
                       'created'))

        self.sim = simulations[0]
        self.sim.cle = MagicMock()
        self.sim.cle.get_simulation_brain = MagicMock(return_value=brain_data)
        self.sim.cle.set_simulation_brain = MagicMock(return_value=set_ret_ok)
 def setUp(self):
     del simulations[:]
     simulations.append(
         Simulation(0, 'experiment_0', 'default-owner', 'local', 'created'))
     self.sim = simulations[0]
     self.sim.cle = MagicMock()
     self.sim.cle.extend_simulation_timeout = MagicMock(return_value=True)
Exemple #7
0
    def setUp(self):
        patch_StorageClient =\
            patch('hbp_nrp_backend.storage_client_api.StorageClient.StorageClient')
        self.addCleanup(patch_StorageClient.stop)
        self.mock_StorageClient = patch_StorageClient.start()
        self.mock_storageClient_instance = self.mock_StorageClient.return_value

        del simulations[:]
        simulations.append(Simulation(
            0, 'experiments/experiment_data/test_1.exc', None, 'default-owner', 'created'))
        simulations.append(Simulation(
            1, 'experiments/experiment_data/test_1.exc', None, 'im-not-the-owner', 'created'))

        self.experiment_id = "0000-0000"
        # Correct request
        self.correct_reset_url = '/simulation/0/' + self.experiment_id + '/reset'
    def test_brain_reset_from_storage(self, mock_get_exp_data,
                                      mock_get_model_path,
                                      mock_get_experiment_path):
        simulations.append(
            Simulation(3, 'experiments/experiment_data/test_5.exc', None,
                       'default-owner', 'created'))
        simulations[2].cle = mock.MagicMock()
        simulations[2].cle.set_simulation_brain.return_value = Mock(
            error_message="")

        mock_get_experiment_path.return_value = PATH
        mock_get_model_path.return_value = os.path.join(PATH, 'models')

        bibi_path = os.path.join(PATH,
                                 'experiments/experiment_data/bibi_4.bibi')
        experiment_file_path = os.path.join(
            PATH, 'experiments/experiment_data/test_5.exc')

        with open(experiment_file_path) as exd_file:
            experiment = exp_conf_api_gen.CreateFromDocument(exd_file.read())

        with open(bibi_path) as b_file:
            bibi = bibi_api_gen.CreateFromDocument(b_file.read())

        mock_get_exp_data.return_value = experiment, bibi

        with patch("__builtin__.open", mock_open(read_data="data")) as mock_file, \
                patch('os.listdir', return_value=['nrpTemp']) as mock_tempfile:
            response = self.client.put('/simulation/2/reset',
                                       data=json.dumps({
                                           'resetType':
                                           ResetSimulationRequest.RESET_BRAIN
                                       }))
            self.assertEqual(200, response.status_code)
Exemple #9
0
    def setUp(self):
        del simulations[:]
        simulations.append(
            Simulation(0, 'experiment_0', 'default-owner', 'local', 'created'))
        simulations.append(
            Simulation(1, 'experiment_1', 'untrusted-owner', 'local',
                       'created'))

        self.sim = simulations[0]
        self.sim.cle = MagicMock()
        self.sim.cle.get_simulation_brain = MagicMock(return_value=brain_data)
        self.sim.cle.set_simulation_brain = MagicMock(return_value=set_ret_ok)

        self.path_can_view = patch(
            'hbp_nrp_backend.__UserAuthentication.UserAuthentication.can_view')
        self.path_can_view.start().return_value = True
Exemple #10
0
    def setUp(self):
        self.storage_client = SimulationResetStorage.storage_client
        self.mock_storage_client = mock.MagicMock()

        SimulationResetStorage.storage_client = self.mock_storage_client
        del simulations[:]
        simulations.append(
            Simulation(0, 'experiments/experiment_data/test_1.exc',
                       'default-owner', 'created'))
        simulations.append(
            Simulation(1, 'experiments/experiment_data/test_1.exc',
                       'im-not-the-owner', 'created'))

        self.experiment_id = "0000-0000"
        # Correct request
        self.correct_reset_url = '/simulation/0/{exp_id}/reset'.format(
            exp_id=self.experiment_id)
Exemple #11
0
 def setUp(self):
     del simulations[:]
     simulations.append(
         Simulation(0,
                    'experiment1',
                    'default-owner',
                    'local',
                    'view',
                    state='paused'))
Exemple #12
0
    def setUp(self):
        del simulations[:]
        simulations.append(Simulation(0, 'experiment1', 'default-owner', 'created'))
        simulations[0].cle = mock.MagicMock()
        simulations[0].cle.set_simulation_populations = mock.MagicMock(return_value=set_ret_ok)
        simulations[0].cle.get_populations = mock.MagicMock(return_value=neurons)

        self.path_can_view = mock.patch('hbp_nrp_backend.__UserAuthentication.UserAuthentication.can_view')
        self.path_can_view.start().return_value = True
Exemple #13
0
    def setUp(self):
        rospy.ServiceProxy = MockServiceProxy
        rospy.wait_for_service = mock.Mock(return_value=mock.Mock())

        self.mc = MaterialControl()
        self.lc = LightControl()

        del simulations[:]
        simulations.append(Simulation(0, 'test', 'default-owner', 'created'))
Exemple #14
0
 def test_simulation_constructor(self):
     sim_id = 2
     experiment_conf = 'some_exp_id'
     owner = 'some_owner'
     sim_gzserver_host = 'some_gzserver_host'
     self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                    sim_gzserver_host, 'view')
     self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                    sim_gzserver_host, 'view', 'created')
     self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                    sim_gzserver_host, 'view', 'paused')
Exemple #15
0
    def setUp(self):
        del simulations[:]
        simulations.append(Simulation(
            0, 'experiment1', None, 'default-owner', 'created'))
        simulations[0].cle = MagicMock()
        self.test_directory = os.path.split(__file__)[0]
        csv_path = os.path.join(self.test_directory,
                                "csv_mock", "csv_mock.csv")
        self.files = MagicMock(name='left_wheel_join_position.csv',
                               temporary_path=csv_path)

        simulations[0].cle.get_simulation_CSV_recorders_files = MagicMock(
            return_value=self.files)
        patch_StorageClient = patch(
            'hbp_nrp_backend.storage_client_api.StorageClient.StorageClient')
        self.addCleanup(patch_StorageClient.stop)
        self.mock_StorageClient = patch_StorageClient.start()
        self.mock_storageClient_instance = self.mock_StorageClient.return_value
Exemple #16
0
class TestSimulation(unittest.TestCase):
    def create_sm_mock(self, sm_id, sim_id, sm_path=None):
        sm = Mock(sm_id=sm_id, sim_id=sim_id, sm_path=sm_path)
        self.__simulation.state_machines.append(sm)
        return sm

    def setUp(self):
        self.patch_state = patch(
            'hbp_nrp_backend.simulation_control.__Simulation.Simulation.state')
        self.mock_state = self.patch_state.start()

        #monkey patch StateMachineManager.create_state_machine
        self.original_sm_manager_create_state_machine = sm_manager_module.StateMachineManager.create_state_machine
        sm_manager_module.StateMachineManager.create_state_machine = self.create_sm_mock

        sim_id = 2
        experiment_conf = 'some_exp_id'
        owner = 'some_owner'
        sim_gzserver_host = 'some_gzserver_host'
        self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                       sim_gzserver_host)
        sm_path = path.join(path.split(__file__)[0], "sm_mock.py")

        self.assertIsNotNone(self.__simulation.kill_datetime)
        self.assertIsNotNone(self.__simulation.lifecycle)
        self.assertIsNotNone(self.__simulation.creation_datetime)

        self.assertGreater(self.__simulation.kill_datetime,
                           self.__simulation.creation_datetime)

        self.create_sm_mock('SM1', 0, sm_path)
        self.create_sm_mock('SM2', 0)
        self.create_sm_mock('SM3', 0, sm_path)

        with open(sm_path, "r") as sm_file:
            self.valid_sm_code = sm_file.read()

    def tearDown(self):
        self.mock_state = self.patch_state.stop()
        sm_manager_module.StateMachineManager.create_state_machine = self.original_sm_manager_create_state_machine

    def test_set_killtime_early(self):
        self.__simulation.kill_datetime = self.__simulation.creation_datetime
        self.assertEqual(self.__simulation.kill_datetime,
                         self.__simulation.creation_datetime)

    def test_simulation_constructor(self):
        sim_id = 2
        experiment_conf = 'some_exp_id'
        owner = 'some_owner'
        sim_gzserver_host = 'some_gzserver_host'
        self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                       sim_gzserver_host, 'view')
        self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                       sim_gzserver_host, 'view', 'created')
        self.__simulation = Simulation(sim_id, experiment_conf, None, owner,
                                       sim_gzserver_host, 'view', 'paused')

    def test_simulation_get_state_machine_code(self):
        self.assertIsNotNone(self.__simulation.state_machine_manager)
        code = self.__simulation.get_state_machine_code('SM2')
        self.assertFalse(code)
        code = self.__simulation.get_state_machine_code('SM3')
        self.assertEqual(self.valid_sm_code, code)
        response = self.__simulation.get_state_machine_code('non-existing-SM')
        self.assertFalse(response)

    def test_simulation_set_state_machine_code(self):
        another_valid_code = "import smach_ros\n" +\
            "from mock import Mock\n" +\
            "sm = Mock()"
        self.__simulation.state = 'paused'
        response = self.__simulation.set_state_machine_code(
            'SM2', another_valid_code)

        sm2_updated_code = self.__simulation.get_state_machine_code('SM2')
        self.assertEqual(another_valid_code, sm2_updated_code)
        self.__simulation.state_machines[
            1].request_termination.assert_called_once_with()
        self.__simulation.state_machines[
            1].wait_termination.assert_called_once_with()

        # The create method is mandatory in the state machine code
        self.__simulation.state_machines[0].initialize_sm = Mock(
            side_effect=AttributeError)
        self.__simulation.state_machines[0].is_running = False
        invalid_code = "def spwan_state_machine():\n return None"
        self.assertRaises(AttributeError,
                          self.__simulation.set_state_machine_code, 'SM1',
                          invalid_code)

        # Creation of a new state machine
        response = self.__simulation.set_state_machine_code(
            'SM4', another_valid_code)
        sm4_updated_code = self.__simulation.get_state_machine_code('SM4')
        self.assertEqual(another_valid_code, sm4_updated_code)

    def test_simulation_delete_state_machine(self):
        self.assertIsNotNone(self.__simulation.state_machine_manager)
        number_of_state_machines = len(self.__simulation.state_machines)
        self.__simulation.delete_state_machine('SM2')
        self.assertEqual(number_of_state_machines - 1,
                         len(self.__simulation.state_machines))
        success, error_message = self.__simulation.delete_state_machine('SM2')
        self.assertFalse(success)
        self.assertTrue(error_message)

        number_of_state_machines = len(self.__simulation.state_machines)
        self.__simulation.delete_state_machine('SM1')
        self.__simulation.delete_state_machine('SM3')
        self.assertEqual(number_of_state_machines - 2,
                         len(self.__simulation.state_machines))

        success, error_message = self.__simulation.delete_state_machine('SM1')
        self.assertFalse(success)
        self.assertTrue(error_message)
    def setUp(self):
        del simulations[:]
        simulations.append(Simulation(0, 'experiment_0', 'default-owner', 'local', 'created'))
        simulations.append(Simulation(1, 'experiment_1', 'untrusted-owner', 'local', 'created'))
        self.sim = simulations[0]
        self.sim.cle = MagicMock()

        self.object_tfs = [
            MockObject({
                "devices": [
                    MockObject({
                        'name': 'foo',
                        'type': 'footype',
                        'neurons': MockObject({
                          'name': 'foopop',
                            'type': 'foopoptype',
                            'ids': [0,8,15],
                            'start': 0,
                            'stop': 8,
                            'step': 15
                        })
                    })
                ],
                'topics': [
                    MockObject({
                        'name': 'bar',
                        'topic': 'bartopic',
                        'type': 'bartype',
                        'publishing': 'barpub'
                    })
                ],
                'variables': [
                    MockObject({
                        'name': 'varname',
                        'type': 'vartype',
                        'initial_value': 'varvalue'
                    })
                ],
                'name': 'tf',
                'type': 'tf_type',
                'code': 'pass'
            })
        ]

        self.dict_tf = {
                    u'name': u'tf',
                    u'type': u'tf_type',
                    u'code': u'pass',
                    u'devices': [{
                        u'name': u'foo',
                        u'type': u'footype',
                        u'neurons': {
                            u'name': u'foopop',
                            u'type': u'foopoptype',
                            u'ids': [
                                {u'id': 0},
                                {u'id': 8},
                                {u'id': 15}
                            ],
                            u'start': 0,
                            u'stop': 8,
                            u'step': 15
                        }
                    }],
                    u'topics': [{
                        u'name': u'bar',
                        u'topic': u'bartopic',
                        u'type': u'bartype',
                        u'publishing': u'barpub'
                    }],
                    u'variables': [{
                        u'name': u'varname',
                        u'type': u'vartype',
                        u'initial_value': u'varvalue'
                    }]
                }
Exemple #18
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
        }
Exemple #19
0
 def setUp(self):
     del simulations[:]
     simulations.append(
         Simulation(0, 'experiment1', None, 'default-owner', 'created'))