From ceebdee3812dc5acf0af2cf4c77d673777090358 Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 30 Nov 2023 18:11:25 +0800 Subject: [PATCH] test: add unit test cases for github reporting (#299) Co-authored-by: rick --- .github/workflows/build.yaml | 10 +- e2e/compose.yaml | 9 ++ e2e/entrypoint.sh | 14 ++- pkg/runner/writer_github_pr_comment.go | 7 +- pkg/runner/writer_github_pr_comment_test.go | 131 ++++++++++++++++++++ 5 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 pkg/runner/writer_github_pr_comment_test.go diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index e158a554..777652c9 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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: @@ -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 diff --git a/e2e/compose.yaml b/e2e/compose.yaml index de56772a..2d61fc65 100644 --- a/e2e/compose.yaml +++ b/e2e/compose.yaml @@ -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 @@ -19,6 +21,10 @@ services: condition: service_healthy # minio: # condition: service_started + volumes: + - type: volume + source: cache + target: /var/data links: - etcd - mysql @@ -91,3 +97,6 @@ services: timeout: 30s retries: 10 start_period: 3s + +volumes: + cache: diff --git a/e2e/entrypoint.sh b/e2e/entrypoint.sh index 60e511c2..d67b3191 100755 --- a/e2e/entrypoint.sh +++ b/e2e/entrypoint.sh @@ -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" ] @@ -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 diff --git a/pkg/runner/writer_github_pr_comment.go b/pkg/runner/writer_github_pr_comment.go index 4036b0b1..2c95d8d2 100644 --- a/pkg/runner/writer_github_pr_comment.go +++ b/pkg/runner/writer_github_pr_comment.go @@ -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{ @@ -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 diff --git a/pkg/runner/writer_github_pr_comment_test.go b/pkg/runner/writer_github_pr_comment_test.go new file mode 100644 index 00000000..982379da --- /dev/null +++ b/pkg/runner/writer_github_pr_comment_test.go @@ -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 +}