Improvements to the EPP Smoke Test

by on May 17, 2013 in Technology
Technology Image

Introduction

In an earlier post, I’d created a short script to test that EPP nodes were working. The script was painfully simple, and provided a file whose contents was date-stamped with the date/time that the script ran, and the file was captured in the normal manner, and displayed in the genealogy.

After receiving feedback from customers, I was happy to modify the script to make it quite a bit more useful. The goal of the script was twofold:

  1. Confirm that the EPP node is working
  2. Display the command line argument name/value pairs that were passed to the script

 

The Code

import string
import getopt
import sys
import datetime

BASE_DIR = ""

def writeFile( fName, lstLines ):

global BASE_DIR
fOut = open( BASE_DIR + fName, "w" )
fOut.writelines( lstLines )
fOut.close()

def main():

outFile = ""
lines = []
opts = []
extraargs = []

args = sys.argv[1:]

# this script is set up to look for a -o, anything else must be
# handled though. The easiest way to do this is to remove the –
# signs from the non -o options

for loop in range( 0, len(args) ):
if args[loop].startswith('-') and args[loop] != '-o':
args[loop] = string.replace( args[loop], '-', '' )

try:
opts, extraargs = getopt.gnu_getopt( args, "o:" )
except getopt.GetoptError, err:
print "whoops"

for o,p in opts:
lines.append( "option: " + o + " value: " + p + "\n" )
if o == '-o':
outFile = p

for loop in range( 0, len(extraargs)-1, 2 ):
lines.append( "option: -" + extraargs[loop] + " value: " + extraargs[loop+1] + "\n" )

if len(outFile) > 0:

lines.append( "It worked\n" )
lines.append( str( datetime.datetime.now() ) )

writeFile( outFile, lines )

if __name__ == "__main__":
    main()

 

Discussion

This script uses the getopt Python module to handle the command line arguments passed to it. However, the purpose of this module has been somewhat subverted in order to make the script as generic as possible. For example, the getopt module is very strict about which arguments or flags it will respond to, and is designed to throw exceptions if you deviate from the lexicon defined. As such, using it to echo back any and all arguments, including undefined ones involves the following small hack:

    for loop in range( 0, len(args) ):
       if args[loop].startswith('-') and args[loop] != '-o':
           args[loop] = string.replace( args[loop], '-', '' )

In essence the only argument defined is the -o option. Any other options have the minus sign removed from them, and this stops getopt throwing exceptions. The minus signs are then introduced when we display the name/value pairs:

  lines.append( "option: -" + extraargs[loop] + " value: " + extraargs[loop+1] + "\n" )

 

Usage

Assuming the above script is called epp_test.py, invoking it in the following manner:

    /usr/bin/python epp_test.py -o output.txt -a 90125 -U http://www.bbc.co.uk/news 

Will return a file with contents similar to:

option: -o value: output.txt
option: -a value: 90125
option: -U value: http://www.bbc.co.uk/news

It worked

 

 

Share