From c81bb880f034a7f40054ba04ebc3fafb600031f6 Mon Sep 17 00:00:00 2001 From: qevolg <2227465945@qq.com> Date: Tue, 3 Dec 2024 16:03:24 +0800 Subject: [PATCH] test(keeper): add test cases --- tools/keeper/api/adapter2_test.go | 36 +++++++++++++++++++ tools/keeper/api/gen_metric.go | 9 ++--- tools/keeper/api/gen_metric_test.go | 41 +++++++++++++++++++++ tools/keeper/api/tables_test.go | 51 ++++++++++++++++++++++++++ tools/keeper/cmd/command.go | 1 - tools/keeper/cmd/command_test.go | 55 +++++++++++++++++++++++++++++ tools/keeper/cmd/empty_test.go | 8 ----- tools/keeper/util/util.go | 2 -- 8 files changed, 188 insertions(+), 15 deletions(-) create mode 100644 tools/keeper/api/tables_test.go create mode 100644 tools/keeper/cmd/command_test.go delete mode 100644 tools/keeper/cmd/empty_test.go diff --git a/tools/keeper/api/adapter2_test.go b/tools/keeper/api/adapter2_test.go index e6fd263c43..4ce003bd47 100644 --- a/tools/keeper/api/adapter2_test.go +++ b/tools/keeper/api/adapter2_test.go @@ -2,6 +2,7 @@ package api import ( "context" + "fmt" "net/http" "net/http/httptest" "strings" @@ -96,3 +97,38 @@ func TestAdapter2(t *testing.T) { conn.Exec(context.Background(), "drop database "+c.Metrics.Database.Name, util.GetQidOwn()) } + +func Test_adapterTableSql(t *testing.T) { + conn, _ := db.NewConnector("root", "taosdata", "127.0.0.1", 6041, false) + defer conn.Close() + + dbName := "db_202412031446" + conn.Exec(context.Background(), "create database "+dbName, util.GetQidOwn()) + defer conn.Exec(context.Background(), "drop database "+dbName, util.GetQidOwn()) + + conn, _ = db.NewConnectorWithDb("root", "taosdata", "127.0.0.1", 6041, dbName, false) + defer conn.Close() + + conn.Exec(context.Background(), adapterTableSql, util.GetQidOwn()) + + testCases := []struct { + ep string + wantErr bool + }{ + {"", false}, + {"hello", false}, + {strings.Repeat("a", 128), false}, + {strings.Repeat("a", 255), false}, + {strings.Repeat("a", 256), true}, + } + + for i, tc := range testCases { + sql := fmt.Sprintf("create table d%d using adapter_requests tags ('%s', 0)", i, tc.ep) + _, err := conn.Exec(context.Background(), sql, util.GetQidOwn()) + if tc.wantErr { + assert.Error(t, err) // [0x2653] Value too long for column/tag: endpoint + } else { + assert.NoError(t, err) + } + } +} diff --git a/tools/keeper/api/gen_metric.go b/tools/keeper/api/gen_metric.go index a78076d6db..bb0908589a 100644 --- a/tools/keeper/api/gen_metric.go +++ b/tools/keeper/api/gen_metric.go @@ -748,13 +748,14 @@ func (gm *GeneralMetric) initColumnSeqMap() error { } func (gm *GeneralMetric) createSTables() error { - var createTableSql = "create stable if not exists taosd_cluster_basic " + - "(ts timestamp, first_ep varchar(255), first_ep_dnode_id INT, cluster_version varchar(20)) " + - "tags (cluster_id varchar(50))" - if gm.conn == nil { return errNoConnection } + + createTableSql := "create stable if not exists taosd_cluster_basic " + + "(ts timestamp, first_ep varchar(255), first_ep_dnode_id INT, cluster_version varchar(20)) " + + "tags (cluster_id varchar(50))" + _, err := gm.conn.Exec(context.Background(), createTableSql, util.GetQidOwn()) if err != nil { return err diff --git a/tools/keeper/api/gen_metric_test.go b/tools/keeper/api/gen_metric_test.go index 88987d6544..b66e61ff07 100644 --- a/tools/keeper/api/gen_metric_test.go +++ b/tools/keeper/api/gen_metric_test.go @@ -7,6 +7,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/taosdata/taoskeeper/db" @@ -255,6 +256,7 @@ func TestGenMetric(t *testing.T) { } }) } + func TestGetSubTableName(t *testing.T) { tests := []struct { stbName string @@ -356,3 +358,42 @@ func TestGetSubTableName(t *testing.T) { }) } } + +func Test_createSTables(t *testing.T) { + conn, _ := db.NewConnector("root", "taosdata", "127.0.0.1", 6041, false) + defer conn.Close() + + dbName := "db_202412031527" + conn.Exec(context.Background(), "create database "+dbName, util.GetQidOwn()) + defer conn.Exec(context.Background(), "drop database "+dbName, util.GetQidOwn()) + + conn, _ = db.NewConnectorWithDb("root", "taosdata", "127.0.0.1", 6041, dbName, false) + defer conn.Close() + + gm := GeneralMetric{conn: conn} + gm.createSTables() + + testCases := []struct { + ep string + wantErr bool + }{ + {"", false}, + {"hello", false}, + {strings.Repeat("a", 128), false}, + {strings.Repeat("a", 255), false}, + {strings.Repeat("a", 256), true}, + } + + conn.Exec(context.Background(), + "create table d0 using taosd_cluster_basic tags('cluster_id')", util.GetQidOwn()) + + for _, tc := range testCases { + sql := fmt.Sprintf("insert into d0 (ts, first_ep) values(%d, '%s')", time.Now().UnixMilli(), tc.ep) + _, err := conn.Exec(context.Background(), sql, util.GetQidOwn()) + if tc.wantErr { + assert.Error(t, err) // [0x2653] Value too long for column/tag: endpoint + } else { + assert.NoError(t, err) + } + } +} diff --git a/tools/keeper/api/tables_test.go b/tools/keeper/api/tables_test.go new file mode 100644 index 0000000000..18962f0231 --- /dev/null +++ b/tools/keeper/api/tables_test.go @@ -0,0 +1,51 @@ +package api + +import ( + "context" + "fmt" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/taosdata/taoskeeper/db" + "github.com/taosdata/taoskeeper/util" +) + +func TestCreateClusterInfoSql(t *testing.T) { + conn, _ := db.NewConnector("root", "taosdata", "127.0.0.1", 6041, false) + defer conn.Close() + + dbName := "db_202412031539" + conn.Exec(context.Background(), "create database "+dbName, util.GetQidOwn()) + defer conn.Exec(context.Background(), "drop database "+dbName, util.GetQidOwn()) + + conn, _ = db.NewConnectorWithDb("root", "taosdata", "127.0.0.1", 6041, dbName, false) + defer conn.Close() + + conn.Exec(context.Background(), CreateClusterInfoSql, util.GetQidOwn()) + + testCases := []struct { + ep string + wantErr bool + }{ + {"", false}, + {"hello", false}, + {strings.Repeat("a", 128), false}, + {strings.Repeat("a", 255), false}, + {strings.Repeat("a", 256), true}, + } + + conn.Exec(context.Background(), + "create table d0 using cluster_info tags('cluster_id')", util.GetQidOwn()) + + for _, tc := range testCases { + sql := fmt.Sprintf("insert into d0 (ts, first_ep) values(%d, '%s')", time.Now().UnixMilli(), tc.ep) + _, err := conn.Exec(context.Background(), sql, util.GetQidOwn()) + if tc.wantErr { + assert.Error(t, err) // [0x2653] Value too long for column/tag: endpoint + } else { + assert.NoError(t, err) + } + } +} diff --git a/tools/keeper/cmd/command.go b/tools/keeper/cmd/command.go index 3e054a1963..33d2482628 100644 --- a/tools/keeper/cmd/command.go +++ b/tools/keeper/cmd/command.go @@ -315,7 +315,6 @@ func (cmd *Command) TransferDataToDest(data *db.Data, dstTable string, tagNum in // cluster_info func (cmd *Command) TransferTaosdClusterBasicInfo() error { - ctx := context.Background() endTime := time.Now() diff --git a/tools/keeper/cmd/command_test.go b/tools/keeper/cmd/command_test.go new file mode 100644 index 0000000000..b75b0a75ac --- /dev/null +++ b/tools/keeper/cmd/command_test.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "context" + "fmt" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/taosdata/taoskeeper/db" + "github.com/taosdata/taoskeeper/infrastructure/config" + "github.com/taosdata/taoskeeper/util" +) + +func TestTransferTaosdClusterBasicInfo(t *testing.T) { + config.InitConfig() + + conn, _ := db.NewConnector("root", "taosdata", "127.0.0.1", 6041, false) + defer conn.Close() + + dbName := "db_202412031539" + conn.Exec(context.Background(), "create database "+dbName, util.GetQidOwn()) + defer conn.Exec(context.Background(), "drop database "+dbName, util.GetQidOwn()) + + conn, _ = db.NewConnectorWithDb("root", "taosdata", "127.0.0.1", 6041, dbName, false) + defer conn.Close() + + cmd := Command{conn: conn, fromTime: time.Now().Add(time.Duration(1 * time.Hour))} + cmd.TransferTaosdClusterBasicInfo() + + testCases := []struct { + ep string + wantErr bool + }{ + {"", false}, + {"hello", false}, + {strings.Repeat("a", 128), false}, + {strings.Repeat("a", 255), false}, + {strings.Repeat("a", 256), true}, + } + + conn.Exec(context.Background(), + "create table d0 using taosd_cluster_basic tags('cluster_id')", util.GetQidOwn()) + + for _, tc := range testCases { + sql := fmt.Sprintf("insert into d0 (ts, first_ep) values(%d, '%s')", time.Now().UnixMilli(), tc.ep) + _, err := conn.Exec(context.Background(), sql, util.GetQidOwn()) + if tc.wantErr { + assert.Error(t, err) // [0x2653] Value too long for column/tag: endpoint + } else { + assert.NoError(t, err) + } + } +} diff --git a/tools/keeper/cmd/empty_test.go b/tools/keeper/cmd/empty_test.go deleted file mode 100644 index 143df6893c..0000000000 --- a/tools/keeper/cmd/empty_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package cmd - -import ( - "testing" -) - -func TestEmpty(t *testing.T) { -} diff --git a/tools/keeper/util/util.go b/tools/keeper/util/util.go index a739c23760..6711db71e1 100644 --- a/tools/keeper/util/util.go +++ b/tools/keeper/util/util.go @@ -118,9 +118,7 @@ func GetQid(qidStr string) uint64 { } func GetQidOwn() uint64 { - id := atomic.AddUint64(&globalCounter64, 1) - if id > 0x00ffffffffffffff { atomic.StoreUint64(&globalCounter64, 1) id = 1