Debian Codegraph @

testenv/test/base_test.py

raw
1 import os
2 import shutil
3 import shlex
4 import traceback
5 import re
6 import time
7 import sys
8 from subprocess import call
9 from misc.colour_terminal import print_red, print_blue
10 from exc.test_failed import TestFailed
11 import conf
12
13 HTTP = "HTTP"
14 HTTPS = "HTTPS"
15
16 SKIP_TEST = 77
17
18 class BaseTest:
19
20 """
21 Class that defines methods common to both HTTP and FTP Tests.
22 Note that this is an abstract class, subclasses must implement
23 * stop_server()
24 * instantiate_server_by(protocol)
25 """
26
27 def __init__(self, pre_hook, test_params, post_hook, protocols, req_protocols):
28 """
29 Define the class-wide variables (or attributes).
30 Attributes should not be defined outside __init__.
31 """
32 self.name = os.path.basename(os.path.realpath(sys.argv[0]))
33 # if pre_hook == None, then {} (an empty dict object) is passed to
34 # self.pre_configs
35 self.pre_configs = pre_hook or {}
36
37 self.test_params = test_params or {}
38 self.post_configs = post_hook or {}
39 self.protocols = protocols
40
41 if req_protocols is None:
42 self.req_protocols = map(lambda p: p.lower(), self.protocols)
43 else:
44 self.req_protocols = req_protocols
45
46 self.servers = []
47 self.domains = []
48 self.ports = []
49
50 self.addr = None
51 self.port = -1
52
53 self.wget_options = ''
54 self.urls = []
55 self.envs = dict()
56
57 self.tests_passed = True
58 self.ready = False
59 self.init_test_env()
60
61 self.ret_code = 0
62
63 def get_test_dir(self):
64 return self.name + '-test'
65
66 def init_test_env(self):
67 test_dir = self.get_test_dir()
68 try:
69 os.mkdir(test_dir)
70 except FileExistsError:
71 shutil.rmtree(test_dir)
72 os.mkdir(test_dir)
73 os.chdir(test_dir)
74
75 def get_domain_addr(self, addr):
76 # TODO if there's a multiple number of ports, wouldn't it be
77 # overridden to the port of the last invocation?
78 # Set the instance variables 'addr' and 'port' so that
79 # they can be queried by test cases.
80 self.addr = str(addr[0])
81 self.port = str(addr[1])
82
83 return [self.addr, self.port]
84
85 def server_setup(self):
86 print_blue("Running Test %s" % self.name)
87 for protocol in self.protocols:
88 instance = self.instantiate_server_by(protocol)
89 self.servers.append(instance)
90
91 # servers instantiated by different protocols may differ in
92 # ports and etc.
93 # so we should record different domains respect to servers.
94 domain = self.get_domain_addr(instance.server_address)
95 self.domains.append('localhost')
96 self.ports.append(domain[1])
97
98 def exec_wget(self):
99 cmd_line = self.gen_cmd_line()
100 params = shlex.split(cmd_line)
101 print(params)
102 envs = {"HOME": os.getcwd()}
103 envs.update(**self.envs)
104 print(envs)
105
106 if os.getenv("SERVER_WAIT"):
107 time.sleep(float(os.getenv("SERVER_WAIT")))
108
109 try:
110 ret_code = call(params, env=envs)
111 except FileNotFoundError:
112 raise TestFailed("The Wget Executable does not exist at the "
113 "expected path.")
114
115 return ret_code
116
117 def gen_cmd_line(self):
118 test_path = os.path.abspath(".")
119 if os.getenv("WGET_PATH"):
120 wget_path = os.path.abspath(os.getenv("WGET_PATH"))
121 else:
122 wget_path = os.path.abspath(os.path.join(test_path,
123 "..", '..', 'src',
124 "wget"))
125 wget_options = '--debug --no-config %s' % self.wget_options
126
127 valgrind = os.getenv("VALGRIND_TESTS", "")
128 gdb = os.getenv("GDB_TESTS", "")
129
130 # GDB has precedence over Valgrind
131 # If both VALGRIND_TESTS and GDB_TESTS are defined,
132 # GDB will be executed.
133 if gdb == "1":
134 cmd_line = 'gdb --args %s %s ' % (wget_path, wget_options)
135 elif valgrind == "1":
136 cmd_line = 'valgrind --error-exitcode=301 ' \
137 '--leak-check=full ' \
138 '--track-origins=yes ' \
139 '--show-leak-kinds=all ' \
140 '--gen-suppressions=all ' \
141 '--suppressions=../valgrind-suppressions-ssl ' \
142 '%s %s ' % (wget_path, wget_options)
143 elif valgrind not in ("", "0"):
144 cmd_line = '%s %s %s ' % (os.getenv("VALGRIND_TESTS", ""),
145 wget_path,
146 wget_options)
147 else:
148 cmd_line = '%s %s ' % (wget_path, wget_options)
149
150 for req_protocol, urls, domain, port in zip(self.req_protocols,
151 self.urls,
152 self.domains,
153 self.ports):
154 # zip is function for iterating multiple lists at the same time.
155 # e.g. for item1, item2 in zip([1, 5, 3],
156 # ['a', 'e', 'c']):
157 # print(item1, item2)
158 # generates the following output:
159 # 1 a
160 # 5 e
161 # 3 c
162 for url in urls:
163 cmd_line += '%s://%s:%s/%s ' % (req_protocol, domain, port, url)
164
165
166 print(cmd_line)
167
168 return cmd_line
169
170 def __test_cleanup(self):
171 os.chdir('..')
172 try:
173 if not os.getenv("NO_CLEANUP"):
174 shutil.rmtree(self.get_test_dir())
175 except:
176 print("Unknown Exception while trying to remove Test Environment.")
177 self.tests_passed = False
178
179 def _exit_test(self):
180 self.__test_cleanup()
181
182 def begin(self):
183 return 0 if self.tests_passed else 100
184
185 def call_test(self):
186 self.hook_call(self.test_params, 'Test Option')
187
188 try:
189 self.ret_code = self.exec_wget()
190 except TestFailed as e:
191 raise e
192 finally:
193 self.stop_server()
194
195 def do_test(self):
196 self.pre_hook_call()
197 self.call_test()
198 self.post_hook_call()
199
200 def hook_call(self, configs, name):
201 for conf_name, conf_arg in configs.items():
202 try:
203 # conf.find_conf(conf_name) returns the required conf class,
204 # then the class is instantiated with conf_arg, then the
205 # conf instance is called with this test instance itself to
206 # invoke the desired hook
207 conf.find_conf(conf_name)(conf_arg)(self)
208 except AttributeError:
209 self.stop_server()
210 raise TestFailed("%s %s not defined." %
211 (name, conf_name))
212
213 def pre_hook_call(self):
214 self.hook_call(self.pre_configs, 'Pre Test Function')
215
216 def post_hook_call(self):
217 self.hook_call(self.post_configs, 'Post Test Function')
218
219 def _replace_substring(self, string):
220 """
221 Replace first occurrence of "{{name}}" in @string with
222 "getattr(self, name)".
223 """
224 pattern = re.compile(r'\{\{\w+\}\}')
225 match_obj = pattern.search(string)
226 if match_obj is not None:
227 rep = match_obj.group()
228 temp = getattr(self, rep.strip('{}'))
229 string = string.replace(rep, temp)
230 return string
231
232 def instantiate_server_by(self, protocol):
233 """
234 Subclasses must override this method to actually instantiate servers
235 for test cases.
236 """
237 raise NotImplementedError
238
239 def stop_server(self):
240 """
241 Subclasses must implement this method in order to stop certain
242 servers of different types.
243 """
244 raise NotImplementedError
245
246 @staticmethod
247 def get_server_rules(file_obj):
248 """
249 The handling of expect header could be made much better when the
250 options are parsed in a true and better fashion. For an example,
251 see the commented portion in Test-basic-auth.py.
252 """
253 server_rules = {}
254 for rule_name, rule in file_obj.rules.items():
255 server_rules[rule_name] = conf.find_conf(rule_name)(rule)
256 return server_rules
257
258 def __enter__(self):
259 """
260 Initialization for with statement.
261 """
262 return self
263
264 def __exit__(self, exc_type, exc_val, exc_tb):
265 """
266 If the with statement got executed with no exception raised, then
267 exc_type, exc_val, exc_tb are all None.
268 """
269 if exc_val:
270 self.tests_passed = False
271 if exc_type is TestFailed:
272 print_red('Error: %s.' % exc_val.error)
273 else:
274 print_red('Unhandled exception caught.')
275 print(exc_val)
276 traceback.print_tb(exc_tb)
277 self.__test_cleanup()
278
279 return self.tests_passed