Skip to content

Commit

Permalink
test(keeper): add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qevolg committed Dec 3, 2024
1 parent f1902dd commit c81bb88
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 15 deletions.
36 changes: 36 additions & 0 deletions tools/keeper/api/adapter2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -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)
}
}
}
9 changes: 5 additions & 4 deletions tools/keeper/api/gen_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions tools/keeper/api/gen_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/taosdata/taoskeeper/db"
Expand Down Expand Up @@ -255,6 +256,7 @@ func TestGenMetric(t *testing.T) {
}
})
}

func TestGetSubTableName(t *testing.T) {
tests := []struct {
stbName string
Expand Down Expand Up @@ -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)
}
}
}
51 changes: 51 additions & 0 deletions tools/keeper/api/tables_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
1 change: 0 additions & 1 deletion tools/keeper/cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
55 changes: 55 additions & 0 deletions tools/keeper/cmd/command_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
8 changes: 0 additions & 8 deletions tools/keeper/cmd/empty_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions tools/keeper/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c81bb88

Please sign in to comment.