awk优化钉钉通知测试报告

一、背景

在之前的博客 Go服务自动触发单元测试覆盖率 中钉钉通知的效果实现如下图:

最近RD提出对本次和上次覆盖率的比对需求,并把比对结果也显示在钉钉通知上。

二、实现思路

要实现数据比对,就需要对上次的数据进行存储,那该怎么存储呢?

1、存储到MySQL

2、以文件的形式存储到slave机器上,然后再读取文件获得上次的数据

因为就只是一个数据而已,所以就不劳烦MySQL了,确定选择方法2。

go test命令输出的原始数据格式:

localhost:z_data aaa$ cat this_yuanshi.txt 
ok      k-p/dashboard/api    0.014s    coverage: 85.7% of statements
FAIL    k-p/dashboard/auth [build failed]
ok      k-p/dashboard/auth/api    0.013s    coverage: 53.8% of statements
ok      k-p/dashboard/auth/jwe    3.527s    coverage: 61.0% of statements
ok      k-p/dashboard/cert/ecdsa    0.014s    coverage: 13.2% of statements
FAIL    k-p/dashboard/client [build failed]
ok      k-p/dashboard/client/api    0.318s    coverage: 16.7% of statements
ok      k-p/dashboard/client/csrf    0.020s    coverage: 88.2% of statements
2020-07-22T15:39:57.371+0800    DEBUG    logger/logger.go:53    logger init ok
--- FAIL: TestInit (0.00s)
panic: open /workspace/code/xes/k-p/cicd/conf/conf.json: no such file or directory [recovered]
    panic: open /workspace/code/xes/k-p/cicd/conf/conf.json: no such file or directory

goroutine 7 [running]:

覆盖率取coverage后百分比的平均数,前提是本行记录以“ok”开头。

目标有了,那怎么拿到覆盖率?分两步走:

第一步:把符合以“ok”开头的行写入一个新txt文件,如:guolv.txt

find this_yuanshi.txt | xargs cat | grep ^ok > guolv.txt

第二步:求guolv.txt中每行coverage后百分比的平均数

this_num=$(awk 'BEGIN{count=0}{count+=$5}END{printf("%.1f%%",count/NR)}' guolv.txt)

有了数据,接下来就是 发送钉钉通知 和 本次数据覆盖上次数据 两步。

完整的脚本:

#!/bin/bash
source /etc/profile
source /Users/aaa/.bash_profile
set +x
this_path="/Users/xes/CI/reports/***/z_data"

git clone https://***/***/***.git k-p

cd kubernetes-platform

echo "生成.out文件"
go test ./... -coverprofile=cover.out > $this_path/this_yuanshi.txt

cd $this_path
#获取本次的覆盖率
find this_yuanshi.txt | xargs cat | grep ^ok > guolv.txt
this_num=$(awk 'BEGIN{count=0}{count+=$5}END{printf("%.1f%%",count/NR)}' guolv.txt)

#获取上次的覆盖率,如果没有则创建文件
if [./last_num.txt]
then
    last_num=$(cat ./last_num.txt)
    echo $last_num
else
    touch ./last_num.txt
fi

cd -
echo "生成Html报告"
go tool cover -html=cover.out -o coverage.html

#发送钉钉通知
cd $this_path
this_time=$(cat this_num.txt)
last_time=$(cat last_num.txt)
now=$(echo $this_time|cut -b 1,2,3,4)
last=$(echo $last_time|cut -b 1,2,3,4)
change=$(echo | awk "{print $now - $last}")
json='{"msgtype":"link","link":{"text":"本次:'"$this_time"'   上次:'"$last_time"'   变化:'"$change%"'\n\n点击查看本次和上次的HTML报告","title":"k-p单元测试覆盖率统计","picUrl":"https://***/***/avatar1/18b85ab1eddf66dd5be67d62489db637_s120.jpg ","messageUrl":"http://***:8080/***-platform-unitcover"}}' 
curl -H "Content-Type:application/json;charset=utf-8" -X POST -d "$json" https://oapi.dingtalk.com/robot/send?access_token=29d4bf78f0105e66f6d8d387bb4658ed42a6979452b6fdc7232527***

#把本次的覆盖率赋值给上次
cp ./this_num.txt ./last_num.txt

三、优化后效果

 

posted @ 2020-11-02 14:36  海布里Simple  阅读(233)  评论(0编辑  收藏  举报