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 | excludes = [''] |
---|
26 | |
---|
27 | import tempfile, shutil, getpass, random |
---|
28 | from hodlib.Common.types import typeValidator |
---|
29 | |
---|
30 | # All test-case classes should have the naming convention test_.* |
---|
31 | class test_typeValidator(unittest.TestCase): |
---|
32 | def setUp(self): |
---|
33 | self.originalDir = os.getcwd() |
---|
34 | self.validator = typeValidator(self.originalDir) |
---|
35 | self.tempDir = tempfile.mkdtemp(dir='/tmp/hod-%s' % getpass.getuser(), |
---|
36 | prefix='test_Types_typeValidator_tempDir') |
---|
37 | self.tempFile = tempfile.NamedTemporaryFile(dir=self.tempDir) |
---|
38 | |
---|
39 | # verification : error strings |
---|
40 | self.errorStringsForVerify = { |
---|
41 | 'pos_int' : 0, |
---|
42 | 'uri' : '%s is an invalid uri', |
---|
43 | 'directory' : 0, |
---|
44 | 'file' : 0, |
---|
45 | } |
---|
46 | |
---|
47 | # verification : valid vals |
---|
48 | self.verifyValidVals = [ |
---|
49 | ('pos_int', 0), |
---|
50 | ('pos_int', 1), |
---|
51 | ('directory', self.tempDir), |
---|
52 | ('directory', '/tmp/hod-%s/../../%s' % \ |
---|
53 | (getpass.getuser(), self.tempDir)), |
---|
54 | ('file', self.tempFile.name), |
---|
55 | ('file', '/tmp/hod-%s/../../%s' % \ |
---|
56 | (getpass.getuser(), self.tempFile.name)), |
---|
57 | ('uri', 'file://localhost/' + self.tempDir), |
---|
58 | ('uri', 'file:///' + self.tempDir), |
---|
59 | ('uri', 'file:///tmp/hod-%s/../../%s' % \ |
---|
60 | (getpass.getuser(), self.tempDir)), |
---|
61 | ('uri', 'file://localhost/tmp/hod-%s/../../%s' % \ |
---|
62 | (getpass.getuser(), self.tempDir)), |
---|
63 | ('uri', 'http://hadoop.apache.org/core/'), |
---|
64 | ('uri', self.tempDir), |
---|
65 | ('uri', '/tmp/hod-%s/../../%s' % \ |
---|
66 | (getpass.getuser(), self.tempDir)), |
---|
67 | ] |
---|
68 | |
---|
69 | # generate an invalid uri |
---|
70 | randomNum = random.random() |
---|
71 | while os.path.exists('/%s' % randomNum): |
---|
72 | # Just to be sure :) |
---|
73 | randomNum = random.random() |
---|
74 | invalidUri = 'file://localhost/%s' % randomNum |
---|
75 | |
---|
76 | # verification : invalid vals |
---|
77 | self.verifyInvalidVals = [ |
---|
78 | ('pos_int', -1), |
---|
79 | ('uri', invalidUri), |
---|
80 | ('directory', self.tempFile.name), |
---|
81 | ('file', self.tempDir), |
---|
82 | ] |
---|
83 | |
---|
84 | # normalization : vals |
---|
85 | self.normalizeVals = [ |
---|
86 | ('pos_int', 1, 1), |
---|
87 | ('pos_int', '1', 1), |
---|
88 | ('directory', self.tempDir, self.tempDir), |
---|
89 | ('directory', '/tmp/hod-%s/../../%s' % \ |
---|
90 | (getpass.getuser(), self.tempDir), |
---|
91 | self.tempDir), |
---|
92 | ('file', self.tempFile.name, self.tempFile.name), |
---|
93 | ('file', '/tmp/hod-%s/../../%s' % \ |
---|
94 | (getpass.getuser(), self.tempFile.name), |
---|
95 | self.tempFile.name), |
---|
96 | ('uri', 'file://localhost' + self.tempDir, |
---|
97 | 'file://' + self.tempDir), |
---|
98 | ('uri', 'file://127.0.0.1' + self.tempDir, |
---|
99 | 'file://' + self.tempDir), |
---|
100 | ('uri', 'http://hadoop.apache.org/core', |
---|
101 | 'http://hadoop.apache.org/core'), |
---|
102 | ('uri', self.tempDir, self.tempDir), |
---|
103 | ('uri', '/tmp/hod-%s/../../%s' % \ |
---|
104 | (getpass.getuser(), self.tempDir), |
---|
105 | self.tempDir), |
---|
106 | ] |
---|
107 | pass |
---|
108 | |
---|
109 | # All testMethods have to have their names start with 'test' |
---|
110 | def testnormalize(self): |
---|
111 | for (type, originalVal, normalizedVal) in self.normalizeVals: |
---|
112 | # print type, originalVal, normalizedVal,\ |
---|
113 | # self.validator.normalize(type, originalVal) |
---|
114 | assert(self.validator.normalize(type, originalVal) == normalizedVal) |
---|
115 | pass |
---|
116 | |
---|
117 | def test__normalize(self): |
---|
118 | # Special test for functionality of private method __normalizedPath |
---|
119 | tmpdir = tempfile.mkdtemp(dir=self.originalDir) #create in self.originalDir |
---|
120 | oldWd = os.getcwd() |
---|
121 | os.chdir('/') |
---|
122 | tmpdirName = re.sub(".*/","",tmpdir) |
---|
123 | # print re.sub(".*/","",tmpdirName) |
---|
124 | # print os.path.join(self.originalDir,tmpdir) |
---|
125 | (type, originalVal, normalizedVal) = \ |
---|
126 | ('file', tmpdirName, \ |
---|
127 | os.path.join(self.originalDir,tmpdirName)) |
---|
128 | assert(self.validator.normalize(type, originalVal) == normalizedVal) |
---|
129 | os.chdir(oldWd) |
---|
130 | os.rmdir(tmpdir) |
---|
131 | pass |
---|
132 | |
---|
133 | def testverify(self): |
---|
134 | # test verify method |
---|
135 | |
---|
136 | # test valid vals |
---|
137 | for (type,value) in self.verifyValidVals: |
---|
138 | valueInfo = { 'isValid' : 0, 'normalized' : 0, 'errorData' : 0 } |
---|
139 | valueInfo = self.validator.verify(type,value) |
---|
140 | # print type, value, valueInfo |
---|
141 | assert(valueInfo['isValid'] == 1) |
---|
142 | |
---|
143 | # test invalid vals |
---|
144 | for (type,value) in self.verifyInvalidVals: |
---|
145 | valueInfo = { 'isValid' : 0, 'normalized' : 0, 'errorData' : 0 } |
---|
146 | valueInfo = self.validator.verify(type,value) |
---|
147 | # print type, value, valueInfo |
---|
148 | assert(valueInfo['isValid'] == 0) |
---|
149 | if valueInfo['errorData'] != 0: |
---|
150 | # if there is any errorData, check |
---|
151 | assert(valueInfo['errorData'] == \ |
---|
152 | self.errorStringsForVerify[type] % value) |
---|
153 | |
---|
154 | pass |
---|
155 | |
---|
156 | def tearDown(self): |
---|
157 | self.tempFile.close() |
---|
158 | if os.path.exists(self.tempDir): |
---|
159 | shutil.rmtree(self.tempDir) |
---|
160 | pass |
---|
161 | |
---|
162 | class TypesTestSuite(BaseTestSuite): |
---|
163 | def __init__(self): |
---|
164 | # suite setup |
---|
165 | BaseTestSuite.__init__(self, __name__, excludes) |
---|
166 | pass |
---|
167 | |
---|
168 | def cleanUp(self): |
---|
169 | # suite tearDown |
---|
170 | pass |
---|
171 | |
---|
172 | def RunTypesTests(): |
---|
173 | # modulename_suite |
---|
174 | suite = TypesTestSuite() |
---|
175 | testResult = suite.runTests() |
---|
176 | suite.cleanUp() |
---|
177 | return testResult |
---|
178 | |
---|
179 | if __name__ == "__main__": |
---|
180 | RunTypesTests() |
---|