IPC unit tests and testing framework improvements
[platform/core/security/vasum.git] / tests / scripts / vsm_launch_test.py
1 #!/usr/bin/env python
2
3 from xml.dom import minidom
4 from vsm_test_parser import Logger, Parser
5 import subprocess
6 import argparse
7 import os
8 import re
9
10 _defLaunchArgs = ["--report_format=XML",
11                   "--catch_system_errors=no",
12                   "--log_level=test_suite",
13                   "--report_level=detailed",
14                  ]
15
16 log = Logger()
17
18 def _checkIfBinExists(binary):
19     # Check if binary is accessible through PATH env and with no additional paths
20     paths = [s + "/" for s in os.environ["PATH"].split(os.pathsep)]
21     paths.append('')
22     existsInPaths = any([os.path.isfile(path + binary) and os.access(path + binary, os.X_OK)
23                   for path in paths])
24
25     if not existsInPaths:
26         log.error(binary + " NOT FOUND.")
27
28     return existsInPaths
29
30
31
32 def launchTest(cmd=[], externalToolCmd=[], parsing=True):
33     """Default function used to launch test binary.
34
35     Creates a new subprocess and parses it's output
36     """
37     if not _checkIfBinExists(cmd[0]):
38         return
39     if externalToolCmd and not _checkIfBinExists(externalToolCmd[0]):
40         return
41
42     log.info("Starting " + cmd[0] + " ...")
43
44     if parsing:
45         parser = Parser()
46         commandString = " ".join(externalToolCmd + cmd + _defLaunchArgs)
47         log.info("Invoking `" + commandString + "`")
48         p = subprocess.Popen(externalToolCmd + cmd + _defLaunchArgs,
49                              stdout=subprocess.PIPE,
50                              stderr=subprocess.STDOUT)
51         testResult = parser.parseOutputFromProcess(p)
52         if testResult != "":
53             domResult = minidom.parseString(testResult)
54             log.XMLSummary(domResult)
55             log.failedTestSummary(cmd[0])
56         if p.returncode < 0:
57             log.terminatedBySignal(" ".join(cmd), -p.returncode)
58     else:
59         # Launching process without coloring does not require report in XML form
60         # Avoid providing --report_format=XML, redirect std* by default to system's std*
61         commandString = " ".join(externalToolCmd + cmd + _defLaunchArgs[1:])
62         log.info("Invoking `" + commandString + "`")
63         p = subprocess.Popen(externalToolCmd + cmd + _defLaunchArgs[1:])
64         p.wait()
65
66     log.info(cmd[0] + " finished.")
67
68
69
70 _valgrindCmd = ["valgrind"]
71 _gdbCmd = ["gdb", "--args"]
72
73 def main():
74     argparser = argparse.ArgumentParser(description="Test binary launcher for vasum.")
75     group = argparser.add_mutually_exclusive_group()
76     group.add_argument('--valgrind', action='store_true',
77                         help='Launch test binary inside Valgrind (assuming it is installed).')
78     group.add_argument('--gdb', action='store_true',
79                         help='Launch test binary with a tool specified by $VSM_DEBUGGER variable. '
80                             +'Defaults to gdb.')
81     argparser.add_argument('binary', nargs=argparse.REMAINDER,
82                         help='Binary to be launched using script.')
83
84     args = argparser.parse_known_args()
85
86     if args[0].binary:
87         if args[0].gdb:
88             debuggerVar = os.getenv("VSM_DEBUGGER")
89             if (debuggerVar):
90                 _customDebuggerCmd = debuggerVar.split()
91             else:
92                 _customDebuggerCmd = _gdbCmd
93             launchTest(args[0].binary, externalToolCmd=_customDebuggerCmd + args[1], parsing=False)
94         elif args[0].valgrind:
95             launchTest(args[0].binary, externalToolCmd=_valgrindCmd + args[1])
96         else:
97             launchTest(args[0].binary, parsing=True)
98     else:
99         log.info("Test binary not provided! Exiting.")
100
101
102
103 if __name__ == "__main__":
104     main()