def test_unix_device_can_execute_cmds(): unix = UnixLocal(io_type='terminal', variant='threaded') cmd = unix.get_cmd(cmd_name='ls', cmd_params={"options": "-l"}) r = cmd() assert (r is not None) cmd = unix.get_cmd('whoami') r = cmd() assert (r is not None)
def test_device_disable_and_enable_logging(buffer_connection): from moler.device.unixlocal import UnixLocal dev1 = UnixLocal(io_connection=buffer_connection) dev1.disable_logging() assert False == dev1.io_connection.moler_connection._enabled_logging dev1.disable_logging() assert False == dev1.io_connection.moler_connection._enabled_logging dev1.enable_logging() assert True == dev1.io_connection.moler_connection._enabled_logging dev1.enable_logging() assert True == dev1.io_connection.moler_connection._enabled_logging
def test_device_unix_can_return_cd_command(configure_net_1_connection): from moler.device.unixlocal import UnixLocal from moler.cmd.unix.cd import Cd ux = UnixLocal.from_named_connection(connection_name='net_1') assert hasattr(ux, 'get_cmd') assert isinstance(ux.get_cmd(cmd_name='cd', path="/home/user/"), Cd)
def test_device_unix_can_not_execute_cmds_in_incorect_state(): unix = UnixLocal(io_type='terminal', variant='threaded') unix.establish_connection() unix.goto_state(UnixLocal.not_connected) with pytest.raises( DeviceFailure, match= r'Failed to create .*-object for .* is unknown for state .* of device .*' ): unix.get_cmd(cmd_name='cd', cmd_params={"path": "/home/user/"})
def test_device_add_neighbour_device_without_bidirectional(buffer_connection): from moler.device.unixlocal import UnixLocal dev1 = UnixLocal(io_connection=buffer_connection) dev2 = UnixLocal(io_connection=buffer_connection) dev1.add_neighbour_device(neighbour_device=dev2, bidirectional=False) neighbour_devices = dev1.get_neighbour_devices(device_type=UnixLocal) assert 1 == len(neighbour_devices) neighbour_devices = dev2.get_neighbour_devices(device_type=UnixLocal) assert 0 == len(neighbour_devices)
def test_unix_local_cmd_with_event(): unix = UnixLocal(io_type='terminal', variant='threaded') unix.establish_connection() unix.goto_state(UnixLocal.unix_local) rets = {'ping': None, 'whoami': None} def callback_response(): cmd_whoami = unix.get_cmd(cmd_name="whoami") rets['whoami'] = cmd_whoami() event_reconnect = unix.get_event(event_name="ping_response", event_params={}) event_reconnect.add_event_occurred_callback(callback=callback_response, ) event_reconnect.start() cmd_ping = unix.get_cmd(cmd_name="ping", cmd_params={ 'destination': '127.0.0.1', 'options': '-c 1' }) rets['ping'] = cmd_ping(timeout=5) MolerTest.sleep(1) assert rets['ping'] is not None assert rets['whoami'] is not None
def test_device_logging_suffix(buffer_connection): from moler.config.loggers import change_logging_suffix import moler from moler.device.unixlocal import UnixLocal suffix = "DEVICE_SUFFIX" dev1 = UnixLocal(io_connection=buffer_connection) device_name = dev1.name change_logging_suffix(None) dev1.set_logging_suffix(suffix) for logger_name in moler.config.loggers._logging_suffixes.keys(): if device_name in logger_name: assert suffix == moler.config.loggers._logging_suffixes[ logger_name] else: assert None is moler.config.loggers._logging_suffixes[logger_name] dev1.set_logging_suffix(None) for logger_name in moler.config.loggers._logging_suffixes.keys(): assert None is moler.config.loggers._logging_suffixes[logger_name]
def test_device_directly_created_must_be_given_io_connection( buffer_connection): from moler.device.unixlocal import UnixLocal dev = UnixLocal(io_connection=buffer_connection) assert dev.io_connection == buffer_connection
def test_device_may_be_created_on_named_connection(configure_net_1_connection): from moler.device.unixlocal import UnixLocal dev = UnixLocal.from_named_connection(connection_name='net_1') assert dev.io_connection is not None assert dev.io_connection.name == 'net_1'
def test_device_add_neighbour_device(buffer_connection): from moler.device.unixlocal import UnixLocal dev1 = UnixLocal(io_connection=buffer_connection) dev2 = UnixLocal(io_connection=buffer_connection) neighbour_devices = dev1.get_neighbour_devices(device_type=UnixLocal) assert 0 == len(neighbour_devices) dev1.add_neighbour_device(neighbour_device=dev2, bidirectional=True) neighbour_devices = dev1.get_neighbour_devices(device_type=UnixLocal) assert 1 == len(neighbour_devices) neighbour_devices = dev2.get_neighbour_devices(device_type=UnixLocal) assert 1 == len(neighbour_devices) # device is added only once dev1.add_neighbour_device(neighbour_device=dev2) neighbour_devices = dev1.get_neighbour_devices(device_type=UnixLocal) assert 1 == len(neighbour_devices) neighbour_devices = dev1.get_neighbour_devices(device_type=None) assert 1 == len(neighbour_devices) neighbour_devices = dev1.get_neighbour_devices(device_type=int) assert 0 == len(neighbour_devices)