Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/html2 #23

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config_files/autotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ global:
http_rule_files:
- "./config_files/my_http_api.yml"

#grpc_rule_files:
grpc_rule_files:
# - "./config_files/my_grpc_api.yml"


Expand Down
18 changes: 18 additions & 0 deletions internal/command/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"embed"
"fmt"
"github.com/antchfx/jsonquery"
"github.com/flosch/pongo2/v6"
Expand All @@ -10,6 +11,23 @@ import (
"strings"
)

//go:embed template/*.tpl
var mytpl embed.FS

type ResultInfo struct {
Total int
SuccessCount int
FailedCount int
}

type CaseShow struct {
ID uint64
Description string
State string
Reason string
Link string
}

func templateRender(tplStr string) (string, error) {
// Compile the template first (i. e. creating the AST)
tpl, err := pongo2.FromString(tplStr)
Expand Down
40 changes: 38 additions & 2 deletions internal/command/grpc_automate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"context"
"fmt"
"github.com/lianggaoqiang/progress"
"github.com/vearne/autotest/internal/config"
"github.com/vearne/autotest/internal/model"
Expand Down Expand Up @@ -48,7 +49,7 @@ func GrpcAutomateTest(grpcTestCases map[string][]*config.TestCaseGrpc) {
slog.Info("GrpcTestCases, total:%v, finishCount:%v, successCount:%v, failedCount:%v",
total, finishCount, successCount, failedCount)
// generate report file
GenReportFileGrpc(filePath, tcResultList)
GenReportFileGrpc(filePath, tcResultList, info)
}
slog.Info("[end]GrpcTestCases, total:%v, cost:%v", total, time.Since(begin))
}
Expand Down Expand Up @@ -146,7 +147,7 @@ func HandleSingleFileGrpc(workerNum int, filePath string) (*ResultInfo, []GrpcTe
FailedCount: failedCount}, tcResultList
}

func GenReportFileGrpc(testCasefilePath string, tcResultList []GrpcTestCaseResult) {
func GenReportFileGrpc(testCasefilePath string, tcResultList []GrpcTestCaseResult, info *ResultInfo) {
filename := filepath.Base(testCasefilePath)
name := strings.TrimSuffix(filename, filepath.Ext(filename))
filename = name + ".csv"
Expand All @@ -156,6 +157,7 @@ func GenReportFileGrpc(testCasefilePath string, tcResultList []GrpcTestCaseResul
sort.Slice(tcResultList, func(i, j int) bool {
return tcResultList[i].ID < tcResultList[j].ID
})
// 1. csv file
var records [][]string
records = append(records, []string{"id", "desc", "state", "reason"})
for _, item := range tcResultList {
Expand All @@ -167,4 +169,38 @@ func GenReportFileGrpc(testCasefilePath string, tcResultList []GrpcTestCaseResul
item.Desc, item.State.String(), reasonStr})
}
util.WriterCSV(reportPath, records)
// 2. html file
dirName := util.MD5(reportDirPath + name)

var caseResults []CaseShow
for _, item := range tcResultList {
caseResults = append(caseResults, CaseShow{ID: item.ID, Description: item.Desc,
State: item.State.String(), Reason: item.Reason.String(),
Link: fmt.Sprintf("./%v/%v.html", dirName, item.ID)})
}
obj := map[string]any{
"info": info,
"tcResultList": caseResults,
}
// index file
err := RenderTpl(mytpl, "template/index.tpl", obj, filepath.Join(reportDirPath, name+".html"))
if err != nil {
slog.Error("RenderTpl, %v", err)
return
}

// case file
for _, item := range tcResultList {
data := map[string]any{
"Error": item.Error,
"reqDetail": item.ReqDetail(),
"respDetail": item.RespDetail(),
}
err := RenderTpl(mytpl, "template/case.tpl", data,
filepath.Join(reportDirPath, dirName, strconv.Itoa(int(item.ID))+".html"))
if err != nil {
slog.Error("RenderTpl, %v", err)
return
}
}
}
45 changes: 41 additions & 4 deletions internal/command/grpc_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/fullstorydev/grpcurl"

// ignore SA1019 we have to import this because it appears in exported API
"github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/jhump/protoreflect/desc"
Expand Down Expand Up @@ -31,6 +32,38 @@ type GrpcTestCaseResult struct {
Request config.RequestGrpc
TestCase *config.TestCaseGrpc
KeyValues map[string]any
Error error
Response *model.GrpcResp
}

func (t *GrpcTestCaseResult) ReqDetail() string {
var builder strings.Builder
builder.WriteString(fmt.Sprintf("ADDRESS: %v\n", t.Request.Address))
builder.WriteString(fmt.Sprintf("SYMBOL: %v\n", t.Request.Symbol))
builder.WriteString("HEADERS:\n")
for _, item := range t.Request.Headers {
builder.WriteString(fmt.Sprintf("%v\n", item))
}
builder.WriteString("BODY:\n")
builder.WriteString(fmt.Sprintf("%v\n", t.Request.Body))
return builder.String()
}

func (t *GrpcTestCaseResult) RespDetail() string {
if t.Response == nil {
return ""
}

var builder strings.Builder
builder.WriteString(fmt.Sprintf("GRPC.CODE: %v\n", t.Response.Code))
builder.WriteString(fmt.Sprintf("GRPC.MESSAGE %v\n", t.Response.Message))
builder.WriteString("HEADERS:\n")
for _, item := range t.Response.Headers {
builder.WriteString(fmt.Sprintf("%v\n", item))
}
builder.WriteString("BODY:\n")
builder.WriteString(fmt.Sprintf("%v\n", t.Response.Body))
return builder.String()
}

type GrpcTestCallable struct {
Expand All @@ -40,7 +73,6 @@ type GrpcTestCallable struct {

func (m *GrpcTestCallable) Call(ctx context.Context) *executor.GPResult {
r := executor.GPResult{}
var dialErr error
var cc *grpc.ClientConn
var rf grpcurl.RequestParser
var formatter grpcurl.Formatter
Expand Down Expand Up @@ -94,6 +126,7 @@ func (m *GrpcTestCallable) Call(ctx context.Context) *executor.GPResult {
if err != nil {
tcResult.State = model.StateFailed
tcResult.Reason = model.ReasonTemplateRenderError
tcResult.Error = err
r.Value = tcResult
r.Err = err
return &r
Expand All @@ -117,12 +150,12 @@ func (m *GrpcTestCallable) Call(ctx context.Context) *executor.GPResult {
goto ERROR
}

cc, dialErr = dial(reqInfo.Address)
if dialErr != nil {
cc, err = dial(reqInfo.Address)
if err != nil {
zaplog.Error("GrpcTestCallable-dial",
zap.Uint64("testCaseId", m.testcase.ID),
zap.String("address", reqInfo.Address),
zap.Error(dialErr),
zap.Error(err),
)
goto ERROR
}
Expand Down Expand Up @@ -154,6 +187,8 @@ func (m *GrpcTestCallable) Call(ctx context.Context) *executor.GPResult {
goto ERROR
}

tcResult.Response = &handler.resp

if resource.GlobalConfig.Global.Debug {
debugPrint(reqInfo, handler.resp)
}
Expand Down Expand Up @@ -187,6 +222,7 @@ func (m *GrpcTestCallable) Call(ctx context.Context) *executor.GPResult {
ERROR:
tcResult.State = model.StateFailed
tcResult.Reason = model.ReasonRequestFailed
tcResult.Error = err
r.Value = tcResult
r.Err = err
return &r
Expand Down Expand Up @@ -237,6 +273,7 @@ func getDescSourceWitchCache(ctx context.Context, address string) (grpcurl.Descr
})
if err != nil {
zaplog.Error("getDescSourceWitchCache", zap.Error(err))
return nil, err
}

s = v.(grpcurl.DescriptorSource)
Expand Down
18 changes: 1 addition & 17 deletions internal/command/http_automate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ import (
"time"
)

//go:embed template/*.tpl
var mytpl embed.FS

type ResultInfo struct {
Total int
SuccessCount int
FailedCount int
}

type CaseShow struct {
ID uint64
Description string
State string
Reason string
Link string
}

func HttpAutomateTest(httpTestCases map[string][]*config.TestCaseHttp) {
total := 0
for _, testcases := range httpTestCases {
Expand Down Expand Up @@ -117,6 +100,7 @@ func GenReportFileHttp(testCasefilePath string, tcResultList []HttpTestCaseResul
return
}

// case file
for _, item := range tcResultList {
data := map[string]any{
"Error": item.Error,
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
version = "v0.1.5"
version = "v0.1.6"
)

func main() {
Expand Down
Loading