Skip to content

Commit

Permalink
test: add unit test cases for github reporting (LinuxSuRen#299)
Browse files Browse the repository at this point in the history
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Nov 30, 2023
1 parent 23355f9 commit ceebdee
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 15 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,6 @@ jobs:
sudo atest service status
atest convert -p .github/testing/core.yaml --converter jmeter -t sample.jmx
# - name: Report API Test
# if: github.event.pull_request.user.login == 'linuxsuren'
# uses: thollander/actions-comment-pull-request@v2.4.3
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# with:
# filePath: .github/workflows/report.md
# comment_tag: execution
- name: Run JMeter Tests
uses: rbhadti94/apache-jmeter-action@v0.5.0
with:
Expand Down Expand Up @@ -118,6 +110,8 @@ jobs:
- name: Run e2e
env:
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PULL_REQUEST: ${{ github.event.number }}
run: |
sudo curl -L https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod u+x /usr/local/bin/docker-compose
Expand Down
9 changes: 9 additions & 0 deletions e2e/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
dockerfile: e2e/Dockerfile
environment:
GITEE_TOKEN: "$GITEE_TOKEN"
GITHUB_TOKEN: "$GITHUB_TOKEN"
PULL_REQUEST: "$PULL_REQUEST"
depends_on:
etcd:
condition: service_healthy
Expand All @@ -19,6 +21,10 @@ services:
condition: service_healthy
# minio:
# condition: service_started
volumes:
- type: volume
source: cache
target: /var/data
links:
- etcd
- mysql
Expand Down Expand Up @@ -91,3 +97,6 @@ services:
timeout: 30s
retries: 10
start_period: 3s

volumes:
cache:
14 changes: 8 additions & 6 deletions e2e/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
set -e

mkdir -p /root/.config/atest
mkdir -p /var/data

nohup atest server&
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}"

echo "start to run testing"
kind=orm target=mysql:3306 driver=mysql atest run -p test-suite-common.yaml
kind=orm target=mariadb:3306 driver=mysql atest run -p test-suite-common.yaml
kind=etcd target=etcd:2379 atest run -p test-suite-common.yaml
kind=mongodb target=mongo:27017 atest run -p test-suite-common.yaml
echo "start to run testing: $cmd"
kind=orm target=mysql:3306 driver=mysql $cmd
kind=orm target=mariadb:3306 driver=mysql $cmd
kind=etcd target=etcd:2379 $cmd
kind=mongodb target=mongo:27017 $cmd
kind=orm target=postgres:5432 driver=postgres $cmd

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

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

cat /root/.config/atest/stores.yaml
Expand Down
7 changes: 6 additions & 1 deletion pkg/runner/writer_github_pr_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewGithubPRCommentWriter(opt *GithubPRCommentOption) (ReportResultWriter, e
var err error

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

func (w *githubPRCommentWriter) Output(result []ReportResult) (err error) {
if w.PR <= 0 {
log.Println("skip reporting to GitHub due to without a valid PR number")
return
}

if result, err = w.loadExistData(result); err != nil {
err = fmt.Errorf("failed to load exist data: %v", err)
return
Expand Down
131 changes: 131 additions & 0 deletions pkg/runner/writer_github_pr_comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
MIT License
Copyright (c) 2023 API Testing Authors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package runner

import (
"net/http"
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/h2non/gock"
)

func TestGithubPRCommentWriter(t *testing.T) {
t.Run("lack of parameters", func(t *testing.T) {
_, err := NewGithubPRCommentWriter(&GithubPRCommentOption{})
assert.Error(t, err)

_, err = NewGithubPRCommentWriter(&GithubPRCommentOption{
Repo: "repo",
})
assert.Error(t, err)

_, err = NewGithubPRCommentWriter(&GithubPRCommentOption{
Identity: "id",
Token: "token",
})
assert.Error(t, err)

_, err = NewGithubPRCommentWriter(&GithubPRCommentOption{
Repo: "repo",
Identity: "id",
Token: "token",
})
assert.NoError(t, err)
})

t.Run("pr number is invalid", func(t *testing.T) {
writer, err := NewGithubPRCommentWriter(&GithubPRCommentOption{
Repo: "linuxsuren/test",
Identity: "id",
Token: "token",
})
assert.NoError(t, err)

err = writer.Output(nil)
assert.NoError(t, err)
assert.Nil(t, writer.WithAPIConverage(nil))
})

t.Run("error with getting comments", func(t *testing.T) {
defer gock.Off()
gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusBadRequest)
writer := createWriter(t)

err := writer.Output(nil)
assert.Error(t, err)
})

tmpF, tErr := os.CreateTemp(os.TempDir(), "report")
if !assert.NoError(t, tErr) {
return
}
defer os.RemoveAll(tmpF.Name())

t.Run("create new comment", func(t *testing.T) {
defer gock.Off()
gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusOK).JSON([]comment{})
gock.New("https://api.github.com").Post("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusCreated)
writer := createWriterWithReport(tmpF.Name(), t)

err := writer.Output([]ReportResult{{
API: "/api",
}})
assert.NoError(t, err)
})

t.Run("update comment", func(t *testing.T) {
defer gock.Off()
gock.New("https://api.github.com").Get("/repos/linuxsuren/test/issues/1/comments").Reply(http.StatusOK).JSON([]comment{{
ID: 1234,
Body: "id",
}})
gock.New("https://api.github.com").Patch("/repos/linuxsuren/test/issues/comments/1234").Reply(http.StatusOK)
writer := createWriterWithReport(tmpF.Name(), t)

err := writer.Output([]ReportResult{{
API: "/api",
}})
assert.NoError(t, err)
})
}

func createWriter(t *testing.T) ReportResultWriter {
return createWriterWithReport("", t)
}

func createWriterWithReport(report string, t *testing.T) ReportResultWriter {
writer, err := NewGithubPRCommentWriter(&GithubPRCommentOption{
Repo: "linuxsuren/test",
Identity: "id",
Token: "token",
PR: 1,
ReportFile: report,
})
assert.NoError(t, err)
return writer
}

0 comments on commit ceebdee

Please sign in to comment.