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 | from hodlib.HodRing.hodRing import MRSystemDirectoryManager, createMRSystemDirectoryManager |
---|
25 | from hodlib.Common.threads import simpleCommand |
---|
26 | |
---|
27 | excludes = [] |
---|
28 | |
---|
29 | # duplicating temporarily until HADOOP-2848 is committed. |
---|
30 | class MyMockLogger: |
---|
31 | def __init__(self): |
---|
32 | self.__logLines = {} |
---|
33 | |
---|
34 | def info(self, message): |
---|
35 | self.__logLines[message] = 'info' |
---|
36 | |
---|
37 | def critical(self, message): |
---|
38 | self.__logLines[message] = 'critical' |
---|
39 | |
---|
40 | def warn(self, message): |
---|
41 | self.__logLines[message] = 'warn' |
---|
42 | |
---|
43 | def debug(self, message): |
---|
44 | # don't track debug lines. |
---|
45 | pass |
---|
46 | |
---|
47 | # verify a certain message has been logged at the defined level of severity. |
---|
48 | def hasMessage(self, message, level): |
---|
49 | if not self.__logLines.has_key(message): |
---|
50 | return False |
---|
51 | return self.__logLines[message] == level |
---|
52 | |
---|
53 | class test_MRSystemDirectoryManager(unittest.TestCase): |
---|
54 | |
---|
55 | def setUp(self): |
---|
56 | self.log = MyMockLogger() |
---|
57 | |
---|
58 | def testCleanupArgsString(self): |
---|
59 | sysDirMgr = MRSystemDirectoryManager(1234, '/user/hod/mapredsystem/hoduser.123.abc.com', \ |
---|
60 | 'def.com:5678', '/usr/bin/hadoop', self.log) |
---|
61 | str = sysDirMgr.toCleanupArgs() |
---|
62 | self.assertTrue(" --jt-pid 1234 --mr-sys-dir /user/hod/mapredsystem/hoduser.123.abc.com --fs-name def.com:5678 --hadoop-path /usr/bin/hadoop ", str) |
---|
63 | |
---|
64 | def testCreateMRSysDirInvalidParams(self): |
---|
65 | # test that no mr system directory manager is created if required keys are not present |
---|
66 | # this case will test scenarios of non jobtracker daemons. |
---|
67 | keys = [ 'jt-pid', 'mr-sys-dir', 'fs-name', 'hadoop-path' ] |
---|
68 | map = { 'jt-pid' : 1234, |
---|
69 | 'mr-sys-dir' : '/user/hod/mapredsystem/hoduser.def.com', |
---|
70 | 'fs-name' : 'ghi.com:1234', |
---|
71 | 'hadoop-path' : '/usr/bin/hadoop' |
---|
72 | } |
---|
73 | for key in keys: |
---|
74 | val = map[key] |
---|
75 | map[key] = None |
---|
76 | self.assertEquals(createMRSystemDirectoryManager(map, self.log), None) |
---|
77 | map[key] = val |
---|
78 | |
---|
79 | def testUnresponsiveJobTracker(self): |
---|
80 | # simulate an unresponsive job tracker, by giving a command that runs longer than the retries |
---|
81 | # verify that the program returns with the right error message. |
---|
82 | sc = simpleCommand("sleep", "sleep 300") |
---|
83 | sc.start() |
---|
84 | pid = sc.getPid() |
---|
85 | while pid is None: |
---|
86 | pid = sc.getPid() |
---|
87 | sysDirMgr = MRSystemDirectoryManager(pid, '/user/yhemanth/mapredsystem/hoduser.123.abc.com', \ |
---|
88 | 'def.com:5678', '/usr/bin/hadoop', self.log, retries=3) |
---|
89 | sysDirMgr.removeMRSystemDirectory() |
---|
90 | self.log.hasMessage("Job Tracker did not exit even after a minute. Not going to try and cleanup the system directory", 'warn') |
---|
91 | sc.kill() |
---|
92 | sc.wait() |
---|
93 | sc.join() |
---|
94 | |
---|
95 | class HodCleanupTestSuite(BaseTestSuite): |
---|
96 | def __init__(self): |
---|
97 | # suite setup |
---|
98 | BaseTestSuite.__init__(self, __name__, excludes) |
---|
99 | pass |
---|
100 | |
---|
101 | def cleanUp(self): |
---|
102 | # suite tearDown |
---|
103 | pass |
---|
104 | |
---|
105 | def RunHodCleanupTests(): |
---|
106 | # modulename_suite |
---|
107 | suite = HodCleanupTestSuite() |
---|
108 | testResult = suite.runTests() |
---|
109 | suite.cleanUp() |
---|
110 | return testResult |
---|
111 | |
---|
112 | if __name__ == "__main__": |
---|
113 | RunHodCleanupTests() |
---|