Scripts on My Tool Belt - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Scripts on My Tool Belt

Description:

ftp ls. 200 PORT command successful. 150 Opening ASCII mode data connection ... Some old Software QA magazine articles at www.tejasconsulting.com (use at your ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 26
Provided by: dannyf
Category:
Tags: belt | ls | magazine | scripts | tool

less

Transcript and Presenter's Notes

Title: Scripts on My Tool Belt


1
Scripts on My Tool Belt
  • Danny R. Faught
  • Tejas Software Consulting
  • www.tejasconsulting.com
  • Software Test Automation Conference
  • Fall 2001

2
My aim for this talk
  • Convince you that test automation is more than
    automating test execution
  • Show some examples of the kinds of things Ive
    accomplished with scripting languages, using
    simplified code samples
  • Make you aware of three different scripting
    languages (shells, perl, and expect)

3
Why scripting?
  • Scripting languages are easier to learn than
    compiled languages.
  • Scripts get more done in less code and less
    coding time than compiled languages.
  • This can make the difference between using
    automation and not trying at all.

4
Part 1 Shell Scripting
  • Shell scripts allow you to automate system
    commands, and gives you some rudimentary control
    logic (variables, looping, etc.).
  • Shells were developed for Unix, but are also
    available on Windows and other platforms.
  • There are several shells to choose from your
    login shell may not be the best choice.
  • For our examples, well use the bash shell.

5
Shell script example simplemon
  • A simple script to monitor the basic health of a
    system over the network
  • Designed to run on Windows in the cygwin
    environment
  • These commands are external to bash ping, grep,
    echo, rm, sleep, even true and test
  • Uses the open source Blat program to send email

6
simplemon
  • 1 !/bin/bash
  • 2 target127.0.0.1
  • 3 notifymyaddr_at_myhost.com
  • 4 while true
  • 5 do
  • 6 ping -n 1 target simplemon.out 21
  • 7 grep -q "Reply from" simplemon.out
  • 8 ping_status?
  • 9 if test ping_status ! 0
  • 10 then
  • 11 echo PING STATUS ping_status
    simplemon.out
  • 12 blat simplemon.out -s "simplemon
    status" -t "notify"
  • 13 rm simplemon.out
  • 14 exit 1
  • 15 fi
  • 16 sleep 30
  • 17 done

7
Shell script portability
  • What if you wanted to run on Unix as well?
  • Shell script portability is difficult because the
    real work is spread across many external
    programs.
  • To make the script portable, we use uname to
    check the OS, and add functions to do the ping
    and to send mail.

8
Portable mail function
  • 1 osuname -s
  • 2 ...
  • 3 function send_mail
  • 4 file1
  • 5 subject2
  • 6 to3
  • 7 if test "os" "CYGWIN_NT-4.0"
  • 8 then
  • 9 blat file -s "subject" -t "to"
  • 10 else
  • 11 mailx -s "subject" "to"
  • 12 fi
  • 13

9
Portable ping function
  • 1 function one_ping
  • 2 host1
  • 3 if test "os" CYGWIN_NT-4.0
  • 4 then
  • 5 ping -n 1 host ping.out
  • 6 cat ping.out
  • 7 grep -q "Reply from" ping.out
  • 8 status?
  • 9 rm ping.out
  • 10 test status 0
  • 11 elif test "os" HP-UX
  • 12 then
  • 13 outputping host 64 1
  • 14 echo output
  • 15 echo output grep -q " 0 packet
    loss"
  • 16 else ...

10
Part 2 Perl
  • Perl is a general-purpose scripting language that
    runs on a very wide variety of platforms.
  • Youll either love its power, or hate its
    complexity.

11
Test dataa perl one-liner
  • perl -e 'print \0" x 1000000' file
  • Quickly generates a file of a million null
    characters.
  • Very useful as an input file, and likely to break
    any software that tries to read it.
  • Its very easy to recreate the file with
    different contents or a different size to isolate
    a failure.
  • (this example uses bash command line syntax)

12
Another one-liner
  • perl -e print ((A" x 72 . \n)
  • x 10000)' file
  • Uses a different character and a more reasonable
    line size.
  • This example creates a file with 10,000 lines,
    each of which consists of a string of 72 A
    characters.

13
The Sandwich Model
  • Scripts may be used at all three levels.
  • The bottom and top of the sandwich often uses
    customized code, when its difficult to add
    features to the general-purpose harness

14
Front end for a commercial tool
  • Say were using the eValid tool for web testing.
    Errors show up in the log like this (this is a
    single long line)
  • 2001/06/25 154949 6 3 15 Project Group Test2
    ERROR 3035 441 - Document not found
    http//tejasconsulting.com/badlink 404
  • We want a front end that can run an eValid test,
    report back whether it failed, and summarize the
    failure.
  • With further enhancement, it should run large
    sets of tests and summarize the results.

15
evrun script
  • 1 !/usr/bin/perl -w
  • 2 testname shift _at_ARGV or die "must specify
    test name\n"
  • 3 system "eValid -B testname.evs -CK"
  • 4 status ?
  • 5 path "c/Program Files/Software
    Research/eValid/Program/Project/Group/"
  • 6 open LOG, "path/testname-M.log" or die
    "open !"
  • 7 my err 0
  • (continued)

16
evrun, continued
  • 8 while ()
  • 9 if (/ERROR/ ! /Command completed/)
  • 10 s/.ERROR\s\d\s\d\s\S\s(.)/1/
  • 11 print
  • 12 err
  • 13
  • 14
  • 15 close LOG
  • 16 print "eValid status status\n"
  • 17 print "Error count err\n"
  • 18 exit err

17
evrun output
  • C\evevrun Test2
  • Document not found http//tejasconsulting.com/bad
    link 404
  • eValid status 16384
  • Error count 1
  • Note there are other ways that the test can
    fail, such as a timeout. Further analysis of the
    possible log file entries would be needed to make
    this script reliable.

18
Part 3 expect
  • Expect is a scripting language for automating
    interactive command-line interfaces.
  • Based on tcl (tool command language)
  • Also the basis for the freeware test harness
    DejaGnu

19
Examplethe crypt command
  • You dont need expect for simple commands just
    write to standard input.
  • Commands like crypt and passwd on Unix,
    however, read directly from the terminal.
  • Expect provides a pseudo-terminal that allows you
    to automate these commands.

20
crypt.exp
  • 1 !/usr/bin/expect -f
  • 2 spawn -nottycopy /bin/sh -c "crypt crypt.out"
  • 3 expect
  • 4 timeout send_error "TIMEOUT!\n" exit
    -1
  • 5 eof send_error "EOF!\n" exit -1
  • 6 "Enter key"
  • 7
  • 8 send "this_is_the_key\r"
  • 9 expect
  • 10 timeout send_error "TIMEOUT!\n" exit
    -1
  • 11 eof
  • 12
  • 13 set status wait
  • 14 exit lindex status 3

21
Windows example ftp
  • 1 !/usr/bin/expect -f
  • 2 expect_before
  • 3 timeout send_error "TIMEOUT!\n" exit -1
  • 4
  • 5 spawn c/winnt2/system32/ftp.exe myftphost.com
  • 6 expect User
  • 7 expect ""
  • 8 send faught\r"
  • 9 expect "Password"
  • 10 send pw\r"
  • 11 expect "ftp"
  • 12 send "ls\r"
  • 13 expect "ftp"
  • 14 send "quit\r"
  • 15 expect "Goodbye."

22
Output from ftp script
  • c\ ftp.exp
  • spawn c/winnt2/system32/ftp.exe myftphost.com
  • Connected to myftphost.com.
  • 220 myftphost.com FTP server (Version 1.7.109.12)
    ready.
  • User (myftphost(none)) faught
  • 331 Password required for faught.
  • Password
  • 230 User faught logged in.
  • ftp ls
  • 200 PORT command successful.
  • 150 Opening ASCII mode data connection for file
    list.
  • file1
  • file2
  • 226 Transfer complete.
  • 848 bytes received in 0.33 seconds (2.56
    Kbytes/sec)
  • ftp quit
  • 221 Goodbye.

23
Web Resources
  • Cygwin www.cygwin.com (for bash install)
  • Bash cnswww.cns.cwru.edu/chet/bash/bashtop.html
  • Blat http//pages.infinit.net/che/blat/blat.html
  • Perl www.perl.com (and many others)
  • Expect http//expect.nist.gov/
  • Windows NT port of expect http//bmrc.berkeley.ed
    u/people/chaffee/expectnt.html

24
Books and Articles
  • Learning the bash Shell, 2nd Edition, ISBN
    1-56592-347-2
  • Programming Perl, 3rd Edition, ISBN 0-596-00027-8
    (and others from OReilly)
  • Exploring Expect, ISBN 1-56592-090-2
  • Some old Software QA magazine articles at
    www.tejasconsulting.com (use at your own risk)
    The Shell Game, Testing Interactive Programs,
    and Using Perl Scripts
  • Hey Vendors, Give Us Real Scripting Languages,
    by Bret Pettichord, posted on StickyMinds.com

25
Wrap-up
  • Some knowledge of at least one scripting language
    can make a tester much more productive!
  • Most scripting languages were designed for Unix,
    but can be made to work under Windows and other
    systems.
  • For annotated versions of the scripts in this
    presentation, see www.tejasconsulting.com/resourc
    es/toolbelt/
  • Thanks for listening!-Danny (faught_at_tejasconsulti
    ng.com)
Write a Comment
User Comments (0)
About PowerShow.com