Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: using Test::* modules for generic testing of non perl stuff?

by tstock (Curate)
on Oct 16, 2004 at 15:47 UTC ( [id://399772]=note: print w/replies, xml ) Need Help??


in reply to using Test::* modules for generic testing of non perl stuff?

I use the perl Test framework to test lots-a-things. These are all crude test examples below -
# check webservice like( `lynx http://tiago.com -head -dump`, qr/200 OK/, 'HEAD request using lynx' ); # disk space for (`df`) { my ($filesystem, $use) = /^(\S+) .+ (\d+)%/; next unless ($filesystem && $use); ok($use < 70, "$use \%filesystem"); } # running daemons my @ps = `ps -e`; for my $daemon (qw(crond syslogd ntpd)) { my $cnt = grep /$daemon/, @ps; ok($cnt, "$daemon ($cnt)"); } # software version like( `nmap --version`, qr/\W3\.\d/, 'nmap version test' ); # binaries and path for my $bin ('ping','kill','pod2text', 'nslookup', 'tail') { my $loc = `which $bin`; chomp $loc; ok(stat($loc), $loc); ok(-x _, 'executable by effective uid/gid'); } # ping the machine $ip = '66.39.54.27'; my $ret = join('', `ping -c 5 -W 1 $ip`); like($ret, qr/ 0\% packet loss/, 'packet loss'); + # DNS $ip = '66.39.54.27'; $name = 'perlmonks.org'; like(`host $ip`, qr/$name/, 'DNS'); like(`host $name`, qr/$ip/, 'reverse DNS');
It's also not hard to make other languages output in the format usable by Test::Harness, so incidently, if anyone care to make a shell library of functions that mimic the Test::More module, drop me a note, I'm interested.

Tiago

Replies are listed 'Best First'.
Re^2: using Test::* modules for generic testing of non perl stuff?
by linux454 (Pilgrim) on Oct 19, 2004 at 15:28 UTC
    Something I whipped up just this morning. It should work with Test::Harness, it may be missing some of the Test::More methods, let me know if I've mised the mark or something else needs to be added.
    #!/bin/bash # This is a shell library for interfacing with Perl's Test::Harness fr +amework # Copyright 2004. Michael Grubb SHOULD_PRINT_PLAN=0 TEST_PLAN=0 TEST_COUNTER=0 MATCH_PROG=/bin/egrep MATCH_ARGS="-q" plan() { if [ "${1}" == "no_plan" ] ; then SHOULD_PRINT_PLAN=1 TEST_PLAN=0 else TEST_PLAN="${1}" SHOULD_PRINT_PLAN=1 _printPlan SHOULD_PRINT_PLAN=0 fi } endtests() { _printPlan } ok() { _updatePlan if [ $1 != 0 ]; then echo -n "not " fi echo "ok ${TEST_COUNTER} ${2} # \$1 is '$1'" } is() { _updatePlan if [ $1 != ${2} ] ; then echo -n "not " fi echo "ok ${TEST_COUNTER} ${3} # expecting '$1' got '$2'" } match() { _updatePlan echo "$2" | ${MATCH_PROG} ${MATCH_ARGS} ${1} rv=$? if [ $rv -ne 0 ] ; then echo -n "not " fi echo -n "ok ${TEST_COUNTER} ${3} " if [ $rv -ne 0 ] ; then echo "# '$2' did not match '$1'" else echo fi } diag() { echo "# $1" 1>&2 } _updatePlan() { TEST_COUNTER=$((${TEST_COUNTER}+1)) if [ ${SHOULD_PRINT_PLAN} ] ; then TEST_PLAN=${TEST_COUNTER} ; fi } _printPlan() { if [ ${SHOULD_PRINT_PLAN} -eq 1 ] ; then echo "1..${TEST_PLAN}" fi }

    And here is a script that shows it's usage:

    #!/bin/bash . ./Test.sh plan "no_plan" diag "# TEST_PLAN=$TEST_PLAN" ok 0 "Test 1" ok 0 "Test 2" ok 2 "Test 3" is "this" "this" is "this" "that" is 1 0 is 0 1 is 1 1 match "^ab[cd]efg$" "abcefg" match "^ab[cd]efg$" "abdefg" match "^ab[cd]efg$" "abcdefg" match "^ab[cd]efg$" "abhefg" endtests # only needed when "no_plan" is used
    Hope this helps. Oh, and by default the "match" function uses egrep -q but you can override that if you set MATCH_PROG and MATCH_ARGS after you source Test.sh. Let me know if this is useful. Also note that getting Test::Harness to run this is an excercise left to the reader.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://399772]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-23 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found