blob: 5f972323c61d3187b0eb793f3026249647476ce6 [file] [log] [blame]
Klement Sekera8f2a4ea2017-05-04 06:15:18 +02001#!/usr/bin/env python
2""" VAPI test """
3
4from __future__ import division
5import unittest
6import os
7import signal
8import subprocess
9from threading import Thread
10from log import single_line_delim
Klement Sekerad3e671e2017-09-29 12:36:37 +020011from framework import VppTestCase, running_extended_tests, \
12 running_on_centos, VppTestRunner
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020013
14
15class Worker(Thread):
16 def __init__(self, args, logger):
17 self.logger = logger
18 self.args = args
19 self.result = None
20 super(Worker, self).__init__()
21
22 def run(self):
23 executable = self.args[0]
24 self.logger.debug("Running executable w/args `%s'" % self.args)
25 env = os.environ.copy()
26 env["CK_LOG_FILE_NAME"] = "-"
27 self.process = subprocess.Popen(
28 self.args, shell=False, env=env, preexec_fn=os.setpgrp,
29 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30 out, err = self.process.communicate()
31 self.logger.debug("Finished running `%s'" % executable)
32 self.logger.info("Return code is `%s'" % self.process.returncode)
33 self.logger.info(single_line_delim)
34 self.logger.info("Executable `%s' wrote to stdout:" % executable)
35 self.logger.info(single_line_delim)
36 self.logger.info(out)
37 self.logger.info(single_line_delim)
38 self.logger.info("Executable `%s' wrote to stderr:" % executable)
39 self.logger.info(single_line_delim)
40 self.logger.error(err)
41 self.logger.info(single_line_delim)
42 self.result = self.process.returncode
43
44
45@unittest.skipUnless(running_extended_tests(), "part of extended tests")
46class VAPITestCase(VppTestCase):
47 """ VAPI test """
48
Klement Sekeradc15be22017-06-12 06:49:33 +020049 def test_vapi_c(self):
50 """ run C VAPI tests """
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020051 var = "BR"
52 built_root = os.getenv(var, None)
53 self.assertIsNotNone(built_root,
54 "Environment variable `%s' not set" % var)
Klement Sekeradc15be22017-06-12 06:49:33 +020055 executable = "%s/vapi_test/vapi_c_test" % built_root
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020056 worker = Worker(
57 [executable, "vapi client", self.shm_prefix], self.logger)
58 worker.start()
Klement Sekeradc15be22017-06-12 06:49:33 +020059 timeout = 60
60 worker.join(timeout)
61 self.logger.info("Worker result is `%s'" % worker.result)
62 error = False
63 if worker.result is None:
64 try:
65 error = True
66 self.logger.error(
67 "Timeout! Worker did not finish in %ss" % timeout)
68 os.killpg(os.getpgid(worker.process.pid), signal.SIGTERM)
69 worker.join()
70 except:
71 raise Exception("Couldn't kill worker-spawned process")
72 if error:
73 raise Exception(
74 "Timeout! Worker did not finish in %ss" % timeout)
75 self.assert_equal(worker.result, 0, "Binary test return code")
76
Klement Sekerad3e671e2017-09-29 12:36:37 +020077 @unittest.skipIf(running_on_centos(), "Centos's gcc can't compile our C++")
Klement Sekeradc15be22017-06-12 06:49:33 +020078 def test_vapi_cpp(self):
79 """ run C++ VAPI tests """
80 var = "BR"
81 built_root = os.getenv(var, None)
82 self.assertIsNotNone(built_root,
83 "Environment variable `%s' not set" % var)
84 executable = "%s/vapi_test/vapi_cpp_test" % built_root
85 worker = Worker(
86 [executable, "vapi client", self.shm_prefix], self.logger)
87 worker.start()
88 timeout = 120
Klement Sekera8f2a4ea2017-05-04 06:15:18 +020089 worker.join(timeout)
90 self.logger.info("Worker result is `%s'" % worker.result)
91 error = False
92 if worker.result is None:
93 try:
94 error = True
95 self.logger.error(
96 "Timeout! Worker did not finish in %ss" % timeout)
97 os.killpg(os.getpgid(worker.process.pid), signal.SIGTERM)
98 worker.join()
99 except:
100 raise Exception("Couldn't kill worker-spawned process")
101 if error:
102 raise Exception(
103 "Timeout! Worker did not finish in %ss" % timeout)
104 self.assert_equal(worker.result, 0, "Binary test return code")
105
106
107if __name__ == '__main__':
108 unittest.main(testRunner=VppTestRunner)