Skip to content

Commit ceebdee

Browse files
authored
test: add unit test cases for github reporting (#299)
Co-authored-by: rick <[email protected]>
1 parent 23355f9 commit ceebdee

File tree

5 files changed

+156
-15
lines changed

5 files changed

+156
-15
lines changed

.github/workflows/build.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ jobs:
5858
sudo atest service status
5959
6060
atest convert -p .github/testing/core.yaml --converter jmeter -t sample.jmx
61-
# - name: Report API Test
62-
# if: github.event.pull_request.user.login == 'linuxsuren'
63-
# uses: thollander/[email protected]
64-
# env:
65-
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
66-
# with:
67-
# filePath: .github/workflows/report.md
68-
# comment_tag: execution
6961
- name: Run JMeter Tests
7062
uses: rbhadti94/[email protected]
7163
with:
@@ -118,6 +110,8 @@ jobs:
118110
- name: Run e2e
119111
env:
120112
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
113+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
114+
PULL_REQUEST: ${{ github.event.number }}
121115
run: |
122116
sudo curl -L https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
123117
sudo chmod u+x /usr/local/bin/docker-compose

e2e/compose.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ services:
66
dockerfile: e2e/Dockerfile
77
environment:
88
GITEE_TOKEN: "$GITEE_TOKEN"
9+
GITHUB_TOKEN: "$GITHUB_TOKEN"
10+
PULL_REQUEST: "$PULL_REQUEST"
911
depends_on:
1012
etcd:
1113
condition: service_healthy
@@ -19,6 +21,10 @@ services:
1921
condition: service_healthy
2022
# minio:
2123
# condition: service_started
24+
volumes:
25+
- type: volume
26+
source: cache
27+
target: /var/data
2228
links:
2329
- etcd
2430
- mysql
@@ -91,3 +97,6 @@ services:
9197
timeout: 30s
9298
retries: 10
9399
start_period: 3s
100+
101+
volumes:
102+
cache:

e2e/entrypoint.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
set -e
33

44
mkdir -p /root/.config/atest
5+
mkdir -p /var/data
56

67
nohup atest server&
8+
cmd="atest run -p test-suite-common.yaml --report github --report-github-identity e2e-testing --report-file /var/data/report.json --report-github-repo linuxsuren/api-testing --report-github-pr ${PULL_REQUEST:-0}"
79

8-
echo "start to run testing"
9-
kind=orm target=mysql:3306 driver=mysql atest run -p test-suite-common.yaml
10-
kind=orm target=mariadb:3306 driver=mysql atest run -p test-suite-common.yaml
11-
kind=etcd target=etcd:2379 atest run -p test-suite-common.yaml
12-
kind=mongodb target=mongo:27017 atest run -p test-suite-common.yaml
10+
echo "start to run testing: $cmd"
11+
kind=orm target=mysql:3306 driver=mysql $cmd
12+
kind=orm target=mariadb:3306 driver=mysql $cmd
13+
kind=etcd target=etcd:2379 $cmd
14+
kind=mongodb target=mongo:27017 $cmd
15+
kind=orm target=postgres:5432 driver=postgres $cmd
1316

1417
# TODO online git repository is unstable, need to fix
1518
# if [ -z "$GITEE_TOKEN" ]
@@ -21,7 +24,6 @@ kind=mongodb target=mongo:27017 atest run -p test-suite-common.yaml
2124
# fi
2225

2326
# TODO need to fix below cases
24-
kind=orm target=postgres:5432 driver=postgres atest run -p test-suite-common.yaml
2527
# kind=s3 target=minio:9000 atest run -p test-suite-common.yaml
2628

2729
cat /root/.config/atest/stores.yaml

pkg/runner/writer_github_pr_comment.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewGithubPRCommentWriter(opt *GithubPRCommentOption) (ReportResultWriter, e
4646
var err error
4747

4848
opt.Token = util.EmptyThenDefault(opt.Token, os.Getenv("GITHUB_TOKEN"))
49-
if opt.Repo == "" || opt.Identity == "" || opt.Token == "" || opt.PR <= 0 {
49+
if opt.Repo == "" || opt.Identity == "" || opt.Token == "" {
5050
err = fmt.Errorf("GitHub report parameters are not enough")
5151
}
5252
return &githubPRCommentWriter{
@@ -88,6 +88,11 @@ func (w *githubPRCommentWriter) loadExistData(newData []ReportResult) (result []
8888
}
8989

9090
func (w *githubPRCommentWriter) Output(result []ReportResult) (err error) {
91+
if w.PR <= 0 {
92+
log.Println("skip reporting to GitHub due to without a valid PR number")
93+
return
94+
}
95+
9196
if result, err = w.loadExistData(result); err != nil {
9297
err = fmt.Errorf("failed to load exist data: %v", err)
9398
return
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2023 API Testing Authors.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
package runner
26+
27+
import (
28+
"net/http"
29+
"os"
30+
"testing"
31+
32+
"github.com/stretchr/testify/assert"
33+
34+
"github.com/h2non/gock"
35+
)
36+
37+
func TestGithubPRCommentWriter(t *testing.T) {
38+
t.Run("lack of parameters", func(t *testing.T) {
39+
_, err := NewGithubPRCommentWriter(&GithubPRCommentOption{})
40+
assert.Error(t, err)
41+
42+
_, err = NewGithubPRCommentWriter(&GithubPRCommentOption{
43+
Repo: "repo",
44+
})
45+
assert.Error(t, err)
46+
47+
_, err = NewGithubPRCommentWriter(&GithubPRCommentOption{
48+
Identity: "id",
49+
Token: "token",
50+
})
51+
assert.Error(t, err)
52+
53+
_, err = NewGithubPRCommentWriter(&GithubPRCommentOption{
54+
Repo: "repo",
55+
Identity: "id",
56+
Token: "token",
57+
})
58+
assert.NoError(t, err)
59+
})
60+
61+
t.Run("pr number is invalid", func(t *testing.T) {
62+
writer, err := NewGithubPRCommentWriter(&GithubPRCommentOption{
63+
Repo: "linuxsuren/test",
64+
Identity: "id",
65+
Token: "token",
66+
})
67+
assert.NoError(t, err)
68+
69+
err = writer.Output(nil)
70+
assert.NoError(t, err)
71+
assert.Nil(t, writer.WithAPIConverage(nil))
72+
})
73+
74+
t.Run("error with getting comments", func(t *testing.T) {
75+
defer gock.Off()
76+
gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusBadRequest)
77+
writer := createWriter(t)
78+
79+
err := writer.Output(nil)
80+
assert.Error(t, err)
81+
})
82+
83+
tmpF, tErr := os.CreateTemp(os.TempDir(), "report")
84+
if !assert.NoError(t, tErr) {
85+
return
86+
}
87+
defer os.RemoveAll(tmpF.Name())
88+
89+
t.Run("create new comment", func(t *testing.T) {
90+
defer gock.Off()
91+
gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusOK).JSON([]comment{})
92+
gock.New("https://api.github.com").Post("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusCreated)
93+
writer := createWriterWithReport(tmpF.Name(), t)
94+
95+
err := writer.Output([]ReportResult{{
96+
API: "/api",
97+
}})
98+
assert.NoError(t, err)
99+
})
100+
101+
t.Run("update comment", func(t *testing.T) {
102+
defer gock.Off()
103+
gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusOK).JSON([]comment{{
104+
ID: 1234,
105+
Body: "id",
106+
}})
107+
gock.New("https://api.github.com").Patch("/repos/linuxsuren/test/issues/comments/1234").Reply(http.StatusOK)
108+
writer := createWriterWithReport(tmpF.Name(), t)
109+
110+
err := writer.Output([]ReportResult{{
111+
API: "/api",
112+
}})
113+
assert.NoError(t, err)
114+
})
115+
}
116+
117+
func createWriter(t *testing.T) ReportResultWriter {
118+
return createWriterWithReport("", t)
119+
}
120+
121+
func createWriterWithReport(report string, t *testing.T) ReportResultWriter {
122+
writer, err := NewGithubPRCommentWriter(&GithubPRCommentOption{
123+
Repo: "linuxsuren/test",
124+
Identity: "id",
125+
Token: "token",
126+
PR: 1,
127+
ReportFile: report,
128+
})
129+
assert.NoError(t, err)
130+
return writer
131+
}

0 commit comments

Comments
 (0)