Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@MolerTest.raise_background_exceptions(check_steps_end=True)
def can_select_device_loaded_from_config_dict(moler_config, device_factory):
conn_config = {
'LOGGER': {
'PATH': '/tmp/',
'RAW_LOG': True,
'DATE_FORMAT': '%d %H:%M:%S'
},
'DEVICES': {
'UNIX_LOCAL': {
'DEVICE_CLASS': 'moler.device.unixlocal.UnixLocal',
'INITIAL_STATE': 'UNIX_LOCAL'
}
}
}
moler_config.load_config(config=conn_config, config_type='dict')
def on_new_data(self, data, time_recv):
self.received_data.append(data)
buffer_observer1 = BufferObserver()
buffer_observer2 = BufferObserver()
buffer_observer3 = BufferObserver()
moler_conn = ThreadedMolerConnection()
moler_conn.subscribe(observer=buffer_observer1.on_new_data, connection_closed_handler=do_nothing_func)
moler_conn.subscribe(observer=buffer_observer2.on_new_data, connection_closed_handler=do_nothing_func)
used_io = buffer_transport_class(moler_connection=moler_conn) # external-IO internally sets .how2send
used_io.write(input_bytes=b"incoming data") # inject to buffer for next line read
used_io.read()
MolerTest.sleep(1, True) # Processing in separate thread so have to wait.
assert b"incoming data" in buffer_observer1.received_data
assert b"incoming data" in buffer_observer2.received_data
assert b"incoming data" not in buffer_observer3.received_data # that one was not subscribed
def on_new_line(self, line, is_full_line):
if self.raise_unicode:
self.nr += 1
exc = UnicodeDecodeError("utf-8", b'abcdef', 0, 1, "Unknown")
raise exc
super(Wait4promptUnicodeError, self).on_new_line(line, is_full_line)
output = "bash\n"
dict_output = {'line': u'abcbash', 'matched': u'bash', 'named_groups': {}, 'groups': (), 'time': 0}
event = Wait4promptUnicodeError(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1)
event._ignore_unicode_errors = False
event.raise_unicode = True
event.start(timeout=0.1)
buffer_connection.moler_connection.data_received("abc".encode("utf-8"), datetime.datetime.now())
MolerTest.sleep(0.01)
event.raise_unicode = False
MolerTest.sleep(0.01)
buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now())
with pytest.raises(MolerException):
event.await_done()
event = Wait4promptUnicodeError(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1)
event._ignore_unicode_errors = True
event.raise_unicode = True
event.start(timeout=0.1)
buffer_connection.moler_connection.data_received("abc".encode("utf-8"), datetime.datetime.now())
event.raise_unicode = False
buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now())
event.await_done()
occurrence = event.get_last_occurrence()
occurrence['time'] = 0
def test_sleep_for_threaded_variant():
sleep_time = 1
start_time = time.time()
MolerTest.sleep(sleep_time)
stop_time = time.time()
elapsed = stop_time - start_time
assert round(elapsed) == sleep_time
if self.raise_unicode:
self.nr += 1
exc = UnicodeDecodeError("utf-8", b'abcdef', 0, 1, "Unknown")
raise exc
super(Wait4promptUnicodeError, self).on_new_line(line, is_full_line)
output = "bash\n"
dict_output = {'line': u'abcbash', 'matched': u'bash', 'named_groups': {}, 'groups': (), 'time': 0}
event = Wait4promptUnicodeError(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1)
event._ignore_unicode_errors = False
event.raise_unicode = True
event.start(timeout=0.1)
buffer_connection.moler_connection.data_received("abc".encode("utf-8"), datetime.datetime.now())
MolerTest.sleep(0.01)
event.raise_unicode = False
MolerTest.sleep(0.01)
buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now())
with pytest.raises(MolerException):
event.await_done()
event = Wait4promptUnicodeError(connection=buffer_connection.moler_connection, prompt="bash", till_occurs_times=1)
event._ignore_unicode_errors = True
event.raise_unicode = True
event.start(timeout=0.1)
buffer_connection.moler_connection.data_received("abc".encode("utf-8"), datetime.datetime.now())
event.raise_unicode = False
buffer_connection.moler_connection.data_received(output.encode("utf-8"), datetime.datetime.now())
event.await_done()
occurrence = event.get_last_occurrence()
occurrence['time'] = 0
assert occurrence == dict_output
'UNIX_LOCAL': {
'DEVICE_CLASS': 'moler.device.unixlocal.UnixLocal',
'INITIAL_STATE': 'UNIX_LOCAL'
}
}
}
moler_config.load_config(config=conn_config, config_type='dict')
device = device_factory.get_device(name='UNIX_LOCAL')
assert device.__module__ == 'moler.device.unixlocal'
assert device.__class__.__name__ == 'UnixLocal'
device.__del__()
MolerTest.steps_end()
can_select_device_loaded_from_config_dict(moler_config, device_factory)
def remove_all_devices(cls, clear_device_history=False):
"""
Remove all created devices.
:param clear_device_history: set True to clear the history of devices. Caution: you may overwrite your logs!
:return: None
"""
devices = copy_list(cls._devices.keys(), deep_copy=False)
for device_name in devices:
cls.remove_device(name=device_name)
devices_config.clear()
if clear_device_history:
MolerTest.warning("All history of devices will be forgotten. The same names can be used again with"
" different meaning!")
cls._clear()
put_interfaces_down()
time.sleep(5)
put_interfaces_up()
time.sleep(3)
finally:
# test teardown
ping.cancel()
ping_lost_detector.cancel()
ping_back_detector.cancel()
if ping_times["lost_connection_time"] == 0:
MolerTest.error("Network outage did not occur.")
if ping_times["reconnection_time"] == 0:
MolerTest.error("Network outage did not finish on time.")
def ping_is_on_callback():
MolerTest.info("Ping works")
def outage_callback(device_name, ping_times):
MolerTest.info("Network outage on {}".format(device_name))
ping_times["lost_connection_time"] = time.time()