Skip to content

Commit d18f00f

Browse files
committed
feat: complete a very basic test
0 parents  commit d18f00f

22 files changed

+2020
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage.out
2+
bin/
3+
.idea/

.goreleaser.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This is an example .goreleaser.yml file with some sensible defaults.
2+
# Make sure to check the documentation at https://goreleaser.com
3+
before:
4+
hooks:
5+
# You may remove this if you don't use go modules.
6+
- go mod tidy
7+
builds:
8+
- env:
9+
- CGO_ENABLED=0
10+
id: atest-store-iotdb
11+
binary: atest-store-iotdb
12+
goos:
13+
- linux
14+
- windows
15+
- darwin
16+
ignore:
17+
- goarch: 386
18+
ldflags:
19+
- -w
20+
- -s
21+
- -X github.com/linuxsuren/api-testing/pkg/version.version={{.Version}} -X github.com/linuxsuren/api-testing/pkg/version.commit={{.Commit}} -X github.com/linuxsuren/api-testing/pkg/version.date={{.Date}}
22+
archives:
23+
- name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}"
24+
builds:
25+
- atest-store-iotdb
26+
format_overrides:
27+
- goos: windows
28+
format: zip
29+
files:
30+
- README.md
31+
checksum:
32+
name_template: 'checksums.txt'
33+
snapshot:
34+
name_template: "{{ incpatch .Version }}-next"
35+
changelog:
36+
sort: asc
37+
filters:
38+
exclude:
39+
- '^docs:'
40+
- '^test:'

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ARG GO_BUILDER=docker.io/library/golang:1.22
2+
ARG BASE_IMAGE=docker.io/library/alpine:3.12
3+
4+
FROM ${GO_BUILDER} AS builder
5+
6+
ARG VERSION
7+
ARG GOPROXY
8+
WORKDIR /workspace
9+
COPY . .
10+
11+
RUN GOPROXY=${GOPROXY} go mod download
12+
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s -X github.com/linuxsuren/api-testing/pkg/version.version=${VERSION}\
13+
-X github.com/linuxsuren/api-testing/pkg/version.date=$(date +%Y-%m-%d)" -o atest-store-iotdb .
14+
15+
FROM ${BASE_IMAGE}
16+
17+
LABEL org.opencontainers.image.source=https://github.com/LinuxSuRen/atest-ext-store-orm
18+
LABEL org.opencontainers.image.description="ORM database Store Extension of the API Testing."
19+
20+
COPY --from=builder /workspace/atest-store-iotdb /usr/local/bin/atest-store-iotdb
21+
22+
CMD [ "atest-store-iotdb" ]

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fmt:
2+
go mod tidy
3+
go fmt ./...
4+
build:
5+
go build -o bin/atest-store-iotdb .
6+
cp: build
7+
cp bin/atest-store-iotdb ~/.config/atest/bin/
8+
test:
9+
go test ./... -cover -v -coverprofile=coverage.out
10+
go tool cover -func=coverage.out
11+
build-image:
12+
docker build . -t e2e-extension
13+
hd:
14+
curl https://linuxsuren.github.io/tools/install.sh|bash
15+
init-env: hd
16+
hd i cli/cli
17+
gh extension install linuxsuren/gh-dev
18+
run-e2e:
19+
cd e2e && ./start.sh

cmd/root.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2025 API Testing Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
ext "github.com/linuxsuren/api-testing/pkg/extension"
20+
"github.com/linuxsuren/api-testing/pkg/version"
21+
"github.com/linuxsuren/atest-ext-store-iotdb/pkg"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func NewRootCommand() (c *cobra.Command) {
26+
opt := &option{
27+
Extension: ext.NewExtension("orm", "store", 7071),
28+
}
29+
c = &cobra.Command{
30+
Use: opt.GetFullName(),
31+
Short: "Storage extension of api-testing",
32+
RunE: opt.runE,
33+
}
34+
opt.AddFlags(c.Flags())
35+
c.Flags().IntVarP(&opt.historyLimit, "history-limit", "", 1000, "History record items count limit")
36+
c.Flags().BoolVarP(&opt.version, "version", "", false, "Print the version then exit")
37+
return
38+
}
39+
40+
func (o *option) runE(c *cobra.Command, args []string) (err error) {
41+
defer func() {
42+
if r := recover(); r != nil {
43+
c.Println(r)
44+
}
45+
}()
46+
47+
if o.version {
48+
c.Println(version.GetVersion())
49+
c.Println(version.GetDate())
50+
return
51+
}
52+
remoteServer := pkg.NewRemoteServer(o.historyLimit)
53+
err = ext.CreateRunner(o.Extension, c, remoteServer)
54+
return
55+
}
56+
57+
type option struct {
58+
*ext.Extension
59+
historyLimit int
60+
version bool
61+
}

cmd/root_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
*
3+
MIT License
4+
5+
Copyright (c) 2025 API Testing Authors.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
*/
25+
package cmd
26+
27+
import (
28+
"context"
29+
"io"
30+
"testing"
31+
"time"
32+
33+
"github.com/stretchr/testify/assert"
34+
)
35+
36+
func TestRootCommand(t *testing.T) {
37+
t.Run("invalid port", func(t *testing.T) {
38+
c := NewRootCommand()
39+
c.SetOut(io.Discard)
40+
assert.Equal(t, "atest-store-orm", c.Use)
41+
42+
c.SetArgs([]string{"--port", "abc"})
43+
err := c.Execute()
44+
assert.Error(t, err)
45+
})
46+
47+
t.Run("a random port", func(t *testing.T) {
48+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
49+
defer cancel()
50+
51+
c := NewRootCommand()
52+
c.SetContext(ctx)
53+
54+
c.SetArgs([]string{"--port", "0"})
55+
err := c.Execute()
56+
assert.Error(t, err)
57+
})
58+
}

e2e/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM docker.io/library/e2e-extension as ext
2+
FROM ghcr.io/linuxsuren/api-testing:master
3+
4+
WORKDIR /workspace
5+
COPY . .
6+
COPY --from=ext /usr/local/bin/atest-store-orm /usr/local/bin/atest-store-orm
7+
8+
CMD [ "/workspace/entrypoint.sh" ]

e2e/compose.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
version: '3.1'
2+
services:
3+
testing:
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
depends_on:
8+
iotdb:
9+
condition: service_healthy
10+
extension:
11+
condition: service_started
12+
volumes:
13+
- type: volume
14+
source: cache
15+
target: /var/data
16+
links:
17+
- iotdb
18+
extension:
19+
build:
20+
context: ..
21+
dockerfile: Dockerfile
22+
args:
23+
- "GO_BUILDER=ghcr.io/linuxsuren/library/golang:1.22"
24+
- "BASE_IMAGE=ghcr.io/linuxsuren/library/alpine:3.12"
25+
- GOPROXY=${GOPROXY}
26+
27+
iotdb:
28+
image: ghcr.io/linuxsuren/apache/iotdb:1.3.2-standalone
29+
environment:
30+
POSTGRES_USER: root
31+
POSTGRES_PASSWORD: root
32+
POSTGRES_DB: atest
33+
healthcheck:
34+
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/6667"]
35+
interval: 3s
36+
timeout: 30s
37+
retries: 10
38+
ports:
39+
- 6667
40+
volumes:
41+
cache:

e2e/entrypoint.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
5+
mkdir -p /root/.config/atest
6+
mkdir -p /var/data
7+
8+
echo "start to run server"
9+
nohup atest server&
10+
11+
kind=iotdb target=iotdb driver=iotdb atest run -p testing-data-query.yaml
12+
13+
cat /root/.config/atest/stores.yaml

e2e/start.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
file=$1
4+
if [ "$file" == "" ]
5+
then
6+
file=compose.yaml
7+
fi
8+
9+
docker compose version
10+
docker compose -f "$file" down
11+
docker compose -f "$file" up --build testing --exit-code-from testing --remove-orphans

e2e/testing-data-query.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!api-testing
2+
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json
3+
name: atest
4+
api: |
5+
{{default "http://localhost:8080" (env "SERVER")}}/api/v1
6+
param:
7+
store: "{{randAlpha 3}}"
8+
server: |
9+
{{default "http://localhost:8080" (env "SERVER")}}
10+
items:
11+
- name: CreateStore
12+
before:
13+
items:
14+
- httpReady("{{.param.server}}/healthz", 2400)
15+
request:
16+
api: /stores
17+
method: POST
18+
body: |
19+
{
20+
"name": "{{.param.store}}",
21+
"url": "{{env "target"}}",
22+
"username": "{{default "root" (env "username")}}",
23+
"password": "{{default "root" (env "password")}}",
24+
"kind": {
25+
"name": "atest-store-{{env "kind"}}"
26+
},
27+
"properties": [{
28+
"key": "driver",
29+
"value": "{{default "mysql" (env "driver")}}"
30+
}, {
31+
"key": "database",
32+
"value": "{{default "atest" (env "dbname")}}"
33+
}, {
34+
"key": "bucket",
35+
"value": "bucket"
36+
}, {
37+
"key": "region",
38+
"value": "cn"
39+
}, {
40+
"key": "disablessl",
41+
"value": "true"
42+
}, {
43+
"key": "targetPath",
44+
"value": "api-testing"
45+
}]
46+
}
47+
- name: query
48+
before:
49+
items:
50+
- sleep(3)
51+
request:
52+
api: /data/query
53+
method: POST
54+
header:
55+
X-Store-Name: "{{.param.store}}"
56+
body: |
57+
{
58+
"sql": "",
59+
"key": ""
60+
}

0 commit comments

Comments
 (0)