testXmlrpc.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #Licensed to the Apache Software Foundation (ASF) under one
  2. #or more contributor license agreements. See the NOTICE file
  3. #distributed with this work for additional information
  4. #regarding copyright ownership. The ASF licenses this file
  5. #to you under the Apache License, Version 2.0 (the
  6. #"License"); you may not use this file except in compliance
  7. #with the License. You may obtain a copy of the License at
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #Unless required by applicable law or agreed to in writing, software
  10. #distributed under the License is distributed on an "AS IS" BASIS,
  11. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. #See the License for the specific language governing permissions and
  13. #limitations under the License.
  14. import unittest, os, sys, re, threading, time
  15. myDirectory = os.path.realpath(sys.argv[0])
  16. rootDirectory = re.sub("/testing/.*", "", myDirectory)
  17. sys.path.append(rootDirectory)
  18. from hodlib.Common.xmlrpc import hodXRClient
  19. from hodlib.Common.socketServers import hodXMLRPCServer
  20. from hodlib.GridServices.service import ServiceUtil
  21. from hodlib.Common.util import hodInterrupt, HodInterruptException
  22. from testing.lib import BaseTestSuite
  23. excludes = []
  24. global serverPort
  25. serverPort = None
  26. class test_HodXRClient(unittest.TestCase):
  27. def setUp(self):
  28. pass
  29. # All testMethods have to have their names start with 'test'
  30. def testSuccess(self):
  31. global serverPort
  32. client = hodXRClient('http://localhost:' + str(serverPort), retryRequests=False)
  33. self.assertEqual(client.testing(), True)
  34. pass
  35. def testFailure(self):
  36. """HOD should raise Exception when unregistered rpc is called"""
  37. global serverPort
  38. client = hodXRClient('http://localhost:' + str(serverPort), retryRequests=False)
  39. self.assertRaises(Exception, client.noMethod)
  40. pass
  41. def testTimeout(self):
  42. """HOD should raise Exception when rpc call times out"""
  43. # Give client some random nonexistent url
  44. serverPort = ServiceUtil.getUniqRandomPort(h='localhost',low=40000,high=50000)
  45. client = hodXRClient('http://localhost:' + str(serverPort), retryRequests=False)
  46. self.assertRaises(Exception, client.testing)
  47. pass
  48. def testInterrupt(self):
  49. """ HOD should raise HodInterruptException when interrupted"""
  50. def interrupt(testClass):
  51. testClass.assertRaises(HodInterruptException, client.testing)
  52. serverPort = ServiceUtil.getUniqRandomPort(h='localhost',low=40000,high=50000)
  53. client = hodXRClient('http://localhost:' + str(serverPort))
  54. myThread = threading.Thread(name='testinterrupt', target=interrupt,args=(self,))
  55. # Set the global interrupt
  56. hodInterrupt.setFlag()
  57. myThread.start()
  58. myThread.join()
  59. pass
  60. def tearDown(self):
  61. pass
  62. class XmlrpcTestSuite(BaseTestSuite):
  63. def __init__(self):
  64. # suite setup
  65. BaseTestSuite.__init__(self, __name__, excludes)
  66. def rpcCall():
  67. return True
  68. global serverPort
  69. serverPort = ServiceUtil.getUniqRandomPort(h='localhost',low=40000,high=50000)
  70. self.server = hodXMLRPCServer('localhost', [serverPort])
  71. self.server.register_function(rpcCall, 'testing')
  72. self.thread = threading.Thread(name="server",
  73. target=self.server._serve_forever)
  74. self.thread.start()
  75. time.sleep(1) # give some time to start server
  76. def cleanUp(self):
  77. # suite tearDown
  78. self.server.stop()
  79. self.thread.join()
  80. def RunXmlrpcTests():
  81. # modulename_suite
  82. suite = XmlrpcTestSuite()
  83. testResult = suite.runTests()
  84. suite.cleanUp()
  85. return testResult
  86. if __name__ == "__main__":
  87. RunXmlrpcTests()