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, re, sys |
---|
17 | |
---|
18 | class BaseTestSuite(): |
---|
19 | def __init__(self, name, excludes): |
---|
20 | self.name = name |
---|
21 | self.excludes = excludes |
---|
22 | pass |
---|
23 | |
---|
24 | def runTests(self): |
---|
25 | # Create a runner |
---|
26 | self.runner = unittest.TextTestRunner() |
---|
27 | |
---|
28 | # Get all the test-case classes |
---|
29 | # From module import * |
---|
30 | mod = __import__(self.name, fromlist=['*']) |
---|
31 | modItemsList = dir(mod) |
---|
32 | |
---|
33 | allsuites = [] |
---|
34 | |
---|
35 | # Create all the test suites |
---|
36 | for modItem in modItemsList: |
---|
37 | if re.search(r"^test_", modItem): |
---|
38 | # Yes this is a test class |
---|
39 | if modItem not in self.excludes: |
---|
40 | test_class = getattr(mod, modItem) |
---|
41 | allsuites.append(unittest.makeSuite(test_class)) |
---|
42 | |
---|
43 | # Create a master suite to be run. |
---|
44 | alltests = unittest.TestSuite(tuple(allsuites)) |
---|
45 | |
---|
46 | # Run the master test suite. |
---|
47 | runner = self.runner.run(alltests) |
---|
48 | if(runner.wasSuccessful()): return 0 |
---|
49 | printLine( "%s test(s) failed." % runner.failures.__len__()) |
---|
50 | printLine( "%s test(s) threw errors." % runner.errors.__len__()) |
---|
51 | return runner.failures.__len__() + runner.errors.__len__() |
---|
52 | |
---|
53 | def cleanUp(self): |
---|
54 | # suite tearDown |
---|
55 | pass |
---|
56 | |
---|
57 | def printLine(str): |
---|
58 | print >>sys.stderr, str |
---|
59 | |
---|
60 | def printSeparator(): |
---|
61 | str = "" |
---|
62 | for i in range(0,79): |
---|
63 | str = str + "*" |
---|
64 | print >>sys.stderr, "\n", str, "\n" |
---|
65 | |
---|
66 | # This class captures all log messages logged by hodRunner and other classes. |
---|
67 | # It is then used to verify that certain log messages have come. This is one |
---|
68 | # way to validate that messages printed to the logger are correctly written. |
---|
69 | class MockLogger: |
---|
70 | def __init__(self): |
---|
71 | self.__logLines = {} |
---|
72 | |
---|
73 | def info(self, message): |
---|
74 | self.__logLines[message] = 'info' |
---|
75 | |
---|
76 | def critical(self, message): |
---|
77 | self.__logLines[message] = 'critical' |
---|
78 | |
---|
79 | def warn(self, message): |
---|
80 | self.__logLines[message] = 'warn' |
---|
81 | |
---|
82 | def debug(self, message): |
---|
83 | # don't track debug lines. |
---|
84 | pass |
---|
85 | |
---|
86 | # verify a certain message has been logged at the defined level of severity. |
---|
87 | def hasMessage(self, message, level): |
---|
88 | if not self.__logLines.has_key(message): |
---|
89 | return False |
---|
90 | return self.__logLines[message] == level |
---|
91 | |
---|
92 | # Stub class to test cluster manipulation operations. |
---|
93 | class MockHadoopCluster: |
---|
94 | |
---|
95 | def __init__(self): |
---|
96 | # store the operations received. |
---|
97 | self.__operations = {} |
---|
98 | |
---|
99 | def delete_job(self, jobid): |
---|
100 | self.__operations['delete_job'] = [jobid] |
---|
101 | |
---|
102 | def is_cluster_deallocated(self, dummy): |
---|
103 | return False |
---|
104 | |
---|
105 | def wasOperationPerformed(self, operation, args): |
---|
106 | if self.__operations.has_key(operation): |
---|
107 | actualArgs = self.__operations[operation] |
---|
108 | for arg in actualArgs: |
---|
109 | if arg not in args: |
---|
110 | break |
---|
111 | else: |
---|
112 | return True |
---|
113 | return False |
---|