Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 2 | # Copyright (C) 2016 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 6 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 7 | import base64 |
| 8 | import zlib |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 9 | import unittest |
| 10 | |
| 11 | from oeqa.core.exception import OEQAMissingVariable |
| 12 | |
| 13 | def _validate_td_vars(td, td_vars, type_msg): |
| 14 | if td_vars: |
| 15 | for v in td_vars: |
| 16 | if not v in td: |
| 17 | raise OEQAMissingVariable("Test %s need %s variable but"\ |
| 18 | " isn't into td" % (type_msg, v)) |
| 19 | |
| 20 | class OETestCase(unittest.TestCase): |
| 21 | # TestContext and Logger instance set by OETestLoader. |
| 22 | tc = None |
| 23 | logger = None |
| 24 | |
| 25 | # td has all the variables needed by the test cases |
| 26 | # is the same across all the test cases. |
| 27 | td = None |
| 28 | |
| 29 | # td_vars has the variables needed by a test class |
| 30 | # or test case instance, if some var isn't into td a |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 31 | # OEQAMissingVariable exception is raised |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | td_vars = None |
| 33 | |
| 34 | @classmethod |
| 35 | def _oeSetUpClass(clss): |
| 36 | _validate_td_vars(clss.td, clss.td_vars, "class") |
Brad Bishop | c8f4712 | 2019-06-24 09:36:18 -0400 | [diff] [blame] | 37 | if hasattr(clss, 'setUpHooker') and callable(getattr(clss, 'setUpHooker')): |
| 38 | clss.setUpHooker() |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 39 | clss.setUpClassMethod() |
| 40 | |
| 41 | @classmethod |
| 42 | def _oeTearDownClass(clss): |
| 43 | clss.tearDownClassMethod() |
| 44 | |
| 45 | def _oeSetUp(self): |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 46 | try: |
| 47 | for d in self.decorators: |
| 48 | d.setUpDecorator() |
| 49 | except: |
| 50 | for d in self.decorators: |
| 51 | d.tearDownDecorator() |
| 52 | raise |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 53 | self.setUpMethod() |
| 54 | |
| 55 | def _oeTearDown(self): |
| 56 | for d in self.decorators: |
| 57 | d.tearDownDecorator() |
| 58 | self.tearDownMethod() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 59 | |
| 60 | class OEPTestResultTestCase: |
| 61 | """ |
| 62 | Mix-in class to provide functions to make interacting with extraresults for |
| 63 | the purposes of storing ptestresult data. |
| 64 | """ |
| 65 | @staticmethod |
| 66 | def _compress_log(log): |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 67 | logdata = log.encode("utf-8") if isinstance(log, str) else log |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 68 | logdata = zlib.compress(logdata) |
| 69 | logdata = base64.b64encode(logdata).decode("utf-8") |
| 70 | return {"compressed" : logdata} |
| 71 | |
| 72 | def ptest_rawlog(self, log): |
| 73 | if not hasattr(self, "extraresults"): |
| 74 | self.extraresults = {"ptestresult.sections" : {}} |
| 75 | self.extraresults["ptestresult.rawlogs"] = {"log" : self._compress_log(log)} |
| 76 | |
| 77 | def ptest_section(self, section, duration = None, log = None, logfile = None, exitcode = None): |
| 78 | if not hasattr(self, "extraresults"): |
| 79 | self.extraresults = {"ptestresult.sections" : {}} |
| 80 | |
| 81 | sections = self.extraresults.get("ptestresult.sections") |
| 82 | if section not in sections: |
| 83 | sections[section] = {} |
| 84 | |
| 85 | if log is not None: |
| 86 | sections[section]["log"] = self._compress_log(log) |
| 87 | elif logfile is not None: |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 88 | with open(logfile, "rb") as f: |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 89 | sections[section]["log"] = self._compress_log(f.read()) |
| 90 | |
| 91 | if duration is not None: |
| 92 | sections[section]["duration"] = duration |
| 93 | if exitcode is not None: |
| 94 | sections[section]["exitcode"] = exitcode |
| 95 | |
| 96 | def ptest_result(self, section, test, result): |
| 97 | if not hasattr(self, "extraresults"): |
| 98 | self.extraresults = {"ptestresult.sections" : {}} |
| 99 | |
| 100 | sections = self.extraresults.get("ptestresult.sections") |
| 101 | if section not in sections: |
| 102 | sections[section] = {} |
| 103 | resultname = "ptestresult.{}.{}".format(section, test) |
| 104 | self.extraresults[resultname] = {"status" : result} |
| 105 | |