import distro import getpass import jinja2 import json import os import platform import sys import report from report import testSession as testSessionBase from unittest import jsonlog from config import APP_NAME as ROOT_LOGGER_NAME DELAY_SET_GET = 0.1 STATES_SW = (True, False) STATES_BR = range(0, 101, 20) STATES_CT = range(0, 11, 2) class testSession(testSessionBase): def __init__(self, mqtt_client, tcel=report.TCEL_FULL): self.mqtt_client = mqtt_client self.full_test_data = {} param = { jsonlog.TRUN_EXEC_LVL: tcel, 'interpreter': 'python'+sys.version.split(" ")[0] } super().__init__(['__unittest__', ROOT_LOGGER_NAME], **param) # Get testobject information self.mqtt_client.add_callback('__info__', self.__test_object_information__) self.mqtt_client.send('__info__', json.dumps(None)) def __set_base_data__(self, **kwargs): self.full_test_data[jsonlog.MAIN_KEY_SYSTEM_INFO] = self.__system_info__() self.full_test_data[jsonlog.MAIN_KEY_TESTOBJECT_INFO] = {"Name": "-"} self.full_test_data[jsonlog.MAIN_KEY_UNITTEST_INFO] = self.__unittest_information__() self.full_test_data[jsonlog.MAIN_KEY_SPECIFICATION] = self.__specification__() self.full_test_data[jsonlog.MAIN_KEY_LOST_SOULS] = {} self.full_test_data[jsonlog.MAIN_KEY_TESTRUNS] = [] return super().__set_base_data__(**kwargs) def __system_info__(self): system_info = {} system_info[jsonlog.SYSI_ARCHITECTURE] = platform.architecture()[0] system_info[jsonlog.SYSI_MACHINE] = platform.machine() system_info[jsonlog.SYSI_HOSTNAME] = platform.node() system_info[jsonlog.SYSI_DISTRIBUTION] = distro.name() + " " + distro.version() + " (" + distro.codename() + ")" system_info[jsonlog.SYSI_SYSTEM] = platform.system() system_info[jsonlog.SYSI_KERNEL] = platform.release() + ' (%s)' % platform.version() system_info[jsonlog.SYSI_USERNAME] = getpass.getuser() system_info[jsonlog.SYSI_PATH] = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) return system_info def __test_object_information__(self, client, userdata, message): data = json.loads(message.payload) if data is not None: self.full_test_data[jsonlog.MAIN_KEY_TESTOBJECT_INFO]['Name'] = data.get("app_name", "-") self.full_test_data[jsonlog.MAIN_KEY_TESTOBJECT_INFO]['Version'] = data.get("version", {}).get("readable", "-") self.full_test_data[jsonlog.MAIN_KEY_TESTOBJECT_INFO]['Git URL'] = data.get("git", {}).get("url", "-") self.full_test_data[jsonlog.MAIN_KEY_TESTOBJECT_INFO]['Git REF'] = data.get("git", {}).get("ref", "-") def __unittest_information__(self): return {} def __specification__(self): return {} def __finalise__(self): self.full_test_data[jsonlog.MAIN_KEY_LOST_SOULS][jsonlog.LOST_ITEMLIST] = [] self.full_test_data[jsonlog.MAIN_KEY_LOST_SOULS][jsonlog.LOST_TESTCASELIST] = self["uid_list_sorted"][:] self.full_test_data[jsonlog.MAIN_KEY_TESTRUNS].append(self) def export_results_to(self, template_file, path): self.__finalise__() # # Export json testrun data # with open(os.path.join(path, "testrun.json"), "w") as fh: fh.write(json.dumps(self.full_test_data, indent=4)) # # Export jinja templated data # template_path = os.path.dirname(template_file) template_filename = os.path.basename(template_file) jenv = jinja2.Environment(loader=jinja2.FileSystemLoader(template_path)) template = jenv.get_template(template_filename) with open(os.path.join(path, "testrun.tex"), "w") as fh: fh.write(template.render(data=self.full_test_data))