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