Beispiel #1
0
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()
Beispiel #2
0
def get_device(name, connection, device_output, test_file_path):
    dir_path = os.path.dirname(os.path.realpath(test_file_path))
    load_config(
        os.path.join(dir_path, os.pardir, os.pardir, 'test', 'resources',
                     'device_config.yml'))
    device = DeviceFactory.get_device(name, io_connection=connection)
    _prepare_device(device=device,
                    connection=connection,
                    device_output=device_output)
    return device
Beispiel #3
0
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)
Beispiel #4
0
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)
Beispiel #5
0
def get_device(name, connection, device_output, test_file_path):
    dir_path = os.path.dirname(os.path.realpath(test_file_path))
    load_config(
        os.path.join(dir_path, os.pardir, os.pardir, 'test', 'resources',
                     'device_config.yml'))

    device = DeviceFactory.get_device(name)
    device.io_connection = connection
    device.io_connection.name = device.name
    device.io_connection.moler_connection.name = device.name

    device.io_connection.remote_inject_response(device_output)
    device.io_connection.set_device(device)

    return device
Beispiel #6
0
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()
Beispiel #7
0
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()
Beispiel #8
0
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)

Beispiel #9
0
    for client_thread in jobs_on_connections:
        client_thread.join()
    logger.debug('all jobs observing connections are done')


# ==============================================================================
if __name__ == '__main__':
    from threaded_ping_server import start_ping_servers, stop_ping_servers
    from asyncio_common import configure_logging
    import os
    from moler.config import load_config
    # -------------------------------------------------------------------
    # Configure moler connections (backend code)
    # 1) configure variant by YAML config file
    # 2) ver.2 - configure named connections by YAML config file
    load_config(config=os.path.join(os.path.dirname(__file__), "..",
                                    "named_connections.yml"))

    # 3) take default class used to realize tcp-threaded-connection
    # -------------------------------------------------------------------

    configure_logging()

    net_1 = ('localhost', 5671)
    net_2 = ('localhost', 5672)
    connections2serve = [(net_1, '10.0.2.15'), (net_2, '10.0.2.16')]
    connections2observe4ip = [('net_1', '10.0.2.15'), ('net_2', '10.0.2.16')]
    servers = start_ping_servers(connections2serve)
    try:
        main(connections2observe4ip)
    finally:
        stop_ping_servers(servers)
Beispiel #10
0
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:
"""
        # using as synchronous function (so we want verb to express action)
        detect_network_up = net_up_detector
        net_up_time = detect_network_up()
        timestamp = time.strftime("%H:%M:%S", time.localtime(net_up_time))
        logger.debug('Network {} is back "up" from {}'.format(ping_ip, timestamp))


# ==============================================================================
if __name__ == '__main__':
    import os
    from moler.config import load_config
    from moler.config import connections as conn_cfg
    # -------------------------------------------------------------------
    # Configure moler connections (backend code)
    # 1) select variant of TCP
    load_config(path=os.path.join(os.path.dirname(__file__), "connection_variant.yml"))

    # 2) ver.1 - configure named connections by python code
    conn_cfg.define_connection(name='net_1', io_type='tcp', host='localhost', port=5671)
    conn_cfg.define_connection(name='net_2', io_type='tcp', host='localhost', port=5672)

    # 3) take default class used to realize tcp-threaded-connection
    # -------------------------------------------------------------------

    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s |%(name)-40s |%(message)s',
        datefmt='%H:%M:%S',
        stream=sys.stderr,
    )
    connections2observe4ip = [(('localhost', 5671), 'net_1', '10.0.2.15'),
Beispiel #12
0
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
"""
Beispiel #13
0
from moler.config import load_config
from moler.device.device import DeviceFactory

load_config(path='my_devices.yml')

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"})
ls_cmd.connection.newline = '\r\n'           # tweak since rebex remote console uses such one

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:
  permissions       : -rw-------
  hard_links_count  : 1
  owner             : demo
  group             : users
  size_raw          : 403
  size_bytes        : 403
  date              : Apr 08  2014
  name              : readme.txt
"""
Beispiel #14
0
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.")
Beispiel #15
0
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'))  # description of available devices
# load_config(config={'DEVICES': {'MyMachine': {'DEVICE_CLASS': 'moler.device.unixlocal.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
"""
        logger.debug('observe ' + info)
        # using as synchronous function (so we want verb to express action)
        detect_network_up = net_up_detector
        net_up_time = detect_network_up()
        timestamp = time.strftime("%H:%M:%S", time.localtime(net_up_time))
        logger.debug('Network {} is back "up" from {}'.format(ping_ip, timestamp))


# ==============================================================================
if __name__ == '__main__':
    import os
    from moler.config import load_config
    # -------------------------------------------------------------------
    # Configure moler connections (backend code)
    # ver.3 - configure by YAML config file
    load_config(config=os.path.join(os.path.dirname(__file__), "connections_new_variant.yml"))

    # configure class used to realize tcp-threaded-connection
    # (default one tcp.ThreadedTcp has no logger)
    # This constitutes plugin system - you can exchange connection implementation

    def tcp_thd_conn(port, host='localhost', name=None):
        moler_conn = ObservableConnection(decoder=lambda data: data.decode("utf-8"))
        conn_logger_name = 'threaded.tcp-connection({}:{})'.format(host, port)
        conn_logger = logging.getLogger(conn_logger_name)
        io_conn = tcp.ThreadedTcp(moler_connection=moler_conn,
                                  port=port, host=host, logger=conn_logger)
        return io_conn

    ConnectionFactory.register_construction(io_type="tcp",
                                            variant="threaded-and-logged",
Beispiel #17
0
# configure library directly from dict
load_config(config={
    'DEVICES': {
        'DEFAULT_CONNECTION': {
            'CONNECTION_DESC': {
                'io_type': 'terminal',
                'variant': 'threaded'
            }
        },
        'RebexTestMachine': {
            'DEVICE_CLASS': 'moler.device.unixremote.UnixRemote',
            'CONNECTION_HOPS': {
                'UNIX_LOCAL': {
                    'UNIX_REMOTE': {
                        'execute_command': 'ssh',
                        'command_params': {
                            'expected_prompt': 'demo@',
                            'host': 'test.rebex.net',
                            'login': '******',
                            'password': '******',
                            'set_timeout': None
                        }
                    }
                }
            }
        }
    }
},
            config_type='dict')

remote_unix = DeviceFactory.get_device(
Beispiel #18
0
from moler.config import load_config
from moler.device.device import DeviceFactory

load_config(config='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'],