test_queue.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # A script to call the program queue to test that it works
00003 #
00004 # This script creates users and programs; as such, it expects to be run
00005 #  against a clean database
00006 #
00007 # Author: Austin Hendrix
00008 
00009 import roslib; roslib.load_manifest('program_queue')
00010 import rospy
00011 
00012 from program_queue.srv import *
00013 from program_queue.msg import *
00014 
00015 def client(name, t):
00016    rospy.wait_for_service(name)
00017    return rospy.ServiceProxy(name, t)
00018 
00019 if __name__ == '__main__':
00020    rospy.init_node('test_queue')
00021    
00022    # create a user
00023    user = client('create_user', CreateUser)('testuser', 'testpw')
00024    token = user.token
00025 
00026    if token == 0:
00027       print "Failed to create user"
00028 
00029    # test that the token from user creation works
00030    program = client('create_program', CreateProgram)(token)
00031    program_id = program.id
00032    print "Program ID: %d"%program_id
00033 
00034    # test that we can update our program
00035    client('update_program', UpdateProgram)(token, Program(ProgramInfo(
00036       program_id, 'testprogram', ProgramInfo.PYTHON, 'testuser'), 'print "Hello World"\n'))
00037 
00038    # test that logout works
00039    client('logout', Logout)(token)
00040 
00041    # test that logging back in works
00042    user = client('login', Login)('testuser', 'testpw')
00043    token = user.token
00044 
00045    if token == 0:
00046       print "Failed to log in"
00047 
00048    if user.is_admin:
00049       print "Test user is admin and shouldn't be"
00050 
00051    # test that we can retrieve our program
00052    program = client('get_program', GetProgram)(program_id)
00053    if program.program.info.id != program_id:
00054       print "Failed to retrieve program"
00055 
00056    if program.program.info.name != 'testprogram':
00057       print "Failed to set program name"
00058 
00059    if program.program.info.type != ProgramInfo.PYTHON:
00060       print "Failed to set program type"
00061 
00062    if program.program.code != 'print "Hello World"\n':
00063       print "Failed to set program code"
00064 
00065    # test that we can queue our program
00066    q = client('queue_program', QueueProgram)(token, program_id)
00067    print "Queue position %d"%q.queue_position
00068    queue_position = q.queue_position
00069 
00070    # test that our program is in the queue
00071    q = client('get_queue', GetQueue)()
00072    if not program_id in [ p.id for p in q.programs ]:
00073       print "Failed to put program into queue"
00074 
00075    # test that our program is in the right place in the queue
00076    if q.programs[queue_position].id != program_id:
00077       print "Queue position does not match returned queue"
00078 
00079 
00080    # test dequeue
00081    client('dequeue_program', DequeueProgram)(token, program_id)
00082 
00083    # TODO: test that dequeue was successful
00084 
00085    programs = client('get_my_programs', GetMyPrograms)(token)
00086 
00087    # validate that the test user has one program
00088    if len(programs.programs) != 1:
00089       print "GetMyPrograms expected one program; got %d"%len(programs.programs)
00090 
00091 
00092    # test login as admin
00093    admin = client('login', Login)('admin', 'admin')
00094    if admin.token == 0:
00095       print "Failed to log in as admin"
00096 
00097    if not admin.is_admin:
00098       print "Admin user should be an admin but isn't"
00099 
00100    # run_program
00101    client('run_program', RunProgram)(admin.token, program_id)
00102 
00103    # get_output
00104    output = client('get_output', GetOutput)(token, program_id, 0)
00105 
00106    if len(output.output) != 1:
00107       print "Expected 1 output; got %d"%len(output.output)
00108    else:
00109       print "Run date: " + str(output.output[0].header.stamp.to_sec())
00110       print "Output: %s"%output.output[0].output
00111 
00112 
00113    # test get_programs
00114    programs = client('get_programs', GetPrograms)()
00115 
00116    if len(programs.programs) != 1:
00117       print "Expected 1 program, got %d"%len(programs.programs)
00118 
00119    # TODO:
00120    # * services to test as admin:
00121    #  * clear_queue
00122    #  * start_queue
00123    #  * stop_queue


program_queue
Author(s): Austin Hendrix
autogenerated on Wed Aug 26 2015 15:37:32