def test_maintenance_mode(self): action = master_api.basic_action(self.master_version) fields = {'action_type': 1, 'action_number': 2} pty = DummyPty([ master_api.to_cli_mode().create_input(0), bytearray(b'error list\r\n'), bytearray(b'exit\r\n') ]) SetUpTestInjections(controller_serial=pty) comm = MasterCommunicator(init_master=False) comm.start() comm.start_maintenance_mode() pty.fd.write(bytearray(b'OK')) self.assertRaises(InMaintenanceModeException, comm.do_command, action, fields) self.assertEqual(bytearray(b'OK'), comm.get_maintenance_data()) comm.send_maintenance_data(bytearray(b'error list\r\n')) pty.fd.write(bytearray(b'the list\n')) self.assertEqual(bytearray(b'the list\n'), comm.get_maintenance_data()) comm.stop_maintenance_mode()
def start_maintenance_mode(self): """ Start maintenance mode. :raises: :class`InMaintenanceModeException` if master is in maintenance mode. """ if self.__maintenance_mode: raise InMaintenanceModeException() while True: try: self.__maintenance_queue.get(block=False) except Empty: break self.__maintenance_mode = True self.send_maintenance_data(master_api.to_cli_mode().create_input(0))
def test_maintenance_passthrough(self): action = master_api.basic_action(self.master_version) fields = {'action_type': 1, 'action_number': 2} pty = DummyPty([ master_api.to_cli_mode().create_input(0), bytearray(b'error list\r\n'), bytearray(b'exit\r\n') ]) SetUpTestInjections(controller_serial=pty) comm = MasterCommunicator(init_master=False) comm.enable_passthrough() comm.start() ready = threading.Event() def get_passthrough(): """ Background thread that reads the passthrough data. """ self.assertEqual('Before maintenance', comm.get_passthrough_data()) ready.set() self.assertEqual('After maintenance', comm.get_passthrough_data()) thread = threading.Thread(target=get_passthrough) thread.start() pty.fd.write(bytearray(b'Before maintenance')) ready.wait(2) comm.start_maintenance_mode() pty.fd.write(bytearray(b'OK')) time.sleep(0.2) self.assertRaises(InMaintenanceModeException, comm.do_command, action, fields) self.assertEqual(bytearray(b'OK'), comm.get_maintenance_data()) comm.send_maintenance_data(bytearray(b'error list\r\n')) pty.fd.write(bytearray(b'the list\n')) time.sleep(0.2) self.assertEqual(bytearray(b'the list\n'), comm.get_maintenance_data()) comm.stop_maintenance_mode() pty.fd.write(bytearray(b'After maintenance')) thread.join(2) assert not thread.is_alive()