blob: bc4446a938ec31eef0d3012bbfd44802ab63980b [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002# Copyright (C) 2016 Intel Corporation
Brad Bishopc342db32019-05-15 21:57:59 -04003#
4# SPDX-License-Identifier: MIT
5#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006
Brad Bishopa34c0302019-09-23 22:34:48 -04007import base64
8import zlib
Brad Bishop6e60e8b2018-02-01 10:27:11 -05009import unittest
10
11from oeqa.core.exception import OEQAMissingVariable
12
13def _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
20class 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 Bishopd7bf8c12018-02-25 22:55:05 -050031 # OEQAMissingVariable exception is raised
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032 td_vars = None
33
34 @classmethod
35 def _oeSetUpClass(clss):
36 _validate_td_vars(clss.td, clss.td_vars, "class")
Brad Bishopc8f47122019-06-24 09:36:18 -040037 if hasattr(clss, 'setUpHooker') and callable(getattr(clss, 'setUpHooker')):
38 clss.setUpHooker()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050039 clss.setUpClassMethod()
40
41 @classmethod
42 def _oeTearDownClass(clss):
43 clss.tearDownClassMethod()
44
45 def _oeSetUp(self):
Andrew Geisslerc926e172021-05-07 16:11:35 -050046 try:
47 for d in self.decorators:
48 d.setUpDecorator()
49 except:
50 for d in self.decorators:
51 d.tearDownDecorator()
52 raise
Brad Bishop6e60e8b2018-02-01 10:27:11 -050053 self.setUpMethod()
54
55 def _oeTearDown(self):
56 for d in self.decorators:
57 d.tearDownDecorator()
58 self.tearDownMethod()
Brad Bishopa34c0302019-09-23 22:34:48 -040059
60class 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 Bishop00e122a2019-10-05 11:10:57 -040067 logdata = log.encode("utf-8") if isinstance(log, str) else log
Brad Bishopa34c0302019-09-23 22:34:48 -040068 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 Bishop00e122a2019-10-05 11:10:57 -040088 with open(logfile, "rb") as f:
Brad Bishopa34c0302019-09-23 22:34:48 -040089 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