testThreads.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 testing.lib import BaseTestSuite
  19. # module specific imports
  20. import os, tempfile, random
  21. excludes = []
  22. import getpass
  23. from hodlib.Common.threads import simpleCommand
  24. from testing.helper import sampleText
  25. # All test-case classes should have the naming convention test_.*
  26. class test_SimpleCommand(unittest.TestCase):
  27. def setUp(self):
  28. self.rootDir = '/tmp/hod-%s' % getpass.getuser()
  29. if not os.path.exists(self.rootDir):
  30. os.mkdir(self.rootDir)
  31. self.prefix= 'ThreadsTestSuite.test_SimpleCommand'
  32. self.testFile = None
  33. pass
  34. def testRedirectedStdout(self):
  35. self.testFile= tempfile.NamedTemporaryFile(dir=self.rootDir, \
  36. prefix=self.prefix)
  37. cmd=simpleCommand('helper','%s %s 1 1>%s' % \
  38. (sys.executable, \
  39. os.path.join(rootDirectory, "testing", "helper.py"), \
  40. self.testFile.name))
  41. cmd.start()
  42. cmd.join()
  43. self.testFile.seek(0)
  44. stdout = self.testFile.read()
  45. # print stdout, sampleText
  46. assert(stdout == sampleText)
  47. pass
  48. def testRedirectedStderr(self):
  49. self.testFile= tempfile.NamedTemporaryFile(dir=self.rootDir, \
  50. prefix=self.prefix)
  51. cmd=simpleCommand('helper','%s %s 2 2>%s' % \
  52. (sys.executable, \
  53. os.path.join(rootDirectory, "testing", "helper.py"), \
  54. self.testFile.name))
  55. cmd.start()
  56. cmd.join()
  57. self.testFile.seek(0)
  58. stderror = self.testFile.read()
  59. # print stderror, sampleText
  60. assert(stderror == sampleText)
  61. pass
  62. def tearDown(self):
  63. if self.testFile: self.testFile.close()
  64. pass
  65. class ThreadsTestSuite(BaseTestSuite):
  66. def __init__(self):
  67. # suite setup
  68. BaseTestSuite.__init__(self, __name__, excludes)
  69. pass
  70. def cleanUp(self):
  71. # suite tearDown
  72. pass
  73. def RunThreadsTests():
  74. # modulename_suite
  75. suite = ThreadsTestSuite()
  76. testResult = suite.runTests()
  77. suite.cleanUp()
  78. return testResult
  79. if __name__ == "__main__":
  80. RunThreadsTests()