make test: VCL extended test cases
[vpp.git] / test / test_vcl.py
1 #!/usr/bin/env python
2 """ Vpp VCL tests """
3
4 import unittest
5 import os
6 import signal
7 from framework import VppTestCase, VppTestRunner, running_extended_tests, \
8     Worker
9 from vpp_ip_route import VppIpTable, VppIpRoute, VppRoutePath
10
11
12 class VclAppWorker(Worker):
13     """ VCL Test Application Worker """
14
15     def __init__(self, build_dir, appname, args, logger, env={}):
16         vcl_lib_dir = "%s/vpp/.libs" % build_dir
17         app = "%s/%s" % (vcl_lib_dir, appname)
18         if not os.path.isfile(app):
19             app = "%s/vpp/%s" % (build_dir, appname)
20             env.update({'LD_PRELOAD':
21                         "%s/libvcl_ldpreload.so.0.0.0" % vcl_lib_dir})
22         self.args = [app] + args
23         super(VclAppWorker, self).__init__(self.args, logger, env)
24
25
26 class VclTestCase(VppTestCase):
27     """ VCL Test Class """
28
29     def __init__(self, methodName):
30         var = "VPP_TEST_BUILD_DIR"
31         self.build_dir = os.getenv(var, None)
32         if self.build_dir is None:
33             raise Exception("Environment variable `%s' not set" % var)
34         self.vppDebug = 'vpp_debug' in self.build_dir
35         self.server_addr = "127.0.0.1"
36         self.server_port = "22000"
37         self.timeout = 3
38         self.echo_phrase = "Hello, world! Jenny is a friend of mine."
39
40         super(VclTestCase, self).__init__(methodName)
41
42     def cut_thru_setup(self):
43         self.vapi.session_enable_disable(is_enabled=1)
44
45     def cut_thru_tear_down(self):
46         self.vapi.session_enable_disable(is_enabled=0)
47
48     def cut_thru_test(self, server_app, client_app, client_args):
49         self.env = {'VCL_API_PREFIX': self.shm_prefix,
50                     'VCL_APP_SCOPE_LOCAL': "true"}
51
52         worker_server = VclAppWorker(self.build_dir, server_app,
53                                      [self.server_port],
54                                      self.logger, self.env)
55         worker_server.start()
56         self.sleep(0.2)
57         worker_client = VclAppWorker(self.build_dir, client_app, client_args,
58                                      self.logger, self.env)
59         worker_client.start()
60         worker_client.join(self.timeout)
61         self.validateResults(worker_client, worker_server, self.timeout)
62
63     def thru_host_stack_setup(self):
64         self.vapi.session_enable_disable(is_enabled=1)
65         self.create_loopback_interfaces(range(2))
66
67         table_id = 0
68
69         for i in self.lo_interfaces:
70             i.admin_up()
71
72             if table_id != 0:
73                 tbl = VppIpTable(self, table_id)
74                 tbl.add_vpp_config()
75
76             i.set_table_ip4(table_id)
77             i.config_ip4()
78             table_id += 1
79
80         # Configure namespaces
81         self.vapi.app_namespace_add(namespace_id="0", secret=1234,
82                                     sw_if_index=self.loop0.sw_if_index)
83         self.vapi.app_namespace_add(namespace_id="1", secret=5678,
84                                     sw_if_index=self.loop1.sw_if_index)
85
86         # Add inter-table routes
87         ip_t01 = VppIpRoute(self, self.loop1.local_ip4, 32,
88                             [VppRoutePath("0.0.0.0",
89                                           0xffffffff,
90                                           nh_table_id=1)])
91         ip_t10 = VppIpRoute(self, self.loop0.local_ip4, 32,
92                             [VppRoutePath("0.0.0.0",
93                                           0xffffffff,
94                                           nh_table_id=0)], table_id=1)
95         ip_t01.add_vpp_config()
96         ip_t10.add_vpp_config()
97
98     def thru_host_stack_tear_down(self):
99         for i in self.lo_interfaces:
100             i.unconfig_ip4()
101             i.set_table_ip4(0)
102             i.admin_down()
103
104         self.vapi.session_enable_disable(is_enabled=0)
105
106     def thru_host_stack_test(self, server_app, client_app, client_args):
107         self.env = {'VCL_API_PREFIX': self.shm_prefix,
108                     'VCL_APP_SCOPE_GLOBAL': "true",
109                     'VCL_APP_NAMESPACE_ID': "0",
110                     'VCL_APP_NAMESPACE_SECRET': "1234"}
111
112         worker_server = VclAppWorker(self.build_dir, server_app,
113                                      [self.server_port],
114                                      self.logger, self.env)
115         worker_server.start()
116         self.sleep(0.2)
117
118         self.env.update({'VCL_APP_NAMESPACE_ID': "1",
119                          'VCL_APP_NAMESPACE_SECRET': "5678"})
120         worker_client = VclAppWorker(self.build_dir, client_app, client_args,
121                                      self.logger, self.env)
122         worker_client.start()
123         worker_client.join(self.timeout)
124
125         self.validateResults(worker_client, worker_server, self.timeout)
126
127     def validateResults(self, worker_client, worker_server, timeout):
128         if os.path.isdir('/proc/{}'.format(worker_server.process.pid)):
129             self.logger.info("Killing server worker process (pid %d)" %
130                              worker_server.process.pid)
131             os.killpg(os.getpgid(worker_server.process.pid), signal.SIGTERM)
132             worker_server.join()
133         self.logger.info("Client worker result is `%s'" % worker_client.result)
134         error = False
135         if worker_client.result is None:
136             try:
137                 error = True
138                 self.logger.error(
139                     "Timeout: %ss! Killing client worker process (pid %d)" %
140                     (timeout, worker_client.process.pid))
141                 os.killpg(os.getpgid(worker_client.process.pid),
142                           signal.SIGTERM)
143                 worker_client.join()
144             except:
145                 self.logger.debug(
146                     "Couldn't kill client worker process")
147                 raise
148         if error:
149             raise Exception(
150                 "Timeout! Client worker did not finish in %ss" % timeout)
151         self.assert_equal(worker_client.result, 0, "Binary test return code")
152
153
154 class VCLCutThruTestCase(VclTestCase):
155     """ VCL Cut Thru Tests """
156
157     def setUp(self):
158         super(VCLCutThruTestCase, self).setUp()
159
160         self.cut_thru_setup()
161         self.client_echo_test_args = [self.server_addr, self.server_port,
162                                       "-E", self.echo_phrase, "-X"]
163         self.client_uni_dir_nsock_timeout = 60
164         self.client_uni_dir_nsock_test_args = [self.server_addr,
165                                                self.server_port,
166                                                "-I", "5", "-U", "-X"]
167         self.client_bi_dir_nsock_timeout = 120
168         self.client_bi_dir_nsock_test_args = [self.server_addr,
169                                               self.server_port,
170                                               "-I", "2", "-B", "-X"]
171
172     def tearDown(self):
173         self.cut_thru_tear_down()
174
175         super(VCLCutThruTestCase, self).tearDown()
176
177     def test_ldp_cut_thru_echo(self):
178         """ run LDP cut thru echo test """
179
180         self.cut_thru_test("sock_test_server", "sock_test_client",
181                            self.client_echo_test_args)
182
183     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
184     def test_ldp_cut_thru_uni_dir_nsock(self):
185         """ run LDP cut thru uni-directional (multiple sockets) test """
186
187         self.timeout = self.client_uni_dir_nsock_timeout
188         self.cut_thru_test("sock_test_server", "sock_test_client",
189                            self.client_uni_dir_nsock_test_args)
190
191     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
192     def test_ldp_cut_thru_bi_dir_nsock(self):
193         """ run LDP cut thru bi-directional (multiple sockets) test """
194
195         self.timeout = self.client_bi_dir_nsock_timeout
196         self.cut_thru_test("sock_test_server", "sock_test_client",
197                            self.client_bi_dir_nsock_test_args)
198
199     def test_vcl_cut_thru_echo(self):
200         """ run VCL cut thru echo test """
201
202         self.cut_thru_test("vcl_test_server", "vcl_test_client",
203                            self.client_echo_test_args)
204
205     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
206     def test_vcl_cut_thru_uni_dir_nsock(self):
207         """ run VCL cut thru uni-directional (multiple sockets) test """
208
209         self.timeout = self.client_uni_dir_nsock_timeout
210         self.cut_thru_test("vcl_test_server", "vcl_test_client",
211                            self.client_uni_dir_nsock_test_args)
212
213     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
214     def test_vcl_cut_thru_bi_dir_nsock(self):
215         """ run VCL cut thru bi-directional (multiple sockets) test """
216
217         self.timeout = self.client_bi_dir_nsock_timeout
218         self.cut_thru_test("vcl_test_server", "vcl_test_client",
219                            self.client_bi_dir_nsock_test_args)
220
221
222 class VCLThruHostStackTestCase(VclTestCase):
223     """ VCL Thru Host Stack Tests """
224
225     def setUp(self):
226         super(VCLThruHostStackTestCase, self).setUp()
227
228         self.thru_host_stack_setup()
229         self.client_echo_test_args = [self.loop0.local_ip4,
230                                       self.server_port,
231                                       "-E", self.echo_phrase, "-X"]
232
233     def tearDown(self):
234         self.thru_host_stack_tear_down()
235
236         super(VCLThruHostStackTestCase, self).tearDown()
237
238     def test_ldp_thru_host_stack_echo(self):
239         """ run LDP thru host stack echo test """
240
241         self.thru_host_stack_test("sock_test_server", "sock_test_client",
242                                   self.client_echo_test_args)
243         # TBD: Remove these when VPP thru host teardown config bug is fixed.
244         self.thru_host_stack_test("vcl_test_server", "vcl_test_client",
245                                   self.client_echo_test_args)
246
247     def test_vcl_thru_host_stack_echo(self):
248         """ run VCL thru host stack echo test """
249
250         # TBD: Enable this when VPP  thru host teardown config bug is fixed.
251         # self.thru_host_stack_test("vcl_test_server", "vcl_test_client",
252         #                           self.client_echo_test_args)
253
254     # TBD: Remove VCLThruHostStackExtended*TestCase classes and move
255     #      tests here when VPP  thru host teardown/setup config bug
256     #      is fixed.
257
258
259 class VCLThruHostStackExtendedATestCase(VclTestCase):
260     """ VCL Thru Host Stack Extended Tests """
261
262     def setUp(self):
263         super(VCLThruHostStackExtendedATestCase, self).setUp()
264
265         self.thru_host_stack_setup()
266         if self.vppDebug:
267             self.client_bi_dir_nsock_timeout = 120
268         else:
269             self.client_bi_dir_nsock_timeout = 60
270         self.client_bi_dir_nsock_test_args = [self.loop0.local_ip4,
271                                               self.server_port,
272                                               "-I", "2", "-B", "-X"]
273
274     def tearDown(self):
275         self.thru_host_stack_tear_down()
276
277         super(VCLThruHostStackExtendedATestCase, self).tearDown()
278
279     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
280     def test_vcl_thru_host_stack_bi_dir_nsock(self):
281         """ run VCL thru host stack bi-directional (multiple sockets) test """
282
283         self.timeout = self.client_bi_dir_nsock_timeout
284         self.thru_host_stack_test("vcl_test_server", "vcl_test_client",
285                                   self.client_bi_dir_nsock_test_args)
286
287
288 class VCLThruHostStackExtendedBTestCase(VclTestCase):
289     """ VCL Thru Host Stack Extended Tests """
290
291     def setUp(self):
292         super(VCLThruHostStackExtendedBTestCase, self).setUp()
293
294         self.thru_host_stack_setup()
295         if self.vppDebug:
296             self.client_bi_dir_nsock_timeout = 120
297         else:
298             self.client_bi_dir_nsock_timeout = 60
299         self.client_bi_dir_nsock_test_args = [self.loop0.local_ip4,
300                                               self.server_port,
301                                               "-I", "2", "-B", "-X"]
302
303     def tearDown(self):
304         self.thru_host_stack_tear_down()
305
306         super(VCLThruHostStackExtendedBTestCase, self).tearDown()
307
308     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
309     def test_ldp_thru_host_stack_bi_dir_nsock(self):
310         """ run LDP thru host stack bi-directional (multiple sockets) test """
311
312         self.timeout = self.client_bi_dir_nsock_timeout
313         self.thru_host_stack_test("sock_test_server", "sock_test_client",
314                                   self.client_bi_dir_nsock_test_args)
315
316
317 class VCLThruHostStackExtendedCTestCase(VclTestCase):
318     """ VCL Thru Host Stack Extended Tests """
319
320     def setUp(self):
321         super(VCLThruHostStackExtendedCTestCase, self).setUp()
322
323         self.thru_host_stack_setup()
324         if self.vppDebug:
325             self.client_uni_dir_nsock_timeout = 120
326             self.numSockets = "2"
327         else:
328             self.client_uni_dir_nsock_timeout = 120
329             self.numSockets = "5"
330
331         self.client_uni_dir_nsock_test_args = [self.loop0.local_ip4,
332                                                self.server_port,
333                                                "-I", self.numSockets,
334                                                "-U", "-X"]
335
336     def tearDown(self):
337         self.thru_host_stack_tear_down()
338
339         super(VCLThruHostStackExtendedCTestCase, self).tearDown()
340
341     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
342     def test_ldp_thru_host_stack_uni_dir_nsock(self):
343         """ run LDP thru host stack uni-directional (multiple sockets) test """
344
345         self.timeout = self.client_uni_dir_nsock_timeout
346         self.thru_host_stack_test("sock_test_server", "sock_test_client",
347                                   self.client_uni_dir_nsock_test_args)
348
349
350 class VCLThruHostStackExtendedDTestCase(VclTestCase):
351     """ VCL Thru Host Stack Extended Tests """
352
353     def setUp(self):
354         super(VCLThruHostStackExtendedDTestCase, self).setUp()
355
356         self.thru_host_stack_setup()
357         if self.vppDebug:
358             self.client_uni_dir_nsock_timeout = 120
359             self.numSockets = "2"
360         else:
361             self.client_uni_dir_nsock_timeout = 120
362             self.numSockets = "5"
363
364         self.client_uni_dir_nsock_test_args = [self.loop0.local_ip4,
365                                                self.server_port,
366                                                "-I", self.numSockets,
367                                                "-U", "-X"]
368
369     def tearDown(self):
370         self.thru_host_stack_tear_down()
371
372         super(VCLThruHostStackExtendedDTestCase, self).tearDown()
373
374     @unittest.skipUnless(running_extended_tests(), "part of extended tests")
375     def test_vcl_thru_host_stack_uni_dir_nsock(self):
376         """ run VCL thru host stack uni-directional (multiple sockets) test """
377
378         self.timeout = self.client_uni_dir_nsock_timeout
379         self.thru_host_stack_test("vcl_test_server", "vcl_test_client",
380                                   self.client_uni_dir_nsock_test_args)
381
382
383 if __name__ == '__main__':
384     unittest.main(testRunner=VppTestRunner)