aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/agl_test_report.py
blob: 4376d35bd6b24d16738e341b5d89eaa91334015e (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import json
import shutil
import zipfile
import subprocess
from jinja2 import Environment,FileSystemLoader

import plugins.agl_test_conf as conf

#Compress the tmp log to .zip
def log_compress(THIS_TEST):
    base_name = conf.get_log_dir(THIS_TEST)
    root_dir = conf.get_tmp_log_dir(THIS_TEST)
    shutil.make_archive(base_name,'zip',root_dir)


#Get all test cases status
#The type of test_cases_values_and_status is list,it's looks like that:
#[['test_id', 'values', 'status'], ['rpm01', 'TEST-PASS', 'passed'],....]
#The type of case_status is directory,it's looks like:
#{'rpm03': 'passed', 'rpm02': 'passed', 'rpm01': 'passed'}
def get_case_status(test_cases_values_and_status):
    num = len(test_cases_values_and_status)
    case_status = {}
    for i in range(num):
        if (i==0):
            continue
        case_status[test_cases_values_and_status[i][0]] = test_cases_values_and_status[i][2]
    return case_status

#Input format
# {
#  "test_id": ['test_id', 'values', 'status'],
#  "test_id": ['test_id', 'values', 'status']
# }
#Output format
# {
#  'test_id': 'status',
#  'test_id': 'status'
# }
def format_caselist(case_info_list: dict):
    case_status = dict()
    for key in case_info_list:
        case_info = case_info_list[key]
        case_status[key] = case_info[2]
    return case_status


#Input format
# {
#  "test_id": ['test_id', 'values', 'status'],
#  "test_id": ['test_id', 'values', 'status']
# }
#Output format
# {
#  'collected': collected_num,
#  'passed': passed_num,
#  'failed': failed_num,
#  'skipped": skipped_num
# }
def format_summary(case_info_list):
    collected_num = passed_num = xpassed_num = failed_num = xfailed_num = skipped_num = 0
    for key in case_info_list:
        case_info = case_info_list[key]
        collected_num = collected_num + 1
        if (case_info[2] == "passed"):
            passed_num = passed_num + 1
        elif (case_info[2] == "xpassed"):
            xpassed_num = xpassed_num + 1
        elif (case_info[2] == "failed"):
            failed_num = failed_num + 1
        elif (case_info[2] == "xfailed"):
            xfailed_num = xfailed_num + 1
        else:
            skipped_num = skipped_num + 1
    summary = dict()
    summary["collected"] = collected_num
    summary["passed"] = passed_num
    summary["xpassed"] = xpassed_num
    summary["failed"] = failed_num
    summary["xfailed"] = xfailed_num
    summary["skipped"] = skipped_num
    return summary

def format_json_data(name, test_set_status, summary, case_status):
    data = summary
    data["test_name"] = name
    data["test_status"] = test_set_status
    data["case_status"] = case_status
    return summary

#Write the test result to a json file
def write_data_to_json(THIS_TEST,test_set_status,summary,case_status):
    #The data that will be written into the json file
    data =  {
                'test_status': test_set_status,
                'test_name': THIS_TEST,
                'collected': summary[0][1],
                'passed': summary[1][1],
                'xpassed': summary[2][1],
                'failed': summary[3][1],
                'xfailed': summary[4][1],
                'skipped': summary[5][1],
                'case_status': case_status
            }

    #Write the "data" to the json file
    write_data_to_file(data, "json")

def write_to_html_file(THIS_TEST,html):
    html_path = conf.get_html_filename(THIS_TEST)
    html_file = open(html_path,"w")
    html_file.write(html)
    html_file.close()

def create_gen_web_page(filename, tlpname, html_data):
    #Get timestamp
    date_F = subprocess.getoutput("date +%F")
    html_data["date_F"] = date_F
    date_T = subprocess.getoutput("date +%T")
    html_data["date_T"] = date_T
    # here, jinja2 module should be installed first
    from jinja2 import Environment,FileSystemLoader
    env = Environment(loader=FileSystemLoader(conf.get_tpl_dir()))
    template = env.get_template(tlpname)
    with open(filename,'w+') as f:
        html_content = template.render(data=html_data)
        f.write(html_content)
    f.close()

def write_data_to_file(data, file_type = "json"):
    testname = data["test_name"]
    if (file_type == "json"):
        filename = conf.get_json_filename(testname)
        with open(filename,'w') as f:
            json.dump(data,f,indent=4,sort_keys=False)
        f.close()
    elif (file_type == "html"):
        filename = conf.get_html_filename(testname)
        tlpname = conf.get_default_html_tpl()
        create_gen_web_page(filename, tlpname, data)
    else:
        #TODO waiting for adding
        print("ERROR: file type %s is error. Expect json or html." % file_type)

def change_json_to_html(test_set_name):
    json_file = conf.get_json_filename(test_set_name)
    html_file = conf.get_html_filename(test_set_name)
    #read data from json file
    summary_data = dict()
    with open(json_file,'r') as f:
        summary_data = json.load(f)
    f.close()

    #format json data to html data
    html_data = format_html_data(summary_data)

    #save data to html file
    write_data_to_file(html_data, "html")

#def format_case_status_table(case_status):
def format_html_data(summary_data):
    #init all rows
    case_status = summary_data["case_status"]
    html_data = ""
    for key in case_status:
        html_data += "\t\t\t<tbody class=\"" +  case_status[key] + "\">\n"
        html_data += "\t\t\t\t<tr>\n"
        html_data += "\t\t\t\t<td class=\"col-result\">"
        html_data += str.capitalize(case_status[key]) +"</td>\n"
        html_data += "\t\t\t\t<td>" + key  + "</td>\n"
        html_data += "\t\t\t\t</tr>\n"
        html_data += "\t\t\t</tbody>\n"
    summary_data["case_status_html"] = html_data

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

    return summary_data

def generate_total_summary_files(time_stamp):
    #Get the summary data and write to report.json file
    test_list = conf.TMP_LOGS_DIR + "test_list_" + str(time_stamp) + ".json"
    summary_data = get_summary_data(test_list)
    if(summary_data['summary']['summary_total'] == 0):
        return 0
    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')
    zip_name = "agl-test-log-" + version + '-' + machine_name + '-' + time_stamp

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

    #Get current timestamp for total summary html report
    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 total 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 total 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"
    make_zip_file(test_list, base_name, root_dir)

def make_zip_file(test_list, base_name, root_dir):
    with open(test_list, 'r') as f:
        test_suites = json.load(f)
    f.close()
    zip_name = base_name + ".zip"
    zipf = zipfile.ZipFile(zip_name, 'a')
    summ_file = root_dir + "/" + "summary-report.html"
    zipf.write(summ_file, "summary-report.html")

    for key in test_suites.keys():
        sub_dir = root_dir + "/" + key + "/"
        zipf.write(sub_dir, key)
        if(test_suites[key]["status"] == "run"):
            zipf.write(sub_dir+"/log.zip", key+"/log.zip")
            zipf.write(sub_dir+"/report.html", key+"/report.html")
    zipf.close()

#Summarize all reports.json file
def get_summary_data(test_list):
    summary_data = {}
    summary_total = summary_passed = summary_failed = summary_skipped = 0
    with open(test_list,'r') as f:
        test_suites = json.load(f)
    f.close()

    for key in test_suites.keys():
        if(test_suites[key]["status"] == "skip"):
            this_summary = {
                'total': "null",
                'passed': "null",
                'xpassed': "null",
                'failed': "null",
                'xfailed': "null",
                'skipped': "null",
                'test_status': "skipped",
            }
            summary_data[key] = this_summary

            summary_total = summary_total + 1
            summary_skipped = summary_skipped + 1

        if(test_suites[key]["status"] == "run"):
            report = test_suites[key]["path"]
            report_json = conf.TMP_LOGS_DIR + report
            with open(report_json,'r') as f:
                data = json.load(f)
            f.close()

            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[key] = 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

    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_total_summary_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