• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# Copyright (C) 2018 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import argparse
17import difflib
18import glob
19import importlib
20import os
21import subprocess
22import sys
23import tempfile
24
25ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
26TEST_DATA_DIR = os.path.join(ROOT_DIR, "test", "trace_processor")
27
28def trace_processor_command(trace_processor_path, trace_path, query_path):
29  return [trace_processor_path, '-q', query_path, trace_path]
30
31def main():
32  parser = argparse.ArgumentParser()
33  parser.add_argument('--index', type=str, help='location of index file',
34                      default=os.path.join(TEST_DATA_DIR, "index"))
35  parser.add_argument('--trace-descriptor', type=str,
36                      help='$(dirname trace_processor)/gen/protos/trace/trace.descriptor')
37  parser.add_argument('trace_processor', type=str,
38                      help='location of trace processor binary')
39  args = parser.parse_args()
40
41  with open(args.index, 'r') as file:
42    index_lines = file.readlines()
43
44  if args.trace_descriptor:
45    trace_descriptor_path = args.trace_descriptor
46  else:
47    out_path = os.path.dirname(args.trace_processor)
48    trace_protos_path = os.path.join(out_path, "gen", "protos", "trace")
49    trace_descriptor_path = os.path.join(trace_protos_path, "trace.descriptor")
50
51  test_failure = 0
52  index_dir = os.path.dirname(args.index)
53  for line in index_lines:
54    stripped = line.strip()
55    if stripped.startswith('#'):
56      continue
57    elif not stripped:
58      continue
59
60    [trace_fname, query_fname, expected_fname] = stripped.split(' ')
61
62    trace_path = os.path.abspath(os.path.join(index_dir, trace_fname))
63    query_path = os.path.abspath(os.path.join(index_dir, query_fname))
64    expected_path = os.path.abspath(os.path.join(index_dir, expected_fname))
65    if not os.path.exists(trace_path):
66      print("Trace file not found {}".format(trace_path))
67      return 1
68    elif not os.path.exists(query_path):
69      print("Query file not found {}".format(query_path))
70      return 1
71    elif not os.path.exists(expected_path):
72      print("Expected file not found {}".format(expected_path))
73      return 1
74
75    if trace_path.endswith(".py"):
76      with tempfile.NamedTemporaryFile() as out:
77        python_cmd = [
78          "python",
79          trace_path,
80          trace_descriptor_path
81        ]
82        subprocess.check_call(
83          python_cmd,
84          stdout=out
85        )
86        cmd = trace_processor_command(
87            args.trace_processor, out.name, query_path)
88        actual_raw = subprocess.check_output(cmd)
89    else:
90      cmd = trace_processor_command(
91          args.trace_processor, trace_path, query_path)
92      actual_raw = subprocess.check_output(cmd)
93
94    actual = actual_raw.decode("utf-8")
95    actual_lines = actual_raw.splitlines(True)
96
97    with open(expected_path, "r") as expected_file:
98      expected = expected_file.read()
99      if expected != actual:
100        sys.stderr.write(
101          "Expected did not match actual for trace {} and query {}\n"
102          .format(trace_path, query_path))
103        sys.stderr.write("Expected file: {}\n".format(expected_path))
104        sys.stderr.write("Commandline: {}\n".format(' '.join(cmd)))
105
106        expected_lines = expected.splitlines(True)
107        diff = difflib.unified_diff(expected_lines, actual_lines,
108                                    fromfile="expected", tofile="actual")
109        for line in diff:
110          sys.stderr.write(line)
111        test_failure += 1
112
113  if test_failure == 0:
114    print("All tests passed successfully")
115    return 0
116  else:
117    print("Total failures: {}".format(test_failure))
118    return 1
119
120if __name__ == '__main__':
121  sys.exit(main())
122