-
Notifications
You must be signed in to change notification settings - Fork 0
/
HsGrabberTest.py
250 lines (186 loc) · 6.95 KB
/
HsGrabberTest.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
#!/usr/bin/env python
"""
Test HsGrabber code
"""
import getpass
import logging
import re
import unittest
import HsGrabber
import HsMessage
import HsTestUtil
from HsBase import HsBase
from HsPrefix import HsPrefix
from LoggingTestCase import LoggingTestCase
class MyGrabber(HsGrabber.HsGrabber):
"Test wrapper around HsGrabber"
def __init__(self):
self.__poller = None
self.__publisher = None
self.__sender = None
super(MyGrabber, self).__init__()
def create_poller(self, sockets):
"Create mock poller"
if self.__poller is not None:
raise Exception("Cannot create multiple poller sockets")
self.__poller = HsTestUtil.Mock0MQPoller("Poller")
return self.__poller
def create_publisher(self, host):
"Create mock publisher"
if self.__publisher is not None:
raise Exception("Cannot create multiple publisher sockets")
self.__publisher = HsTestUtil.Mock0MQSocket("Publisher")
return self.__publisher
def create_sender(self, host):
"Create mock sender"
if self.__sender is not None:
raise Exception("Cannot create multiple sender sockets")
self.__sender = HsTestUtil.Mock0MQSocket("Sender")
return self.__sender
def validate(self):
"Validate all mock sockets"
self.close_all()
for sock in (self.__publisher, self.__sender, self.__poller):
if sock is not None:
sock.validate()
class HsGrabberTest(LoggingTestCase):
"Test HsGrabber code"
MATCH_ANY = re.compile(r"^.*$")
def setUp(self):
"Set log level to see all log messages"
super(HsGrabberTest, self).setUp()
# by default, check all log messages
self.setLogLevel(0)
def test_negative_time(self):
"Test request with negative time window"
start_ticks = 15789006796024620
stop_ticks = start_ticks - 1000000000
copydir = None
# create the grabber object
hsg = MyGrabber()
# don't check DEBUG/INFO log messages
self.setLogLevel(logging.WARN)
# add all expected log messages
msg = "Requesting negative time range (%.2f).\n" \
"Try another time window." % \
((stop_ticks - start_ticks) / 1E10)
self.expect_log_message(msg)
# run it!
hsg.send_alert(start_ticks, stop_ticks, copydir,
print_to_console=False)
hsg.validate()
def test_nonstandard_time(self):
"Test request with large time window"
start_ticks = 15789006796024620
stop_ticks = start_ticks + int(1E10 * (HsGrabber.WARN_SECONDS + 1))
copydir = "/not/valid/path"
secrange = (stop_ticks - start_ticks) / 1E10
# create the grabber object
hsg = MyGrabber()
# add all JSON and response messages
expected = {
"start_ticks": start_ticks,
"stop_ticks": stop_ticks,
"destination_dir": copydir,
"prefix": HsPrefix.ANON,
"request_id": self.MATCH_ANY,
"msgtype": HsMessage.INITIAL,
"version": HsMessage.CURRENT_VERSION,
"username": getpass.getuser(),
"host": self.MATCH_ANY,
"extract": False,
"copy_dir": None,
"hubs": None,
}
hsg.sender.add_expected(expected)
# don't check DEBUG/INFO log messages
self.setLogLevel(logging.WARN)
# add all expected log messages
msg = "Warning: You are requesting %.2f seconds of data\n" \
"Normal requests are %d seconds or less" % \
(secrange, HsGrabber.WARN_SECONDS)
self.expect_log_message(msg)
# run it!
hsg.send_alert(start_ticks, stop_ticks, copydir,
print_to_console=False)
hsg.validate()
# add grabber to poller socket
hsg.poller.add_poll_result(hsg.sender)
# add final grabber response
hsg.sender.add_incoming("DONE\0")
timeout = 1
hsg.wait_for_response(timeout=timeout, print_to_console=False)
def test_huge_time(self):
"Test request with huge time window"
start_ticks = 15789006796024620
stop_ticks = start_ticks + \
int(1E10 * (HsBase.MAX_REQUEST_SECONDS + 1))
copydir = None
secrange = (stop_ticks - start_ticks) / 1E10
# create the grabber object
hsg = MyGrabber()
# don't check DEBUG/INFO log messages
self.setLogLevel(logging.WARN)
# add all expected log messages
msg = "Request for %.2f seconds is too huge.\n" \
"HsWorker processes request only up to %d seconds.\n" \
"Try a smaller time window." % \
(secrange, HsBase.MAX_REQUEST_SECONDS)
self.expect_log_message(msg)
# run it!
hsg.send_alert(start_ticks, stop_ticks, copydir,
print_to_console=False)
hsg.validate()
def test_timeout(self):
"Test publisher timeout"
# create the grabber object
hsg = MyGrabber()
# add grabber to poller socket
hsg.poller.add_poll_result(None)
# don't check DEBUG/INFO log messages
self.setLogLevel(logging.WARN)
# change the timeout
timeout = 1
# add all expected log messages
self.expect_log_message("No response from expcont's HsPublisher"
" within %s seconds.\nAbort request." %
(timeout, ))
# run it!
hsg.wait_for_response(timeout=timeout, print_to_console=False)
hsg.validate()
def test_working(self):
"Test that everything works"
start_ticks = 15789006796024620
stop_ticks = 15789066796024620
copydir = "/somewhere/else"
# create the grabber object
hsg = MyGrabber()
# add all JSON and response messages
expected = {
"start_ticks": start_ticks,
"stop_ticks": stop_ticks,
"destination_dir": copydir,
"prefix": HsPrefix.ANON,
"request_id": self.MATCH_ANY,
"msgtype": HsMessage.INITIAL,
"version": HsMessage.CURRENT_VERSION,
"username": getpass.getuser(),
"host": self.MATCH_ANY,
"extract": False,
"copy_dir": None,
"hubs": None,
}
hsg.sender.add_expected(expected)
hsg.sender.add_incoming("DONE\0")
# add grabber to poller socket
hsg.poller.add_poll_result(hsg.sender)
# don't check DEBUG/INFO log messages
self.setLogLevel(logging.WARN)
# run it!
hsg.send_alert(start_ticks, stop_ticks, copydir,
print_to_console=False)
timeout = 1
hsg.wait_for_response(timeout=timeout, print_to_console=False)
hsg.validate()
if __name__ == '__main__':
unittest.main()