1516N/A#!/usr/bin/python
430N/A#
430N/A# CDDL HEADER START
430N/A#
430N/A# The contents of this file are subject to the terms of the
430N/A# Common Development and Distribution License (the "License").
430N/A# You may not use this file except in compliance with the License.
430N/A#
430N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
430N/A# or http://www.opensolaris.org/os/licensing.
430N/A# See the License for the specific language governing permissions
430N/A# and limitations under the License.
430N/A#
430N/A# When distributing Covered Code, include this CDDL HEADER in each
430N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
430N/A# If applicable, add the following below this CDDL HEADER, with the
430N/A# fields enclosed by brackets "[]" replaced with your own identifying
430N/A# information: Portions Copyright [yyyy] [name of copyright owner]
430N/A#
430N/A# CDDL HEADER END
430N/A#
430N/A
3143N/A#
3158N/A# Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
3143N/A#
430N/A
3143N/Afrom __future__ import print_function
430N/Aimport os
430N/Aimport sys
430N/Aimport unittest
430N/A
1715N/ABASELINE_MATCH=0
1715N/ABASELINE_MISMATCH=1
1715N/A
430N/Aclass BaseLine(object):
1715N/A """Test result baseline recording and checking. """
430N/A sep1 = '=' * 70
430N/A sep2 = '-' * 70
430N/A
1715N/A def __init__(self, filename="baseline.txt", generate=False):
430N/A
1715N/A # filename from which to get or store baseline results
1715N/A self.__filename = filename
1715N/A # 'generating' keeps track of whether we are currently
1715N/A # generating a baseline or not: if either the baseline doesn't
1715N/A # exist or the "-g" option is specified on the commandline.
1715N/A self.__generating = generate
1715N/A # List of tuples (name, result) for failed tests
1715N/A self.__failed_list = []
1715N/A # dict of "test name" -> "result"
1715N/A self.__results = {}
430N/A
1715N/A def handleresult(self, name, actualresult):
430N/A """Add a result if we're generating the baseline file,
1715N/A otherwise check it against the current result set.
1715N/A Returns a value to indicate whether the result matched
1715N/A the baseline."""
1715N/A
1715N/A if self.__generating:
1715N/A self.__results[name] = actualresult
1715N/A return BASELINE_MATCH
430N/A
1715N/A if self.expectedresult(name) != actualresult:
1715N/A self.__failed_list.append((name, actualresult))
1715N/A return BASELINE_MISMATCH
1715N/A return BASELINE_MATCH
430N/A
1715N/A def expectedresult(self, name):
1715N/A # The assumption if we're generating, or if we don't
1715N/A # have a result in baseline, is that the test should pass.
1715N/A if self.__generating:
1715N/A return "pass"
1715N/A return self.__results.get(name, "pass")
430N/A
430N/A def getfailures(self):
430N/A """Return the list of failed tests."""
1715N/A return self.__failed_list
430N/A
430N/A def reportfailures(self):
430N/A """Display all test cases that failed to match the baseline
430N/A and their result.
430N/A """
430N/A lst = self.getfailures()
430N/A if lst:
3143N/A print("", file=sys.stderr)
3143N/A print(self.sep1, file=sys.stderr)
3143N/A print("BASELINE MISMATCH: The following results didn't "
3143N/A "match the baseline.", file=sys.stderr)
3143N/A print(self.sep2, file=sys.stderr)
430N/A for name, result in lst:
3158N/A print("{0}: {1}".format(name, result), file=sys.stderr)
3143N/A print(self.sep2, file=sys.stderr)
3143N/A print("", file=sys.stderr)
430N/A
430N/A def store(self):
430N/A """Store the result set."""
430N/A # Only store the result set if we're generating a baseline
1715N/A if not self.__generating:
430N/A return
430N/A try:
3234N/A f = open(self.__filename, "w")
3171N/A except IOError as xxx_todo_changeme:
3171N/A (err, msg) = xxx_todo_changeme.args
3143N/A print("ERROR: storing baseline:", file=sys.stderr)
3158N/A print("Failed to open {0}: {1}".format(
3158N/A self.__filename, msg), file=sys.stderr)
430N/A return
448N/A
3143N/A # Sort the results to make baseline diffs easier
3234N/A results_sorted = list(self.__results.keys())
3143N/A results_sorted.sort()
3158N/A print("# Writing baseline to {0}.".format(self.__filename),
3143N/A file=sys.stderr)
448N/A for s in results_sorted:
3158N/A f.write("{0}|{1}{2}".format(
3158N/A s, self.__results[s], os.linesep))
430N/A f.flush()
430N/A f.close()
430N/A
430N/A def load(self):
430N/A """Load the result set."""
1715N/A if not os.path.exists(self.__filename):
1715N/A self.__generating = True
430N/A return
430N/A
430N/A try:
3234N/A f = open(self.__filename, "r")
3171N/A except IOError as xxx_todo_changeme1:
3171N/A (err, msg) = xxx_todo_changeme1.args
3143N/A print("ERROR: loading baseline:", file=sys.stderr)
3158N/A print("Failed to open {0}: {1}".format(
3158N/A self.__filename, msg), file=sys.stderr)
430N/A return
430N/A for line in f.readlines():
430N/A n, r = line.split('|')
1715N/A self.__results[n] = r.rstrip('\n')
430N/A f.close()
2499N/A
2499N/Aclass ReadOnlyBaseLine(BaseLine):
2499N/A def store(self):
2499N/A raise NotImplementedError()