Skip to content

LNST Task API

jtluka edited this page Aug 27, 2013 · 35 revisions

After some time of writing LNST tasks in XML you probably keep repeating:

"XML is such a bad language to write sequence of commands, it's so limited. This should be done differently!!"

And you're quite right. We got tired of this, too. Therefore we've added a new feature that allows you to use your own python program that drives the test execution with ability to access all of the LNST infrastructure - machine properties, command execution, template functions, background execution - from the program. Besides the LNST infrastructure you can benefit from the Python language facilities as loops, conditional executions and whatever Python provides in it's library.

Note that the machine requirements description still needs to be provided as an XML code in the recipe.

Usage in LNST recipe

Let's assume that your Python program you're using for the task execution is called task_check_ping.py. To include it in the recipe use following code:

<lnstrecipe>
    <machines>
        <machine id="1">
          <interfaces>
            <eth network="ttnet" id="testiface"/>
              <addresses>
                <address value="192.168.100.240/24"/>
              </addresses>
            </eth>
          </interfaces>
        </machine>
        <machine id="2">
          <interfaces>
            <eth network="ttnet" id="testiface" type="eth">
              <addresses>
                <address value="192.168.100.215/24"/>
              </addresses>
            </eth>
          </interfaces>
        </machine>
    </machines> 

    <!-- This is it! -->
    <task python="task_check_ping.py"/>
</lnstrecipe>

Content of the task_check_ping

    # Mandatory import, the ctl handle contains the API
    from lnst.Controller.Task import ctl

    # Get handles for the machines
    m1 = ctl.get_machine("1")
    m2 = ctl.get_machine("2")

    # Set a config option (persistent=True)
    m1.config("/proc/sys/net/ipv4/conf/all/forwarding", "0", True)

    # run a shell command
    devname = m2.get_devname("testiface")
    m1.run("echo %s" % devname, timeout=30)

    # prepare a module for execution
    ping_mod = ctl.get_module("IcmpPing", addr=m2.get_ip("testiface", 0),
                              count=40, interval=0.2, limit_rate=95)

    # run the module twice on machine one
    ping_test = m1.run(ping_mod, timeout=30)
    ping_test = m1.run(ping_mod, bg=True, timeout=30)

    # make the controller wait
    ctl.wait(5)

    # interrupt the process
    ping_test.intr()

Clone this wiki locally