aboutsummaryrefslogtreecommitdiffstats
path: root/conftest.py
blob: fb89772ff95ebcf34612f9caccecb6c06505dafd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding:utf-8 -*-
import pytest
import json
import shutil
import subprocess
from jinja2 import Environment,FileSystemLoader

import plugins.agl_test_conf as conf


@pytest.fixture(scope='session' ,autouse=True)
def setup_compress_function():
    #Before the test start, clean the env
    report_json = conf.TMP_LOGS_DIR + "report.json"
    output = subprocess.run(['ls',report_json],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    if(output.returncode == 0):
        subprocess.run(['rm',report_json])

    #Makdir of TMP_TEST_REPORT and REPORT_LOGS_DIR
    subprocess.run(['mkdir','-p',conf.TMP_TEST_REPORT])
    subprocess.run(['mkdir','-p',conf.REPORT_LOGS_DIR])

    yield
    #Collect report.json from all test sets to generate a report.json for all the test sets
    report_files = conf.TMP_LOGS_DIR + "report_files"
    with open(report_files,'w') as report_f:
        subprocess.run(['find','-name','report.json'],cwd=conf.TMP_LOGS_DIR,stdout=report_f)
    report_f.close()

    #Get the summary data and write to report.json file
    summary_data = get_summary_data(report_files)
    summary_json = conf.TMP_LOGS_DIR + "/report.json"
    with open(summary_json, 'w') as summary_file:
        json.dump(summary_data,summary_file,indent=4,sort_keys=False)
    summary_file.close()

    #Get zip file name
    issue = subprocess.getoutput('cat /etc/issue')
    version = issue[23:-7]
    machine_name = subprocess.getoutput('uname -n')
    date = subprocess.getoutput('date +%Y%m%d')
    zip_name = "agl-test-log-" + version + '-' + machine_name + '-' + date

    #Get summary_html_data
    summary_html_data = format_html_data(summary_data)
    summary_html_data["log_zip_name"] = zip_name

    #Get timestamp
    date_F = subprocess.getoutput("date +%F")
    summary_html_data["date_F"] = date_F
    date_T = subprocess.getoutput("date +%T")
    summary_html_data["date_T"] = date_T

    #Creat summary report in html
    env = Environment(loader=FileSystemLoader(conf.get_tpl_dir()))
    template = env.get_template("all_test_suites_tpl.html")
    html_path = conf.TMP_LOGS_DIR + "test-report/summary-report.html"
    with open(html_path, "w") as html_file:
        html_content = template.render(data=summary_html_data)
        html_file.write(html_content)
    html_file.close()

    #Copy summary report file
    source_file = conf.TMP_LOGS_DIR + "test-report/summary-report.html"
    target_file = conf.REPORT_LOGS_DIR + "summary-report.html"
    shutil.copyfile(source_file,target_file)

    #Package the test report
    base_name = conf.REPORT_LOGS_DIR + zip_name
    root_dir = conf.TMP_LOGS_DIR + "test-report"
    shutil.make_archive(base_name,"zip",root_dir)


#Summarize all reports.json file
def get_summary_data(report_files):
    summary_data = {}
    summary_total = summary_passed = summary_failed = summary_skipped = 0
    files = open(report_files)
    while True:
        report = files.readline()
        if not report:
            break
        report = report[1:-1]
        report_json = conf.TMP_LOGS_DIR + report
        with open(report_json,'r') as f:
            data = json.load(f)

            total = passed = xpassed = failed = xfailed = skipped = 0
            total = data["collected"]
            passed = data["passed"]
            xpassed = data["xpassed"]
            failed = data["failed"]
            xfailed = data["xfailed"]
            skipped = data["skipped"]
            test_status = data["test_status"]
            test_name = data["test_name"]

            this_summary = {
                'total': total,
                'passed': passed,
                'xpassed': xpassed,
                'failed': failed,
                'xfailed': xfailed,
                'skipped': skipped,
                'test_status': test_status,
            }
            summary_data[test_name] = this_summary

            summary_total = summary_total + 1
            if(test_status=="passed"):
                summary_passed = summary_passed + 1
            elif(test_status=="failed"):
                summary_failed = summary_failed + 1
            else:
                summary_skipped = summary_skipped + 1
        f.close()
    summary_data["summary"] = {
        "summary_total": summary_total,
        "summary_passed": summary_passed,
        "summary_failed": summary_failed,
        "summary_skipped": summary_skipped,
    }

    status = ""
    if (summary_data["summary"]["summary_total"] == summary_data["summary"]["summary_skipped"]):
        status = "skipped"
    elif (summary_data["summary"]["summary_failed"] == 0):
        status = "passed"
    else:
        status = "failed"
    summary_data["summary"]["status"] = status

    return summary_data

def format_html_data(summary_data):
    html_data = ""
    #init all rows
    for key in summary_data:
        if(key != "summary"):
            html_data += "\t\t\t<tbody class=\""
            html_data += summary_data[key]["test_status"] + "\">\n"
            html_data += "\t\t\t\t<tr>\n"
            html_data += "\t\t\t\t<td class=\"col-result\">"
            html_data += str.capitalize(summary_data[key]["test_status"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t<td>" + key + "</td>\n"
            html_data += "\t\t\t\t<td>" + str(summary_data[key]["total"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t<td>" + str(summary_data[key]["passed"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t<td>" + str(summary_data[key]["skipped"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t<td>" + str(summary_data[key]["failed"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t<td>" + str(summary_data[key]["xfailed"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t<td>" + str(summary_data[key]["xpassed"])
            html_data += "</td>\n"
            html_data += "\t\t\t\t</tr>\n"
            html_data += "\t\t\t</tbody>\n"

    summary_data["test_suite_table_html"] = html_data

    #Create summry status in html
    summry_status_html = ""
    summry_status_html += "\t\t<p>test suite status : <span class=\""
    summry_status_html += summary_data["summary"]["status"] + "\">"
    summry_status_html += str.capitalize(summary_data["summary"]["status"])
    summry_status_html += "</span></p>"
    summary_data["summry_status_html"] = summry_status_html

    return summary_data