[120] | 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 | |
---|
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | |
---|
| 11 | #Unless required by applicable law or agreed to in writing, software |
---|
| 12 | #distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | #See the License for the specific language governing permissions and |
---|
| 15 | #limitations under the License. |
---|
| 16 | import unittest, os, sys, re, threading, time |
---|
| 17 | |
---|
| 18 | myDirectory = os.path.realpath(sys.argv[0]) |
---|
| 19 | rootDirectory = re.sub("/testing/.*", "", myDirectory) |
---|
| 20 | |
---|
| 21 | sys.path.append(rootDirectory) |
---|
| 22 | |
---|
| 23 | from testing.lib import BaseTestSuite |
---|
| 24 | |
---|
| 25 | # module specific imports |
---|
| 26 | import os, tempfile, random |
---|
| 27 | |
---|
| 28 | excludes = [] |
---|
| 29 | |
---|
| 30 | import getpass |
---|
| 31 | from hodlib.Common.threads import simpleCommand |
---|
| 32 | from testing.helper import sampleText |
---|
| 33 | |
---|
| 34 | # All test-case classes should have the naming convention test_.* |
---|
| 35 | class test_SimpleCommand(unittest.TestCase): |
---|
| 36 | def setUp(self): |
---|
| 37 | self.rootDir = '/tmp/hod-%s' % getpass.getuser() |
---|
| 38 | if not os.path.exists(self.rootDir): |
---|
| 39 | os.mkdir(self.rootDir) |
---|
| 40 | self.prefix= 'ThreadsTestSuite.test_SimpleCommand' |
---|
| 41 | self.testFile = None |
---|
| 42 | pass |
---|
| 43 | |
---|
| 44 | def testRedirectedStdout(self): |
---|
| 45 | self.testFile= tempfile.NamedTemporaryFile(dir=self.rootDir, \ |
---|
| 46 | prefix=self.prefix) |
---|
| 47 | cmd=simpleCommand('helper','%s %s 1 1>%s' % \ |
---|
| 48 | (sys.executable, \ |
---|
| 49 | os.path.join(rootDirectory, "testing", "helper.py"), \ |
---|
| 50 | self.testFile.name)) |
---|
| 51 | |
---|
| 52 | cmd.start() |
---|
| 53 | cmd.join() |
---|
| 54 | |
---|
| 55 | self.testFile.seek(0) |
---|
| 56 | stdout = self.testFile.read() |
---|
| 57 | # print stdout, sampleText |
---|
| 58 | assert(stdout == sampleText) |
---|
| 59 | pass |
---|
| 60 | |
---|
| 61 | def testRedirectedStderr(self): |
---|
| 62 | self.testFile= tempfile.NamedTemporaryFile(dir=self.rootDir, \ |
---|
| 63 | prefix=self.prefix) |
---|
| 64 | cmd=simpleCommand('helper','%s %s 2 2>%s' % \ |
---|
| 65 | (sys.executable, \ |
---|
| 66 | os.path.join(rootDirectory, "testing", "helper.py"), \ |
---|
| 67 | self.testFile.name)) |
---|
| 68 | cmd.start() |
---|
| 69 | cmd.join() |
---|
| 70 | |
---|
| 71 | self.testFile.seek(0) |
---|
| 72 | stderror = self.testFile.read() |
---|
| 73 | # print stderror, sampleText |
---|
| 74 | assert(stderror == sampleText) |
---|
| 75 | pass |
---|
| 76 | |
---|
| 77 | def tearDown(self): |
---|
| 78 | if self.testFile: self.testFile.close() |
---|
| 79 | pass |
---|
| 80 | |
---|
| 81 | class ThreadsTestSuite(BaseTestSuite): |
---|
| 82 | def __init__(self): |
---|
| 83 | # suite setup |
---|
| 84 | BaseTestSuite.__init__(self, __name__, excludes) |
---|
| 85 | pass |
---|
| 86 | |
---|
| 87 | def cleanUp(self): |
---|
| 88 | # suite tearDown |
---|
| 89 | pass |
---|
| 90 | |
---|
| 91 | def RunThreadsTests(): |
---|
| 92 | # modulename_suite |
---|
| 93 | suite = ThreadsTestSuite() |
---|
| 94 | testResult = suite.runTests() |
---|
| 95 | suite.cleanUp() |
---|
| 96 | return testResult |
---|
| 97 | |
---|
| 98 | if __name__ == "__main__": |
---|
| 99 | RunThreadsTests() |
---|