| 1 | #!/usr/bin/env python
|
|---|
| 2 |
|
|---|
| 3 | #############################################
|
|---|
| 4 | #Author: Damiano Barboni <barboni@meeo.it>
|
|---|
| 5 | #Version: 0.0.1
|
|---|
| 6 | #Description: Test parallel wcs request
|
|---|
| 7 | #Changelog: Fri Dec 27 09:54:39 CET 2012
|
|---|
| 8 | # First version for resMEEO
|
|---|
| 9 | #############################################
|
|---|
| 10 |
|
|---|
| 11 | import httplib
|
|---|
| 12 | import urllib
|
|---|
| 13 | import sys
|
|---|
| 14 | import threading
|
|---|
| 15 | import random
|
|---|
| 16 | import unittest
|
|---|
| 17 | import subprocess
|
|---|
| 18 | import time
|
|---|
| 19 | import os
|
|---|
| 20 |
|
|---|
| 21 | ####################################################
|
|---|
| 22 | ######## global variables definition ###############
|
|---|
| 23 | ####################################################
|
|---|
| 24 |
|
|---|
| 25 | WCS_host = "earthserverclimate"
|
|---|
| 26 | WCS_port = 8080
|
|---|
| 27 | #WCS_request = "/petascope/wcs2?service=WCS&Request=GetCoverage&version=2.0.0&subsetX=x(501166,501166)&subsetY=y(4429057,4429057)&subsetT=t(2001-01-01,2001-12-31)&CoverageId=AT2_SPCL56_32632_400"
|
|---|
| 28 | WCS_request = "/petascope/wcs2?service=WCS&Request=GetCoverage&version=2.0.0&subsetX=x(679276,679276)&subsetY=y(4929231,4929231)&subsetT=t(2005-01-01,2010-12-31)&CoverageId=CRESCO_PM10_32632_20000"
|
|---|
| 29 | N_REQUEST = 10
|
|---|
| 30 |
|
|---|
| 31 | ######################################################
|
|---|
| 32 |
|
|---|
| 33 | class requestThread(threading.Thread):
|
|---|
| 34 | def __init__(self, i):
|
|---|
| 35 | threading.Thread.__init__(self)
|
|---|
| 36 | self.th_number = i
|
|---|
| 37 |
|
|---|
| 38 | #run method
|
|---|
| 39 | def run(self):
|
|---|
| 40 | start = time.time()
|
|---|
| 41 | print "Thread %d STARTED" %(self.th_number)
|
|---|
| 42 |
|
|---|
| 43 | #get xml response
|
|---|
| 44 | conn = httplib.HTTPConnection(WCS_host, WCS_port)
|
|---|
| 45 | conn.request("GET", WCS_request)
|
|---|
| 46 | r = conn.getresponse()
|
|---|
| 47 | #check if response status is 200
|
|---|
| 48 | r_status = r.status
|
|---|
| 49 | if 200 <= r_status <= 299:
|
|---|
| 50 | xml = r.read()
|
|---|
| 51 | else:
|
|---|
| 52 | xml = ("Tread %d - Unable to get data from WCS server. Bad response status N %d returned by WCS server" %(self.th_number, r_status))
|
|---|
| 53 |
|
|---|
| 54 | filename = "wcs_output_%d.xml" %(self.th_number)
|
|---|
| 55 |
|
|---|
| 56 | #write output to file
|
|---|
| 57 | out_file = open(filename,"w")
|
|---|
| 58 | out_file.write(xml)
|
|---|
| 59 | out_file.close()
|
|---|
| 60 |
|
|---|
| 61 | print "Thread %d ENDED in %f seconds" %(self.th_number,time.time()-start)
|
|---|
| 62 | sys.exit(0)
|
|---|
| 63 |
|
|---|
| 64 | class TestParallelRequest(unittest.TestCase):
|
|---|
| 65 |
|
|---|
| 66 | #initialize test suite
|
|---|
| 67 | def setUp(self):
|
|---|
| 68 | self.test_passed = False
|
|---|
| 69 |
|
|---|
| 70 | def wcs_parallel(self):
|
|---|
| 71 | #thread generation
|
|---|
| 72 | thread_list = []
|
|---|
| 73 | for i in range(0, N_REQUEST):
|
|---|
| 74 | thread_list.append(requestThread(i))
|
|---|
| 75 | #thread execution
|
|---|
| 76 | thread_list[i].start()
|
|---|
| 77 |
|
|---|
| 78 | #Wait for the thread_list tasks to finish
|
|---|
| 79 | for i in range(0,len(thread_list)):
|
|---|
| 80 | thread_list[i].join()
|
|---|
| 81 |
|
|---|
| 82 | #check MD5
|
|---|
| 83 | first_MD5 = None
|
|---|
| 84 | for i in range(0, N_REQUEST):
|
|---|
| 85 | filename = "wcs_output_%d.xml" %(i)
|
|---|
| 86 | if os.path.isfile(filename):
|
|---|
| 87 | command = "md5sum %s" %(filename)
|
|---|
| 88 | p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|---|
| 89 | retval = p.wait()
|
|---|
| 90 | self.assertEqual(retval,0,p.stderr.read())
|
|---|
| 91 | md5 = p.stdout.read().split(" ")[0]
|
|---|
| 92 | if first_MD5 == None:
|
|---|
| 93 | #for the first file only save the MD5, other files must have the same code
|
|---|
| 94 | first_MD5 = md5
|
|---|
| 95 | else:
|
|---|
| 96 | self.assertEqual(md5, first_MD5, "Test Failed - md5 for file %s is different than previous" %(filename))
|
|---|
| 97 |
|
|---|
| 98 | #search for Exceptions on wcs xml resposne
|
|---|
| 99 | for i in range(0, N_REQUEST):
|
|---|
| 100 | filename = "wcs_output_%d.xml" %(i)
|
|---|
| 101 | self.assertFalse('Exception' in open(filename).read(), "Test Failed - file %s contains an exception:\n%s" %(filename,open(filename).read()))
|
|---|
| 102 |
|
|---|
| 103 | #test is passed
|
|---|
| 104 | self.test_passed = True
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | def tearDown(self):
|
|---|
| 111 | if not self.test_passed:
|
|---|
| 112 | print "Unittest system won't remove output file automatically"
|
|---|
| 113 |
|
|---|
| 114 | else:
|
|---|
| 115 | print "Unittest system will remove output file automatically"
|
|---|
| 116 | for i in range(0, N_REQUEST):
|
|---|
| 117 | filename = "wcs_output_%d.xml" %(i)
|
|---|
| 118 | if os.path.isfile(filename):
|
|---|
| 119 | os.remove(filename)
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 | def makeSuite():
|
|---|
| 125 | suite = unittest.TestSuite()
|
|---|
| 126 | suite.addTest(TestParallelRequest('wcs_parallel'))
|
|---|
| 127 |
|
|---|
| 128 | return suite
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 | if __name__ == "__main__":
|
|---|
| 132 | unittest.TextTestRunner(verbosity=3).run(makeSuite())
|
|---|
| 133 |
|
|---|