test_utils.py 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
"""
Utilities and mock modules for unittests of statistics modules

"""
import os
import io
import time
import sys
import threading
import tempfile
Naoki Kambe's avatar
Naoki Kambe committed
11
import json
12
13

import msgq
14
import isc.config.cfgmgr
15
16
17
import stats
import stats_httpd

Naoki Kambe's avatar
Naoki Kambe committed
18
19
20
21
# Change value of BIND10_MSGQ_SOCKET_FILE in environment variables
if 'BIND10_MSGQ_SOCKET_FILE' not in os.environ:
    os.environ['BIND10_MSGQ_SOCKET_FILE'] = tempfile.mktemp(prefix='msgq_socket_')

Naoki Kambe's avatar
Naoki Kambe committed
22
23
def send_command(command_name, module_name, params=None, session=None, nonblock=False, timeout=None):
    if session is not None:
24
        cc_session = session
Naoki Kambe's avatar
Naoki Kambe committed
25
26
27
28
29
    else:
        cc_session = isc.cc.Session()
    if timeout is not None:
        orig_timeout = cc_session.get_timeout()
        cc_session.set_timeout(timeout * 1000)
30
31
32
33
34
35
36
37
38
    command = isc.config.ccsession.create_command(command_name, params)
    seq = cc_session.group_sendmsg(command, module_name)
    try:
        (answer, env) = cc_session.group_recvmsg(nonblock, seq)
        if answer:
            return isc.config.ccsession.parse_answer(answer)
    except isc.cc.SessionTimeout:
        pass
    finally:
Naoki Kambe's avatar
Naoki Kambe committed
39
        if timeout is not None:
40
            cc_session.set_timeout(orig_timeout)
Naoki Kambe's avatar
Naoki Kambe committed
41
42
        if session is None:
            cc_session.close()
43

Naoki Kambe's avatar
Naoki Kambe committed
44
45
def send_shutdown(module_name, **kwargs):
    return send_command("shutdown", module_name, **kwargs)
46
47

class ThreadingServerManager:
Naoki Kambe's avatar
Naoki Kambe committed
48
    def __init__(self, server, *args, **kwargs):
Naoki Kambe's avatar
Naoki Kambe committed
49
50
51
52
53
54
55
56
57
58
59
60
61
        self.server = None
        n = 0
        while True:
            try:
                self.server = server(*args, **kwargs)
            except isc.cc.session.SessionTimeout:
                if self.server is not None:
                    self.server.shutdown()
                # retrying until 3 times
                if n >2: raise
                n = n + 1
                continue
            else: break
Naoki Kambe's avatar
Naoki Kambe committed
62
        self.server_name = server.__name__
63
        self.server._thread = threading.Thread(
Naoki Kambe's avatar
Naoki Kambe committed
64
            name=self.server_name, target=self.server.run)
65
        self.server._thread.daemon = True
66

67
68
69
    def run(self):
        self.server._thread.start()
        self.server._started.wait()
70
        self.server._started.clear()
71
72
73

    def shutdown(self):
        self.server.shutdown()
Naoki Kambe's avatar
Naoki Kambe committed
74
        self.server._thread.join(0) # timeout is 0
75

Naoki Kambe's avatar
Naoki Kambe committed
76
77
78
79
80
81
82
83
def do_nothing(*args, **kwargs): pass

class dummy_sys:
    """Dummy for sys"""
    class dummy_io:
        write = do_nothing
    stdout = stderr = dummy_io()

84
class MockMsgq:
85
    def __init__(self):
86
        self._started = threading.Event()
Naoki Kambe's avatar
Naoki Kambe committed
87
88
89
90
        # suppress output to stdout and stderr
        msgq.sys = dummy_sys()
        msgq.print = do_nothing
        self.msgq = msgq.MsgQ(verbose=False)
91
92
93
94
95
96
97
98
99
100
101
        result = self.msgq.setup()
        if result:
            sys.exit("Error on Msgq startup: %s" % result)

    def run(self):
        self._started.set()
        try:
            self.msgq.run()
        except Exception:
            pass
        finally:
Naoki Kambe's avatar
Naoki Kambe committed
102
103
104
            # explicitly shut down the socket of the msgq before
            # shutting down the msgq
            self.msgq.listen_socket.shutdown(msgq.socket.SHUT_RDWR)
Naoki Kambe's avatar
Naoki Kambe committed
105
            self.msgq.shutdown()
106
107

    def shutdown(self):
Naoki Kambe's avatar
Naoki Kambe committed
108
109
        # do nothing for avoiding shutting down the msgq twice
        pass
110
111

class MockCfgmgr:
112
    def __init__(self):
113
114
115
116
117
118
119
        self._started = threading.Event()
        self.cfgmgr = isc.config.cfgmgr.ConfigManager(
            os.environ['CONFIG_TESTDATA_PATH'], "b10-config.db")
        self.cfgmgr.read_config()

    def run(self):
        self._started.set()
Naoki Kambe's avatar
Naoki Kambe committed
120
121
122
123
        try:
            self.cfgmgr.run()
        except Exception:
            pass
124
125
126

    def shutdown(self):
        self.cfgmgr.running = False
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
class MockBoss:
    spec_str = """\
{
  "module_spec": {
    "module_name": "Boss",
    "module_description": "Mock Master process",
    "config_data": [],
    "commands": [
      {
        "command_name": "sendstats",
        "command_description": "Send data to a statistics module at once",
        "command_args": []
      }
    ],
    "statistics": [
      {
        "item_name": "boot_time",
        "item_type": "string",
        "item_optional": false,
        "item_default": "1970-01-01T00:00:00Z",
        "item_title": "Boot time",
        "item_description": "A date time when bind10 process starts initially",
        "item_format": "date-time"
      }
    ]
  }
}
"""
    _BASETIME = (2011, 6, 22, 8, 14, 8, 2, 173, 0)

158
    def __init__(self):
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        self._started = threading.Event()
        self.running = False
        self.spec_file = io.StringIO(self.spec_str)
        # create ModuleCCSession object
        self.mccs = isc.config.ModuleCCSession(
            self.spec_file,
            self.config_handler,
            self.command_handler)
        self.spec_file.close()
        self.cc_session = self.mccs._session
        self.got_command_name = ''

    def run(self):
        self.mccs.start()
        self.running = True
        self._started.set()
Naoki Kambe's avatar
Naoki Kambe committed
175
176
177
178
179
        try:
            while self.running:
                self.mccs.check_command(False)
        except Exception:
            pass
180
181
182
183
184
185
186
187

    def shutdown(self):
        self.running = False

    def config_handler(self, new_config):
        return isc.config.create_answer(0)

    def command_handler(self, command, *args, **kwargs):
Naoki Kambe's avatar
Naoki Kambe committed
188
        self._started.set()
189
        self.got_command_name = command
190
191
192
193
194
        params = { "owner": "Boss",
                   "data": {
                'boot_time': time.strftime('%Y-%m-%dT%H:%M:%SZ', self._BASETIME)
                }
                   }
195
        if command == 'sendstats':
196
197
198
199
            send_command("set", "Stats", params=params, session=self.cc_session)
            return isc.config.create_answer(0)
        elif command == 'getstats':
            return isc.config.create_answer(0, params)
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
        return isc.config.create_answer(1, "Unknown Command")

class MockAuth:
    spec_str = """\
{
  "module_spec": {
    "module_name": "Auth",
    "module_description": "Mock Authoritative service",
    "config_data": [],
    "commands": [
      {
        "command_name": "sendstats",
        "command_description": "Send data to a statistics module at once",
        "command_args": []
      }
    ],
    "statistics": [
      {
        "item_name": "queries.tcp",
        "item_type": "integer",
        "item_optional": false,
        "item_default": 0,
222
        "item_title": "Queries TCP",
223
224
225
226
227
228
229
230
231
232
233
234
235
236
        "item_description": "A number of total query counts which all auth servers receive over TCP since they started initially"
      },
      {
        "item_name": "queries.udp",
        "item_type": "integer",
        "item_optional": false,
        "item_default": 0,
        "item_title": "Queries UDP",
        "item_description": "A number of total query counts which all auth servers receive over UDP since they started initially"
      }
    ]
  }
}
"""
237
    def __init__(self):
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        self._started = threading.Event()
        self.running = False
        self.spec_file = io.StringIO(self.spec_str)
        # create ModuleCCSession object
        self.mccs = isc.config.ModuleCCSession(
            self.spec_file,
            self.config_handler,
            self.command_handler)
        self.spec_file.close()
        self.cc_session = self.mccs._session
        self.got_command_name = ''
        self.queries_tcp = 3
        self.queries_udp = 2

    def run(self):
        self.mccs.start()
        self.running = True
        self._started.set()
Naoki Kambe's avatar
Naoki Kambe committed
256
257
258
259
260
        try:
            while self.running:
                self.mccs.check_command(False)
        except Exception:
            pass
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

    def shutdown(self):
        self.running = False

    def config_handler(self, new_config):
        return isc.config.create_answer(0)

    def command_handler(self, command, *args, **kwargs):
        self.got_command_name = command
        if command == 'sendstats':
            params = { "owner": "Auth",
                       "data": { 'queries.tcp': self.queries_tcp,
                                 'queries.udp': self.queries_udp } }
            return send_command("set", "Stats", params=params, session=self.cc_session)
        return isc.config.create_answer(1, "Unknown Command")

class MyStats(stats.Stats):
278
    def __init__(self):
279
        self._started = threading.Event()
280
        stats.Stats.__init__(self)
281
282
283

    def run(self):
        self._started.set()
Naoki Kambe's avatar
Naoki Kambe committed
284
285
286
287
        try:
            self.start()
        except Exception:
            pass
288
289

    def shutdown(self):
Naoki Kambe's avatar
Naoki Kambe committed
290
        self.command_shutdown()
291
292

class MyStatsHttpd(stats_httpd.StatsHttpd):
Naoki Kambe's avatar
Naoki Kambe committed
293
294
    ORIG_SPECFILE_LOCATION = stats_httpd.SPECFILE_LOCATION
    def __init__(self, *server_address):
295
        self._started = threading.Event()
Naoki Kambe's avatar
Naoki Kambe committed
296
        if server_address:
297
            stats_httpd.SPECFILE_LOCATION = self.create_specfile(*server_address)
Naoki Kambe's avatar
Naoki Kambe committed
298
299
300
            try:
                stats_httpd.StatsHttpd.__init__(self)
            finally:
301
302
                if hasattr(stats_httpd.SPECFILE_LOCATION, "close"):
                    stats_httpd.SPECFILE_LOCATION.close()
Naoki Kambe's avatar
Naoki Kambe committed
303
304
305
306
                stats_httpd.SPECFILE_LOCATION = self.ORIG_SPECFILE_LOCATION
        else:
            stats_httpd.StatsHttpd.__init__(self)

307
308
309
310
311
312
313
314
315
316
317
318
319
320
    def create_specfile(self, *server_address):
        spec_io = open(self.ORIG_SPECFILE_LOCATION)
        try:
            spec = json.load(spec_io)
            spec_io.close()
            config = spec['module_spec']['config_data']
            for i in range(len(config)):
                if config[i]['item_name'] == 'listen_on':
                    config[i]['item_default'] = \
                        [ dict(address=a[0], port=a[1]) for a in server_address ]
                    break
            return io.StringIO(json.dumps(spec))
        finally:
            spec_io.close()
321
322
323

    def run(self):
        self._started.set()
Naoki Kambe's avatar
Naoki Kambe committed
324
325
326
327
        try:
            self.start()
        except Exception:
            pass
328
329

    def shutdown(self):
Naoki Kambe's avatar
Naoki Kambe committed
330
        self.command_handler('shutdown', None)
331
332

class BaseModules:
333
    def __init__(self):
334
        # MockMsgq
335
        self.msgq = ThreadingServerManager(MockMsgq)
336
        self.msgq.run()
Naoki Kambe's avatar
Naoki Kambe committed
337
338
        # Check whether msgq is ready. A SessionTimeout is raised here if not.
        isc.cc.session.Session().close()
339
        # MockCfgmgr
340
        self.cfgmgr = ThreadingServerManager(MockCfgmgr)
341
342
        self.cfgmgr.run()
        # MockBoss
343
        self.boss = ThreadingServerManager(MockBoss)
344
345
        self.boss.run()
        # MockAuth
346
        self.auth = ThreadingServerManager(MockAuth)
347
348
349
350
351
352
353
354
355
356
357
        self.auth.run()

    def shutdown(self):
        # MockAuth
        self.auth.shutdown()
        # MockBoss
        self.boss.shutdown()
        # MockCfgmgr
        self.cfgmgr.shutdown()
        # MockMsgq
        self.msgq.shutdown()