Skip to content

Commit 0294f5e

Browse files
committed
Add options to run commands before or after tests
1 parent 53987e3 commit 0294f5e

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

util/vm/build.py

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def checkOutBranch( vm, branch, prompt=Prompt ):
512512
vm.sendline( 'sudo make install' )
513513

514514

515-
def interact( vm, prompt=Prompt ):
515+
def interact( vm, tests, pre='', post='', prompt=Prompt ):
516516
"Interact with vm, which is a pexpect object"
517517
login( vm )
518518
log( '* Waiting for login...' )
@@ -539,7 +539,7 @@ def interact( vm, prompt=Prompt ):
539539
vm.expect( prompt )
540540
log( '* Mininet version: ', version )
541541
log( '* Testing Mininet' )
542-
runTests( vm )
542+
runTests( vm, tests=tests, pre=pre, post=post )
543543
log( '* Shutting down' )
544544
vm.sendline( 'sync; sudo shutdown -h now' )
545545
log( '* Waiting for EOF/shutdown' )
@@ -675,8 +675,12 @@ def qcow2size( qcow2 ):
675675
return bytes
676676

677677

678-
def build( flavor='raring32server' ):
679-
"Build a Mininet VM; return vmdk and vdisk size"
678+
def build( flavor='raring32server', tests=None, pre='', post='' ):
679+
"""Build a Mininet VM; return vmdk and vdisk size
680+
tests: tests to run
681+
pre: command line to run in VM before tests
682+
post: command line to run in VM after tests
683+
prompt: shell prompt (default '$ ')"""
680684
global LogFile, Zip
681685
start = time()
682686
date = strftime( '%y%m%d-%H-%M-%S', localtime())
@@ -700,7 +704,7 @@ def build( flavor='raring32server' ):
700704
logfile = open( flavor + '.log', 'w+' )
701705
log( '* Logging results to', abspath( logfile.name ) )
702706
vm = boot( volume, kernel, initrd, logfile )
703-
version = interact( vm )
707+
version = interact( vm, tests=tests, pre=pre, post=post )
704708
size = qcow2size( volume )
705709
vmdk = convert( volume, basename='mininet-vm-' + archFor( flavor ) )
706710
if not SaveQCOW2:
@@ -721,20 +725,28 @@ def build( flavor='raring32server' ):
721725
os.chdir( '..' )
722726

723727

724-
def runTests( vm, tests=None, prompt=Prompt ):
728+
def runTests( vm, tests=None, pre='', post='', prompt=Prompt ):
725729
"Run tests (list) in vm (pexpect object)"
726-
print 'TESTS = ', tests
727730
if not tests:
728-
tests = [ 'sanity', 'core' ]
731+
tests = []
732+
if pre:
733+
log( '* Running command', pre )
734+
vm.sendline( pre )
735+
vm.expect( prompt )
729736
testfns = testDict()
737+
if tests:
738+
log( '* Running tests' )
730739
for test in tests:
731740
if test not in testfns:
732741
raise Exception( 'Unknown test: ' + test )
733742
log( '* Running test', test )
734743
fn = testfns[ test ]
735744
fn( vm )
736745
vm.expect( prompt )
737-
746+
if post:
747+
log( '* Running post-test command', post )
748+
vm.sendline( post )
749+
vm.expect( prompt )
738750

739751
def getMininetVersion( vm ):
740752
"Run mn to find Mininet version in VM"
@@ -745,9 +757,12 @@ def getMininetVersion( vm ):
745757
return version
746758

747759

748-
def bootAndRunTests( image, tests=None, prompt=Prompt ):
760+
def bootAndRunTests( image, tests=None, pre='', post='', prompt=Prompt ):
749761
"""Boot and test VM
750-
tests: list of tests (default: sanity, core)"""
762+
tests: list of tests to run
763+
pre: command line to run in VM before tests
764+
post: command line to run in VM after tests
765+
prompt: shell prompt (default '$ ')"""
751766
bootTestStart = time()
752767
basename = path.basename( image )
753768
image = abspath( image )
@@ -771,8 +786,7 @@ def bootAndRunTests( image, tests=None, prompt=Prompt ):
771786
if Branch:
772787
checkOutBranch( vm, branch=Branch )
773788
vm.expect( prompt )
774-
log( '* Running tests' )
775-
runTests( vm, tests=tests )
789+
runTests( vm, tests=tests, pre=pre, post=post )
776790
# runTests eats its last prompt, but maybe it shouldn't...
777791
log( '* Shutting down' )
778792
vm.sendline( 'sudo shutdown -h now ' )
@@ -827,15 +841,19 @@ def parseArgs():
827841
action='append',
828842
help='Boot and test an existing VM image' )
829843
parser.add_argument( '-t', '--test', metavar='test', default=[],
830-
action='append',
831-
help='specify a test to run' )
844+
action='append',
845+
help='specify a test to run' )
846+
parser.add_argument( '-r', '--run', metavar='cmd', default='',
847+
help='specify a command line to run before tests' )
848+
parser.add_argument( '-p', '--post', metavar='cmd', default='',
849+
help='specify a command line to run after tests' )
832850
parser.add_argument( '-b', '--branch', metavar='branch',
833851
help='For an existing VM image, check out and install'
834852
' this branch before testing' )
835853
parser.add_argument( 'flavor', nargs='*',
836854
help='VM flavor(s) to build (e.g. raring32server)' )
837855
parser.add_argument( '-z', '--zip', action='store_true',
838-
help='archive .ovf and .vmdk into .zip file' )
856+
help='archive .ovf and .vmdk into .zip file' )
839857
args = parser.parse_args()
840858
if args.depend:
841859
depend()
@@ -851,18 +869,21 @@ def parseArgs():
851869
Branch = args.branch
852870
if args.zip:
853871
Zip = True
872+
if not args.test and not args.run and not args.after:
873+
args.test = [ 'sanity', 'core' ]
854874
for flavor in args.flavor:
855875
if flavor not in isoURLs:
856876
print "Unknown build flavor:", flavor
857877
print buildFlavorString()
858878
break
859879
try:
860-
build( flavor )
880+
build( flavor, tests=args.test, pre=args.run, post=args.after )
861881
except Exception as e:
862882
log( '* BUILD FAILED with exception: ', e )
863883
exit( 1 )
864884
for image in args.image:
865-
bootAndRunTests( image, tests=args.test )
885+
bootAndRunTests( image, tests=args.test, pre=args.run,
886+
post=args.after )
866887
if not ( args.depend or args.list or args.clean or args.flavor
867888
or args.image ):
868889
parser.print_help()

0 commit comments

Comments
 (0)