1#!/usr/bin/env python3
2#
3# Copyright (C) 2016 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import os
19import re
20import shutil
21import subprocess
22import sys
23
24from glob import glob
25
26from tempfile import mkdtemp
27from tempfile import TemporaryFile
28
29# run_jfuzz_test.py success/failure strings.
30SUCCESS_STRING = 'success (no divergences)'
31FAILURE_STRING = 'FAILURE (divergences)'
32
33# Constant returned by string find() method when search fails.
34NOT_FOUND = -1
35
36def main(argv):
37  # Set up.
38  cwd = os.path.dirname(os.path.realpath(__file__))
39  cmd = [cwd + '/run_jfuzz_test.py']
40  parser = argparse.ArgumentParser()
41  parser.add_argument('--num_proc', default=8,
42                      type=int, help='number of processes to run')
43  # Unknown arguments are passed to run_jfuzz_test.py.
44  (args, unknown_args) = parser.parse_known_args()
45  # Run processes.
46  cmd = cmd + unknown_args
47  print()
48  print('**\n**** Nightly JFuzz Testing\n**')
49  print()
50  print('**** Running ****\n\n', cmd, '\n')
51  output_files = [TemporaryFile('wb+') for _ in range(args.num_proc)]
52  processes = []
53  for i, output_file in enumerate(output_files):
54    print('Tester', i)
55    processes.append(subprocess.Popen(cmd, stdout=output_file,
56                                      stderr=subprocess.STDOUT))
57  try:
58    # Wait for processes to terminate.
59    for proc in processes:
60      proc.wait()
61  except KeyboardInterrupt:
62    for proc in processes:
63      proc.kill()
64  # Output results.
65  print('\n**** Results ****\n')
66  output_dirs = []
67  for i, output_file in enumerate(output_files):
68    output_file.seek(0)
69    output_str = output_file.read().decode('ascii')
70    output_file.close()
71    # Extract output directory. Example match: 'Directory : /tmp/tmp8ltpfjng'.
72    directory_match = re.search(r'Directory[^:]*: ([^\n]+)\n', output_str)
73    if directory_match:
74      output_dirs.append(directory_match.group(1))
75    if output_str.find(SUCCESS_STRING) == NOT_FOUND:
76      print('Tester', i, FAILURE_STRING)
77    else:
78      print('Tester', i, SUCCESS_STRING)
79  # Gather divergences.
80  global_out_dir = mkdtemp('jfuzz_nightly')
81  divergence_nr = 0
82  for out_dir in output_dirs:
83    for divergence_dir in glob(out_dir + '/divergence*/'):
84      divergence_nr += 1
85      shutil.copytree(divergence_dir,
86                      global_out_dir + '/divergence' + str(divergence_nr))
87  if divergence_nr > 0:
88    print('\n!!!! Divergences !!!!', divergence_nr)
89  else:
90    print ('\nSuccess')
91  print('\nGlobal output directory:', global_out_dir)
92  print()
93
94if __name__ == '__main__':
95  main(sys.argv)
96