def _single_async_use(self, logout=True): session_id, reservation_id = self._get_reserved() # send the program again, but asynchronously. Though this should work, it is not really very customary # to send_file more than once in the same session. In fact, it is a feature which might get removed in # the future. When/if that happens, this will need to be modified. CONTENT = "content of the program FPGA" reqid = self.client.send_async_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program') # Wait until send_async_file query is actually finished. #self._get_async_response(session_id, reqid) self._wait_async_done(reservation_id, (reqid, )) # We need to wait for the programming to finish, while at the same # time making sure that the tests don't dead-lock. reqid = self.client.send_async_command(reservation_id, Command.Command("STATE")) respcmd = self._get_async_response(reservation_id, reqid) response = respcmd.get_command_string() # Check that the current state is "Ready" self.assertEquals("STATE", response) reqid = self.client.send_async_command( reservation_id, Command.Command("ChangeSwitch on 0")) self._wait_async_done(reservation_id, (reqid, )) reqid = self.client.send_async_command( reservation_id, Command.Command("ClockActivation on 250")) self._wait_async_done(reservation_id, (reqid, )) if logout: self.client.logout(session_id)
def test_jtag_blazer_with_http(self, _): self.cfg_manager._set_value('xilinx_device_to_program', 'JTagBlazer') self.cfg_manager._set_value('xilinx_device_to_send_commands', 'HttpDevice') self.uxm = UdXilinxExperiment.UdXilinxExperiment( None, None, self.cfg_manager ) # No problem self.uxm.do_send_file_to_device(ExperimentUtil.serialize("whatever " * 400), 'program') self.wait_for_programming_to_end() initial_send = 1 # TODO: The followign assert fails but probably shouldn't. Fix it. #self.assertEquals( # initial_send, # len(self.uxm._command_sender._http_device.msgs) # ) self.uxm.do_send_command_to_device("ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals( 1 + initial_send, len(self.uxm._command_sender._http_device.msgs) ) self.assertEquals( "ClockActivation off, ClockActivation on 1500, SetPulse on 3", self.uxm._command_sender._http_device.msgs[0 + initial_send] )
def test_jtag_blazer_with_http(self, _): self.cfg_manager._set_value("xilinx_device_to_program", "JTagBlazer") self.cfg_manager._set_value("xilinx_device_to_send_commands", "HttpDevice") self.uxm = UdXilinxExperiment.UdXilinxExperiment(None, None, self.cfg_manager) # No problem self.uxm.do_send_file_to_device(ExperimentUtil.serialize("whatever " * 400), "program") self.wait_for_programming_to_end() # Initially 1. initial_send = 1 # self.assertEquals( # initial_send, # len(self.uxm._command_sender._http_device.msgs) # ) self.uxm.do_send_command_to_device("ClockActivation off, ClockActivation on 1500, SetPulse on 3") # Constant was originally 1. self.assertEquals(1 + initial_send, len(self.uxm._command_sender._http_device.msgs)) self.assertEquals( "ClockActivation off, ClockActivation on 1500, SetPulse on 3", self.uxm._command_sender._http_device.msgs[0 + initial_send], )
def _test_happy_path(self, translator_name): FILE_CONTENT = ExperimentUtil.serialize('Huuuuuuuuge file!') FILE_INFO = "My file's description" self._cfg_manager._set_value(ProxyServer.WEBLAB_PROXY_SERVER_DEFAULT_TRANSLATOR_NAME, translator_name) fake_time.TIME_TO_RETURN = 1289548551.2617509 # 2010_11_12___07_55_51 laboratory = self.mocker.mock() laboratory.send_command(self.LAB_SESS_ID, Command('Do this!')) self.mocker.result(Command('Done!')) laboratory.send_file(self.LAB_SESS_ID, Command(FILE_CONTENT), FILE_INFO) self.mocker.result(Command('File received!')) self.mocker.replay() proxy = self._create_proxy(laboratories=laboratory, time_mock=fake_time) proxy.do_enable_access(self.RESERVATION_ID, "ud-fpga@FPGA experiments", "student1", self.LAB_COORD_ADDR, self.LAB_SESS_ID) command_response = proxy.send_command(self.RESERVATION_SESS_ID, Command('Do this!')) self.assertEquals(Command('Done!'), command_response) file_response = proxy.send_file(self.RESERVATION_SESS_ID, Command(FILE_CONTENT), FILE_INFO) self.assertEquals(Command('File received!'), file_response) proxy.do_disable_access(self.RESERVATION_ID) commands, files = proxy.do_retrieve_results(self.RESERVATION_ID) return commands, files
def test_digilent_adept_with_http(self, Popen): popen = Popen.return_value popen.wait.return_value = 0 popen.communicate.return_value = ("", "") self.cfg_manager._set_value("xilinx_device_to_program", "DigilentAdept") self.cfg_manager._set_value("xilinx_device_to_send_commands", "HttpDevice") self.uxm = UdXilinxExperiment.UdXilinxExperiment(None, None, self.cfg_manager) # No problem self.uxm.do_send_file_to_device(ExperimentUtil.serialize("whatever " * 400), "program") self.wait_for_programming_to_end() initial_send = 1 self.assertEquals(initial_send, len(self.uxm._command_sender._http_device.msgs)) self.uxm.do_send_command_to_device("ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals(1 + initial_send, len(self.uxm._command_sender._http_device.msgs)) self.assertEquals( "ClockActivation off, ClockActivation on 1500, SetPulse on 3", self.uxm._command_sender._http_device.msgs[0 + initial_send], )
def upload(): """ Handles file uploading. It will extract the required parameters FILE_SENT, FILE_INFO, SESSION_ID, and the optional parameter IS_ASYNC, and call either send_file or send_async_file depending on this last one. @return HTML defined above, with the success or failure response. """ try: file_info, file_sent, session_id, is_async = _check_arguments() file_content = Util.serialize(file_sent) weblab_api.ctx.reservation_id = session_id if not is_async: result = weblab_api.api.send_file(file_content, file_info) else: result = weblab_api.api.send_async_file(file_content, file_info) except FileUploadError as fue: code, message = fue.args return FAULT_HTML_TEMPLATE % {"THE_FAULT_CODE": code, "THE_FAULT_MESSAGE": message} except Exception as e: message = e.args[0] return FAULT_HTML_TEMPLATE % {"THE_FAULT_CODE": PYTHON_GENERAL_EXCEPTION_CODE, "THE_FAULT_MESSAGE": message} else: if not is_async: resultstr = result.commandstring else: resultstr = result print("[DBG] Returning result from file upload:", resultstr) return SUCCESS_HTML_TEMPLATE % {"RESULT": resultstr}
def test_xilinx_with_serial_port(self): self.cfg_manager._set_value("xilinx_device_to_program", "XilinxImpact") self.cfg_manager._set_value("xilinx_device_to_send_commands", "SerialPort") self.uxm = UdXilinxExperiment.UdXilinxExperiment(None, None, self.cfg_manager) # No problem self.uxm.do_send_file_to_device(ExperimentUtil.serialize("whatever " * 400), "program") self.wait_for_programming_to_end() # These were initially 1. initial_open = 1 initial_send = 1 initial_close = 1 self.assertEquals(initial_open, self.uxm._command_sender._serial_port.dict["open"]) self.assertEquals(initial_close, self.uxm._command_sender._serial_port.dict["close"]) self.assertEquals(initial_send, self.uxm._command_sender._serial_port.dict["send"]) self.assertEquals(initial_send, len(self.uxm._command_sender._serial_port.codes)) self.uxm.do_send_command_to_device("ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals(1 + initial_open, self.uxm._command_sender._serial_port.dict["open"]) self.assertEquals(1 + initial_close, self.uxm._command_sender._serial_port.dict["close"]) self.assertEquals(3 + initial_send, self.uxm._command_sender._serial_port.dict["send"]) self.assertEquals(3 + initial_send, len(self.uxm._command_sender._serial_port.codes)) self.assertEquals(37, self.uxm._command_sender._serial_port.codes[0 + initial_send]) self.assertEquals(35, self.uxm._command_sender._serial_port.codes[1 + initial_send]) self.assertEquals(27, self.uxm._command_sender._serial_port.codes[2 + initial_send])
def test_digilent_adept_with_http(self, Popen): popen = Popen.return_value popen.wait.return_value = 0 popen.stdout.read.return_value = '' popen.stderr.read.return_value = '' self.cfg_manager._set_value('xilinx_device_to_program', 'DigilentAdept') self.cfg_manager._set_value('xilinx_device_to_send_commands', 'HttpDevice') self.uxm = UdXilinxExperiment.UdXilinxExperiment( None, None, self.cfg_manager) # No problem self.uxm.do_send_file_to_device( ExperimentUtil.serialize("whatever " * 400), 'program') self.wait_for_programming_to_end() initial_send = 1 self.assertEquals(initial_send, len(self.uxm._command_sender._http_device.msgs)) self.uxm.do_send_command_to_device( "ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals(1 + initial_send, len(self.uxm._command_sender._http_device.msgs)) self.assertEquals( "ClockActivation off, ClockActivation on 1500, SetPulse on 3", self.uxm._command_sender._http_device.msgs[0 + initial_send])
def test_jtag_blazer_with_http(self, _): self.cfg_manager._set_value('xilinx_device_to_program', 'JTagBlazer') self.cfg_manager._set_value('xilinx_device_to_send_commands', 'HttpDevice') self.uxm = UdXilinxExperiment.UdXilinxExperiment( None, None, self.cfg_manager) # No problem self.uxm.do_send_file_to_device( ExperimentUtil.serialize("whatever " * 400), 'program') self.wait_for_programming_to_end() initial_send = 1 self.assertEquals(initial_send, len(self.uxm._command_sender._http_device.msgs)) self.uxm.do_send_command_to_device( "ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals(1 + initial_send, len(self.uxm._command_sender._http_device.msgs)) self.assertEquals( "ClockActivation off, ClockActivation on 1500, SetPulse on 3", self.uxm._command_sender._http_device.msgs[0 + initial_send])
def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(UdPic18Experiment, self).__init__(*args, **kwargs) self._coord_address = coord_address self._locator = locator self._cfg_manager = cfg_manager self._programmer = UdXilinxProgrammer.create(self._cfg_manager) self._command_sender = UdXilinxCommandSender.create(self._cfg_manager) self.conf_demo = self._cfg_manager.get_value("demo", False) self.webcam_url = self._cfg_manager.get_value("webcam_url", None) self.mjpeg_url = self._cfg_manager.get_value("mjpeg_url", None) self.mjpeg_width = self._cfg_manager.get_value("mjpeg_width", None) self.mjpeg_height = self._cfg_manager.get_value("mjpeg_height", None) self._programming_thread = None self._current_state = STATE_NOT_READY self._programmer_time = self._cfg_manager.get_value( 'programmer_time', "25") # Seconds self._switches_reversed = self._cfg_manager.get_value( 'switches_reversed', False) # Seconds self.demo = False file_path = data_filename(os.path.join(module_directory, 'demo.hex')) self.file_content = ExperimentUtil.serialize( open(file_path, "rb").read())
def _single_async_use(self, logout = True): session_id, reservation_id = self._get_reserved() # send the program again, but asynchronously. Though this should work, it is not really very customary # to send_file more than once in the same session. In fact, it is a feature which might get removed in # the future. When/if that happens, this will need to be modified. CONTENT = "content of the program FPGA" reqid = self.client.send_async_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program') # Wait until send_async_file query is actually finished. #self._get_async_response(session_id, reqid) self._wait_async_done(reservation_id, (reqid,)) # We need to wait for the programming to finish, while at the same # time making sure that the tests don't dead-lock. reqid = self.client.send_async_command(reservation_id, Command.Command("STATE")) respcmd = self._get_async_response(reservation_id, reqid) response = respcmd.get_command_string() # Check that the current state is "Ready" self.assertEquals("STATE", response) reqid = self.client.send_async_command(reservation_id, Command.Command("ChangeSwitch on 0")) self._wait_async_done(reservation_id, (reqid,)) reqid = self.client.send_async_command(reservation_id, Command.Command("ClockActivation on 250")) self._wait_async_done(reservation_id, (reqid,)) if logout: self.client.logout(session_id)
def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(UdDemoXilinxExperiment, self).__init__(coord_address, locator, cfg_manager, *args, **kwargs) file_path = data_filename( os.path.join(module_directory, self.FILES[self._board_type])) self.file_content = ExperimentUtil.serialize( open(file_path, "rb").read())
def test_failures(self): CPP_FILE_CONTENT = """Content of the cpp file""" self.compiler.fail_on_next = True self.assertRaises(ExperimentErrors.SendingFileFailureError, self.experiment.do_send_file_to_device, ExperimentUtil.serialize(CPP_FILE_CONTENT), 'program') self.experiment.do_dispose()
def _autoprogram(self): GAME_FILE_PATH = os.path.dirname(__file__) + os.sep + "JUEGO4B.jed" try: serialized_file_content = ExperimentUtil.serialize(open(GAME_FILE_PATH, "r").read()) super(BinaryExperiment, self).do_send_file_to_device(serialized_file_content, "game") except: import traceback traceback.print_stack() import sys sys.stdout.flush()
def _autoprogram(self, label): content = self.contents[label] try: serialized_file_content = ExperimentUtil.serialize(content) super(BinaryExperiment, self).do_send_file_to_device(serialized_file_content, 'game') except: import traceback traceback.print_exc() import sys sys.stderr.flush()
def test_failures(self): CPP_FILE_CONTENT = """Content of the cpp file""" self.compiler.fail_on_next = True self.assertRaises( ExperimentErrors.SendingFileFailureError, self.experiment.do_send_file_to_device, ExperimentUtil.serialize(CPP_FILE_CONTENT), "program", ) self.experiment.do_dispose()
def upload(): """ Handles file uploading. It will extract the required parameters FILE_SENT, FILE_INFO, SESSION_ID, and the optional parameter IS_ASYNC, and call either send_file or send_async_file depending on this last one. @return HTML defined above, with the success or failure response. """ response_format = request.args.get('format', 'html').lower() try: file_info, file_sent, reservation_id, is_async = _check_arguments() file_content = Util.serialize(file_sent) weblab_api.ctx.reservation_id = reservation_id if not is_async: result = weblab_api.api.send_file(file_content, file_info) else: result = weblab_api.api.send_async_file(file_content, file_info) except FileUploadError as fue: code, message = fue.args if response_format == 'json': return jsonify(is_exception=True, message=message, code=code) else: return FAULT_HTML_TEMPLATE % { 'THE_FAULT_CODE': code, 'THE_FAULT_MESSAGE': message } except Exception as e: message = e.args[0] if response_format == 'json': return jsonify(is_exception=True, message=message, code=PYTHON_GENERAL_EXCEPTION_CODE) else: return FAULT_HTML_TEMPLATE % { 'THE_FAULT_CODE': PYTHON_GENERAL_EXCEPTION_CODE, 'THE_FAULT_MESSAGE': message } else: if not is_async: resultstr = result.commandstring else: resultstr = result print("[DBG] Returning result from file upload:", resultstr) if response_format == 'json': return jsonify(is_exception=False, result={'commandstring': resultstr}) else: return SUCCESS_HTML_TEMPLATE % {'RESULT': resultstr}
def upload(): """ Handles file uploading. It will extract the required parameters FILE_SENT, FILE_INFO, SESSION_ID, and the optional parameter IS_ASYNC, and call either send_file or send_async_file depending on this last one. @return HTML defined above, with the success or failure response. """ response_format = request.args.get('format', 'html').lower() try: file_info, file_sent, reservation_id, is_async = _check_arguments() file_content = Util.serialize(file_sent) weblab_api.ctx.reservation_id = reservation_id if not is_async: result = weblab_api.api.send_file(file_content, file_info) else: result = weblab_api.api.send_async_file(file_content, file_info) except FileUploadError as fue: code, message = fue.args if response_format == 'json': return jsonify(is_exception=True, message = message, code = code) else: return FAULT_HTML_TEMPLATE % { 'THE_FAULT_CODE' : code, 'THE_FAULT_MESSAGE' : message } except Exception as e: message = e.args[0] if response_format == 'json': return jsonify(is_exception=True, message = message, code = PYTHON_GENERAL_EXCEPTION_CODE) else: return FAULT_HTML_TEMPLATE % { 'THE_FAULT_CODE' : PYTHON_GENERAL_EXCEPTION_CODE, 'THE_FAULT_MESSAGE' : message } else: if not is_async: resultstr = result.commandstring else: resultstr = result print("[DBG] Returning result from file upload:", resultstr) if response_format == 'json': return jsonify(is_exception=False, result={ 'commandstring' : resultstr }) else: return SUCCESS_HTML_TEMPLATE % { 'RESULT' : resultstr }
def _autoprogram(self): GAME_FILE_PATH = os.path.dirname(__file__) + os.sep + "JUEGO4B.jed" try: serialized_file_content = ExperimentUtil.serialize( open(GAME_FILE_PATH, "r").read()) super(BinaryExperiment, self).do_send_file_to_device(serialized_file_content, "game") except: import traceback traceback.print_stack() import sys sys.stdout.flush()
def __init__(self, url_maps, protocol, username, password, experiment_name, category_name, program, initial_delay): threading.Thread.__init__(self) self.setName(counter.next_name("BotUser")) self.url, self.url_login = url_maps[protocol] self.protocol = protocol self.username = username self.password = password self.experiment_name = experiment_name self.experiment_category_name = category_name self.program = _Util.serialize(program) self.bot = Client.create_bot(protocol, url=self.url, url_login=self.url_login) self.initial_delay = initial_delay
def _single_sync_use(self, logout = True): session_id, reservation_id = self._get_reserved() CONTENT = "content of the program FPGA" response = self.client.send_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program') self.assertEquals(response.commandstring, 'ack') response = self.client.send_command(reservation_id, Command.Command("STATE")) self.assertEquals(response.commandstring, 'STATE') response = self.client.send_command(reservation_id, Command.Command("ChangeSwitch on 0")) self.assertEquals(response.commandstring, "ChangeSwitch on 0") if logout: self.client.logout(session_id)
def _test_store_file(self, file_content): FILE_PATH = self._cfg_manager.get_value( 'proxy_store_students_programs_path') FILE_CONTENT = ExperimentUtil.serialize(file_content) fake_time.TIME_TO_RETURN = 1289548551.2617509 # 2010_11_12___07_55_51 proxy = self._create_proxy_session_handler(time_mock=fake_time) path, hash = proxy._store_file(Command(FILE_CONTENT), "student1", "my_session_id") self.assertEquals('2010_11_12___07_55_51_261_student1_my_session_id', path) self.assertTrue(os.path.exists('%s%s%s' % (FILE_PATH, os.sep, path))) self.assertEquals( '{sha}14b69e2393ace3feb980510e59dc8a1fc467575a', # sha of 'AAAHuuuuuuuuge file!' hash)
def test_xilinx_with_serial_port(self): self.cfg_manager._set_value('xilinx_device_to_program', 'XilinxImpact') self.cfg_manager._set_value('xilinx_device_to_send_commands', 'SerialPort') self.uxm = UdXilinxExperiment.UdXilinxExperiment( None, None, self.cfg_manager) # No problem self.uxm.do_send_file_to_device( ExperimentUtil.serialize("whatever " * 400), 'program') self.wait_for_programming_to_end() # These were initially 1. initial_open = 1 initial_send = 1 initial_close = 1 self.assertEquals(initial_open, self.uxm._command_sender._serial_port.dict['open']) self.assertEquals(initial_close, self.uxm._command_sender._serial_port.dict['close']) self.assertEquals(initial_send, self.uxm._command_sender._serial_port.dict['send']) self.assertEquals(initial_send, len(self.uxm._command_sender._serial_port.codes)) self.uxm.do_send_command_to_device( "ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals(1 + initial_open, self.uxm._command_sender._serial_port.dict['open']) self.assertEquals(1 + initial_close, self.uxm._command_sender._serial_port.dict['close']) self.assertEquals(3 + initial_send, self.uxm._command_sender._serial_port.dict['send']) self.assertEquals(3 + initial_send, len(self.uxm._command_sender._serial_port.codes)) self.assertEquals( 37, self.uxm._command_sender._serial_port.codes[0 + initial_send]) self.assertEquals( 35, self.uxm._command_sender._serial_port.codes[1 + initial_send]) self.assertEquals( 27, self.uxm._command_sender._serial_port.codes[2 + initial_send])
def test_send_file(self): CPP_FILE_CONTENT = """Content of the cpp file""" EXE_FILE_CONTENT = """Content of the exe file""" self.compiler.next_content = EXE_FILE_CONTENT self.experiment.do_send_file_to_device( ExperimentUtil.serialize(CPP_FILE_CONTENT), 'program' ) self.experiment.do_dispose() # Check the compiler self.assertEquals( 1, len(self.compiler.file_paths) ) self.assertFalse( os.path.exists( self.compiler.file_paths[0] )) self.assertEquals( 1, len(self.compiler.files_content) ) self.assertEquals( CPP_FILE_CONTENT, self.compiler.files_content[0] ) # Check the launcher self.assertEquals( 1, len(self.launcher.file_paths) ) # self.assertFalse( os.path.exists( # self.launcher.file_paths[0] # )) self.assertEquals( 1, len(self.launcher.files_content) ) self.assertEquals( EXE_FILE_CONTENT, self.launcher.files_content[0] )
def _test_store_file(self, file_content): FILE_PATH = self._cfg_manager.get_value('proxy_store_students_programs_path') FILE_CONTENT = ExperimentUtil.serialize(file_content) fake_time.TIME_TO_RETURN = 1289548551.2617509 # 2010_11_12___07_55_51 proxy = self._create_proxy_session_handler(time_mock=fake_time) path, hash = proxy._store_file(Command(FILE_CONTENT), "student1", "my_session_id") self.assertEquals( '2010_11_12___07_55_51_261_student1_my_session_id', path ) self.assertTrue( os.path.exists('%s%s%s' % (FILE_PATH, os.sep, path)) ) self.assertEquals( '{sha}14b69e2393ace3feb980510e59dc8a1fc467575a', # sha of 'AAAHuuuuuuuuge file!' hash )
def _single_sync_use(self, logout=True): session_id, reservation_id = self._get_reserved() CONTENT = "content of the program FPGA" response = self.client.send_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program') self.assertEquals(response.commandstring, 'ack') response = self.client.send_command(reservation_id, Command.Command("STATE")) self.assertEquals(response.commandstring, 'STATE') response = self.client.send_command( reservation_id, Command.Command("ChangeSwitch on 0")) self.assertEquals(response.commandstring, "ChangeSwitch on 0") if logout: self.client.logout(session_id)
def run(self): """ run() Handles file uploading. It will extract the required parameters FILE_SENT, FILE_INFO, SESSION_ID, and the optional parameter IS_ASYNC, and call either send_file or send_async_file depending on this last one. @return HTML defined above, with the success or failure response. """ try: file_info, file_sent, session_id, is_async = self._check_arguments() file_content = Util.serialize(file_sent) sid = SessionId(session_id) if(not is_async): result = self.server.send_file(sid, file_content, file_info) else: result = self.server.send_async_file(sid, file_content, file_info) except FileUploadError as fue: code, message = fue.args return FAULT_HTML_TEMPLATE % { 'THE_FAULT_CODE' : code, 'THE_FAULT_MESSAGE' : message } except Exception as e: message = e.args[0] return FAULT_HTML_TEMPLATE % { 'THE_FAULT_CODE' : PYTHON_GENERAL_EXCEPTION_CODE, 'THE_FAULT_MESSAGE' : message } else: if not is_async: resultstr = result.commandstring else: resultstr = result print "[DBG] Returning result from file upload: " + resultstr return SUCCESS_HTML_TEMPLATE % { 'RESULT' : resultstr }
def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(UdPic18Experiment,self).__init__(*args, **kwargs) self._coord_address = coord_address self._locator = locator self._cfg_manager = cfg_manager self._programmer = UdXilinxProgrammer.create(self._cfg_manager) self._command_sender = UdXilinxCommandSender.create(self._cfg_manager) self.conf_demo = self._cfg_manager.get_value("demo", False) self.webcam_url = self._cfg_manager.get_value("webcam_url", None) self.mjpeg_url = self._cfg_manager.get_value("mjpeg_url", None) self.mjpeg_width = self._cfg_manager.get_value("mjpeg_width", None) self.mjpeg_height = self._cfg_manager.get_value("mjpeg_height", None) self._programming_thread = None self._current_state = STATE_NOT_READY self._programmer_time = self._cfg_manager.get_value('programmer_time', "25") # Seconds self._switches_reversed = self._cfg_manager.get_value('switches_reversed', False) # Seconds self.demo = False file_path = data_filename(os.path.join(module_directory, 'demo.hex')) self.file_content = ExperimentUtil.serialize(open(file_path, "rb").read())
def _test_happy_path(self, translator_name): FILE_CONTENT = ExperimentUtil.serialize('Huuuuuuuuge file!') FILE_INFO = "My file's description" self._cfg_manager._set_value( ProxyServer.WEBLAB_PROXY_SERVER_DEFAULT_TRANSLATOR_NAME, translator_name) fake_time.TIME_TO_RETURN = 1289548551.2617509 # 2010_11_12___07_55_51 laboratory = self.mocker.mock() laboratory.send_command(self.LAB_SESS_ID, Command('Do this!')) self.mocker.result(Command('Done!')) laboratory.send_file(self.LAB_SESS_ID, Command(FILE_CONTENT), FILE_INFO) self.mocker.result(Command('File received!')) self.mocker.replay() proxy = self._create_proxy(laboratories=(laboratory, ), time_mock=fake_time) proxy.do_enable_access(self.RESERVATION_ID, "ud-fpga@FPGA experiments", "student1", self.LAB_COORD_ADDR, self.LAB_SESS_ID) command_response = proxy.send_command(self.RESERVATION_SESS_ID, Command('Do this!')) self.assertEquals(Command('Done!'), command_response) file_response = proxy.send_file(self.RESERVATION_SESS_ID, Command(FILE_CONTENT), FILE_INFO) self.assertEquals(Command('File received!'), file_response) proxy.do_disable_access(self.RESERVATION_ID) commands, files = proxy.do_retrieve_results(self.RESERVATION_ID) return commands, files
def test_single_uses_timeout(self): self.fake_impact1.clear() self.fake_impact2.clear() self.fake_serial_port1.clear() self.fake_serial_port2.clear() # 6 users get into the system session_id1 = self.real_login.login('student1','password') session_id2 = self.real_login.login('student2','password') session_id3 = self.real_login.login('student3','password') session_id4 = self.real_login.login('student4','password') session_id5 = self.real_login.login('student5','password') session_id6 = self.real_login.login('student6','password') # they all have access to the ud-fpga experiment experiments1 = self.real_ups.list_experiments(session_id1) fpga_experiments1 = [ exp.experiment for exp in experiments1 if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments1), 1 ) experiments2 = self.real_ups.list_experiments(session_id2) fpga_experiments2 = [ exp.experiment for exp in experiments2 if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments2), 1 ) experiments3 = self.real_ups.list_experiments(session_id3) fpga_experiments3 = [ exp.experiment for exp in experiments3 if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments3), 1 ) experiments4 = self.real_ups.list_experiments(session_id4) fpga_experiments4 = [ exp.experiment for exp in experiments4 if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments4), 1 ) experiments5 = self.real_ups.list_experiments(session_id5) fpga_experiments5 = [ exp.experiment for exp in experiments5 if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments5), 1 ) experiments6 = self.real_ups.list_experiments(session_id6) fpga_experiments6 = [ exp.experiment for exp in experiments6 if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments6), 1 ) # 3 users try to reserve the experiment status1 = self.real_ups.reserve_experiment( session_id1, fpga_experiments1[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) reservation_id1 = status1.reservation_id status2 = self.real_ups.reserve_experiment( session_id2, fpga_experiments2[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) reservation_id2 = status2.reservation_id status3 = self.real_ups.reserve_experiment( session_id3, fpga_experiments3[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) reservation_id3 = status3.reservation_id # wait until it is reserved short_time = 0.1 times = 10.0 / short_time while times > 0: time.sleep(short_time) new_status = self.real_ups.get_reservation_status(reservation_id1) if not isinstance(new_status, Reservation.WaitingConfirmationReservation): break times -= 1 # first user got the device. The other two are in WaitingReservation reservation1 = self.real_ups.get_reservation_status( reservation_id1 ) self.assertTrue( isinstance( reservation1, Reservation.ConfirmedReservation ) ) reservation2 = self.real_ups.get_reservation_status( reservation_id2 ) self.assertTrue( isinstance( reservation2, Reservation.WaitingReservation ) ) self.assertEquals( 0, reservation2.position) reservation3 = self.real_ups.get_reservation_status( reservation_id3 ) self.assertTrue( isinstance( reservation3, Reservation.WaitingReservation ) ) self.assertEquals( 1, reservation3.position) # Another user tries to reserve the experiment. He goes to the WaitingReservation, position 2 status4 = self.real_ups.reserve_experiment( session_id4, fpga_experiments4[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) reservation_id4 = status4.reservation_id reservation4 = self.real_ups.get_reservation_status( reservation_id4 ) self.assertTrue( isinstance( reservation4, Reservation.WaitingReservation ) ) self.assertEquals( 2, reservation4.position) # The state of other users does not change reservation1 = self.real_ups.get_reservation_status( reservation_id1 ) self.assertTrue( isinstance( reservation1, Reservation.ConfirmedReservation ) ) reservation2 = self.real_ups.get_reservation_status( reservation_id2 ) self.assertTrue( isinstance( reservation2, Reservation.WaitingReservation ) ) self.assertEquals( 0, reservation2.position) reservation3 = self.real_ups.get_reservation_status( reservation_id3 ) self.assertTrue( isinstance( reservation3, Reservation.WaitingReservation ) ) self.assertEquals( 1, reservation3.position ) # The user number 2 frees the experiment self.real_ups.finished_experiment(reservation_id2) # Whenever he tries to do poll or send_command, he receives an exception try: self.real_ups.poll(reservation_id2) self.real_ups.poll(reservation_id2) self.real_ups.poll(reservation_id2) except core_exc.NoCurrentReservationError: pass # All right :-) # send a program CONTENT = "content of the program FPGA" self.real_ups.send_file(reservation_id1, ExperimentUtil.serialize(CONTENT), 'program') # We need to wait for the programming to finish. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming") and time.time() - start_time < XILINX_TIMEOUT: respcmd = self.real_ups.send_command(reservation_id1, Command.Command("STATE")) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) self.real_ups.send_command(reservation_id1, Command.Command("ChangeSwitch on 0")) self.real_ups.send_command(reservation_id1, Command.Command("ClockActivation on 250")) # end session self.real_ups.logout(session_id1) # Checking the commands sent self.assertEquals( 1, len(self.fake_impact1._paths) ) self.assertEquals( 0, len(self.fake_impact2._paths) ) self.assertEquals( CONTENT, self.fake_impact1._paths[0] ) initial_open = 1 initial_send = 1 initial_close = 1 initial_total = initial_open + initial_send + initial_close # ChangeSwitch on 0 self.assertEquals( (0 + initial_total,1), self.fake_serial_port1.dict['open'][0 + initial_open] ) self.assertEquals( (1 + initial_total,1), self.fake_serial_port1.dict['send'][0 + initial_send] ) self.assertEquals( (2 + initial_total,None), self.fake_serial_port1.dict['close'][0 + initial_close] ) # ClockActivation on 250 self.assertEquals( (3 + initial_total,1), self.fake_serial_port1.dict['open'][1 + initial_open] ) self.assertEquals( (4 + initial_total,32), self.fake_serial_port1.dict['send'][1 + initial_send] ) self.assertEquals( (5 + initial_total,None), self.fake_serial_port1.dict['close'][1 + initial_close] )
def __init__(self, coord_address, locator, cfg_manager, *args, **kwargs): super(UdDemoXilinxExperiment,self).__init__(coord_address, locator, cfg_manager, *args, **kwargs) file_path = data_filename(os.path.join(module_directory, self.FILES[self._xilinx_device])) self.file_content = ExperimentUtil.serialize(open(file_path, "rb").read())
def test_xilinx_with_serial_port(self): self.cfg_manager._set_value('xilinx_device_to_program', 'XilinxImpact') self.cfg_manager._set_value('xilinx_device_to_send_commands', 'SerialPort') self.uxm = UdXilinxExperiment.UdXilinxExperiment( None, None, self.cfg_manager ) # No problem self.uxm.do_send_file_to_device(ExperimentUtil.serialize("whatever " * 400), 'program') self.wait_for_programming_to_end() initial_open = 1 initial_send = 1 initial_close = 1 # TODO: The following assert fails but probably shouldn't. Fix it. self.assertEquals( initial_open, self.uxm._command_sender._serial_port.dict['open'] ) self.assertEquals( initial_close, self.uxm._command_sender._serial_port.dict['close'] ) self.assertEquals( initial_send, self.uxm._command_sender._serial_port.dict['send'] ) self.assertEquals( initial_send, len(self.uxm._command_sender._serial_port.codes) ) self.uxm.do_send_command_to_device("ClockActivation off, ClockActivation on 1500, SetPulse on 3") self.assertEquals( 1 + initial_open, self.uxm._command_sender._serial_port.dict['open'] ) self.assertEquals( 1 + initial_close, self.uxm._command_sender._serial_port.dict['close'] ) self.assertEquals( 3 + initial_send, self.uxm._command_sender._serial_port.dict['send'] ) self.assertEquals( 3 + initial_send, len(self.uxm._command_sender._serial_port.codes) ) self.assertEquals( 37, self.uxm._command_sender._serial_port.codes[0 + initial_send] ) self.assertEquals( 35, self.uxm._command_sender._serial_port.codes[1 + initial_send] ) self.assertEquals( 27, self.uxm._command_sender._serial_port.codes[2 + initial_send] )
def test_two_multiple_uses_of_different_devices(self): user1_session_id = self.client.login('intstudent1','password') user1_experiments = self.client.list_experiments(user1_session_id) self.assertEquals( 2, len(user1_experiments)) dummy1_experiments = [ exp.experiment for exp in user1_experiments if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy1_experiments), 1) # reserve it status = self.client.reserve_experiment( user1_session_id, dummy1_experiments[0].to_experiment_id(), "{}", "{}") user1_reservation_id = status.reservation_id user2_session_id = self.client.login('intstudent2','password') user2_experiments = self.client.list_experiments(user2_session_id) self.assertEquals( 2, len(user2_experiments)) dummy2_experiments = [ exp.experiment for exp in user2_experiments if exp.experiment.name == 'dummy2' ] self.assertEquals( len(dummy2_experiments), 1) # reserve it status = self.client.reserve_experiment(user2_session_id, dummy2_experiments[0].to_experiment_id(), "{}", "{}") user2_reservation_id = status.reservation_id short_time = 0.1 times = 9.0 / short_time while times > 0: time.sleep(short_time) new_status1 = self.client.get_reservation_status(user1_reservation_id) new_status2 = self.client.get_reservation_status(user2_reservation_id) if not isinstance(new_status1, Reservation.WaitingConfirmationReservation): if not isinstance(new_status2, Reservation.WaitingConfirmationReservation): break times -= 1 self.assertTrue(isinstance(self.client.get_reservation_status(user1_reservation_id), Reservation.ConfirmedReservation)) self.assertTrue(isinstance(self.client.get_reservation_status(user2_reservation_id), Reservation.ConfirmedReservation)) # send a program CONTENT1 = "content of the program DUMMY1" response = self.client.send_file(user1_reservation_id, ExperimentUtil.serialize(CONTENT1), 'program') self.assertEquals('ack', response.commandstring) # We need to wait for the programming to finish. respcmd = self.client.send_command(user1_reservation_id, Command.Command("STATE DUMMY1")) response = respcmd.get_command_string() # Check that the current state is "Ready" self.assertEquals("STATE DUMMY1", response) CONTENT2 = "content of the program PLD" response = self.client.send_file(user2_reservation_id, ExperimentUtil.serialize(CONTENT2), 'program') self.assertEquals('ack', response.commandstring) # We need to wait for the programming to finish. respcmd = self.client.send_command(user2_reservation_id, Command.Command("STATE DUMMY2")) # Check that the current state is "Ready" self.assertEquals("STATE DUMMY2", respcmd.commandstring) # end session self.client.logout(user1_session_id) self.client.logout(user2_session_id)
def test_single_uses_timeout(self): # 6 users get into the system session_id1 = self.client.login('intstudent1', 'password') session_id2 = self.client.login('intstudent2', 'password') session_id3 = self.client.login('intstudent3', 'password') session_id4 = self.client.login('intstudent4', 'password') session_id5 = self.client.login('intstudent5', 'password') session_id6 = self.client.login('intstudent6', 'password') # they all have access to the ud-fpga experiment experiments1 = self.client.list_experiments(session_id1) dummy_experiments1 = [ exp.experiment for exp in experiments1 if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy_experiments1), 1) experiments2 = self.client.list_experiments(session_id2) dummy_experiments2 = [ exp.experiment for exp in experiments2 if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy_experiments2), 1) experiments3 = self.client.list_experiments(session_id3) dummy_experiments3 = [ exp.experiment for exp in experiments3 if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy_experiments3), 1) experiments4 = self.client.list_experiments(session_id4) dummy_experiments4 = [ exp.experiment for exp in experiments4 if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy_experiments4), 1) experiments5 = self.client.list_experiments(session_id5) dummy_experiments5 = [ exp.experiment for exp in experiments5 if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy_experiments5), 1) experiments6 = self.client.list_experiments(session_id6) dummy_experiments6 = [ exp.experiment for exp in experiments6 if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy_experiments6), 1) # 3 users try to reserve the experiment status1 = self.client.reserve_experiment( session_id1, dummy_experiments1[0].to_experiment_id(), "{}", "{}") reservation_id1 = status1.reservation_id status2 = self.client.reserve_experiment( session_id2, dummy_experiments2[0].to_experiment_id(), "{}", "{}") reservation_id2 = status2.reservation_id status3 = self.client.reserve_experiment( session_id3, dummy_experiments3[0].to_experiment_id(), "{}", "{}") reservation_id3 = status3.reservation_id # wait until it is reserved short_time = 0.1 times = 10.0 / short_time while times > 0: time.sleep(short_time) new_status = self.client.get_reservation_status(reservation_id1) if not isinstance(new_status, Reservation.WaitingConfirmationReservation): break times -= 1 # first user got the device. The other two are in WaitingReservation reservation1 = self.client.get_reservation_status(reservation_id1) self.assertTrue( isinstance(reservation1, Reservation.ConfirmedReservation)) reservation2 = self.client.get_reservation_status(reservation_id2) self.assertTrue( isinstance(reservation2, Reservation.WaitingReservation)) self.assertEquals(0, reservation2.position) reservation3 = self.client.get_reservation_status(reservation_id3) self.assertTrue( isinstance(reservation3, Reservation.WaitingReservation)) self.assertEquals(1, reservation3.position) # Another user tries to reserve the experiment. He goes to the WaitingReservation, position 2 status4 = self.client.reserve_experiment( session_id4, dummy_experiments4[0].to_experiment_id(), "{}", "{}") reservation_id4 = status4.reservation_id reservation4 = self.client.get_reservation_status(reservation_id4) self.assertTrue( isinstance(reservation4, Reservation.WaitingReservation)) self.assertEquals(2, reservation4.position) # The state of other users does not change reservation1 = self.client.get_reservation_status(reservation_id1) self.assertTrue( isinstance(reservation1, Reservation.ConfirmedReservation)) reservation2 = self.client.get_reservation_status(reservation_id2) self.assertTrue( isinstance(reservation2, Reservation.WaitingReservation)) self.assertEquals(0, reservation2.position) reservation3 = self.client.get_reservation_status(reservation_id3) self.assertTrue( isinstance(reservation3, Reservation.WaitingReservation)) self.assertEquals(1, reservation3.position) # The user number 2 frees the experiment self.client.finished_experiment(reservation_id2) # Whenever he tries to do poll or send_command, he receives an exception try: time.sleep(1) self.client.poll(reservation_id2) self.client.poll(reservation_id2) self.client.poll(reservation_id2) except Exception as e: pass # All right :-) self.assertTrue("does not have any experiment" in repr(e)) else: self.fail("Expected exception when polling") # send a program CONTENT = "content of the program DUMMY1" self.client.send_file(reservation_id1, ExperimentUtil.serialize(CONTENT), 'program') # We need to wait for the programming to finish. response = self.client.send_command(reservation_id1, Command.Command("STATE")) self.assertEquals("STATE", response.commandstring) self.client.logout(session_id1)
def _single_sync_use(self, logout = True): self.fake_impact1.clear() self.fake_impact2.clear() self.fake_serial_port1.clear() self.fake_serial_port2.clear() session_id = self.real_login.login('student1','password') user_information = self.real_ups.get_user_information(session_id) self.assertEquals( 'student1', user_information.login ) self.assertEquals( 'Name of student 1', user_information.full_name ) self.assertEquals( '*****@*****.**', user_information.email ) experiments = self.real_ups.list_experiments(session_id) self.assertEquals( 5, len(experiments)) fpga_experiments = [ exp.experiment for exp in experiments if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments), 1 ) # reserve it status = self.real_ups.reserve_experiment( session_id, fpga_experiments[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) reservation_id = status.reservation_id # wait until it is reserved short_time = 0.1 times = 13.0 / short_time while times > 0: new_status = self.real_ups.get_reservation_status(reservation_id) if not isinstance(new_status, Reservation.WaitingConfirmationReservation) and not isinstance(new_status, Reservation.WaitingReservation): break times -= 1 time.sleep(short_time) reservation = self.real_ups.get_reservation_status( reservation_id ) self.assertTrue( isinstance( reservation, Reservation.ConfirmedReservation ), "Reservation %s is not Confirmed, as expected by this time" % reservation ) # send a program synchronously (the "traditional" way) CONTENT = "content of the program FPGA" self.real_ups.send_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program') # We need to wait for the programming to finish, while at the same # time making sure that the tests don't dead-lock. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming") and time.time() - start_time < XILINX_TIMEOUT: respcmd = self.real_ups.send_command(reservation_id, Command.Command("STATE")) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) # We need to wait for the programming to finish, while at the same # time making sure that the tests don't dead-lock. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming") and time.time() - start_time < XILINX_TIMEOUT: respcmd = self.real_ups.send_command(reservation_id, Command.Command("STATE")) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) self.real_ups.send_command(reservation_id, Command.Command("ChangeSwitch on 0")) self.real_ups.send_command(reservation_id, Command.Command("ClockActivation on 250")) # Checking the commands sent # Note that the number of paths is 2 now that we send a file twice (sync and async). self.assertEquals( 1, len(self.fake_impact1._paths) ) self.assertEquals( 0, len(self.fake_impact2._paths) ) self.assertEquals( CONTENT, self.fake_impact1._paths[0] ) initial_open = 1 initial_send = 1 initial_close = 1 initial_total = initial_open + initial_send + initial_close # ChangeSwitch on 0 self.assertEquals( (0 + initial_total,1), self.fake_serial_port1.dict['open'][0 + initial_open] ) self.assertEquals( (1 + initial_total,1), self.fake_serial_port1.dict['send'][0 + initial_send] ) self.assertEquals( (2 + initial_total,None), self.fake_serial_port1.dict['close'][0 + initial_close] ) # ClockActivation on 250 self.assertEquals( (3 + initial_total,1), self.fake_serial_port1.dict['open'][1 + initial_open] ) self.assertEquals( (4 + initial_total,32), self.fake_serial_port1.dict['send'][1 + initial_send] ) self.assertEquals( (5 + initial_total,None), self.fake_serial_port1.dict['close'][1 + initial_close] ) # end session # Note: Before async commands were implemented, this was actually done before # checking the commands sent. If it was that way for a reason, it might be # necessary to change it in the future. if logout: self.real_ups.logout(session_id)
def do_before_send_file(self, session, file): session['log'] += "before_send_file " file_content = ExperimentUtil.deserialize(file.commandstring) return Command(ExperimentUtil.serialize("AAA%s" % file_content))
def test_two_multiple_uses_of_different_devices(self): user1_session_id = self.client.login('intstudent1', 'password') user1_experiments = self.client.list_experiments(user1_session_id) self.assertEquals(2, len(user1_experiments)) dummy1_experiments = [ exp.experiment for exp in user1_experiments if exp.experiment.name == 'dummy1' ] self.assertEquals(len(dummy1_experiments), 1) # reserve it status = self.client.reserve_experiment( user1_session_id, dummy1_experiments[0].to_experiment_id(), "{}", "{}") user1_reservation_id = status.reservation_id user2_session_id = self.client.login('intstudent2', 'password') user2_experiments = self.client.list_experiments(user2_session_id) self.assertEquals(2, len(user2_experiments)) dummy2_experiments = [ exp.experiment for exp in user2_experiments if exp.experiment.name == 'dummy2' ] self.assertEquals(len(dummy2_experiments), 1) # reserve it status = self.client.reserve_experiment( user2_session_id, dummy2_experiments[0].to_experiment_id(), "{}", "{}") user2_reservation_id = status.reservation_id short_time = 0.1 times = 9.0 / short_time while times > 0: time.sleep(short_time) new_status1 = self.client.get_reservation_status( user1_reservation_id) new_status2 = self.client.get_reservation_status( user2_reservation_id) if not isinstance(new_status1, Reservation.WaitingConfirmationReservation): if not isinstance(new_status2, Reservation.WaitingConfirmationReservation): break times -= 1 self.assertTrue( isinstance( self.client.get_reservation_status(user1_reservation_id), Reservation.ConfirmedReservation)) self.assertTrue( isinstance( self.client.get_reservation_status(user2_reservation_id), Reservation.ConfirmedReservation)) # send a program CONTENT1 = "content of the program DUMMY1" response = self.client.send_file(user1_reservation_id, ExperimentUtil.serialize(CONTENT1), 'program') self.assertEquals('ack', response.commandstring) # We need to wait for the programming to finish. respcmd = self.client.send_command(user1_reservation_id, Command.Command("STATE DUMMY1")) response = respcmd.get_command_string() # Check that the current state is "Ready" self.assertEquals("STATE DUMMY1", response) CONTENT2 = "content of the program PLD" response = self.client.send_file(user2_reservation_id, ExperimentUtil.serialize(CONTENT2), 'program') self.assertEquals('ack', response.commandstring) # We need to wait for the programming to finish. respcmd = self.client.send_command(user2_reservation_id, Command.Command("STATE DUMMY2")) # Check that the current state is "Ready" self.assertEquals("STATE DUMMY2", respcmd.commandstring) # end session self.client.logout(user1_session_id) self.client.logout(user2_session_id)
def _single_async_use(self, logout = True): self.fake_impact1.clear() self.fake_impact2.clear() self.fake_serial_port1.clear() self.fake_serial_port2.clear() session_id = self.real_login.login('student1','password') user_information = self.real_ups.get_user_information(session_id) self.assertEquals( 'student1', user_information.login ) self.assertEquals( 'Name of student 1', user_information.full_name ) self.assertEquals( '*****@*****.**', user_information.email ) experiments = self.real_ups.list_experiments(session_id) self.assertEquals( 5, len(experiments)) fpga_experiments = [ exp.experiment for exp in experiments if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments), 1 ) # reserve it status = self.real_ups.reserve_experiment( session_id, fpga_experiments[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) reservation_id = status.reservation_id # wait until it is reserved short_time = 0.1 # Time extended from 9.0 to 15.0 because at times the test failed, possibly for that reason. times = 15.0 / short_time while times > 0: time.sleep(short_time) new_status = self.real_ups.get_reservation_status(reservation_id) if not isinstance(new_status, Reservation.WaitingConfirmationReservation) and not isinstance(new_status, Reservation.WaitingReservation): break times -= 1 reservation = self.real_ups.get_reservation_status( reservation_id ) self.assertTrue( isinstance( reservation, Reservation.ConfirmedReservation ), "Reservation %s is not Confirmed, as expected by this time" % reservation ) # send the program again, but asynchronously. Though this should work, it is not really very customary # to send_file more than once in the same session. In fact, it is a feature which might get removed in # the future. When/if that happens, this will need to be modified. CONTENT = "content of the program FPGA" reqid = self.real_ups.send_async_file(reservation_id, ExperimentUtil.serialize(CONTENT), 'program') # Wait until send_async_file query is actually finished. #self._get_async_response(session_id, reqid) self._wait_async_done(reservation_id, (reqid,)) # We need to wait for the programming to finish, while at the same # time making sure that the tests don't dead-lock. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming") and time.time() - start_time < XILINX_TIMEOUT: reqid = self.real_ups.send_async_command(reservation_id, Command.Command("STATE")) respcmd = self._get_async_response(reservation_id, reqid) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) reqid = self.real_ups.send_async_command(reservation_id, Command.Command("ChangeSwitch on 0")) self._wait_async_done(reservation_id, (reqid,)) reqid = self.real_ups.send_async_command(reservation_id, Command.Command("ClockActivation on 250")) self._wait_async_done(reservation_id, (reqid,)) # Checking the commands sent # Note that the number of paths is 2 now that we send a file twice (sync and async). self.assertEquals( 1, len(self.fake_impact1._paths) ) self.assertEquals( 0, len(self.fake_impact2._paths) ) self.assertEquals( CONTENT, self.fake_impact1._paths[0] ) initial_open = 1 initial_send = 1 initial_close = 1 initial_total = initial_open + initial_send + initial_close # ChangeSwitch on 0 self.assertEquals( (0 + initial_total,1), self.fake_serial_port1.dict['open'][0 + initial_open] ) self.assertEquals( (1 + initial_total,1), self.fake_serial_port1.dict['send'][0 + initial_send] ) self.assertEquals( (2 + initial_total,None), self.fake_serial_port1.dict['close'][0 + initial_close] ) # ClockActivation on 250 self.assertEquals( (3 + initial_total,1), self.fake_serial_port1.dict['open'][1 + initial_open] ) self.assertEquals( (4 + initial_total,32), self.fake_serial_port1.dict['send'][1 + initial_send] ) self.assertEquals( (5 + initial_total,None), self.fake_serial_port1.dict['close'][1 + initial_close] ) if logout: self.real_ups.logout(session_id)
def test_single_uses_timeout(self): # 6 users get into the system session_id1 = self.client.login('intstudent1','password') session_id2 = self.client.login('intstudent2','password') session_id3 = self.client.login('intstudent3','password') session_id4 = self.client.login('intstudent4','password') session_id5 = self.client.login('intstudent5','password') session_id6 = self.client.login('intstudent6','password') # they all have access to the ud-fpga experiment experiments1 = self.client.list_experiments(session_id1) dummy_experiments1 = [ exp.experiment for exp in experiments1 if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy_experiments1), 1 ) experiments2 = self.client.list_experiments(session_id2) dummy_experiments2 = [ exp.experiment for exp in experiments2 if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy_experiments2), 1 ) experiments3 = self.client.list_experiments(session_id3) dummy_experiments3 = [ exp.experiment for exp in experiments3 if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy_experiments3), 1 ) experiments4 = self.client.list_experiments(session_id4) dummy_experiments4 = [ exp.experiment for exp in experiments4 if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy_experiments4), 1 ) experiments5 = self.client.list_experiments(session_id5) dummy_experiments5 = [ exp.experiment for exp in experiments5 if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy_experiments5), 1 ) experiments6 = self.client.list_experiments(session_id6) dummy_experiments6 = [ exp.experiment for exp in experiments6 if exp.experiment.name == 'dummy1' ] self.assertEquals( len(dummy_experiments6), 1 ) # 3 users try to reserve the experiment status1 = self.client.reserve_experiment(session_id1, dummy_experiments1[0].to_experiment_id(), "{}", "{}") reservation_id1 = status1.reservation_id status2 = self.client.reserve_experiment(session_id2, dummy_experiments2[0].to_experiment_id(), "{}", "{}") reservation_id2 = status2.reservation_id status3 = self.client.reserve_experiment(session_id3, dummy_experiments3[0].to_experiment_id(), "{}", "{}") reservation_id3 = status3.reservation_id # wait until it is reserved short_time = 0.1 times = 10.0 / short_time while times > 0: time.sleep(short_time) new_status = self.client.get_reservation_status(reservation_id1) if not isinstance(new_status, Reservation.WaitingConfirmationReservation): break times -= 1 # first user got the device. The other two are in WaitingReservation reservation1 = self.client.get_reservation_status(reservation_id1) self.assertTrue(isinstance(reservation1, Reservation.ConfirmedReservation)) reservation2 = self.client.get_reservation_status(reservation_id2) self.assertTrue(isinstance(reservation2, Reservation.WaitingReservation)) self.assertEquals( 0, reservation2.position) reservation3 = self.client.get_reservation_status(reservation_id3) self.assertTrue(isinstance(reservation3, Reservation.WaitingReservation)) self.assertEquals( 1, reservation3.position) # Another user tries to reserve the experiment. He goes to the WaitingReservation, position 2 status4 = self.client.reserve_experiment(session_id4, dummy_experiments4[0].to_experiment_id(), "{}", "{}") reservation_id4 = status4.reservation_id reservation4 = self.client.get_reservation_status(reservation_id4) self.assertTrue(isinstance( reservation4, Reservation.WaitingReservation)) self.assertEquals( 2, reservation4.position) # The state of other users does not change reservation1 = self.client.get_reservation_status(reservation_id1) self.assertTrue(isinstance( reservation1, Reservation.ConfirmedReservation)) reservation2 = self.client.get_reservation_status(reservation_id2) self.assertTrue(isinstance( reservation2, Reservation.WaitingReservation)) self.assertEquals( 0, reservation2.position) reservation3 = self.client.get_reservation_status(reservation_id3) self.assertTrue(isinstance(reservation3, Reservation.WaitingReservation)) self.assertEquals( 1, reservation3.position ) # The user number 2 frees the experiment self.client.finished_experiment(reservation_id2) # Whenever he tries to do poll or send_command, he receives an exception try: time.sleep(1) self.client.poll(reservation_id2) self.client.poll(reservation_id2) self.client.poll(reservation_id2) except Exception as e: pass # All right :-) self.assertTrue("does not have any experiment" in repr(e)) else: self.fail("Expected exception when polling") # send a program CONTENT = "content of the program DUMMY1" self.client.send_file(reservation_id1, ExperimentUtil.serialize(CONTENT), 'program') # We need to wait for the programming to finish. response = self.client.send_command(reservation_id1, Command.Command("STATE")) self.assertEquals("STATE", response.commandstring) self.client.logout(session_id1)
def test_single_uses_timeout(self): self.fake_impact1.clear() self.fake_impact2.clear() self.fake_serial_port1.clear() self.fake_serial_port2.clear() # 6 users get into the system session_id1 = self.real_login.login('student1', 'password') session_id2 = self.real_login.login('student2', 'password') session_id3 = self.real_login.login('student3', 'password') session_id4 = self.real_login.login('student4', 'password') session_id5 = self.real_login.login('student5', 'password') session_id6 = self.real_login.login('student6', 'password') # they all have access to the ud-fpga experiment experiments1 = self.real_ups.list_experiments(session_id1) fpga_experiments1 = [ exp.experiment for exp in experiments1 if exp.experiment.name == 'ud-fpga' ] self.assertEquals(len(fpga_experiments1), 1) experiments2 = self.real_ups.list_experiments(session_id2) fpga_experiments2 = [ exp.experiment for exp in experiments2 if exp.experiment.name == 'ud-fpga' ] self.assertEquals(len(fpga_experiments2), 1) experiments3 = self.real_ups.list_experiments(session_id3) fpga_experiments3 = [ exp.experiment for exp in experiments3 if exp.experiment.name == 'ud-fpga' ] self.assertEquals(len(fpga_experiments3), 1) experiments4 = self.real_ups.list_experiments(session_id4) fpga_experiments4 = [ exp.experiment for exp in experiments4 if exp.experiment.name == 'ud-fpga' ] self.assertEquals(len(fpga_experiments4), 1) experiments5 = self.real_ups.list_experiments(session_id5) fpga_experiments5 = [ exp.experiment for exp in experiments5 if exp.experiment.name == 'ud-fpga' ] self.assertEquals(len(fpga_experiments5), 1) experiments6 = self.real_ups.list_experiments(session_id6) fpga_experiments6 = [ exp.experiment for exp in experiments6 if exp.experiment.name == 'ud-fpga' ] self.assertEquals(len(fpga_experiments6), 1) # 3 users try to reserve the experiment status1 = self.real_ups.reserve_experiment( session_id1, fpga_experiments1[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1")) reservation_id1 = status1.reservation_id status2 = self.real_ups.reserve_experiment( session_id2, fpga_experiments2[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1")) reservation_id2 = status2.reservation_id status3 = self.real_ups.reserve_experiment( session_id3, fpga_experiments3[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1")) reservation_id3 = status3.reservation_id # wait until it is reserved short_time = 0.1 times = 10.0 / short_time while times > 0: time.sleep(short_time) new_status = self.real_ups.get_reservation_status(reservation_id1) if not isinstance(new_status, Reservation.WaitingConfirmationReservation): break times -= 1 # first user got the device. The other two are in WaitingReservation reservation1 = self.real_ups.get_reservation_status(reservation_id1) self.assertTrue( isinstance(reservation1, Reservation.ConfirmedReservation)) reservation2 = self.real_ups.get_reservation_status(reservation_id2) self.assertTrue( isinstance(reservation2, Reservation.WaitingReservation)) self.assertEquals(0, reservation2.position) reservation3 = self.real_ups.get_reservation_status(reservation_id3) self.assertTrue( isinstance(reservation3, Reservation.WaitingReservation)) self.assertEquals(1, reservation3.position) # Another user tries to reserve the experiment. He goes to the WaitingReservation, position 2 status4 = self.real_ups.reserve_experiment( session_id4, fpga_experiments4[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1")) reservation_id4 = status4.reservation_id reservation4 = self.real_ups.get_reservation_status(reservation_id4) self.assertTrue( isinstance(reservation4, Reservation.WaitingReservation)) self.assertEquals(2, reservation4.position) # The state of other users does not change reservation1 = self.real_ups.get_reservation_status(reservation_id1) self.assertTrue( isinstance(reservation1, Reservation.ConfirmedReservation)) reservation2 = self.real_ups.get_reservation_status(reservation_id2) self.assertTrue( isinstance(reservation2, Reservation.WaitingReservation)) self.assertEquals(0, reservation2.position) reservation3 = self.real_ups.get_reservation_status(reservation_id3) self.assertTrue( isinstance(reservation3, Reservation.WaitingReservation)) self.assertEquals(1, reservation3.position) # The user number 2 frees the experiment self.real_ups.finished_experiment(reservation_id2) # Whenever he tries to do poll or send_command, he receives an exception try: self.real_ups.poll(reservation_id2) self.real_ups.poll(reservation_id2) self.real_ups.poll(reservation_id2) except core_exc.NoCurrentReservationError: pass # All right :-) # send a program CONTENT = "content of the program FPGA" self.real_ups.send_file(reservation_id1, ExperimentUtil.serialize(CONTENT), 'program') # We need to wait for the programming to finish. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming" ) and time.time() - start_time < XILINX_TIMEOUT: respcmd = self.real_ups.send_command(reservation_id1, Command.Command("STATE")) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) self.real_ups.send_command(reservation_id1, Command.Command("ChangeSwitch on 0")) self.real_ups.send_command(reservation_id1, Command.Command("ClockActivation on 250")) # end session self.real_ups.logout(session_id1) # Checking the commands sent self.assertEquals(1, len(self.fake_impact1._paths)) self.assertEquals(0, len(self.fake_impact2._paths)) self.assertEquals(CONTENT, self.fake_impact1._paths[0]) initial_open = 1 initial_send = 1 initial_close = 1 initial_total = initial_open + initial_send + initial_close # ChangeSwitch on 0 self.assertEquals( (0 + initial_total, 1), self.fake_serial_port1.dict['open'][0 + initial_open]) self.assertEquals( (1 + initial_total, 1), self.fake_serial_port1.dict['send'][0 + initial_send]) self.assertEquals( (2 + initial_total, None), self.fake_serial_port1.dict['close'][0 + initial_close]) # ClockActivation on 250 self.assertEquals( (3 + initial_total, 1), self.fake_serial_port1.dict['open'][1 + initial_open]) self.assertEquals( (4 + initial_total, 32), self.fake_serial_port1.dict['send'][1 + initial_send]) self.assertEquals( (5 + initial_total, None), self.fake_serial_port1.dict['close'][1 + initial_close])
def test_two_multiple_uses_of_different_devices(self): user1_session_id = self.real_login.login('student1','password') user1_experiments = self.real_ups.list_experiments(user1_session_id) self.assertEquals( 5, len(user1_experiments) ) fpga_experiments = [ exp.experiment for exp in user1_experiments if exp.experiment.name == 'ud-fpga' ] self.assertEquals( len(fpga_experiments), 1 ) # reserve it status = self.real_ups.reserve_experiment( user1_session_id, fpga_experiments[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) user1_reservation_id = status.reservation_id user2_session_id = self.real_login.login('student2','password') user2_experiments = self.real_ups.list_experiments(user2_session_id) self.assertEquals( 7, len(user2_experiments) ) pld_experiments = [ exp.experiment for exp in user2_experiments if exp.experiment.name == 'ud-pld' ] self.assertEquals( len(pld_experiments), 1 ) # reserve it status = self.real_ups.reserve_experiment( user2_session_id, pld_experiments[0].to_experiment_id(), "{}", "{}", ClientAddress.ClientAddress("127.0.0.1") ) user2_reservation_id = status.reservation_id short_time = 0.1 times = 9.0 / short_time while times > 0: time.sleep(short_time) new_status1 = self.real_ups.get_reservation_status(user1_reservation_id) new_status2 = self.real_ups.get_reservation_status(user2_reservation_id) if not isinstance(new_status1, Reservation.WaitingConfirmationReservation): if not isinstance(new_status2, Reservation.WaitingConfirmationReservation): break times -= 1 self.assertTrue( isinstance( self.real_ups.get_reservation_status( user1_reservation_id ), Reservation.ConfirmedReservation ) ) self.assertTrue( isinstance( self.real_ups.get_reservation_status( user2_reservation_id ), Reservation.ConfirmedReservation ) ) # send a program CONTENT1 = "content of the program FPGA" self.real_ups.send_file(user1_reservation_id, ExperimentUtil.serialize(CONTENT1), 'program') # We need to wait for the programming to finish. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming") and time.time() - start_time < XILINX_TIMEOUT: respcmd = self.real_ups.send_command(user1_reservation_id, Command.Command("STATE")) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) self.real_ups.send_command(user1_reservation_id, Command.Command("ChangeSwitch off 1")) self.real_ups.send_command(user1_reservation_id, Command.Command("ClockActivation on 250")) CONTENT2 = "content of the program PLD" self.real_ups.send_file(user2_reservation_id, ExperimentUtil.serialize(CONTENT2), 'program') # We need to wait for the programming to finish. start_time = time.time() response = "STATE=not_ready" while response in ("STATE=not_ready", "STATE=programming") and time.time() - start_time < XILINX_TIMEOUT: respcmd = self.real_ups.send_command(user1_reservation_id, Command.Command("STATE")) response = respcmd.get_command_string() time.sleep(0.2) # Check that the current state is "Ready" self.assertEquals("STATE=ready", response) self.real_ups.send_command(user2_reservation_id, Command.Command("ChangeSwitch on 0")) self.real_ups.send_command(user2_reservation_id, Command.Command("ClockActivation on 250")) # end session self.real_ups.logout(user1_session_id) self.real_ups.logout(user2_session_id) # Checking the commands sent self.assertEquals( 1, len(self.fake_impact1._paths) ) self.assertEquals( 1, len(self.fake_impact2._paths) ) self.assertEquals( CONTENT1, self.fake_impact1._paths[0] ) self.assertEquals( CONTENT2, self.fake_impact2._paths[0] ) initial_open = 1 initial_send = 1 initial_close = 1 initial_total = initial_open + initial_send + initial_close # ChangeSwitch off 1 self.assertEquals( (0 + initial_total,1), self.fake_serial_port1.dict['open'][0 + initial_open] ) self.assertEquals( (1 + initial_total,4), self.fake_serial_port1.dict['send'][0 + initial_send] ) self.assertEquals( (2 + initial_total,None), self.fake_serial_port1.dict['close'][0 + initial_close] ) self.assertEquals( (0 + initial_total,1), self.fake_serial_port2.dict['open'][0 + initial_open] ) self.assertEquals( (1 + initial_total,1), self.fake_serial_port2.dict['send'][0 + initial_send] ) self.assertEquals( (2 + initial_total,None), self.fake_serial_port2.dict['close'][0 + initial_close] ) # ClockActivation on 250 self.assertEquals( (3 + initial_total,1), self.fake_serial_port1.dict['open'][1 + initial_open] ) self.assertEquals( (4 + initial_total,32), self.fake_serial_port1.dict['send'][1 + initial_send] ) self.assertEquals( (5 + initial_total,None), self.fake_serial_port1.dict['close'][1 + initial_close] ) self.assertEquals( (3 + initial_total,1), self.fake_serial_port2.dict['open'][1 + initial_open] ) self.assertEquals( (4 + initial_total,32), self.fake_serial_port2.dict['send'][1 + initial_send] ) self.assertEquals( (5 + initial_total,None), self.fake_serial_port2.dict['close'][1 + initial_close] )