Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add s3 storage backend extension #151

Merged
merged 2 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial --force-coverage-parser go -r coverage.out
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial --force-coverage-parser go -r collector-coverage.out
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial --force-coverage-parser go -r store-orm-coverage.out
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial --force-coverage-parser go -r store-s3-coverage.out
bash <(curl -Ls https://coverage.codacy.com/get.sh) final
- name: API Test
run: |
Expand All @@ -48,6 +49,17 @@ jobs:
- name: Image
run: make build-image

BuildImage:
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18.x
- uses: actions/checkout@v3.0.0
- name: Image
run: make build-image

BuildEmbedUI:
runs-on: ubuntu-20.04
steps:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ RUN GOPROXY=${GOPROXY} go mod download
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s -X github.com/linuxsuren/api-testing/pkg/version.version=${VERSION}" -o atest .
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s" -o atest-collector extensions/collector/main.go
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s" -o atest-store-orm extensions/store-orm/main.go
RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s" -o atest-store-s3 extensions/store-s3/main.go

FROM node:20-alpine3.17 AS ui

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ test-collector:
test-store-orm:
go test github.com/linuxsuren/api-testing/extensions/store-orm/./... -cover -v -coverprofile=store-orm-coverage.out
go tool cover -func=store-orm-coverage.out
test-all-backend: test test-collector test-store-orm
test-store-s3:
go test github.com/linuxsuren/api-testing/extensions/store-s3/./... -cover -v -coverprofile=store-s3-coverage.out
go tool cover -func=store-s3-coverage.out
test-all-backend: test test-collector test-store-orm test-store-s3
test-all: test-all-backend test-ui

install-precheck:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This is a API testing tool.
* Output reference between TestCase
* Run in server mode, and provide the [gRPC](pkg/server/server.proto) and HTTP endpoint
* [VS Code extension](https://github.com/LinuxSuRen/vscode-api-testing) support
* Multiple storage backends supported(Local, ORM Database, S3, etc)
* [HTTP API record](extensions/collector)

## Get started
Expand Down
26 changes: 26 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ podman run -p 7071:7071 \
ghcr.io/linuxsuren/api-testing:master atest-store-orm
```

### ORM S3 Storage
You can use a S3 compatible storage as the storage backend.

```shell
# The default port is 7072
podman run --network host \
ghcr.io/linuxsuren/api-testing:master atest-store-s3
```

See also the expected configuration below:

```yaml
- name: s3
url: http://172.11.0.13:30999 # address of the s3 server
kind:
name: s3
url: localhost:7072 # address of the s3 storage extension
properties:
accessKeyID: 6e03rIMChrsZ6YZl
secretAccessKey: F0xH6o2qRYTyAUyRuXO81B4gj7zUrSaj
disableSSL: true
forcepathstyle: true
bucket: vm1
region: cn
```

## Extensions
Developers could have a storage extension. Implement a gRPC server according to [loader.proto](../pkg/testing/remote/loader.proto) is required.

Expand Down
54 changes: 54 additions & 0 deletions extensions/store-s3/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"net"

"github.com/linuxsuren/api-testing/extensions/store-s3/pkg"
"github.com/linuxsuren/api-testing/pkg/testing/remote"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)

func NewRootCmd(s3Creator pkg.S3Creator) (cmd *cobra.Command) {
opt := &option{
S3Creator: s3Creator,
}
cmd = &cobra.Command{
Use: "store-s3",
Short: "S3 storage extension of api-testing",
RunE: opt.runE,
}
flags := cmd.Flags()
flags.IntVarP(&opt.port, "port", "p", 7072, "The port of gRPC server")
return cmd
}

func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
removeServer := pkg.NewRemoteServer(o.S3Creator)

var lis net.Listener
lis, err = net.Listen("tcp", fmt.Sprintf(":%d", o.port))
if err != nil {
return
}

gRPCServer := grpc.NewServer()
remote.RegisterLoaderServer(gRPCServer, removeServer)
cmd.Println("S3 storage extension is running at port", o.port)

go func() {
<-cmd.Context().Done()
gRPCServer.Stop()
}()

err = gRPCServer.Serve(lis)
return
}

type option struct {
port int

// inner fields
S3Creator pkg.S3Creator
}
44 changes: 44 additions & 0 deletions extensions/store-s3/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"context"
"io"
"testing"
"time"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestNewRootCmd(t *testing.T) {
t.Run("not run", func(t *testing.T) {
cmd := newRootCmdForTest()
assert.NotNil(t, cmd)
assert.Equal(t, "store-s3", cmd.Use)
assert.Equal(t, "7072", cmd.Flags().Lookup("port").Value.String())
})

t.Run("invalid port", func(t *testing.T) {
cmd := newRootCmdForTest()
cmd.SetArgs([]string{"--port", "-1"})
err := cmd.Execute()
assert.Error(t, err)
})

t.Run("stop the command", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

cmd := newRootCmdForTest()
cmd.SetContext(ctx)
cmd.SetArgs([]string{"--port", "0"})
err := cmd.Execute()
assert.NoError(t, err)
})
}

func newRootCmdForTest() *cobra.Command {
cmd := NewRootCmd(nil)
cmd.SetOut(io.Discard)
return cmd
}
8 changes: 8 additions & 0 deletions extensions/store-s3/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/linuxsuren/api-testing/extensions/store-s3

go 1.20

require (
github.com/aws/aws-sdk-go v1.44.312 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
)
38 changes: 38 additions & 0 deletions extensions/store-s3/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
github.com/aws/aws-sdk-go v1.44.312 h1:llrElfzeqG/YOLFFKjg1xNpZCFJ2xraIi3PqSuP+95k=
github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
15 changes: 15 additions & 0 deletions extensions/store-s3/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"os"

"github.com/linuxsuren/api-testing/extensions/store-s3/cmd"
"github.com/linuxsuren/api-testing/extensions/store-s3/pkg"
)

func main() {
cmd := cmd.NewRootCmd(&pkg.DefaultS3Creator{})
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
71 changes: 71 additions & 0 deletions extensions/store-s3/pkg/fake_s3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package pkg

import (
"bytes"
"io"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
)

type S3API interface {
ListObjectsWithContext(ctx aws.Context, input *s3.ListObjectsInput, opts ...request.Option) (*s3.ListObjectsOutput, error)
PutObjectWithContext(ctx aws.Context, input *s3.PutObjectInput, opts ...request.Option) (*s3.PutObjectOutput, error)
GetObjectWithContext(ctx aws.Context, input *s3.GetObjectInput, opts ...request.Option) (*s3.GetObjectOutput, error)
DeleteObjectWithContext(ctx aws.Context, input *s3.DeleteObjectInput, opts ...request.Option) (*s3.DeleteObjectOutput, error)
}

type S3Creator interface {
New(p client.ConfigProvider, cfgs ...*aws.Config) S3API
}

type DefaultS3Creator struct{}

func (d *DefaultS3Creator) New(p client.ConfigProvider, cfgs ...*aws.Config) S3API {
return s3.New(p, cfgs...)
}

type fakeS3 struct {
data map[*string][]byte
}

func (f *fakeS3) New(p client.ConfigProvider, cfgs ...*aws.Config) S3API {
return f
}

func (f *fakeS3) ListObjectsWithContext(ctx aws.Context, input *s3.ListObjectsInput, opts ...request.Option) (output *s3.ListObjectsOutput, err error) {
output = &s3.ListObjectsOutput{}
for k := range f.data {
output.Contents = append(output.Contents, &s3.Object{
Key: k,
})
}
return
}
func (f *fakeS3) PutObjectWithContext(ctx aws.Context, input *s3.PutObjectInput, opts ...request.Option) (*s3.PutObjectOutput, error) {
data, err := io.ReadAll(input.Body)
f.data[input.Key] = data
return nil, err
}
func (f *fakeS3) GetObjectWithContext(ctx aws.Context, input *s3.GetObjectInput, opts ...request.Option) (output *s3.GetObjectOutput, err error) {
for k := range f.data {
if *input.Key == *k {
output = &s3.GetObjectOutput{
Body: io.NopCloser(bytes.NewReader(f.data[k])),
}
break
}
}
return
}
func (f *fakeS3) DeleteObjectWithContext(ctx aws.Context, input *s3.DeleteObjectInput, opts ...request.Option) (*s3.DeleteObjectOutput, error) {
for k := range f.data {
if *input.Key == *k {
delete(f.data, k)
break
}
}
return nil, nil
}