Skip to content

Commit

Permalink
feat: support to set the prefix of mock server (#402) (#410)
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 Apr 30, 2024
1 parent ffac34f commit 02026b2
Show file tree
Hide file tree
Showing 44 changed files with 1,677 additions and 1,142 deletions.
2 changes: 1 addition & 1 deletion .github/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

make fmt test-all
make fmt test
20 changes: 20 additions & 0 deletions .github/workflows/qodana_code_quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Qodana
on:
workflow_dispatch:
pull_request:
push:
branches:
- master
- feat/mock-timer

jobs:
qodana:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2024.1
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
119 changes: 30 additions & 89 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,28 @@ jobs:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GH_PUBLISH_SECRETS }}
- name: Log into registry ${{ env.REGISTRY_DOCKERHUB }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY_DOCKERHUB }}
username: linuxsuren
password: ${{ secrets.DOCKER_HUB_PUBLISH_SECRETS }}
- name: Log into registry ${{ env.REGISTRY_ALIYUN }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY_ALIYUN }}
username: ${{ secrets.REGISTRY_ALIYUN_USER }}
password: ${{ secrets.REGISTRY_ALIYUN_PUBLISH_SECRETS }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/linuxsuren/api-testing
images: |
${{ env.REGISTRY }}/linuxsuren/api-testing
${{ env.REGISTRY_DOCKERHUB }}/${{ env.IMAGE_NAME }}
${{ env.REGISTRY_ALIYUN }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
Expand All @@ -91,10 +108,21 @@ jobs:
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
platforms: linux/amd64,linux/arm64,darwin/amd64
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: VERSION=${{ steps.vars.outputs.tag }}
- name: Release Helm
run: |
echo ${{ secrets.DOCKER_HUB_PUBLISH_SECRETS }} | helm registry login docker.io -u linuxsuren --password-stdin
export APP_VERSION=${{ steps.vars.outputs.tag }}
export HELM_VERSION=${{ steps.vars.outputs.tag }}
if [[ ! "$line" =~ ^v ]]
then
unset APP_VERSION
unset HELM_VERSION
fi
make helm-package helm-push
# image-operator:
# runs-on: ubuntu-20.04
Expand Down Expand Up @@ -126,90 +154,3 @@ jobs:
# platforms: linux/amd64,linux/arm64
# cache-from: type=gha
# cache-to: type=gha,mode=max

image-dockerhub:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-tags: true
fetch-depth: 0
- name: Set output
id: vars
run: echo "tag=$(git describe --tags)" >> $GITHUB_OUTPUT
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
- name: Log into registry ${{ env.REGISTRY_DOCKERHUB }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY_DOCKERHUB }}
username: linuxsuren
password: ${{ secrets.DOCKER_HUB_PUBLISH_SECRETS }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY_DOCKERHUB }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: VERSION=${{ steps.vars.outputs.tag }}
- name: Release Helm
run: |
echo ${{ secrets.DOCKER_HUB_PUBLISH_SECRETS }} | helm registry login docker.io -u linuxsuren --password-stdin
export APP_VERSION=${{ steps.vars.outputs.tag }}
export HELM_VERSION=${{ steps.vars.outputs.tag }}
if [[ ! "$line" =~ ^v ]]
then
unset APP_VERSION
unset HELM_VERSION
fi
make helm-package helm-push
image-aliyuncs:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-tags: true
fetch-depth: 0
- name: Set output
id: vars
run: echo "tag=$(git describe --tags)" >> $GITHUB_OUTPUT
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf
- name: Log into registry ${{ env.REGISTRY_ALIYUN }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY_ALIYUN }}
username: ${{ secrets.REGISTRY_ALIYUN_USER }}
password: ${{ secrets.REGISTRY_ALIYUN_PUBLISH_SECRETS }}
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY_ALIYUN }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: VERSION=${{ steps.vars.outputs.tag }}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ test-ui-e2e:
test-operator:
cd operator && make test # converage file path: operator/cover.out
test-all-backend: test
test-all: test-all-backend test-ui
test-all: test-all-backend
test-e2e:
cd e2e && ./start.sh && ./start.sh compose-k8s.yaml && ./start.sh compose-external.yaml
fuzz:
Expand Down
6 changes: 3 additions & 3 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

* 支持的协议: HTTP, gRPC, tRPC
* 支持多种格式的测试结果导出: Markdown, HTML, PDF, Stdout
* 通过简单配置模拟服务
* 简单易用的 Mock 服务,支持 OpenAPI
* 支持转换为 [JMeter](https://jmeter.apache.org/) 文件格式
* 支持响应体字段检查或 [eval](https://expr.medv.io/)
* 使用 [JSON schema] 校验响应参数(https://json-schema.org/)
Expand Down Expand Up @@ -133,6 +133,6 @@ docker run -p 1234:8080 -v /var/tmp:/var/tmp \
`api-testing-tech@googlegroups.com`, 欢迎通过此邮件列表讨论与 API Testing 相关的任何问题。
### Githbu Discussion
### GitHub Discussion
[Github Discussion](https://github.com/LinuxSuRen/api-testing/discussions/new/choose)
[GitHub Discussion](https://github.com/LinuxSuRen/api-testing/discussions/new/choose)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is a API testing tool. 🚀

* Supported protocols: HTTP, gRPC, tRPC
* Multiple test report formats: Markdown, HTML, PDF, Stdout
* Mock Server in simple configuration
* Mock Server in simple configuration, and Open API support
* Support converting to [JMeter](https://jmeter.apache.org/) files
* Response Body fields equation check or [eval](https://expr.medv.io/)
* Validate the response body with [JSON schema](https://json-schema.org/)
Expand Down Expand Up @@ -132,6 +132,6 @@ Feel free to talk to us about any questions you may have about API Testing in th
`api-testing-tech@googlegroups.com`, Feel free to discuss everything related to API Testing via this mailing list.
### Githbu discussion
### `GitHub` discussion
[Github Discussion](https://github.com/LinuxSuRen/api-testing/discussions/new/choose)
[GitHub Discussion](https://github.com/LinuxSuRen/api-testing/discussions/new/choose)
14 changes: 11 additions & 3 deletions cmd/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package cmd

import (
"errors"
"os"
"os/signal"
"syscall"

"github.com/linuxsuren/api-testing/pkg/mock"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -55,14 +58,19 @@ func (o *mockOption) preRunE(c *cobra.Command, args []string) (err error) {

func (o *mockOption) runE(c *cobra.Command, args []string) (err error) {
reader := mock.NewLocalFileReader(o.files[0])

server := mock.NewInMemoryServer(o.port)

if err = server.Start(reader, o.prefix); err != nil {
return
}

<-c.Context().Done()
server.Stop()
clean := make(chan os.Signal, 1)
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)

select {
case <-c.Context().Done():
case <-clean:
}
err = server.Stop()
return
}
19 changes: 18 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/linuxsuren/api-testing/pkg/oauth"
template "github.com/linuxsuren/api-testing/pkg/render"
"github.com/linuxsuren/api-testing/pkg/server"
"github.com/linuxsuren/api-testing/pkg/service"
"github.com/linuxsuren/api-testing/pkg/testing"
"github.com/linuxsuren/api-testing/pkg/testing/remote"
"github.com/linuxsuren/api-testing/pkg/util"
Expand Down Expand Up @@ -233,6 +234,11 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
}
}

// create mock server controller
mockInMemoryReader := mock.NewInMemoryReader("")
dynamicMockServer := mock.NewInMemoryServer(0)
mockServerController := server.NewMockServerController(mockInMemoryReader, dynamicMockServer)

clean := make(chan os.Signal, 1)
signal.Notify(clean, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)

Expand All @@ -242,6 +248,7 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
reflection.Register(gRPCServer)
}
server.RegisterRunnerServer(s, remoteServer)
server.RegisterMockServer(s, mockServerController)
serverLogger.Info("gRPC server listening at", "addr", lis.Addr())
s.Serve(lis)
}()
Expand All @@ -256,13 +263,16 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
}()

mux := runtime.NewServeMux(runtime.WithMetadata(server.MetadataStoreFunc))
err = server.RegisterRunnerHandlerFromEndpoint(ctx, mux, "127.0.0.1:7070", []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())})
err = errors.Join(
server.RegisterRunnerHandlerFromEndpoint(ctx, mux, "127.0.0.1:7070", []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}),
server.RegisterMockHandlerFromEndpoint(ctx, mux, "127.0.0.1:7070", []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}))
if err == nil {
mux.HandlePath(http.MethodGet, "/", frontEndHandlerWithLocation(o.consolePath))
mux.HandlePath(http.MethodGet, "/assets/{asset}", frontEndHandlerWithLocation(o.consolePath))
mux.HandlePath(http.MethodGet, "/healthz", frontEndHandlerWithLocation(o.consolePath))
mux.HandlePath(http.MethodGet, "/favicon.ico", frontEndHandlerWithLocation(o.consolePath))
mux.HandlePath(http.MethodGet, "/get", o.getAtestBinary)
mux.HandlePath(http.MethodPost, "/runner/{suite}/{case}", service.WebRunnerHandler)

postRequestProxyFunc := postRequestProxy(o.skyWalking)
mux.HandlePath(http.MethodPost, "/browser/{app}", postRequestProxyFunc)
Expand Down Expand Up @@ -305,6 +315,13 @@ func (o *serverOption) runE(cmd *cobra.Command, args []string) (err error) {
combineHandlers.PutHandler(o.mockPrefix, mockServerHandler)
}

if handler, hErr := dynamicMockServer.SetupHandler(mockInMemoryReader, o.mockPrefix+"/server"); hErr != nil {
err = hErr
return
} else {
combineHandlers.PutHandler(o.mockPrefix+"/server", handler)
}

debugHandler(mux, remoteServer)
o.httpServer.WithHandler(combineHandlers.GetHandler())
serverLogger.Info("HTTP server listening at", "addr", httplis.Addr())
Expand Down
Loading

0 comments on commit 02026b2

Please sign in to comment.