-
Notifications
You must be signed in to change notification settings - Fork 601
/
integ_test_base.py
executable file
·303 lines (257 loc) · 9.83 KB
/
integ_test_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import coverage
import http.client
import os
from pathlib import Path
import platform
import shutil
import signal
import subprocess
import tabpy
import tempfile
import time
import unittest
class IntegTestBase(unittest.TestCase):
"""
Base class for integration tests.
"""
def __init__(self, methodName="runTest"):
super(IntegTestBase, self).__init__(methodName)
self.process = None
self.delete_temp_folder = True
def set_delete_temp_folder(self, delete_temp_folder: bool):
"""
Specify if temporary folder for state, config and log
files should be deleted when test is done.
By default the folder is deleted.
Parameters
----------
delete_test_folder: bool
If True temp folder will be deleted.
"""
self.delete_temp_folder = delete_temp_folder
def _get_state_file_path(self) -> str:
"""
Generates state.ini and returns absolute path to it.
Overwrite this function for tests to run against not default state
file.
Returns
-------
str
Absolute path to state file folder.
"""
state_file = open(os.path.join(self.tmp_dir, "state.ini"), "w+")
state_file.write(
"[Service Info]\n"
"Name = TabPy Serve\n"
"Description = \n"
"Creation Time = 0\n"
"Access-Control-Allow-Origin = \n"
"Access-Control-Allow-Headers = \n"
"Access-Control-Allow-Methods = \n"
"\n"
"[Query Objects Service Versions]\n"
"\n"
"[Query Objects Docstrings]\n"
"\n"
"[Meta]\n"
"Revision Number = 1\n"
)
state_file.close()
return self.tmp_dir
def _get_port(self) -> str:
"""
Returns port TabPy should run on. Default implementation
returns '9004'.
Returns
-------
str
Port number.
"""
return "9004"
def _get_pwd_file(self) -> str:
"""
Returns absolute or relative path to password file.
Overwrite to create and/or specify your own file.
Default implementation returns None which means
TABPY_PWD_FILE setting won't be added to config.
Returns
-------
str
Absolute or relative path to password file.
If None TABPY_PWD_FILE setting won't be added to
config.
"""
return None
def _get_transfer_protocol(self) -> str:
"""
Returns transfer protocol for configuration file.
Default implementation returns None which means
TABPY_TRANSFER_PROTOCOL setting won't be added to config.
Returns
-------
str
Transfer protocol (e.g 'http' or 'https').
If None TABPY_TRANSFER_PROTOCOL setting won't be
added to config.
"""
return None
def _get_certificate_file_name(self) -> str:
"""
Returns absolute or relative certificate file name
for configuration file.
Default implementation returns None which means
TABPY_CERTIFICATE_FILE setting won't be added to config.
Returns
-------
str
Absolute or relative certificate file name.
If None TABPY_CERTIFICATE_FILE setting won't be
added to config.
"""
return None
def _get_key_file_name(self) -> str:
"""
Returns absolute or relative private key file name
for configuration file.
Default implementation returns None which means
TABPY_KEY_FILE setting won't be added to config.
Returns
-------
str
Absolute or relative private key file name.
If None TABPY_KEY_FILE setting won't be
added to config.
"""
return None
def _get_evaluate_timeout(self) -> str:
"""
Returns the configured timeout for the /evaluate method.
Default implementation returns None, which means that
the timeout will default to 30.
Returns
-------
str
Timeout for calling /evaluate.
If None, defaults TABPY_EVALUATE_TIMEOUT setting
will default to '30'.
"""
return None
def _get_config_file_name(self) -> str:
"""
Generates config file. Overwrite this function for tests to
run against not default state file.
Returns
-------
str
Absolute path to config file.
"""
config_file = open(os.path.join(self.tmp_dir, "test.conf"), "w+")
config_file.write(
"[TabPy]\n"
f"TABPY_QUERY_OBJECT_PATH = ./query_objects\n"
f"TABPY_PORT = {self._get_port()}\n"
f"TABPY_STATE_PATH = {self.tmp_dir}\n"
)
pwd_file = self._get_pwd_file()
if pwd_file is not None:
pwd_file = os.path.abspath(pwd_file)
config_file.write(f"TABPY_PWD_FILE = {pwd_file}\n")
transfer_protocol = self._get_transfer_protocol()
if transfer_protocol is not None:
config_file.write(f"TABPY_TRANSFER_PROTOCOL = {transfer_protocol}\n")
cert_file_name = self._get_certificate_file_name()
if cert_file_name is not None:
cert_file_name = os.path.abspath(cert_file_name)
config_file.write(f"TABPY_CERTIFICATE_FILE = {cert_file_name}\n")
key_file_name = self._get_key_file_name()
if key_file_name is not None:
key_file_name = os.path.abspath(key_file_name)
config_file.write(f"TABPY_KEY_FILE = {key_file_name}\n")
evaluate_timeout = self._get_evaluate_timeout()
if evaluate_timeout is not None:
config_file.write(f"TABPY_EVALUATE_TIMEOUT = {evaluate_timeout}\n")
config_file.close()
self.delete_config_file = True
return config_file.name
def setUp(self):
super(IntegTestBase, self).setUp()
prefix = "TabPy_IntegTest_"
self.tmp_dir = tempfile.mkdtemp(prefix=prefix)
# create temporary state.ini
orig_state_file_name = os.path.abspath(
self._get_state_file_path() + "/state.ini"
)
self.state_file_name = os.path.abspath(self.tmp_dir + "/state.ini")
if orig_state_file_name != self.state_file_name:
shutil.copyfile(orig_state_file_name, self.state_file_name)
# create config file
orig_config_file_name = os.path.abspath(self._get_config_file_name())
self.config_file_name = os.path.abspath(
self.tmp_dir + "/" + os.path.basename(orig_config_file_name)
)
if orig_config_file_name != self.config_file_name:
shutil.copyfile(orig_config_file_name, self.config_file_name)
# Platform specific - for integration tests we want to engage
# startup script
with open(self.tmp_dir + "/output.txt", "w") as outfile:
cmd = ["tabpy", "--config=" + self.config_file_name]
preexec_fn = None
if platform.system() == "Windows":
self.py = "python"
else:
self.py = "python3"
preexec_fn = os.setsid
coverage.process_startup()
self.process = subprocess.Popen(
cmd, preexec_fn=preexec_fn, stdout=outfile, stderr=outfile
)
# give the app some time to start up...
time.sleep(5)
def tearDown(self):
# stop TabPy
if self.process is not None:
if platform.system() == "Windows":
subprocess.call(["taskkill", "/F", "/T", "/PID", str(self.process.pid)])
else:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
self.process.kill()
# after shutting down TabPy and before we start it again
# for next test give it some time to terminate.
time.sleep(5)
# remove temporary files
if self.delete_temp_folder:
os.remove(self.state_file_name)
os.remove(self.config_file_name)
shutil.rmtree(self.tmp_dir)
super(IntegTestBase, self).tearDown()
def _get_connection(self) -> http.client.HTTPConnection:
protocol = self._get_transfer_protocol()
url = "localhost:" + self._get_port()
if protocol is not None and protocol.lower() == "https":
connection = http.client.HTTPSConnection(url)
else:
connection = http.client.HTTPConnection(url)
return connection
def _get_username(self) -> str:
return "user1"
def _get_password(self) -> str:
return "P@ssw0rd"
def deploy_models(self, username: str, password: str):
repo_dir = os.path.abspath(os.path.dirname(tabpy.__file__))
path = os.path.join(repo_dir, "models", "deploy_models.py")
with open(self.tmp_dir + "/deploy_models_output.txt", "w") as outfile:
outfile.write(
f"--<< Running {self.py} {path} "
f"{self._get_config_file_name()} >>--\n"
)
input_string = f"{username}\n{password}\n"
outfile.write(f"--<< Input = {input_string} >>--")
coverage.process_startup()
p = subprocess.run(
[self.py, path, self._get_config_file_name()],
input=input_string.encode("utf-8"),
stdout=outfile,
stderr=outfile,
)
def _get_process(self):
return self.process