From a90aad3ae6468236d403b44b77e6e245bc2a22de Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Sep 2022 21:02:27 +0800 Subject: [PATCH 01/12] scaffolding gocase Signed-off-by: tison --- tests/gocase/go.sum | 30 ++++++++++ tests/gocase/unit/command/main_test.go | 51 +++++++++++++++++ tests/gocase/unit/info/main_test.go | 77 ++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 tests/gocase/go.sum create mode 100644 tests/gocase/unit/command/main_test.go create mode 100644 tests/gocase/unit/info/main_test.go diff --git a/tests/gocase/go.sum b/tests/gocase/go.sum new file mode 100644 index 00000000000..8a6efea2986 --- /dev/null +++ b/tests/gocase/go.sum @@ -0,0 +1,30 @@ +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/go-redis/redis/v9 v9.0.0-beta.2 h1:ZSr84TsnQyKMAg8gnV+oawuQezeJR11/09THcWCQzr4= +github.com/go-redis/redis/v9 v9.0.0-beta.2/go.mod h1:Bldcd/M/bm9HbnNPi/LUtYBSD8ttcZYBMupwMXhdU0o= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +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/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tests/gocase/unit/command/main_test.go b/tests/gocase/unit/command/main_test.go new file mode 100644 index 00000000000..6bd9eebb311 --- /dev/null +++ b/tests/gocase/unit/command/main_test.go @@ -0,0 +1,51 @@ +package command + +import ( + "context" + "github.com/go-redis/redis/v9" + "github.com/stretchr/testify/require" + "testing" +) + +func TestCommand(t *testing.T) { + ctx := context.Background() + rdb := redis.NewClient(&redis.Options{Addr: "localhost:6666"}) + + t.Run("Kvrocks supports 180 commands currently", func(t *testing.T) { + r := rdb.Do(ctx, "COMMAND", "COUNT") + v, err := r.Int() + require.NoError(t, err) + require.Equal(t, 180, v) + }) + + t.Run("acquire GET command info by COMMAND INFO", func(t *testing.T) { + r := rdb.Do(ctx, "COMMAND", "INFO", "GET") + vs, err := r.Slice() + require.NoError(t, err) + require.Len(t, vs, 1) + v := vs[0].([]interface{}) + require.Len(t, v, 6) + require.Equal(t, "get", v[0]) + require.EqualValues(t, 2, v[1]) + require.Equal(t, []interface{}{"readonly"}, v[2]) + require.EqualValues(t, 1, v[3]) + require.EqualValues(t, 1, v[4]) + require.EqualValues(t, 1, v[5]) + }) + + t.Run("command entry length check", func(t *testing.T) { + r := rdb.Do(ctx, "COMMAND") + vs, err := r.Slice() + require.NoError(t, err) + v := vs[0].([]interface{}) + require.Len(t, v, 6) + }) + + t.Run("get keys of commands by COMMAND GETKEYS", func(t *testing.T) { + r := rdb.Do(ctx, "COMMAND", "GETKEYS", "GET", "test") + vs, err := r.Slice() + require.NoError(t, err) + require.Len(t, vs, 1) + require.Equal(t, "test", vs[0]) + }) +} diff --git a/tests/gocase/unit/info/main_test.go b/tests/gocase/unit/info/main_test.go new file mode 100644 index 00000000000..7f3b974dfde --- /dev/null +++ b/tests/gocase/unit/info/main_test.go @@ -0,0 +1,77 @@ +package command + +import ( + "context" + "fmt" + "github.com/go-redis/redis/v9" + "github.com/stretchr/testify/require" + "regexp" + "strconv" + "strings" + "testing" + "time" +) + +func TestCommand(t *testing.T) { + ctx := context.Background() + rdb := redis.NewClient(&redis.Options{Addr: "localhost:6666"}) + + FindInfoEntry := func(t *testing.T, section string, key string) string { + r := rdb.Info(ctx, section) + p := regexp.MustCompile(fmt.Sprintf("%s:(.+)", key)) + ms := p.FindStringSubmatch(r.Val()) + require.Len(t, ms, 2) + return strings.TrimSpace(ms[1]) + } + + MustAtoi := func(t *testing.T, s string) int { + i, err := strconv.Atoi(s) + require.NoError(t, err) + return i + } + + t.Run("get rocksdb ops by INFO", func(t *testing.T) { + for i := 0; i < 2; i++ { + k := fmt.Sprintf("key%d", i) + v := fmt.Sprintf("value%d", i) + for j := 0; j < 500; j++ { + rdb.LPush(ctx, k, v) + rdb.LRange(ctx, k, 0, 1) + } + time.Sleep(time.Second) + } + + r := FindInfoEntry(t, "rocksdb", "put_per_sec") + require.Greater(t, MustAtoi(t, r), 0) + r = FindInfoEntry(t, "rocksdb", "get_per_sec") + require.Greater(t, MustAtoi(t, r), 0) + r = FindInfoEntry(t, "rocksdb", "seek_per_sec") + require.Greater(t, MustAtoi(t, r), 0) + r = FindInfoEntry(t, "rocksdb", "next_per_sec") + require.Greater(t, MustAtoi(t, r), 0) + }) + + t.Run("get bgsave information by INFO", func(t *testing.T) { + require.Equal(t, "0", FindInfoEntry(t, "persistence", "bgsave_in_progress")) + require.Equal(t, "-1", FindInfoEntry(t, "persistence", "last_bgsave_time")) + require.Equal(t, "ok", FindInfoEntry(t, "persistence", "last_bgsave_status")) + require.Equal(t, "-1", FindInfoEntry(t, "persistence", "last_bgsave_time_sec")) + + r := rdb.Do(ctx, "bgsave") + v, err := r.Text() + require.NoError(t, err) + require.Equal(t, "OK", v) + + require.Eventually(t, func() bool { + e := MustAtoi(t, FindInfoEntry(t, "persistence", "bgsave_in_progress")) + return e == 0 + }, 5*time.Second, 100*time.Millisecond) + + lastBgsaveTime := MustAtoi(t, FindInfoEntry(t, "persistence", "last_bgsave_time")) + require.Greater(t, lastBgsaveTime, 1640507660) + require.Equal(t, "ok", FindInfoEntry(t, "persistence", "last_bgsave_status")) + lastBgsaveTimeSec := MustAtoi(t, FindInfoEntry(t, "persistence", "last_bgsave_time_sec")) + require.GreaterOrEqual(t, lastBgsaveTimeSec, 0) + require.Less(t, lastBgsaveTimeSec, 3) + }) +} From 529c16f02c5d1bb9d555eb5fcfc02e0223881eeb Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Sep 2022 22:09:42 +0800 Subject: [PATCH 02/12] gocase: wire it Signed-off-by: tison --- tests/gocase/go.sum | 2 + .../command/{main_test.go => command_test.go} | 8 +- .../unit/info/{main_test.go => info_test.go} | 10 ++- tests/gocase/util/server.go | 83 +++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) rename tests/gocase/unit/command/{main_test.go => command_test.go} (90%) rename tests/gocase/unit/info/{main_test.go => info_test.go} (93%) create mode 100644 tests/gocase/util/server.go diff --git a/tests/gocase/go.sum b/tests/gocase/go.sum index 8a6efea2986..ba95a157978 100644 --- a/tests/gocase/go.sum +++ b/tests/gocase/go.sum @@ -12,6 +12,8 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q= +github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI= +github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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= diff --git a/tests/gocase/unit/command/main_test.go b/tests/gocase/unit/command/command_test.go similarity index 90% rename from tests/gocase/unit/command/main_test.go rename to tests/gocase/unit/command/command_test.go index 6bd9eebb311..f4b3715a76c 100644 --- a/tests/gocase/unit/command/main_test.go +++ b/tests/gocase/unit/command/command_test.go @@ -2,14 +2,18 @@ package command import ( "context" - "github.com/go-redis/redis/v9" "github.com/stretchr/testify/require" + "gocase/util" "testing" ) func TestCommand(t *testing.T) { + srv, err := util.StartServer(t, map[string]string{}) + require.NoError(t, err) + defer srv.Close() + ctx := context.Background() - rdb := redis.NewClient(&redis.Options{Addr: "localhost:6666"}) + rdb := srv.Client() t.Run("Kvrocks supports 180 commands currently", func(t *testing.T) { r := rdb.Do(ctx, "COMMAND", "COUNT") diff --git a/tests/gocase/unit/info/main_test.go b/tests/gocase/unit/info/info_test.go similarity index 93% rename from tests/gocase/unit/info/main_test.go rename to tests/gocase/unit/info/info_test.go index 7f3b974dfde..b4b5dde9fd7 100644 --- a/tests/gocase/unit/info/main_test.go +++ b/tests/gocase/unit/info/info_test.go @@ -3,8 +3,8 @@ package command import ( "context" "fmt" - "github.com/go-redis/redis/v9" "github.com/stretchr/testify/require" + "gocase/util" "regexp" "strconv" "strings" @@ -12,9 +12,13 @@ import ( "time" ) -func TestCommand(t *testing.T) { +func TestInfo(t *testing.T) { + srv, err := util.StartServer(t, map[string]string{}) + require.NoError(t, err) + defer srv.Close() + ctx := context.Background() - rdb := redis.NewClient(&redis.Options{Addr: "localhost:6666"}) + rdb := srv.Client() FindInfoEntry := func(t *testing.T, section string, key string) string { r := rdb.Info(ctx, section) diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go new file mode 100644 index 00000000000..5d6f34d85ae --- /dev/null +++ b/tests/gocase/util/server.go @@ -0,0 +1,83 @@ +package util + +import ( + "context" + "fmt" + "github.com/go-redis/redis/v9" + "github.com/stretchr/testify/require" + "net" + "os" + "os/exec" + "testing" + "time" +) + +type KvrocksServer struct { + t *testing.T + cmd *exec.Cmd + r *redis.Client +} + +func (s *KvrocksServer) Client() *redis.Client { + return s.r +} + +func (s *KvrocksServer) Close() { + require.NoError(s.t, s.cmd.Process.Kill()) + require.EqualError(s.t, s.cmd.Wait(), "signal: killed") +} + +func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error) { + b := os.Getenv("KVROCKS_BIN_PATH") + cmd := exec.Command(b) + + addr, err := findFreePort() + if err != nil { + return nil, err + } + configs["bind"] = addr.IP.String() + configs["port"] = fmt.Sprintf("%d", addr.Port) + + d := os.TempDir() + f, err := os.CreateTemp(d, "*.conf") + if err != nil { + return nil, err + } + + for k := range configs { + _, err := f.WriteString(fmt.Sprintf("%s %s\n", k, configs[k])) + if err != nil { + return nil, err + } + } + + cmd.Args = append(cmd.Args, "-c", f.Name()) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + if err := cmd.Start(); err != nil { + return nil, err + } + + r := redis.NewClient(&redis.Options{Addr: addr.String()}) + require.Eventually(t, func() bool { + return r.Ping(context.Background()).Err() == nil + }, time.Minute, time.Second) + + return &KvrocksServer{ + t: t, + cmd: cmd, + r: r, + }, nil +} + +func findFreePort() (*net.TCPAddr, error) { + addr, err := net.ResolveTCPAddr("tcp", "localhost:0") + if err != nil { + return nil, err + } + lis, err := net.ListenTCP("tcp", addr) + defer func() { _ = lis.Close() }() + return lis.Addr().(*net.TCPAddr), nil +} From 57f3fc40eaee43c9f162286f5b23995e51fd01c3 Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Sep 2022 22:29:26 +0800 Subject: [PATCH 03/12] wire outputs Signed-off-by: tison --- tests/gocase/util/server.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index 5d6f34d85ae..85eb93c9eca 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -16,6 +16,8 @@ type KvrocksServer struct { t *testing.T cmd *exec.Cmd r *redis.Client + + clean func() } func (s *KvrocksServer) Client() *redis.Client { @@ -38,8 +40,13 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error configs["bind"] = addr.IP.String() configs["port"] = fmt.Sprintf("%d", addr.Port) - d := os.TempDir() - f, err := os.CreateTemp(d, "*.conf") + dir := os.Getenv("GO_CASE_WORKSPACE") + require.NoError(t, err) + dir, err = os.MkdirTemp(dir, "Server-*") + require.NoError(t, err) + configs["dir"] = dir + + f, err := os.CreateTemp(dir, "*.conf") if err != nil { return nil, err } @@ -52,9 +59,13 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error } cmd.Args = append(cmd.Args, "-c", f.Name()) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + + stdout, err := os.Create(fmt.Sprintf("%s/%s", dir, "stdout")) + require.NoError(t, err) + cmd.Stdout = stdout + stderr, err := os.Create(fmt.Sprintf("%s/%s", dir, "stderr")) + require.NoError(t, err) + cmd.Stderr = stderr if err := cmd.Start(); err != nil { return nil, err @@ -69,6 +80,10 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error t: t, cmd: cmd, r: r, + clean: func() { + stdout.Close() + stderr.Close() + }, }, nil } From 6e38c64a7a25441a24283dd9cff6dc21c881695a Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Sep 2022 22:32:20 +0800 Subject: [PATCH 04/12] wire outputs Signed-off-by: tison --- tests/gocase/go.mod | 16 ++++++++++++++++ tests/gocase/go.sum | 2 -- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 tests/gocase/go.mod diff --git a/tests/gocase/go.mod b/tests/gocase/go.mod new file mode 100644 index 00000000000..2653e4d953c --- /dev/null +++ b/tests/gocase/go.mod @@ -0,0 +1,16 @@ +module gocase + +go 1.19 + +require ( + github.com/go-redis/redis/v9 v9.0.0-beta.2 + github.com/stretchr/testify v1.8.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/tests/gocase/go.sum b/tests/gocase/go.sum index ba95a157978..8a6efea2986 100644 --- a/tests/gocase/go.sum +++ b/tests/gocase/go.sum @@ -12,8 +12,6 @@ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q= -github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI= -github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 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= From 4eef6db6ba78c4f8ceb691176894d583c9fcadf2 Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 1 Sep 2022 22:34:30 +0800 Subject: [PATCH 05/12] add workspace Signed-off-by: tison --- tests/gocase/workspace/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/gocase/workspace/.gitignore diff --git a/tests/gocase/workspace/.gitignore b/tests/gocase/workspace/.gitignore new file mode 100644 index 00000000000..72e8ffc0db8 --- /dev/null +++ b/tests/gocase/workspace/.gitignore @@ -0,0 +1 @@ +* From c09776be27192214fe16838c9d6cd1807052e5b5 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Sep 2022 01:49:48 +0800 Subject: [PATCH 06/12] add gocases to workflows Signed-off-by: tison --- .github/workflows/kvrocks.yaml | 21 +++++++++++++++++++++ tests/gocase/util/server.go | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.github/workflows/kvrocks.yaml b/.github/workflows/kvrocks.yaml index a6106ff2fde..71aa3be5273 100644 --- a/.github/workflows/kvrocks.yaml +++ b/.github/workflows/kvrocks.yaml @@ -160,6 +160,27 @@ jobs: export ${{ matrix.runtime_env_vars }} ./build/unittest + - name: Setup Go + uses: actions/setup-go@v3 + with: + go-version: 1.19.x + + - name: Run Go Integration Cases + working-directory: tests/gocase + run: | + export ${{ matrix.runtime_env_vars }} + export KVROCKS_BIN_PATH="$GITHUB_WORKSPACE/build/kvrocks" + export GO_CASE_WORKSPACE="$GITHUB_WORKSPACE/tests/gocase/workspace" + go test -v ./... + SANITIZER_OUTPUT=$(grep "Sanitizer:" workspace -r || true) + if [[ $SANITIZER_OUTPUT ]]; then + echo "$SANITIZER_OUTPUT" + echo "\ndetail reports:\n" + cat $(find workspace -iname stderr) + echo "sanitizer error was reported, exiting..." + exit 1 + fi + - name: Run Redis Tcl Test run: | export ${{ matrix.runtime_env_vars }} diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index 85eb93c9eca..4cd2d588b68 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -81,8 +81,8 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error cmd: cmd, r: r, clean: func() { - stdout.Close() - stderr.Close() + require.NoError(t, stdout.Close()) + require.NoError(t, stderr.Close()) }, }, nil } From c1c9e699b4b0f55683cd4b3ee770a7a256f9647f Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Sep 2022 01:51:21 +0800 Subject: [PATCH 07/12] remove unit/command test from tcl tests Signed-off-by: tison --- tests/tcl/tests/test_helper.tcl | 1 - tests/tcl/tests/unit/command.tcl | 85 -------------------------------- 2 files changed, 86 deletions(-) delete mode 100644 tests/tcl/tests/unit/command.tcl diff --git a/tests/tcl/tests/test_helper.tcl b/tests/tcl/tests/test_helper.tcl index 8d078854056..b037aa5648e 100644 --- a/tests/tcl/tests/test_helper.tcl +++ b/tests/tcl/tests/test_helper.tcl @@ -56,7 +56,6 @@ set ::all_tests { unit/introspection unit/limits unit/geo - unit/command unit/config unit/scripting integration/slotmigrate diff --git a/tests/tcl/tests/unit/command.tcl b/tests/tcl/tests/unit/command.tcl deleted file mode 100644 index 5d3da15db42..00000000000 --- a/tests/tcl/tests/unit/command.tcl +++ /dev/null @@ -1,85 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -start_server {tags {"command"}} { - test {kvrocks has 180 commands currently} { - r command count - } {180} - - test {acquire GET command info by COMMAND INFO} { - set e [lindex [r command info get] 0] - assert_equal [llength $e] 6 - assert_equal [lindex $e 0] get - assert_equal [lindex $e 1] 2 - assert_equal [lindex $e 2] {readonly} - assert_equal [lindex $e 3] 1 - assert_equal [lindex $e 4] 1 - assert_equal [lindex $e 5] 1 - } - - test {COMMAND - command entry length check} { - set e [lindex [r command] 0] - assert_equal [llength $e] 6 - } - - test {get keys of commands by COMMAND GETKEYS} { - assert_equal {test} [r command getkeys get test] - assert_equal {test test2} [r command getkeys mget test test2] - assert_equal {test} [r command getkeys zadd test 1 m1] - } - - test {get rocksdb ops by COMMAND INFO} { - for {set i 0} {$i < 2} {incr i} { - for {set j 0} {$j < 500} {incr j} { - r lpush key$i value$i - r lrange key$i 0 1 - } - after 1000 - } - - set put_qps [s put_per_sec] - set get_qps [s get_per_sec] - set seek_qps [s seek_per_sec] - set next_qps [s next_per_sec] - - assert {$put_qps > 0} - assert {$get_qps > 0} - assert {$seek_qps > 0} - # prev_per_sec is almost the same as next_per_sec - assert {$next_qps > 0} - } - - test {get bgsave information from INFO command} { - assert_equal 0 [s bgsave_in_progress] - assert_equal -1 [s last_bgsave_time] - assert_equal ok [s last_bgsave_status] - assert_equal -1 [s last_bgsave_time_sec] - - assert_equal {OK} [r bgsave] - wait_for_condition 100 500 { - [s bgsave_in_progress] == 0 - } else { - fail "Fail to bgsave" - } - - set last_bgsave_time [s last_bgsave_time] - assert {$last_bgsave_time > 1640507660} - assert_equal ok [s last_bgsave_status] - set last_bgsave_time_sec [s last_bgsave_time_sec] - assert {$last_bgsave_time_sec < 3 && $last_bgsave_time_sec >= 0} - } -} From 3fa8c243ac814956756deb83291a8b5975e15fe8 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Sep 2022 02:21:23 +0800 Subject: [PATCH 08/12] update license header checks Signed-off-by: tison --- tests/gocase/unit/command/command_test.go | 20 ++++++++++++++++++++ tests/gocase/unit/info/info_test.go | 20 ++++++++++++++++++++ tests/gocase/util/server.go | 20 ++++++++++++++++++++ tools/ci/licenserc.yml | 3 +-- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/tests/gocase/unit/command/command_test.go b/tests/gocase/unit/command/command_test.go index f4b3715a76c..32819f4097d 100644 --- a/tests/gocase/unit/command/command_test.go +++ b/tests/gocase/unit/command/command_test.go @@ -1,3 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + package command import ( diff --git a/tests/gocase/unit/info/info_test.go b/tests/gocase/unit/info/info_test.go index b4b5dde9fd7..352eaecb9d2 100644 --- a/tests/gocase/unit/info/info_test.go +++ b/tests/gocase/unit/info/info_test.go @@ -1,3 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + package command import ( diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index 4cd2d588b68..1c27f360007 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -1,3 +1,23 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + package util import ( diff --git a/tools/ci/licenserc.yml b/tools/ci/licenserc.yml index ccf4bb16e27..0f0325f5bed 100644 --- a/tools/ci/licenserc.yml +++ b/tools/ci/licenserc.yml @@ -24,11 +24,10 @@ header: - '**/CMakeLists.txt' - '**/*.cc' - '**/*.cmake' + - '**/*.go' - '**/*.h' - '**/*.py' - '**/*.sh' - '**/*.tcl' - '**/*.yml' - '**/*.yaml' - paths-ignore: - - 'external/' \ No newline at end of file From a3294c35d2805e15c7b87059ef3b444a475e6439 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Sep 2022 02:30:28 +0800 Subject: [PATCH 09/12] call clean on Close Signed-off-by: tison --- tests/gocase/util/server.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index 1c27f360007..a4e7d98350a 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -47,6 +47,7 @@ func (s *KvrocksServer) Client() *redis.Client { func (s *KvrocksServer) Close() { require.NoError(s.t, s.cmd.Process.Kill()) require.EqualError(s.t, s.cmd.Wait(), "signal: killed") + s.clean() } func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error) { From c08fa7488ff17afde142e41afcae808dd9c36fbf Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Sep 2022 17:32:47 +0800 Subject: [PATCH 10/12] address comments Signed-off-by: tison --- tests/gocase/go.mod | 2 +- tests/gocase/unit/command/command_test.go | 5 +++-- tests/gocase/unit/info/info_test.go | 5 +++-- tests/gocase/util/server.go | 8 +++++--- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/tests/gocase/go.mod b/tests/gocase/go.mod index 2653e4d953c..cb7138c456c 100644 --- a/tests/gocase/go.mod +++ b/tests/gocase/go.mod @@ -1,4 +1,4 @@ -module gocase +module github.com/apache/incubator-kvrocks/tests/gocase go 1.19 diff --git a/tests/gocase/unit/command/command_test.go b/tests/gocase/unit/command/command_test.go index 32819f4097d..11c6f5cc3ba 100644 --- a/tests/gocase/unit/command/command_test.go +++ b/tests/gocase/unit/command/command_test.go @@ -22,9 +22,10 @@ package command import ( "context" - "github.com/stretchr/testify/require" - "gocase/util" "testing" + + "github.com/apache/incubator-kvrocks/tests/gocase/util" + "github.com/stretchr/testify/require" ) func TestCommand(t *testing.T) { diff --git a/tests/gocase/unit/info/info_test.go b/tests/gocase/unit/info/info_test.go index 352eaecb9d2..e0f30741fe2 100644 --- a/tests/gocase/unit/info/info_test.go +++ b/tests/gocase/unit/info/info_test.go @@ -23,13 +23,14 @@ package command import ( "context" "fmt" - "github.com/stretchr/testify/require" - "gocase/util" "regexp" "strconv" "strings" "testing" "time" + + "github.com/apache/incubator-kvrocks/tests/gocase/util" + "github.com/stretchr/testify/require" ) func TestInfo(t *testing.T) { diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index a4e7d98350a..f5f4109b18c 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -23,13 +23,15 @@ package util import ( "context" "fmt" - "github.com/go-redis/redis/v9" - "github.com/stretchr/testify/require" "net" "os" "os/exec" + "path/filepath" "testing" "time" + + "github.com/go-redis/redis/v9" + "github.com/stretchr/testify/require" ) type KvrocksServer struct { @@ -67,7 +69,7 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error require.NoError(t, err) configs["dir"] = dir - f, err := os.CreateTemp(dir, "*.conf") + f, err := os.Create(filepath.Join(dir, "kvrocks.conf")) if err != nil { return nil, err } From 25cbcdd5b76bff26c7cb0e8b50f85507d3bac12a Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 2 Sep 2022 17:37:42 +0800 Subject: [PATCH 11/12] tidy code Signed-off-by: tison --- tests/gocase/util/server.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index f5f4109b18c..9f266ecc3d3 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -83,10 +83,10 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error cmd.Args = append(cmd.Args, "-c", f.Name()) - stdout, err := os.Create(fmt.Sprintf("%s/%s", dir, "stdout")) + stdout, err := os.Create(filepath.Join(dir, "stdout")) require.NoError(t, err) cmd.Stdout = stdout - stderr, err := os.Create(fmt.Sprintf("%s/%s", dir, "stderr")) + stderr, err := os.Create(filepath.Join(dir, "stderr")) require.NoError(t, err) cmd.Stderr = stderr From 3a3e1c571c0854da3d8a0a47b399c61a39717bbf Mon Sep 17 00:00:00 2001 From: tison Date: Sat, 3 Sep 2022 13:06:43 +0800 Subject: [PATCH 12/12] new client Signed-off-by: tison --- tests/gocase/unit/command/command_test.go | 3 ++- tests/gocase/unit/info/info_test.go | 3 ++- tests/gocase/util/server.go | 21 +++++++++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/gocase/unit/command/command_test.go b/tests/gocase/unit/command/command_test.go index 11c6f5cc3ba..b576c05cc3f 100644 --- a/tests/gocase/unit/command/command_test.go +++ b/tests/gocase/unit/command/command_test.go @@ -34,7 +34,8 @@ func TestCommand(t *testing.T) { defer srv.Close() ctx := context.Background() - rdb := srv.Client() + rdb := srv.NewClient() + defer func() { require.NoError(t, rdb.Close()) }() t.Run("Kvrocks supports 180 commands currently", func(t *testing.T) { r := rdb.Do(ctx, "COMMAND", "COUNT") diff --git a/tests/gocase/unit/info/info_test.go b/tests/gocase/unit/info/info_test.go index e0f30741fe2..54acac00c38 100644 --- a/tests/gocase/unit/info/info_test.go +++ b/tests/gocase/unit/info/info_test.go @@ -39,7 +39,8 @@ func TestInfo(t *testing.T) { defer srv.Close() ctx := context.Background() - rdb := srv.Client() + rdb := srv.NewClient() + defer func() { require.NoError(t, rdb.Close()) }() FindInfoEntry := func(t *testing.T, section string, key string) string { r := rdb.Info(ctx, section) diff --git a/tests/gocase/util/server.go b/tests/gocase/util/server.go index 9f266ecc3d3..1b155727011 100644 --- a/tests/gocase/util/server.go +++ b/tests/gocase/util/server.go @@ -35,15 +35,15 @@ import ( ) type KvrocksServer struct { - t *testing.T - cmd *exec.Cmd - r *redis.Client + t *testing.T + cmd *exec.Cmd + addr net.Addr clean func() } -func (s *KvrocksServer) Client() *redis.Client { - return s.r +func (s *KvrocksServer) NewClient() *redis.Client { + return redis.NewClient(&redis.Options{Addr: s.addr.String()}) } func (s *KvrocksServer) Close() { @@ -94,15 +94,16 @@ func StartServer(t *testing.T, configs map[string]string) (*KvrocksServer, error return nil, err } - r := redis.NewClient(&redis.Options{Addr: addr.String()}) + c := redis.NewClient(&redis.Options{Addr: addr.String()}) + defer func() { require.NoError(t, c.Close()) }() require.Eventually(t, func() bool { - return r.Ping(context.Background()).Err() == nil + return c.Ping(context.Background()).Err() == nil }, time.Minute, time.Second) return &KvrocksServer{ - t: t, - cmd: cmd, - r: r, + t: t, + cmd: cmd, + addr: addr, clean: func() { require.NoError(t, stdout.Close()) require.NoError(t, stderr.Close())