Category Archives: python

Getting Started with STAF

I’ve used STAF in a several engagements where I’m tasked with building a distributed testing framework. STAF will give you a platform that you can build on to top off. Its one of those kitchen sink frameworks that takes a while to get ramped up on, and has more than you ever use. I find myself using a small percentage of its tools, wrappering them in Python, and ignoring most of its fancy features. Extensive documentation can be found here http://staf.sourceforge.net/docs.php
I’ve found that it takes a while for people to get started, so this article give you some simple examples to get started and get something working. I’m going to ignore all the fancy features, you can read about them in your infinite free time and figure out how to integrate them into your applications at your leisure. We’ll start off with getting a simple example script running and then using integrating it with Pythons unittest module.

STAF comes with a core that is for the most part implemented in C++. Several services are provided, that come as part of the default install or can be added on later, these might be implemented in Java, C, C++ or Perl, Python.
http://staf.sourceforge.net/current/STAFCMDS.htm
http://staf.sourceforge.net/current/stafsdg.html

I’m going to ignore all but two of these, the FS and Process services. The process service will allow me to kick off program on remote machines, and get the output. The FS service will allow me to upload files to those hosts.
Once you have a basic set up with STAF running on a number of hosts, you can then kick off tests on several hosts at the same time,  collect logs and clean up. For the most part I’m only interested in leveraging STAF’s ability to provide facilities for executing remote procedures, with maybe the occasional call to the FS service to move some small number of files around. Because. The programs I’m writing may also be interacting with VMWare Vcenter, virtual machines, and a database. Its been  sufficient to have everything written as Python libraries that are imported by the test cases that my users have set up.

To get started install the latest version of STAF. I’m going to assume that you are running on some sort of Linux system, as thats what my examples are written on. In a nutshell the install process is download the installation package for your architecture and operating system. Then run the installer – on Windows, double click it, on Unix do something like `sudo sh STAF345-setup-linux.bin’. Accept all the defaults and that should be it.
http://staf.sourceforge.net/current/STAFInstall.pdf

Once its installed, the only thing that I will change is to modify the configuration file /usr/local/staf/bin/STAF.cfg or C:\STAF\bin\STAF.cfg:
Modify the line

# Set default local trust
trust machine local://local level 5

To be

# Set default local trust
trust default level 5

From a security point of view this leaves your test system wide open with the most promiscuous setting, but it will also allow you to run programs remotely from a different host. If you are on a closed test network, this should not be a problem, as always consult the extensive documentation at

http://staf.sourceforge.net/docs.php

At this point restart your staf service:

/usr/local/staf/startSTAFProc.sh

If you are on Windows you can find the appropriate start/stop combination under START menu. On Unix you will find that the command returned , created a file called nohup.out, but probably did not start. You will need to set the environment for STAF to find its libraries

source  /usr/local/staf/STAFEnv.sh
/usr/local/staf/startSTAFProc.sh

See this link for setting scripts up /etc/init.d to start and stop STAF at boot and shutdown.
http://www.mail-archive.com/staf-users@lists.sourceforge.net/msg01066.html

you should be good to go. You can verify that its running by doing:

STAF <host ip address> ping ping

steve@herat:~/projects/staf$ staf  localhost ping ping
Response
--------
PONG

This will verify that its up and running

staf localhost PROCESS START SHELL COMMAND 'ls -l /' WAIT RETURNSTDOUT STDERRTOSTDOUT

or

staf localhost PROCESS START SHELL COMMAND 'dir C:\' WAIT RETURNSTDOUT STDERRTOSTDOUT

This will verify that you can run remote commands on the system

steve@herat:~/projects/staf$ staf localhost PROCESS START SHELL COMMAND 'ls -l /' WAIT RETURNSTDOUT STDERRTOSTDOUT
Response
--------
{
  Return Code: 0
  Key        : <None>
  Files      : [
    {
      Return Code: 0
      Data       : total 96
drwxr-xr-x   2 root root  4096 2010-11-08 09:27 bin
drwxr-xr-x   3 root root  4096 2010-11-08 10:10 boot
drwxr-xr-x   2 root root  4096 2010-11-03 11:46 cdrom
...

At this point you should have a system with STAF installed and configured.
I’ll continue using localhost to illustrate the commands, but it should
be easy to see that you can change all the instances of `localhost’ to the host
where you want to run the commands.

If you look at the documentation there is a source code example that has many interesting features
that at some point you will get to:
http://staf.sourceforge.net/current/STAFPython.htm#Header_Examples

I found myself wading through this example of what is really a comprehensive unit test. If you are like
me with a very short deadline (usually missed) and need to run tests that kick of programs on a remote host
then then you could do something like:

from PySTAF import *
import sys

def createHandle():
    handle = None
    try:
        handle = STAFHandle('foo')
    except STAFException, e:
        print "Error registering with STAF, RC: %d, Result: %s" % (e.rc, e.result)
        sys.exit(e.rc)
    return handle

def  runCommand(handle, host, comm):
    command = "START SHELL COMMAND %s  WAIT RETURNSTDOUT STDERRTOSTDOUT " % (comm)
    result = handle.submit(host, 'PROCESS', command)
    if (result.rc != STAFResult.Ok):
        print "Error on ping request."
    mc = unmarshall(result.result)
    ro = mc.getRootObject()
    data = ro['fileList'][0]['data']
    print "Received RC: %d, Result: %s" % (result.rc, data)

if __name__ == '__main__':
    handle = createHandle()
    runCommand(handle, 'localhost', 'ls -l /')
    runCommand(handle, 'localhost', 'netstat -a')
    runCommand(handle, 'localhost', 'ifconfig')

This bare bones example will run the commands on the localhost and print the output. Not
very sophisticated, but enough to get you started.

You may need to modify your environment to run it:

 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/staf/lib
 export PYTHONPATH=$PYTHONPATH:/usr/local/staf/lib

There is much you can accomplish with this example.
To make it a little more useful, you can leverage the python unit test tools. You may need to install
these separately onto your system.

from PySTAF import *
import unittest
import sys

class SimpleTest(unittest.TestCase):
    def setUp(self):
        self.createHandle()

    def createHandle(self):
        handle = None
        try:
            handle = STAFHandle('foo')
        except STAFException, e:
            print "Error registering with STAF, RC: %d, Result: %s" % (e.rc, e.result)
            sys.exit(e.rc)
        self.handle = handle

    def  runCommand(self, host, comm):
        command = "START SHELL COMMAND %s  WAIT RETURNSTDOUT STDERRTOSTDOUT " % (comm)
        result = self.handle.submit(host, 'PROCESS', command)
        if (result.rc != STAFResult.Ok):
            print "Error on ping request."
        mc = unmarshall(result.result)
        ro = mc.getRootObject()
        data = ro['fileList'][0]['data']
        return data

    def testLs1(self):
        out = self.runCommand('localhost', 'ls  /vmlinuz')
        self.assertEqual(out, '/vmlinuz\n')

    def testLs2s(self):
        out = self.runCommand('localhost', 'ls  /initrd.img')
        self.assertEqual(out, '/initrd.img\n')

if __name__ == '__main__':
    unittest.main()

This is just a rewrite of the first script. I modified it to be object oriented
as test suite requires that you create a class, with each test case defined as a
method in that class. The setup method is run before the other methods and we create the
STAF handle that will be used by all the test cases.
The details on the test suite module can be found here:
http://docs.python.org/library/unittest.html

More to follow …