From 24e15d42bde265b84dc85a3f89b81f13e169e7fc Mon Sep 17 00:00:00 2001 From: Rick Date: Fri, 12 Jul 2024 22:17:32 +0800 Subject: [PATCH 1/3] chore: support to set the version --- .goreleaser.yaml | 1 + Dockerfile | 3 ++- go.mod | 2 +- go.sum | 4 ++-- pkg/server.go | 12 +++++++++++- pkg/server_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 5 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 3e3925e..a473a51 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -18,6 +18,7 @@ builds: ldflags: - -w - -s + - -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}} archives: - name_template: "{{ .Binary }}-{{ .Os }}-{{ .Arch }}" builds: diff --git a/Dockerfile b/Dockerfile index 1436cdf..41e0a3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,8 @@ WORKDIR /workspace COPY . . RUN GOPROXY=${GOPROXY} go mod download -RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s" -o atest-store-orm . +RUN GOPROXY=${GOPROXY} CGO_ENABLED=0 go build -ldflags "-w -s -X github.com/linuxsuren/api-testing/pkg/version.version=${VERSION}\ + -X github.com/linuxsuren/api-testing/pkg/version.date=$(date +%Y-%m-%d)" -o atest-store-orm . FROM ${BASE_IMAGE} diff --git a/go.mod b/go.mod index db07666..50d669d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.2 toolchain go1.22.4 require ( - github.com/linuxsuren/api-testing v0.0.18-0.20240712013124-33c89be15c83 + github.com/linuxsuren/api-testing v0.0.18-0.20240712123625-553bc62434db github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 gorm.io/driver/mysql v1.5.2 diff --git a/go.sum b/go.sum index d8c60fc..eae12ca 100644 --- a/go.sum +++ b/go.sum @@ -96,8 +96,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/linuxsuren/api-testing v0.0.18-0.20240712013124-33c89be15c83 h1:NUGWAWp6fMc9qFERsP0TdJE6lTItEDI6F6v7tDkHNx8= -github.com/linuxsuren/api-testing v0.0.18-0.20240712013124-33c89be15c83/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs= +github.com/linuxsuren/api-testing v0.0.18-0.20240712123625-553bc62434db h1:vRmx1parkB31K0+YNGMf9+likTV1kRtkT7df5r7z6WM= +github.com/linuxsuren/api-testing v0.0.18-0.20240712123625-553bc62434db/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs= github.com/linuxsuren/go-fake-runtime v0.0.4 h1:y+tvBuw6MKTCav8Bo5HWwaXhBx1Z//VAvqI3gpOWqvw= github.com/linuxsuren/go-fake-runtime v0.0.4/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ= github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU= diff --git a/pkg/server.go b/pkg/server.go index 623db48..7496159 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -29,9 +29,9 @@ import ( "github.com/linuxsuren/api-testing/pkg/version" "gorm.io/driver/mysql" "gorm.io/driver/postgres" + "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" - "gorm.io/driver/sqlite" ) type dbserver struct { @@ -273,6 +273,16 @@ func (s *dbserver) Verify(ctx context.Context, in *server.Empty) (reply *server. } return } + +func (s *dbserver) GetVersion(context.Context, *server.Empty) (ver *server.Version, err error) { + ver = &server.Version{ + Version: version.GetVersion(), + Commit: version.GetCommit(), + Date: version.GetDate(), + } + return +} + func (s *dbserver) PProf(ctx context.Context, in *server.PProfRequest) (data *server.PProfData, err error) { log.Println("pprof", in.Name) diff --git a/pkg/server_test.go b/pkg/server_test.go index 3eb1664..742a95b 100644 --- a/pkg/server_test.go +++ b/pkg/server_test.go @@ -86,6 +86,43 @@ func TestNewRemoteServer(t *testing.T) { assert.NoError(t, err) assert.False(t, reply.Ready) }) + + t.Run("invalid orm driver", func(t *testing.T) { + remoteServer := NewRemoteServer() + assert.NotNil(t, remoteServer) + defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{ + Properties: map[string]string{ + "driver": "invalid", + }, + }) + _, err := remoteServer.ListTestSuite(defaultCtx, &server.Empty{}) + assert.Error(t, err) + }) + + t.Run("invalid mysql config", func(t *testing.T) { + remoteServer := NewRemoteServer() + assert.NotNil(t, remoteServer) + defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{ + Properties: map[string]string{ + "driver": "mysql", + }, + }) + _, err := remoteServer.ListTestSuite(defaultCtx, &server.Empty{}) + assert.Error(t, err) + }) + + t.Run("invalid postgres config", func(t *testing.T) { + remoteServer := NewRemoteServer() + assert.NotNil(t, remoteServer) + defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{ + Properties: map[string]string{ + "driver": "postgres", + }, + URL: "0.0.0.0:-123", + }) + _, err := remoteServer.ListTestSuite(defaultCtx, &server.Empty{}) + assert.Error(t, err) + }) } func TestSQLite(t *testing.T) { @@ -183,4 +220,9 @@ func TestSQLite(t *testing.T) { _, err := remoteServer.PProf(defaultCtx, &server.PProfRequest{}) assert.NoError(t, err) }) + + t.Run("GetVersion", func(t *testing.T) { + _, err := remoteServer.GetVersion(defaultCtx, &server.Empty{}) + assert.NoError(t, err) + }) } From 3f65d37d51c27e50e3e213b323b39f78ec0320f2 Mon Sep 17 00:00:00 2001 From: Rick Date: Fri, 12 Jul 2024 22:30:57 +0800 Subject: [PATCH 2/3] add cmd flag to print the version --- cmd/root.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmd/root.go b/cmd/root.go index 66a0e35..0fe8a36 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -17,6 +17,7 @@ package cmd import ( ext "github.com/linuxsuren/api-testing/pkg/extension" + "github.com/linuxsuren/api-testing/pkg/version" "github.com/linuxsuren/atest-ext-store-orm/pkg" "github.com/spf13/cobra" ) @@ -31,10 +32,16 @@ func NewRootCommand() (c *cobra.Command) { RunE: opt.runE, } opt.AddFlags(c.Flags()) + c.Flags().BoolVarP(&opt.version, "version", "", false, "Print the version then exit") return } func (o *option) runE(c *cobra.Command, args []string) (err error) { + if o.version { + c.Println(version.GetVersion()) + c.Println(version.GetDate()) + return + } remoteServer := pkg.NewRemoteServer() err = ext.CreateRunner(o.Extension, c, remoteServer) return @@ -42,4 +49,5 @@ func (o *option) runE(c *cobra.Command, args []string) (err error) { type option struct { *ext.Extension + version bool } From 8def478283e82e28605d9d5605db9cd1570a7e97 Mon Sep 17 00:00:00 2001 From: rick Date: Fri, 12 Jul 2024 14:39:12 +0000 Subject: [PATCH 3/3] update api-testing dep --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 50d669d..cf78e7f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22.2 toolchain go1.22.4 require ( - github.com/linuxsuren/api-testing v0.0.18-0.20240712123625-553bc62434db + github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 gorm.io/driver/mysql v1.5.2 diff --git a/go.sum b/go.sum index eae12ca..4347643 100644 --- a/go.sum +++ b/go.sum @@ -96,8 +96,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/linuxsuren/api-testing v0.0.18-0.20240712123625-553bc62434db h1:vRmx1parkB31K0+YNGMf9+likTV1kRtkT7df5r7z6WM= -github.com/linuxsuren/api-testing v0.0.18-0.20240712123625-553bc62434db/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs= +github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07 h1:NmjqkiR+4KXjWs9H6wGmgQo7r7FJE+RED+E3EwAGsp4= +github.com/linuxsuren/api-testing v0.0.18-0.20240712143814-6ce9363d5a07/go.mod h1:8G3GZtQmSUHyqA/gZlSdFaDIveHQYbFInTibD7g3hMs= github.com/linuxsuren/go-fake-runtime v0.0.4 h1:y+tvBuw6MKTCav8Bo5HWwaXhBx1Z//VAvqI3gpOWqvw= github.com/linuxsuren/go-fake-runtime v0.0.4/go.mod h1:zmh6J78hSnWZo68faMA2eKOdaEp8eFbERHi3ZB9xHCQ= github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursDSE+j00iU=