Skip to content

Commit d19a96f

Browse files
committed
Adds Mockserver
1 parent 5702d8b commit d19a96f

File tree

13 files changed

+733
-0
lines changed

13 files changed

+733
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Mockserver Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'mockserver/v*.*.*'
7+
8+
jobs:
9+
release:
10+
name: Build and Release
11+
runs-on: ubuntu-latest
12+
environment: integration
13+
14+
steps:
15+
- name: Checkout repo
16+
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
17+
- uses: cachix/install-nix-action@08dcb3a5e62fa31e2da3d490afc4176ef55ecd72 # v30
18+
with:
19+
nix_path: nixpkgs=channel:nixos-unstable
20+
21+
- name: Configure AWS Credentials
22+
uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 # v4.0.2
23+
with:
24+
aws-region: ${{ secrets.QA_AWS_REGION }}
25+
role-to-assume: ${{ secrets.QA_AWS_ROLE_TO_ASSUME }}
26+
role-duration-seconds: 3600
27+
- name: Login to Amazon ECR
28+
uses: aws-actions/amazon-ecr-login@062b18b96a7aff071d4dc91bc00c4c1a7945b076 # v2.0.1
29+
with:
30+
mask-password: 'true'
31+
env:
32+
AWS_REGION: ${{ secrets.QA_AWS_REGION }}
33+
- name: Build and release
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
IMAGE_PREFIX: ${{ secrets.QA_AWS_ACCOUNT_NUMBER }}.dkr.ecr.${{ secrets.QA_AWS_REGION }}.amazonaws.com/mockserver
37+
IMAGE_TAG: ${{ github.ref_name}}
38+
run: nix develop -c goreleaser release --rm-dist
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Test Mockserver
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- 'mockserver/v*.*.*'
9+
pull_request:
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout repo
16+
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
17+
- name: Check for changes in Mockserver
18+
uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
19+
id: changes
20+
with:
21+
filters: |
22+
src:
23+
- 'mockserver/**'
24+
- uses: cachix/install-nix-action@08dcb3a5e62fa31e2da3d490afc4176ef55ecd72 # v30
25+
if: steps.changes.outputs.src == 'true'
26+
with:
27+
nix_path: nixpkgs=channel:nixos-unstable
28+
- name: Run tests
29+
if: steps.changes.outputs.src == 'true'
30+
run: |
31+
nix develop -c go test ./mockserver/...

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@ __debug*
7979

8080
import_keys_test.go
8181
tag.py
82+
83+
# mockserver
84+
mockserver/save.json

mockserver/.goreleaser.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
version: 1
2+
project_name: mockserver
3+
4+
env:
5+
- IMG_PRE={{ if index .Env "IMAGE_PREFIX" }}{{ .Env.IMAGE_PREFIX }}{{ else }}localhost:5001{{ end }}
6+
- TAG={{ if index .Env "IMAGE_TAG" }}{{ .Env.IMAGE_TAG }}{{ else }}latest{{ end }}
7+
8+
# Build settings for binaries
9+
builds:
10+
- id: mockserver
11+
main: ./main.go
12+
env:
13+
- CGO_ENABLED=0
14+
goos:
15+
- linux
16+
- darwin
17+
goarch:
18+
- amd64
19+
- arm64
20+
ldflags:
21+
- '-s -w' # Strip debug information to reduce binary size
22+
binary: 'mockserver_{{ .Os }}_{{ .Arch }}'
23+
24+
archives:
25+
- format: binary
26+
27+
dockers:
28+
- image_templates:
29+
- '{{ .Env.IMG_PRE }}/mockserver:{{ .Tag }}'
30+
# - "{{ .Env.IMG_PRE }}/mockserver:latest"
31+
- '{{ .Env.IMG_PRE }}/mockserver:debug' # DEBUG: Using for testing
32+
platforms:
33+
- linux/amd64
34+
- linux/arm64
35+
dockerfile: Dockerfile
36+
use: buildx
37+
38+
# Git tag management
39+
git:
40+
tag: true
41+
42+
changelog:
43+
disable: 'true'
44+
45+
before:
46+
hooks:
47+
- cmd: go mod tidy

mockserver/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM golang:alpine as builder
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
RUN go mod download
7+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -o mockserver .
8+
9+
FROM scratch
10+
11+
COPY --from=builder /app/mockserver /mockserver
12+
13+
EXPOSE 8080
14+
ENTRYPOINT [ "/mockserver" ]

mockserver/README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Mockserver
2+
3+
A simple, high-performing mockserver that can dynamically build new routes with customized responses.
4+
5+
## Use
6+
7+
Call the `/register` endpoint to define a route.
8+
9+
### Curl
10+
11+
```sh
12+
curl -X POST http://localhost:8080/register -d '{
13+
"method": "GET",
14+
"path": "/hello",
15+
"response": "{\"message\": \"Hello, world!\"}",
16+
"status_code": 200,
17+
"content_type": "application/json"
18+
}' -H "Content-Type: application/json"
19+
```
20+
21+
### Go and [Resty](https://github.com/go-resty/resty)
22+
23+
```go
24+
client := resty.New()
25+
26+
route := map[string]interface{}{
27+
"method": "GET",
28+
"path": "/hello",
29+
"response": "{\"message\":\"Hello, world!\"}",
30+
"status_code": 200,
31+
"content_type": "application/json",
32+
}
33+
34+
resp, _ := client.R().
35+
SetHeader("Content-Type", "application/json").
36+
SetBody(route).
37+
Post("http://localhost:8080/register")
38+
```
39+
40+
You can now call your endpoint and receive the JSON response back.
41+
42+
```sh
43+
curl -X GET http://localhost:8080/hello -H "Content-Type: application/json"
44+
# {"message":"Hello, world!"}
45+
```
46+
47+
## Configure
48+
49+
Config is through environment variables.
50+
51+
| **Environment Variable** | **Description** | **Default Value** |
52+
| ------------------------ | -------------------------------------------------------------- | ----------------- |
53+
| `LOG_LEVEL` | Controls the logging level (`debug`, `info`, `warn`, `error`). | `debug` |
54+
| `SAVE_FILE` | Path to the file where routes are saved and loaded. | `save.json` |
55+
56+
## Run
57+
58+
```sh
59+
go run .
60+
```
61+
62+
## Test
63+
64+
```sh
65+
go test ./...
66+
```
67+
68+
## Benchmark
69+
70+
```sh
71+
LOG_LEVEL=disabled go test -bench=. -benchmem -run=^\$
72+
```
73+
74+
Benchmark run on an Apple M3 Max.
75+
76+
```sh
77+
goos: darwin
78+
goarch: arm64
79+
pkg: github.com/smartcontractkit/chainlink-testing-framework/mockserver
80+
BenchmarkRegisterRoute-14 604978 1967 ns/op 6263 B/op 29 allocs/op
81+
BenchmarkRouteResponse-14 16561670 70.62 ns/op 80 B/op 1 allocs/op
82+
BenchmarkSaveRoutes-14 1245 956784 ns/op 636042 B/op 2014 allocs/op
83+
BenchmarkLoadRoutes-14 1020 1185990 ns/op 348919 B/op 9020 allocs/op
84+
```

mockserver/config.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"github.com/rs/zerolog/log"
7+
)
8+
9+
type Config struct {
10+
SaveFile string `json:"save_file"`
11+
LogLevel string `json:"log_level"`
12+
}
13+
14+
func ReadConfig() *Config {
15+
saveFile := os.Getenv("SAVE_FILE")
16+
if saveFile == "" {
17+
log.Warn().Msg("SAVE_FILE is not set. Using default file 'save.json'")
18+
saveFile = "save.json"
19+
}
20+
21+
logLevel := os.Getenv("LOG_LEVEL")
22+
if logLevel == "" {
23+
log.Warn().Msg("LOG_LEVEL is not set. Using default level 'debug'")
24+
logLevel = "debug"
25+
}
26+
log.Info().Str("SaveFile", saveFile).Str("LogLevel", logLevel).Msg("Loaded config")
27+
return &Config{
28+
SaveFile: saveFile,
29+
LogLevel: logLevel,
30+
}
31+
}

mockserver/config_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestConfig(t *testing.T) {
10+
t.Setenv("SAVE_FILE", "kendrick.json")
11+
t.Setenv("LOG_LEVEL", "butterfly")
12+
13+
config := ReadConfig()
14+
assert.Equal(t, config.SaveFile, "kendrick.json")
15+
assert.Equal(t, config.LogLevel, "butterfly")
16+
}
17+
18+
func TestConfigDefaults(t *testing.T) {
19+
config := ReadConfig()
20+
assert.Equal(t, config.SaveFile, "save.json")
21+
assert.Equal(t, config.LogLevel, "debug")
22+
}

mockserver/go.mod

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/smartcontractkit/chainlink-testing-framework/mockserver
2+
3+
go 1.23.2
4+
5+
require (
6+
github.com/rs/zerolog v1.33.0
7+
github.com/stretchr/testify v1.9.0
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/mattn/go-colorable v0.1.13 // indirect
13+
github.com/mattn/go-isatty v0.0.19 // indirect
14+
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
golang.org/x/sys v0.12.0 // indirect
16+
gopkg.in/yaml.v3 v3.0.1 // indirect
17+
)

mockserver/go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
5+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
6+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
7+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
8+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
9+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
10+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
14+
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
15+
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
16+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
17+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
18+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
19+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
20+
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
21+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
22+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
23+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
24+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
25+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)