blob: 180635ac6c66edc607828aa317d204caaa0a7c08 [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):
46 for d in self.decorators:
47 d.setUpDecorator()
48 self.setUpMethod()
49
50 def _oeTearDown(self):
51 for d in self.decorators:
52 d.tearDownDecorator()
53 self.tearDownMethod()
Brad Bishopa34c0302019-09-23 22:34:48 -040054
55class OEPTestResultTestCase:
56 """
57 Mix-in class to provide functions to make interacting with extraresults for
58 the purposes of storing ptestresult data.
59 """
60 @staticmethod
61 def _compress_log(log):
62 logdata = log.encode("utf-8")
63 logdata = zlib.compress(logdata)
64 logdata = base64.b64encode(logdata).decode("utf-8")
65 return {"compressed" : logdata}
66
67 def ptest_rawlog(self, log):
68 if not hasattr(self, "extraresults"):
69 self.extraresults = {"ptestresult.sections" : {}}
70 self.extraresults["ptestresult.rawlogs"] = {"log" : self._compress_log(log)}
71
72 def ptest_section(self, section, duration = None, log = None, logfile = None, exitcode = None):
73 if not hasattr(self, "extraresults"):
74 self.extraresults = {"ptestresult.sections" : {}}
75
76 sections = self.extraresults.get("ptestresult.sections")
77 if section not in sections:
78 sections[section] = {}
79
80 if log is not None:
81 sections[section]["log"] = self._compress_log(log)
82 elif logfile is not None:
83 with open(logfile, "r") as f:
84 sections[section]["log"] = self._compress_log(f.read())
85
86 if duration is not None:
87 sections[section]["duration"] = duration
88 if exitcode is not None:
89 sections[section]["exitcode"] = exitcode
90
91 def ptest_result(self, section, test, result):
92 if not hasattr(self, "extraresults"):
93 self.extraresults = {"ptestresult.sections" : {}}
94
95 sections = self.extraresults.get("ptestresult.sections")
96 if section not in sections:
97 sections[section] = {}
98 resultname = "ptestresult.{}.{}".format(section, test)
99 self.extraresults[resultname] = {"status" : result}
100