def test_return_get_experiment_uses_by_id(self): expected_sess_id = SessionId.SessionId("whatever") expected_usage = ExperimentUsage(10, time.time(), time.time(), '127.0.0.1', ExperimentId("exp","cat")) command_sent = CommandSent(Command.Command("request"), time.time(), Command.Command("response"), time.time()) expected_usage.append_command(command_sent) loaded_file_sent = LoadedFileSent('content-of-the-file', time.time(), Command.Command("response"), time.time(), 'program') expected_usage.append_file(loaded_file_sent) expected_finished_result = FinishedReservationResult(expected_usage) expected_alive_result = WaitingReservationResult() expected_cancelled_result = CancelledReservationResult() self.mock_ups.return_values['get_experiment_uses_by_id'] = (expected_finished_result, expected_alive_result, expected_cancelled_result) results = self.rfm.get_experiment_uses_by_id(expected_sess_id, (SessionId.SessionId('reservation'), SessionId.SessionId('reservation2'), SessionId.SessionId('reservation3') )) self.assertEquals(3, len(results)) self.assertEquals(expected_finished_result.status, results[0].status) self.assertEquals(expected_alive_result.status, results[1].status) self.assertEquals(expected_cancelled_result.status, results[2].status) self.assertEquals(expected_usage, expected_finished_result.experiment_use)
def create_usage(gateway, reservation_id = 'my_reservation_id'): session = gateway.Session() try: student1 = gateway._get_user(session, 'student1') initial_usage = ExperimentUsage() initial_usage.start_date = time.time() initial_usage.end_date = time.time() initial_usage.from_ip = "130.206.138.16" initial_usage.experiment_id = ExperimentId("ud-dummy","Dummy experiments") initial_usage.coord_address = CoordAddress("machine1","instance1","server1") initial_usage.reservation_id = reservation_id file1 = FileSent( 'path/to/file1', '{sha}12345', time.time() ) file2 = FileSent( 'path/to/file2', '{sha}123456', time.time(), Command.Command('response'), time.time(), file_info = 'program' ) command1 = CommandSent( Command.Command("your command1"), time.time() ) command2 = CommandSent( Command.Command("your command2"), time.time(), Command.Command("your response2"), time.time() ) initial_usage.append_command(command1) initial_usage.append_command(command2) initial_usage.append_file(file1) initial_usage.append_file(file2) initial_usage.request_info = {'facebook' : False, 'permission_scope' : 'user', 'permission_id' : student1.id} gateway.store_experiment_usage(student1.login, initial_usage) return student1, initial_usage, command1, command2, file1, file2 finally: session.close()
def create_usage(gateway, reservation_id = 'my_reservation_id'): session = gateway.Session() student1 = gateway._get_user(session, 'student1') initial_usage = ExperimentUsage() initial_usage.start_date = time.time() initial_usage.end_date = time.time() initial_usage.from_ip = "130.206.138.16" initial_usage.experiment_id = ExperimentId("ud-dummy","Dummy experiments") initial_usage.coord_address = CoordAddress.CoordAddress("machine1","instance1","server1") #.translate_address("server1:instance1@machine1") initial_usage.reservation_id = reservation_id file1 = FileSent( 'path/to/file1', '{sha}12345', time.time() ) file2 = FileSent( 'path/to/file2', '{sha}123456', time.time(), Command.Command('response'), time.time(), file_info = 'program' ) command1 = CommandSent( Command.Command("your command1"), time.time() ) command2 = CommandSent( Command.Command("your command2"), time.time(), Command.Command("your response2"), time.time() ) initial_usage.append_command(command1) initial_usage.append_command(command2) initial_usage.append_file(file1) initial_usage.append_file(file2) initial_usage.request_info = {'facebook' : False} gateway.store_experiment_usage(student1.login, initial_usage) return student1, initial_usage, command1, command2, file1, file2
def _parse_experiment_result(self, experiment_result): if experiment_result['status'] == ReservationResult.ALIVE: if experiment_result['running']: return RunningReservationResult() else: return WaitingReservationResult() elif experiment_result['status'] == ReservationResult.CANCELLED: return CancelledReservationResult() elif experiment_result['status'] == ReservationResult.FORBIDDEN: return ForbiddenReservationResult() experiment_use = experiment_result['experiment_use'] experiment_id = ExperimentId(experiment_use['experiment_id']['exp_name'], experiment_use['experiment_id']['cat_name']) addr = experiment_use['coord_address'] if 'machine_id' in addr: coord_address = CoordAddress(addr['machine_id'],addr['instance_id'],addr['server_id']) else: coord_address = CoordAddress(addr['host'],addr['process'],addr['component']) use = ExperimentUsage(experiment_use['experiment_use_id'], experiment_use['start_date'], experiment_use['end_date'], experiment_use['from_ip'], experiment_id, experiment_use['reservation_id'], coord_address, experiment_use['request_info']) for sent_file in experiment_use['sent_files']: response = Command(sent_file['response']['commandstring']) if 'commandstring' in sent_file['response'] and sent_file['response'] is not None else NullCommand if sent_file['file_info'] == {}: file_info = None else: file_info = sent_file['file_info'] unserialized_sent_file = LoadedFileSent( sent_file['file_content'], sent_file['timestamp_before'], response, sent_file['timestamp_after'], file_info) use.append_file(unserialized_sent_file) for command in experiment_use['commands']: request = Command(command['command']['commandstring']) if 'commandstring' in command['command'] and command['command'] is not None else NullCommand response_command = command['response']['commandstring'] if 'commandstring' in command['response'] and command['response'] is not None else None if response_command is None or response_command == {}: response = NullCommand() else: response = Command(response_command) if command['timestamp_after'] is None or command['timestamp_after'] == {}: timestamp_after = None else: timestamp_after = command['timestamp_after'] unserialized_command = CommandSent(request, command['timestamp_before'], response, timestamp_after) use.append_command(unserialized_command) return FinishedReservationResult(use)
def test_get_experiment_uses_by_id(self): port = 15131 self.configurationManager._set_value(self.rfs.FACADE_JSON_PORT, port) self.rfs.start() try: client = WebLabDeustoClient("http://localhost:%s/weblab/" % port) expected_sess_id = SessionId.SessionId("whatever") expected_usage = ExperimentUsage(10, time.time(), time.time(), '127.0.0.1', ExperimentId("exp","cat"), 'reser1', CoordAddress('machine','instance','server')) command_sent = CommandSent(Command.Command("request"), time.time(), Command.Command("response"), time.time()) expected_usage.append_command(command_sent) loaded_file_sent = LoadedFileSent('content-of-the-file', time.time(), Command.Command("response"), time.time(), 'program') expected_usage.append_file(loaded_file_sent) expected_finished_result = FinishedReservationResult(expected_usage) expected_alive_result = RunningReservationResult() expected_cancelled_result = CancelledReservationResult() self.mock_server.return_values['get_experiment_uses_by_id'] = (expected_finished_result, expected_alive_result, expected_cancelled_result) expected_reservations = (SessionId.SessionId('reservation'), SessionId.SessionId('reservation2'), SessionId.SessionId('reservation3') ) results = client.get_experiment_uses_by_id(expected_sess_id, expected_reservations) self.assertEquals( expected_sess_id.id, self.mock_server.arguments['get_experiment_uses_by_id'][0]) self.assertEquals( expected_reservations, tuple(self.mock_server.arguments['get_experiment_uses_by_id'][1])) self.assertEquals(3, len(results)) self.assertEquals(expected_finished_result.status, results[0].status) self.assertEquals(expected_alive_result.status, results[1].status) self.assertEquals(expected_cancelled_result.status, results[2].status) self.assertEquals(expected_usage, results[0].experiment_use) finally: self.rfs.stop()
def test_return_get_experiment_uses_by_id(self): expected_sess_id = SessionId.SessionId("whatever") expected_usage = ExperimentUsage(10, time.time(), time.time(), '127.0.0.1', ExperimentId("exp", "cat")) command_sent = CommandSent(Command.Command("request"), time.time(), Command.Command("response"), time.time()) expected_usage.append_command(command_sent) loaded_file_sent = LoadedFileSent('content-of-the-file', time.time(), Command.Command("response"), time.time(), 'program') expected_usage.append_file(loaded_file_sent) expected_finished_result = FinishedReservationResult(expected_usage) expected_alive_result = WaitingReservationResult() expected_cancelled_result = CancelledReservationResult() self.mock_ups.return_values['get_experiment_uses_by_id'] = ( expected_finished_result, expected_alive_result, expected_cancelled_result) results = self.rfm.get_experiment_uses_by_id( expected_sess_id, (SessionId.SessionId('reservation'), SessionId.SessionId('reservation2'), SessionId.SessionId('reservation3'))) self.assertEquals(3, len(results)) self.assertEquals(expected_finished_result.status, results[0].status) self.assertEquals(expected_alive_result.status, results[1].status) self.assertEquals(expected_cancelled_result.status, results[2].status) self.assertEquals(expected_usage, expected_finished_result.experiment_use)
def _store_two_reservations(self): # # Two users: student2, that started before "any" but finished after "any", and "any" then. Both use # the same experiment. # db_gw = self.ups._db_manager session = db_gw.Session() try: student1 = db_gw._get_user(session, 'student1') student2 = db_gw._get_user(session, 'student2') finally: session.close() reservation_id1 = SessionId.SessionId(u'5') initial_usage1 = ExperimentUsage() initial_usage1.start_date = time.time() initial_usage1.end_date = time.time() initial_usage1.from_ip = u"130.206.138.16" initial_usage1.experiment_id = ExperimentId(u"ud-dummy",u"Dummy experiments") initial_usage1.coord_address = CoordAddress(u"machine1",u"instance1",u"server1") initial_usage1.reservation_id = reservation_id1.id initial_usage1.request_info = { 'permission_scope' : 'user', 'permission_id' : student1.id } valid_file_path = os.path.relpath(os.sep.join(('test','__init__.py'))) file1 = FileSent( valid_file_path, u'{sha}12345', time.time()) file2 = FileSent( valid_file_path, u'{sha}123456', time.time(), Command(u'response'), time.time(), file_info = u'program') command1 = CommandSent( Command(u"your command1"), time.time()) command2 = CommandSent( Command(u"your command2"), time.time(), Command(u"your response2"), time.time()) initial_usage1.append_command(command1) initial_usage1.append_command(command2) initial_usage1.append_file(file1) initial_usage1.append_file(file2) reservation_id2 = SessionId.SessionId(u'6') initial_usage2 = ExperimentUsage() initial_usage2.start_date = time.time() initial_usage2.end_date = time.time() initial_usage2.from_ip = u"130.206.138.16" initial_usage2.experiment_id = ExperimentId(u"ud-dummy",u"Dummy experiments") initial_usage2.coord_address = CoordAddress(u"machine1",u"instance1",u"server1") initial_usage2.reservation_id = reservation_id2.id initial_usage2.request_info = { 'permission_scope' : 'user', 'permission_id' : student2.id } file1 = FileSent( valid_file_path, u'{sha}12345', time.time()) file2 = FileSent( valid_file_path, u'{sha}123456', time.time(), Command(u'response'), time.time(), file_info = u'program') command1 = CommandSent( Command(u"your command1"), time.time()) command2 = CommandSent( Command(u"your command2"), time.time(), Command(u"your response2"), time.time()) initial_usage2.append_command(command1) initial_usage2.append_command(command2) initial_usage2.append_file(file1) initial_usage2.append_file(file2) self.ups._db_manager.store_experiment_usage('student1', initial_usage1) self.ups._db_manager.store_experiment_usage('student2', initial_usage2) return (reservation_id1, reservation_id2), (initial_usage1, initial_usage2)
def _parse_experiment_result(self, experiment_result): if experiment_result['status'] == ReservationResult.ALIVE: if experiment_result['running']: return RunningReservationResult() else: return WaitingReservationResult() elif experiment_result['status'] == ReservationResult.CANCELLED: return CancelledReservationResult() elif experiment_result['status'] == ReservationResult.FORBIDDEN: return ForbiddenReservationResult() experiment_use = experiment_result['experiment_use'] experiment_id = ExperimentId( experiment_use['experiment_id']['exp_name'], experiment_use['experiment_id']['cat_name']) addr = experiment_use['coord_address'] if 'machine_id' in addr: coord_address = CoordAddress(addr['machine_id'], addr['instance_id'], addr['server_id']) else: coord_address = CoordAddress(addr['host'], addr['process'], addr['component']) use = ExperimentUsage(experiment_use['experiment_use_id'], experiment_use['start_date'], experiment_use['end_date'], experiment_use['from_ip'], experiment_id, experiment_use['reservation_id'], coord_address, experiment_use['request_info']) for sent_file in experiment_use['sent_files']: response = Command( sent_file['response']['commandstring'] ) if 'commandstring' in sent_file['response'] and sent_file[ 'response'] is not None else NullCommand if sent_file['file_info'] == {}: file_info = None else: file_info = sent_file['file_info'] unserialized_sent_file = LoadedFileSent( sent_file['file_content'], sent_file['timestamp_before'], response, sent_file['timestamp_after'], file_info) use.append_file(unserialized_sent_file) for command in experiment_use['commands']: request = Command( command['command']['commandstring'] ) if 'commandstring' in command['command'] and command[ 'command'] is not None else NullCommand response_command = command['response'][ 'commandstring'] if 'commandstring' in command[ 'response'] and command['response'] is not None else None if response_command is None or response_command == {}: response = NullCommand() else: response = Command(response_command) if command['timestamp_after'] is None or command[ 'timestamp_after'] == {}: timestamp_after = None else: timestamp_after = command['timestamp_after'] unserialized_command = CommandSent(request, command['timestamp_before'], response, timestamp_after) use.append_command(unserialized_command) return FinishedReservationResult(use)
def _store_two_reservations(self): # # Two users: student2, that started before "any" but finished after "any", and "any" then. Both use # the same experiment. # reservation_id1 = SessionId.SessionId(u'5') initial_usage1 = ExperimentUsage() initial_usage1.start_date = time.time() initial_usage1.end_date = time.time() initial_usage1.from_ip = u"130.206.138.16" initial_usage1.experiment_id = ExperimentId(u"ud-dummy",u"Dummy experiments") initial_usage1.coord_address = CoordAddress.CoordAddress(u"machine1",u"instance1",u"server1") #.translate_address("server1:instance1@machine1") initial_usage1.reservation_id = reservation_id1.id valid_file_path = os.path.relpath(os.sep.join(('test','__init__.py'))) file1 = FileSent( valid_file_path, u'{sha}12345', time.time()) file2 = FileSent( valid_file_path, u'{sha}123456', time.time(), Command(u'response'), time.time(), file_info = u'program') command1 = CommandSent( Command(u"your command1"), time.time()) command2 = CommandSent( Command(u"your command2"), time.time(), Command(u"your response2"), time.time()) initial_usage1.append_command(command1) initial_usage1.append_command(command2) initial_usage1.append_file(file1) initial_usage1.append_file(file2) reservation_id2 = SessionId.SessionId(u'6') initial_usage2 = ExperimentUsage() initial_usage2.start_date = time.time() initial_usage2.end_date = time.time() initial_usage2.from_ip = u"130.206.138.16" initial_usage2.experiment_id = ExperimentId(u"ud-dummy",u"Dummy experiments") initial_usage2.coord_address = CoordAddress.CoordAddress(u"machine1",u"instance1",u"server1") #.translate_address("server1:instance1@machine1") initial_usage2.reservation_id = reservation_id2.id file1 = FileSent( valid_file_path, u'{sha}12345', time.time()) file2 = FileSent( valid_file_path, u'{sha}123456', time.time(), Command(u'response'), time.time(), file_info = u'program') command1 = CommandSent( Command(u"your command1"), time.time()) command2 = CommandSent( Command(u"your command2"), time.time(), Command(u"your response2"), time.time()) initial_usage2.append_command(command1) initial_usage2.append_command(command2) initial_usage2.append_file(file1) initial_usage2.append_file(file2) self.ups._db_manager._gateway.store_experiment_usage('student1', initial_usage1) self.ups._db_manager._gateway.store_experiment_usage('student2', initial_usage2) return (reservation_id1, reservation_id2), (initial_usage1, initial_usage2)