• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16package rec
17
18import (
19	"code.cloudfoundry.org/archiver/compressor"
20	"fmt"
21	"fotff/tester"
22	"fotff/utils"
23	"github.com/jedib0t/go-pretty/v6/table"
24	"github.com/jedib0t/go-pretty/v6/text"
25	"github.com/sirupsen/logrus"
26	"reflect"
27	"sort"
28)
29
30const css = `<head>
31<style type="text/css">
32table{
33    border:1px solid;
34    border-spacing: 0;
35}
36th{
37    font-size: 11;
38	border:1px solid;
39    padding: 10px;
40	background-color: rgb(137,190,178);
41}
42td{
43    font-size: 11;
44    border:1px solid;
45    padding: 10px;
46	background-color: rgb(160,191,124);
47}
48.bg-red{
49	background-color: rgb(220,87,18);
50}
51.bg-yellow{
52	background-color: rgb(244,208,0);
53}
54</style>
55</head>
56`
57
58func Report(curPkg string, taskName string) {
59	subject := fmt.Sprintf("[%s] %s test report", curPkg, taskName)
60	rt := reflect.TypeOf(Record{})
61	tb := table.NewWriter()
62	tb.SetIndexColumn(rt.NumField() + 1)
63	var row = table.Row{"test case"}
64	for i := 0; i < rt.NumField(); i++ {
65		f := rt.Field(i)
66		if f.IsExported() {
67			row = append(row, f.Tag.Get("col"))
68		}
69	}
70	tb.AppendHeader(row)
71	tb.SetRowPainter(func(row table.Row) text.Colors {
72		for _, col := range row {
73			if str, ok := col.(string); ok {
74				if str == tester.ResultFail {
75					return text.Colors{text.BgRed}
76				} else if str == tester.ResultOccasionalFail {
77					return text.Colors{text.BgYellow}
78				}
79			}
80		}
81		return nil
82	})
83	var rows []table.Row
84	for k, rec := range Records {
85		var row = table.Row{k}
86		rv := reflect.ValueOf(rec)
87		for i := 0; i < rv.NumField(); i++ {
88			if rv.Field(i).CanInterface() {
89				row = append(row, rv.Field(i).Interface())
90			}
91		}
92		rows = append(rows, row)
93	}
94	sort.Slice(rows, func(i, j int) bool {
95		return rows[i][0].(string) < rows[j][0].(string)
96	})
97	tb.AppendRows(rows)
98	c := compressor.NewTgz()
99	var attrs []string
100	if utils.LogFile != nil {
101		if err := c.Compress(utils.LogFile.Name(), utils.LogFile.Name()+".tgz"); err != nil {
102			logrus.Errorf("failed to compress %s: %v", utils.LogFile.Name(), err)
103		} else {
104			attrs = append(attrs, utils.LogFile.Name()+".tgz")
105		}
106	}
107	if utils.StdoutFile != nil {
108		if err := c.Compress(utils.StdoutFile.Name(), utils.StdoutFile.Name()+".tgz"); err != nil {
109			logrus.Errorf("failed to compress %s: %v", utils.StdoutFile.Name(), err)
110		} else {
111			attrs = append(attrs, utils.StdoutFile.Name()+".tgz")
112		}
113	}
114	if err := utils.SendMail(subject, css+tb.RenderHTML(), attrs...); err != nil {
115		logrus.Errorf("failed to send report mail: %v", err)
116		return
117	}
118	logrus.Infof("send mail successfully")
119}
120