def load_device_from_config(config): create_at_startup = False if 'DEVICES' in config: if 'DEFAULT_CONNECTION' in config['DEVICES']: default_conn = config['DEVICES'].pop('DEFAULT_CONNECTION') conn_desc = default_conn['CONNECTION_DESC'] dev_cfg.set_default_connection(**conn_desc) if 'CREATE_AT_STARTUP' in config['DEVICES']: create_at_startup = config['DEVICES'].pop('CREATE_AT_STARTUP') for device_name in config['DEVICES']: device_def = config['DEVICES'][device_name] dev_cfg.define_device( name=device_name, device_class=device_def['DEVICE_CLASS'], connection_desc=device_def.get('CONNECTION_DESC', dev_cfg.default_connection), connection_hops={ 'CONNECTION_HOPS': device_def.get('CONNECTION_HOPS', {}) }, initial_state=device_def.get('INITIAL_STATE', None), ) if create_at_startup is True: from moler.device.device import DeviceFactory DeviceFactory.create_all_devices()
def test_network_outage(): load_config(config=os.path.abspath('config/my_devices.yml')) unix1 = DeviceFactory.get_device(name='MyMachine1') unix2 = DeviceFactory.get_device(name='MyMachine2') # test setup - ensure network is up before running test net_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) sudo_ensure_net_up = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": net_up}) sudo_ensure_net_up() # run event observing "network down" no_ping = unix1.get_event(event_name="ping_no_response") no_ping.add_event_occurred_callback(callback=outage_callback, callback_params={'device_name': 'MyMachine1'}) no_ping.start() # run test ping = unix1.get_cmd(cmd_name="ping", cmd_params={"destination": "localhost", "options": "-O"}) ping.start(timeout=120) time.sleep(3) ifconfig_down = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo down"}) sudo_ifconfig_down = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": ifconfig_down}) sudo_ifconfig_down() time.sleep(5) ifconfig_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) sudo_ifconfig_up = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": ifconfig_up}) sudo_ifconfig_up() time.sleep(3) # test teardown ping.cancel() no_ping.cancel()
def clear_all_cfg(): import moler.config as moler_cfg import moler.config.devices as dev_cfg from moler.device.device import DeviceFactory as dev_factory moler_cfg.clear() dev_cfg.clear() dev_factory._clear()
def test_network_outage(): load_config(config=os.path.abspath('config/my_devices.yml')) unix1 = DeviceFactory.get_device(name='MyMachine1') unix2 = DeviceFactory.get_device(name='MyMachine2') ping = unix1.get_cmd(cmd_name="ping", cmd_params={ "destination": "localhost", "options": "-O" }) #ping(timeout=10) ping.start(timeout=120) time.sleep(5)
def test_network_outage(): load_config(config=os.path.abspath('config/my_devices.yml')) unix1 = DeviceFactory.get_device(name='MyMachine1') unix2 = DeviceFactory.get_device(name='MyMachine2') ping = unix1.get_cmd(cmd_name="ping", cmd_params={"destination": "localhost", "options": "-O"}) ping.start(timeout=120) time.sleep(3) ifconfig = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo down"}) sudo = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": ifconfig}) sudo() time.sleep(5)
def test_network_outage(): load_config(config=os.path.abspath('config/my_devices.yml')) unix1 = DeviceFactory.get_device(name='MyMachine1') unix2 = DeviceFactory.get_device(name='MyMachine2') # test setup - ensure network is up before running test ifconfig_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) sudo_ifconfig_up = unix2.get_cmd(cmd_name="sudo", cmd_params={ "password": "******", "cmd_object": ifconfig_up }) sudo_ifconfig_up() # run test ping = unix1.get_cmd(cmd_name="ping", cmd_params={ "destination": "localhost", "options": "-O" }) ping.start(timeout=120) time.sleep(3) ifconfig_down = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo down"}) sudo_ifconfig_down = unix2.get_cmd(cmd_name="sudo", cmd_params={ "password": "******", "cmd_object": ifconfig_down }) sudo_ifconfig_down() time.sleep(5) ifconfig_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) sudo_ifconfig_up = unix2.get_cmd(cmd_name="sudo", cmd_params={ "password": "******", "cmd_object": ifconfig_up }) sudo_ifconfig_up() time.sleep(3) # test teardown ping.cancel()
def test_network_outage(): load_config(config=os.path.abspath('config/my_devices.yml')) unix1 = DeviceFactory.get_device(name='MyMachine1') unix2 = DeviceFactory.get_device(name='MyMachine2') ####################################################### # TEST GOAL: network outage should not exceed 3 seconds ####################################################### # test setup ping_times = {"lost_connection_time": 0, "reconnection_time": 0} # ensure network is up before running test net_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) sudo_ensure_net_up = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": net_up}) sudo_ensure_net_up() # run event observing "network down/up" no_ping = unix1.get_event(event_name="ping_no_response", event_params={"till_occurs_times": 1}) no_ping.add_event_occurred_callback(callback=outage_callback, callback_params={'device_name': 'MyMachine1', 'ping_times': ping_times}) no_ping.start() ping_is_on = unix1.get_event(event_name="ping_response") ping_is_on.add_event_occurred_callback(callback=ping_is_on_callback, callback_params={'ping_times': ping_times}) ping_is_on.start() # run test ping = unix1.get_cmd(cmd_name="ping", cmd_params={"destination": "localhost", "options": "-O"}) ping.start(timeout=120) time.sleep(3) ifconfig_down = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo down"}) sudo_ifconfig_down = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": ifconfig_down}) sudo_ifconfig_down() time.sleep(5) ifconfig_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) sudo_ifconfig_up = unix2.get_cmd(cmd_name="sudo", cmd_params={"password": "******", "cmd_object": ifconfig_up}) sudo_ifconfig_up() time.sleep(3) # test teardown ping.cancel() no_ping.cancel()
'command_params': { 'expected_prompt': 'demo@', 'host': 'test.rebex.net', 'login': '******', 'password': '******', 'set_timeout': None } } } } } } }, config_type='dict') remote_unix = DeviceFactory.get_device( name='RebexTestMachine') # it starts in local shell remote_unix.goto_state(state="UNIX_REMOTE") # make it go to remote shell ls_cmd = remote_unix.get_cmd(cmd_name="ls", cmd_params={"options": "-l"}) remote_files = ls_cmd() if 'readme.txt' in remote_files['files']: print("readme.txt file:") readme_file_info = remote_files['files']['readme.txt'] for attr in readme_file_info: print(" {:<18}: {}".format(attr, readme_file_info[attr])) # result: """ readme.txt file:
import os.path from moler.config import load_config from moler.device.device import DeviceFactory load_config(config=os.path.abspath('my_devices.yml')) my_r_unix = DeviceFactory.get_device(name='MyRemoteMachine') print(my_r_unix)
import os from moler.config import load_config from moler.device.device import DeviceFactory load_config(config=os.path.join(os.path.dirname(__file__), 'my_devices.yml')) my_unix = DeviceFactory.get_device(name='MyMachine') host = 'www.google.com' ping_cmd = my_unix.get_cmd(cmd_name="ping", cmd_params={"destination": host, "options": "-w 6"}) remote_unix = DeviceFactory.get_device(name='RebexTestMachine') remote_unix.goto_state(state="UNIX_REMOTE") ls_cmd = remote_unix.get_cmd(cmd_name="ls", cmd_params={"options": "-l"}) print("Start pinging {} ...".format(host)) ping_cmd.start() # run command in background print("Let's check readme.txt at {} while pinging {} ...".format(remote_unix.name, host)) remote_files = ls_cmd() # foreground "run in the meantime" file_info = remote_files['files']['readme.txt'] print("readme.txt file: owner={fi[owner]}, size={fi[size_bytes]}".format(fi=file_info)) ping_stats = ping_cmd.await_done(timeout=6) # await background command print("ping {}: {}={}, {}={} [{}]".format(host,'packet_loss', ping_stats['packet_loss'], 'time_avg', ping_stats['time_avg'], ping_stats['time_unit'])) # result: """
def device_factory(): from moler.device.device import DeviceFactory as dev_factory yield dev_factory # restore since tests may change configuration dev_factory._clear()
from moler.config import load_config from moler.device.device import DeviceFactory load_config(config='my_devices.yml') # description of available devices # load_config(config={'DEVICES': {'MyMachine': {'DEVICE_CLASS': 'moler.device.unixremote.UnixLocal'}}}, # config_type='dict') my_unix = DeviceFactory.get_device( name='MyMachine') # take specific device out of available ones ps_cmd = my_unix.get_cmd( cmd_name="ps", # take command of that device cmd_params={"options": "-ef"}) processes_info = ps_cmd() # run the command, it returns result for proc_info in processes_info: if 'python' in proc_info['CMD']: print("PID: {info[PID]} CMD: {info[CMD]}".format(info=proc_info)) """ PID: 1817 CMD: /usr/bin/python /usr/share/system-config-printer/applet.py PID: 21825 CMD: /usr/bin/python /home/gl/moler/examples/command/unix_ps.py """
def test_network_outage(): load_config(config=os.path.abspath('config/my_devices.yml')) unix1 = DeviceFactory.get_device(name='MyMachine1') unix2 = DeviceFactory.get_device(name='MyMachine2') ####################################################### # TEST GOAL: network outage should not exceed 3 seconds ####################################################### # test setup - prepare everything required by test ping_times = {"lost_connection_time": 0, "reconnection_time": 0} # ensure network is up before running test net_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) ensure_interfaces_up = unix2.get_cmd(cmd_name="sudo", cmd_params={ "password": "******", "cmd_object": net_up }) # run event observing "network down/up" ping_lost_detector = unix1.get_event(event_name="ping_no_response", event_params={"till_occurs_times": 1}) ping_lost_detector.add_event_occurred_callback(callback=outage_callback, callback_params={ 'device_name': 'MyMachine1', 'ping_times': ping_times }) ping_back_detector = unix1.get_event(event_name="ping_response") ping_back_detector.add_event_occurred_callback( callback=ping_back_detector_callback, callback_params={'ping_times': ping_times}) ifconfig_down = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo down"}) put_interfaces_down = unix2.get_cmd(cmd_name="sudo", cmd_params={ "password": "******", "cmd_object": ifconfig_down }) ifconfig_up = unix2.get_cmd(cmd_name="ifconfig", cmd_params={"options": "lo up"}) put_interfaces_up = unix2.get_cmd(cmd_name="sudo", cmd_params={ "password": "******", "cmd_object": ifconfig_up }) ping = unix1.get_cmd(cmd_name="ping", cmd_params={ "destination": "localhost", "options": "-O" }) # fire ifconfig up command - that one is part of Test Setup logic ensure_interfaces_up() try: ping_lost_detector.start( ) # oh, yes - that is still setup, but we want it here to be cancelled by finally ping_back_detector.start() # run test ping.start(timeout=120) time.sleep(3) 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.")