blob: 0fd7f13bd5cc3c617d5feab134085c0b41ccb977 [file] [log] [blame]
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +00001"""Template test runner class for running TOSA tests."""
2# Copyright (c) 2020-2022, ARM Limited.
3# SPDX-License-Identifier: Apache-2.0
Eric Kunzee5e26762020-10-13 16:11:07 -07004import json
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +00005from enum import IntEnum
6from pathlib import Path
7
8from checker.tosa_result_checker import LogColors
9from checker.tosa_result_checker import print_color
10from checker.tosa_result_checker import test_check
11from json2fbbin import json2fbbin
Eric Kunzee5e26762020-10-13 16:11:07 -070012
Kevin Cheng550ccc52021-03-03 11:21:43 -080013
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000014class TosaTestInvalid(Exception):
15 """Exception raised for errors loading test description.
Eric Kunzee5e26762020-10-13 16:11:07 -070016
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000017 Attributes:
18 path - full path to missing test description file
19 exception = underlying exception
20 """
Eric Kunzee5e26762020-10-13 16:11:07 -070021
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000022 def __init__(self, path, exception):
23 """Initialize test not found error."""
24 self.path = path
25 self.exception = exception
26 self.message = "Invalid test, could not read test description {}: {}".format(
27 self.path, str(self.exception)
28 )
29 super().__init__(self.message)
Kevin Cheng550ccc52021-03-03 11:21:43 -080030
Eric Kunzee5e26762020-10-13 16:11:07 -070031
32class TosaTestRunner:
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000033 """TOSA Test Runner template class for systems under test."""
Eric Kunzee5e26762020-10-13 16:11:07 -070034
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000035 def __init__(self, args, runnerArgs, testDir):
36 """Initialize and load JSON meta data file."""
Eric Kunzee5e26762020-10-13 16:11:07 -070037 self.args = args
38 self.runnerArgs = runnerArgs
39 self.testDir = testDir
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000040 self.testName = Path(self.testDir).name
Eric Kunzee5e26762020-10-13 16:11:07 -070041
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000042 # Check if we want to run binary and if its already converted
43 descFilePath = Path(testDir, "desc.json")
44 descBinFilePath = Path(testDir, "desc_binary.json")
45 if args.binary:
46 if descBinFilePath.is_file():
47 descFilePath = descBinFilePath
Eric Kunzee5e26762020-10-13 16:11:07 -070048
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000049 try:
50 # Load the json test file
51 with open(descFilePath, "r") as fd:
52 self.testDesc = json.load(fd)
53 except Exception as e:
54 raise TosaTestInvalid(str(descFilePath), e)
55
56 # Convert to binary if needed
57 tosaFilePath = Path(testDir, self.testDesc["tosa_file"])
58 if args.binary and tosaFilePath.suffix == ".json":
59 # Convert tosa JSON to binary
60 json2fbbin.json_to_fbbin(
61 Path(args.flatc_path),
62 Path(args.operator_fbs),
63 tosaFilePath,
64 Path(testDir),
65 )
66 # Write new desc_binary file
67 self.testDesc["tosa_file"] = tosaFilePath.stem + ".tosa"
68 with open(descBinFilePath, "w") as fd:
69 json.dump(self.testDesc, fd, indent=2)
70 descFilePath = descBinFilePath
71
72 # Set location of desc.json (or desc_binary.json) file in use
73 self.descFile = str(descFilePath)
74
75 def skipTest(self):
76 """Check if the test is skipped due to test type selection."""
77 expectedFailure = self.testDesc["expected_failure"]
78 if self.args.test_type == "negative" and not expectedFailure:
79 return True
80 elif self.args.test_type == "positive" and expectedFailure:
81 return True
82 return False
83
84 def runTestGraph(self):
85 """Override with function that calls system under test."""
Eric Kunzee5e26762020-10-13 16:11:07 -070086 pass
87
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +000088 def testResult(self, tosaGraphResult, graphMessage=None):
89 """Work out test result based on graph result and output files."""
90 expectedFailure = self.testDesc["expected_failure"]
91 print_result_line = True
92
93 if tosaGraphResult == TosaTestRunner.TosaGraphResult.TOSA_VALID:
94 if expectedFailure:
95 result = TosaTestRunner.Result.UNEXPECTED_PASS
96 resultMessage = "Expected failure test incorrectly passed"
97 else:
98 # Work through all the results produced by the testing, assuming success
99 # but overriding this with any failures found
100 result = TosaTestRunner.Result.EXPECTED_PASS
101 messages = []
102 for resultNum, resultFileName in enumerate(self.testDesc["ofm_file"]):
103 if "expected_result_file" in self.testDesc:
104 try:
105 conformanceFile = Path(
106 self.testDir,
107 self.testDesc["expected_result_file"][resultNum],
108 )
109 except IndexError:
110 result = TosaTestRunner.Result.INTERNAL_ERROR
111 msg = "Internal error: Missing expected_result_file {} in {}".format(
112 resultNum, self.descFile
113 )
114 messages.append(msg)
115 print(msg)
116 break
117 else:
118 conformanceFile = None
119 resultFile = Path(self.testDir, resultFileName)
120
121 if conformanceFile:
122 print_result_line = False # Checker will print one for us
123 chkResult, tolerance, msg = test_check(
124 str(conformanceFile),
125 str(resultFile),
126 test_name=self.testName,
127 )
128 # Change EXPECTED_PASS assumption if we have any failures
129 if chkResult != 0:
130 result = TosaTestRunner.Result.UNEXPECTED_FAILURE
131 messages.append(msg)
132 if self.args.verbose:
133 print(msg)
134 else:
135 # No conformance file to verify, just check results file exists
136 if not resultFile.is_file():
137 result = TosaTestRunner.Result.UNEXPECTED_FAILURE
138 msg = "Results file is missing: {}".format(resultFile)
139 messages.append(msg)
140 print(msg)
141
142 if resultFile.is_file():
143 # Move the resultFile to allow subsequent system under
144 # tests to create them and to test they have been created
145 resultFile = resultFile.rename(
146 resultFile.with_suffix(
147 ".{}{}".format(self.__module__, resultFile.suffix)
148 )
149 )
150
151 resultMessage = "\n".join(messages) if len(messages) > 0 else None
152 else:
153 if (
154 expectedFailure
155 and tosaGraphResult == TosaTestRunner.TosaGraphResult.TOSA_ERROR
156 ):
157 result = TosaTestRunner.Result.EXPECTED_FAILURE
158 resultMessage = None
159 else:
160 result = TosaTestRunner.Result.UNEXPECTED_FAILURE
161 resultMessage = graphMessage
162
163 if print_result_line:
164 if (
165 result == TosaTestRunner.Result.EXPECTED_FAILURE
166 or result == TosaTestRunner.Result.EXPECTED_PASS
167 ):
168 print_color(LogColors.GREEN, "Results PASS {}".format(self.testName))
169 else:
170 print_color(LogColors.RED, "Results FAIL {}".format(self.testName))
171
172 return result, resultMessage
173
Eric Kunzee5e26762020-10-13 16:11:07 -0700174 class Result(IntEnum):
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +0000175 """Test result codes."""
176
Eric Kunzee5e26762020-10-13 16:11:07 -0700177 EXPECTED_PASS = 0
178 EXPECTED_FAILURE = 1
179 UNEXPECTED_PASS = 2
180 UNEXPECTED_FAILURE = 3
181 INTERNAL_ERROR = 4
Jeremy Johnsonbe1a9402021-12-15 17:14:56 +0000182 SKIPPED = 5
183
184 class TosaGraphResult(IntEnum):
185 """The tosa_graph_result codes."""
186
187 TOSA_VALID = 0
188 TOSA_UNPREDICTABLE = 1
189 TOSA_ERROR = 2
190 OTHER_ERROR = 3