#!/usr/bin/env python # Copyright 2016 The Kubernetes Authors All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Creates an HTML report for all jobs starting with a given prefix. Reads the JSON from tests.json, and prints the HTML to stdout. This code is pretty nasty, but gets the job done. It would be really spiffy if this used an HTML template system, but for now we're old-fashioned. We could also generate these with JS, directly from the JSON. That would allow custom filtering and stuff like that. """ from __future__ import print_function import argparse import cgi import collections import json import os import string import sys import time TestMetadata = collections.namedtuple('TestMetadata', [ 'okay', 'unstable', 'failed', 'skipped', ]) def gen_tests(data, prefix, exact_match): """Creates the HTML for all test cases. Args: data: Parsed JSON data that was created by gen_json.py. prefix: Considers Jenkins jobs that start with this. exact_match: Only match Jenkins jobs with name equal to prefix. Returns: (html, TestMetadata) for matching tests """ html = ['') return '\n'.join(html), TestMetadata( totals['okay'], totals['unstable'], totals['failed'], totals['skipped']) def html_header(title, script): """Return html header items.""" html = ['', ''] html.append('') if title: html.append('%s' % cgi.escape(title)) if script: html.append('') html.append('') html.append('') return html def gen_html(data, prefix, exact_match=False): """Creates the HTML for the entire page. Args: Same as gen_tests. Returns: Same as gen_tests. """ tests_html, meta = gen_tests(data, prefix, exact_match) if exact_match: msg = 'Suite %s' % cgi.escape(prefix) elif prefix: msg = 'Suites starting with %s' % cgi.escape(prefix) else: msg = 'All suites' html = html_header(title=msg, script=True) html.append('') html.append(tests_html) html.append('') html.append('') return '\n'.join(html), meta def gen_metadata_links(suites): """Write clickable pass, ustabled, failed stats.""" html = [] for (name, target), meta in sorted(suites.iteritems()): html.append('' % target) html.append('%d' % meta.okay) html.append('%d' % meta.unstable) html.append('%d' % meta.failed) html.append(name) html.append('') return html def write_html(outdir, path, html): """Write html to outdir/path.""" with open(os.path.join(outdir, path), 'w') as buf: buf.write(html) def write_metadata(infile, outdir): """Writes tests-*.html and suite-*.html files. Args: infile: the json file created by gen_json.py outdir: a path to write the html files. """ with open(infile) as buf: data = json.load(buf) prefix_metadata = {} prefixes = [ 'kubernetes', 'kubernetes-e2e', 'kubernetes-soak', 'kubernetes-e2e-gce', 'kubernetes-e2e-gke', 'kubernetes-upgrade', ] for prefix in prefixes: path = 'tests-%s.html' % prefix html, metadata = gen_html(data, prefix, False) write_html(outdir, path, html) prefix_metadata[prefix or 'kubernetes', path] = metadata suite_metadata = {} suites = set() for suite_names in data.values(): suites.update(suite_names.keys()) for suite in sorted(suites): path = 'suite-%s.html' % suite html, metadata = gen_html(data, suite, True) write_html(outdir, path, html) suite_metadata[suite, path] = metadata blocking = { 'kubelet-gce-e2e-ci', 'kubernetes-build', 'kubernetes-e2e-gce', 'kubernetes-e2e-gce-scalability', 'kubernetes-e2e-gce-slow', 'kubernetes-e2e-gke', 'kubernetes-e2e-gke-slow', 'kubernetes-kubemark-5-gce', 'kubernetes-kubemark-500-gce', 'kubernetes-test-go', } blocking_suite_metadata = { k: v for (k, v) in suite_metadata.items() if k[0] in blocking} return prefix_metadata, suite_metadata, blocking_suite_metadata def write_index(outdir, prefixes, suites, blockers): """Write the index.html with links to each view, including stat summaries. Args: outdir: the path to write the index.html file prefixes: the {(prefix, path): TestMetadata} map suites: the {(suite, path): TestMetadata} map blockers: the {(suite, path): TestMetadata} map of blocking suites """ html = html_header(title='Kubernetes Test Summary', script=False) html.append('

Kubernetes Tests

') html.append('Last updated %s' % time.strftime('%F')) html.append('

Tests from suites starting with:

') html.extend(gen_metadata_links(prefixes)) html.append('

Blocking suites:

') html.extend(gen_metadata_links(blockers)) html.append('

All suites:

') html.extend(gen_metadata_links(suites)) html.extend(['', '']) write_html(outdir, 'index.html', '\n'.join(html)) def main(infile, outdir): """Use infile to write test, suite and index html files to outdir.""" prefixes, suites, blockers = write_metadata(infile, outdir) write_index(outdir, prefixes, suites, blockers) def get_options(argv): """Process command line arguments.""" parser = argparse.ArgumentParser() parser.add_argument('--output-dir', required=True, help='where to write output pages') parser.add_argument('--input', required=True, help='JSON test data to read for input') return parser.parse_args(argv) if __name__ == '__main__': OPTIONS = get_options(sys.argv[1:]) main(OPTIONS.input, OPTIONS.output_dir)