def setUp(self): # pseudo args = default_pseudo_arg_parser().parse_args([]) command_port = args.port data_port = args.dataport rbcp_server = RbcpServer() data_generator = PseudoDataGenerator() rbcp_command_handler = PseudoRbcpCommandHandler("pdev$ ") rbcp_command_handler.bind(rbcp_server, data_generator) self.pdev = PseudoDevice(rbcp_command_handler, data_generator, rbcp_server, command_port, data_port) self.pdev.start() # daq self.run_no_txt_path = os.path.join(os.path.dirname(daq.__file__), "run_no.txt") if os.path.isfile(self.run_no_txt_path): os.remove(self.run_no_txt_path) self.log_dir = "log" shutil.rmtree(self.log_dir, ignore_errors=True) self.handler = DaqCommandHandler(PROMPT) self.server = CuiServer(SessionThread, self.handler, 5050) self.server.start() self.cli = CommandClient(PROMPT, "localhost", 5050)
def setUp(self): """ setup pseudo device. """ rbcp_server = RbcpServer() data_generator = DataGenerator() rbcp_command_handler = RbcpCommandHandler(self.PROMPT) rbcp_command_handler.bind(rbcp_server, data_generator) self._pdev = PseudoDevice(rbcp_command_handler, data_generator, rbcp_server, self.COMMAND_PORT, self.DATA_PORT) self._pdev.start() LOGGER.info("TestPseudoRbcp.setup Started pseudo device") # check pseudo device started with connecting command client. retry = 10 while retry > 0: try: command_client = CommandClient(self.PROMPT, "localhost", self.COMMAND_PORT) command_client.close() break except Exception: retry -= 1 if retry == 0: raise time.sleep(0.5) LOGGER.info("setup. command client tested.")
def test_on_command(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) res = cli.send_command("unknown_command") self.assertTrue(res.startswith("NG:Unknown command")) cli.close()
def test_separated_cmd(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) cli._sock.sendall(to_bytes("pw")) time.sleep(1) cli._sock.sendall(to_bytes("d" + os.linesep)) res = cli._receive() print(res) self.assertTrue(res)
def test_on_cmd_ls(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) res = cli.send_command("ls") print(self, res) files = res.splitlines() self.assertTrue(files.count("sitcpy")) self.assertTrue(files.count("tests")) self.assertTrue(files.count("README.md")) res = cli.send_command("ls sitcpy") print(self, res) files = res.splitlines() self.assertTrue(files.count("cui.py")) self.assertTrue(files.count("rbcp.py")) res = cli.send_command("ls arg1 arg2") print(self, res) self.assertTrue(res.startswith("NG:Too many arguments")) res = cli.send_command("ls file_is_not_found") print(self, res) self.assertTrue(res.startswith("NG:Error occurred")) cli.close()
def test_on_cmd_help(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) # all command help res = cli.send_command("help") self.assertTrue(len(res.splitlines()) > 5) # close command help res = cli.send_command("help close") self.assertTrue(res.startswith("close:")) # close and exit command help res = cli.send_command("help close exit") cmd_list = ["close:", "exit:"] self.assertEqual(len(res.splitlines()), len(cmd_list)) for line in res.splitlines(): for cmd in cmd_list: if line.startswith(cmd): cmd_list.remove(cmd) break self.fail("Unknown command help.") self.assertFalse(cmd_list) # unknown command help res = cli.send_command("help unknown_command") self.assertTrue(res.startswith("NG:Unknown command")) cli.close()
def test_on_cmd_state(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) res = cli.send_command("state") print(self, res) self.assertTrue(res.startswith("Sever address:")) cli.close() handler = CommandHandler(PROMPT) # noinspection PyTypeChecker session = SessionThread(None, None, None, None) self.assertTrue(handler.on_cmd_state(session, ["state"]))
def test_on_cmd_stat(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) res = cli.send_command("stat") print(self, res) self.assertTrue(res) for line in res.splitlines(): key_name = line.split("=", 1) self.assertTrue(key_name[0].strip()) self.assertTrue(len(key_name) == 2) res = cli.send_command("stat j") print(self, res) jsn = json.loads(res) self.assertTrue(isinstance(jsn, dict)) for key, val in jsn.items(): self.assertTrue(is_unicode(key)) self.assertTrue(key) self.assertTrue(is_unicode(val)) res = cli.send_command("stat unknown-param") print(self, res) self.assertTrue(res.startswith("NG:Unknown argument")) cli.close()
def test_send_command(self): # pylint: disable=no-self-use cli = CommandClient(PROMPT, "localhost", PORT) print("##### ls") print(cli.send_command("ls")) print("##### stat") print(cli.send_command("stat")) print("##### help") print(cli.send_command("help")) print("##### close") cli.close() print("#####")
def test_debug_command(self): """ using the simple commands commands. """ command_client = CommandClient(self.PROMPT, "localhost", self.COMMAND_PORT) reply = command_client.send_command("help") print(reply) reply = command_client.send_command("help read") print(reply) commands = [ "stat", "state", "pwd", "ls", "pwd pwd pwd", "ls ..", "", ";" ] for command in commands: print(command_client.send_command(command)) ng_commands = ["unknown", "pwd no args", "help unknown"] for command in ng_commands: reply = command_client.send_command(command) self.assertTrue(reply[0:3] == "NG:") reply = command_client.send_command( "write ffffff00 01 02 03 04 05 06 07 08") print(reply) reply = command_client.send_command("read ffffff00 8") print(reply) self.assertTrue(reply.strip() == "01 02 03 04 05 06 07 08") reply = command_client.send_command( "write ffffff00 01 02 03 04 05 06 07 08 09 10 11 12") print(reply) reply = command_client.send_command("read ffffff00 12") print(reply) lines = [val.strip() for val in reply.splitlines()] self.assertEqual(lines[0], "01 02 03 04 05 06 07 08") self.assertEqual(lines[1], "09 10 11 12")
def test_on_cmd_close(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) self.assertEqual(len(self.server._sessions), 1) cli.send_command("close") time.sleep(1) self.assertEqual(len(self.server._sessions), 0) cli.close()
def test_on_cmd_exit(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) self.assertEqual(self.server.state, sitcpy.THREAD_RUNNING) self.assertEqual(len(self.server._sessions), 1) cli.send_command("exit") time.sleep(1) self.assertEqual(self.server.state, sitcpy.THREAD_STOPPED) self.assertEqual(len(self.server._sessions), 0) cli.close()
def test_on_cmd_pwd(self): self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) print("=====") res = cli.send_command("pwd") print(res) self.assertFalse(res.startswith("NG:")) print("=====") res = cli.send_command("pwd ../") print(res) self.assertTrue(res.startswith("NG:")) print("=====") cli.close()
def test_text_handler(self): handler = TextHandler() self.assertEqual(handler.find_delimiter_position(b"abc\r\ndef"), 5) self.assertEqual(handler.linesep, "\r\n") self.assertEqual(handler.find_delimiter_position(b"abc\ndef"), 4) self.assertEqual(handler.linesep, "\n") self.assertEqual(handler.find_delimiter_position(b"abc\rdef"), 4) self.assertEqual(handler.linesep, "\r") self.assertEqual(handler.find_delimiter_position(b""), -1) self.assertEqual(handler.find_delimiter_position(b" abc def "), -1) handler.reply_text( SessionThread(self.server, handler, None, "localhost"), "abc") self.server.start() cli = CommandClient(PROMPT, "localhost", PORT) cli.send_command("stat") cli.close()
class DaqTest(TestCase): def setUp(self): # pseudo args = default_pseudo_arg_parser().parse_args([]) command_port = args.port data_port = args.dataport rbcp_server = RbcpServer() data_generator = PseudoDataGenerator() rbcp_command_handler = PseudoRbcpCommandHandler("pdev$ ") rbcp_command_handler.bind(rbcp_server, data_generator) self.pdev = PseudoDevice(rbcp_command_handler, data_generator, rbcp_server, command_port, data_port) self.pdev.start() # daq self.run_no_txt_path = os.path.join(os.path.dirname(daq.__file__), "run_no.txt") if os.path.isfile(self.run_no_txt_path): os.remove(self.run_no_txt_path) self.log_dir = "log" shutil.rmtree(self.log_dir, ignore_errors=True) self.handler = DaqCommandHandler(PROMPT) self.server = CuiServer(SessionThread, self.handler, 5050) self.server.start() self.cli = CommandClient(PROMPT, "localhost", 5050) def tearDown(self): self.cli.close() self.server.stop() self.server.join() self.pdev.stop() if os.path.isfile(self.run_no_txt_path): os.remove(self.run_no_txt_path) shutil.rmtree(self.log_dir, ignore_errors=True) def test_commands(self): res = self.cli.send_command("reload").strip() self.assertTrue(res.startswith("OK:")) res = self.cli.send_command("reload _file_not_found_error_").strip() self.assertTrue(res.startswith("NG:")) # stat res = self.cli.send_command("stat").strip() self.assertTrue(len(res.splitlines()) > 5) for val in res.splitlines(): if val: self.assertEqual(len(val.split("=", 1)), 2) # rawsave res = self.cli.send_command("rawsave").strip() self.assertEqual(res, "off") res = self.cli.send_command("rawsave off").strip() self.assertEqual(res, "OK:off") res = self.cli.send_command("rawsave").strip() self.assertEqual(res, "off") res = self.cli.send_command("rawsave on").strip() self.assertEqual(res, "OK:on") res = self.cli.send_command("rawsave").strip() self.assertEqual(res, "on") # runno res = self.cli.send_command("runno 1").strip() self.assertEqual(res, "OK:1") res = self.cli.send_command("runno").strip() self.assertEqual(res, "1") res = self.cli.send_command("runno 2").strip() self.assertEqual(res, "OK:2") # exit res = self.cli.send_command("exit") self.assertTrue(res is None) def test_daq(self): self.assertFalse(os.path.isdir(self.log_dir)) self.cli.send_command("rawsave on") res = self.cli.send_command("stat").strip() print("===== stat before daq") print(res) print("/=====") res = self.cli.send_command("run 100").strip() self.assertTrue(res.startswith("NG:")) res = self.cli.send_command("run").strip() print("=====") print(res) print("/=====") time.sleep(1) res = self.cli.send_command("stat").strip() print("===== stat during daq") print(res) print("/=====") res = self.cli.send_command("run").strip() self.assertTrue(res.startswith("NG:")) time.sleep(1) res = self.cli.send_command("stop 100").strip() self.assertTrue(res.startswith("NG:")) res = self.cli.send_command("stop").strip() print("=====") print(res) print("/=====") res = self.cli.send_command("stat").strip() print("===== stat after daq") print(res) print("/=====") self.assertTrue(os.path.isdir(self.log_dir))
class RbcpServerTest(TestCase): def setUp(self): # pseudo args = default_pseudo_arg_parser().parse_args([]) command_port = args.port data_port = args.dataport self.rbcp_server = RbcpServer() data_generator = DataGenerator() rbcp_command_handler = RbcpCommandHandler("pdev$ ") rbcp_command_handler.bind(self.rbcp_server, data_generator) self.pdev = PseudoDevice(rbcp_command_handler, data_generator, self.rbcp_server, command_port, data_port) self.pdev.start() # daq # self.run_no_txt_path = os.path.join( # os.path.dirname(daq.__file__), "run_no.txt") # if os.path.isfile(self.run_no_txt_path): # os.remove(self.run_no_txt_path) # self.log_dir = "/tmp/sitcpdaq" # shutil.rmtree(self.log_dir, ignore_errors=True) self.handler = DaqCommandHandler(PROMPT) self.server = CuiServer(SessionThread, self.handler, 5050) self.server.start() self.cli = CommandClient(PROMPT, "localhost", 5050) def tearDown(self): self.cli.close() self.server.stop() self.server.join() self.pdev.stop() # if os.path.isfile(self.run_no_txt_path): # os.remove(self.run_no_txt_path) # self.log_dir = "/tmp/sitcpdaq" # shutil.rmtree(self.log_dir, ignore_errors=True) def test_register(self): res = self.rbcp_server.read_registers(0xFFFF0000, 8) self.assertEqual(res, bytearray(b"\0\0\0\0\0\0\0\0")) self.rbcp_server.write_registers(0xFFFF0000, b"\1\1\1\1\1\1\1\1") res = self.rbcp_server.read_registers(0xFFFF0000, 8) self.assertEqual(res, bytearray(b"\1\1\1\1\1\1\1\1")) self.rbcp_server.write_registers(0xFFFF0004, b"\2\2\2\2") res = self.rbcp_server.read_registers(0xFFFF0000, 8) self.assertEqual(res, bytearray(b"\1\1\1\1\2\2\2\2")) try: self.rbcp_server.read_registers(0x00000000, 8) except VirtualRegisterOutOfRange: pass else: self.fail() try: self.rbcp_server.read_registers(0xFFFF0000, 65536) except VirtualRegisterOutOfRange: self.fail() try: self.rbcp_server.read_registers(0xFFFF0000, 65537) except VirtualRegisterOutOfRange: pass else: self.fail() try: self.rbcp_server.write_registers(0xFFFF0000, bytearray(65536)) except VirtualRegisterOutOfRange: self.fail() try: self.rbcp_server.write_registers(0xFFFF0000, bytearray(65537)) except VirtualRegisterOutOfRange: pass else: self.fail() def test_initialize_registers(self): self.assertEqual(len(self.rbcp_server.registers), 1) self.rbcp_server.merge_registers() # do noting. res = self.rbcp_server.initialize_registers(TEST_DATA_PATH0) self.assertEqual(res.strip(), "00000000:256 bytes") self.assertEqual(len(self.rbcp_server.registers), 2) res = self.rbcp_server.initialize_registers(TEST_DATA_PATH256) self.assertEqual(res.strip(), "00000100:256 bytes") self.assertEqual(len(self.rbcp_server.registers), 3) self.rbcp_server.merge_registers() self.assertEqual(len(self.rbcp_server.registers), 2) res = self.rbcp_server.get_register_info() self.assertEqual(len(res), 2) def test_dump_registers(self): res = self.rbcp_server.dump_registers() print(res) pass