Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
庄润梓 committed Jul 1, 2024
1 parent 0245666 commit d2ef0e1
Showing 1 changed file with 87 additions and 80 deletions.
167 changes: 87 additions & 80 deletions tests/e2e/greptimedbcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@
package e2e

import (
"context"
"database/sql"
"fmt"
"os"
"os/exec"
"time"

"github.com/go-sql-driver/mysql"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"k8s.io/klog/v2"
)

/*
const (
createTableSQL = `CREATE TABLE dist_table (
createTableSQL = `CREATE TABLE dist_table (
ts TIMESTAMP DEFAULT current_timestamp(),
n INT,
row_id INT,
Expand All @@ -40,31 +45,29 @@ const (
n >= 9
)`

insertDataSQLStr = "INSERT INTO dist_table(n, row_id) VALUES (%d, %d)"
insertDataSQLStr = "INSERT INTO dist_table(n, row_id) VALUES (%d, %d)"

selectDataSQL = `SELECT * FROM dist_table`
testRowIDNum = 10
selectDataSQL = `SELECT * FROM dist_table`

testRowIDNum = 10
)

var (
defaultQueryTimeout = 5 * time.Second
)

// TestData is the schema of test data in SQL table.

type TestData struct {
timestamp string
n int32
rowID int32
}
*/
type TestData struct {
timestamp string
n int32
rowID int32
}

var _ = Describe("Basic test of greptimedb cluster", Ordered, func() {
It("Bootstrap cluster", func() {
var err error
sig := true
err = createCluster()
Expect(err).NotTo(HaveOccurred(), "failed to create cluster")

Expand All @@ -73,70 +76,72 @@ var _ = Describe("Basic test of greptimedb cluster", Ordered, func() {

err = listCluster()
Expect(err).NotTo(HaveOccurred(), "failed to list cluster")
/*
go func() {
forwardRequest()
}()
By("Connecting GreptimeDB")
var db *sql.DB
var conn *sql.Conn
Eventually(func() error {
cfg := mysql.Config{
Net: "tcp",
Addr: "127.0.0.1:4002",
User: "",
Passwd: "",
DBName: "",
AllowNativePasswords: true,
}
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
return err
}
conn, err = db.Conn(context.TODO())
if err != nil {
return err
}
return nil
}, 30*time.Second, time.Second).ShouldNot(HaveOccurred())
By("Execute SQL queries after connecting")
ctx, cancel := context.WithTimeout(context.Background(), defaultQueryTimeout)
defer cancel()
_, err = conn.ExecContext(ctx, createTableSQL)
Expect(err).NotTo(HaveOccurred(), "failed to create SQL table")
ctx, cancel = context.WithTimeout(context.Background(), defaultQueryTimeout)
defer cancel()
for rowID := 1; rowID <= testRowIDNum; rowID++ {
insertDataSQL := fmt.Sprintf(insertDataSQLStr, rowID, rowID)
_, err = conn.ExecContext(ctx, insertDataSQL)
Expect(err).NotTo(HaveOccurred(), "failed to insert data")

go func() {
forwardRequest(sig)
}()

By("Connecting GreptimeDB")
var db *sql.DB
var conn *sql.Conn

Eventually(func() error {
cfg := mysql.Config{
Net: "tcp",
Addr: "127.0.0.1:4002",
User: "",
Passwd: "",
DBName: "",
AllowNativePasswords: true,
}

db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
return err
}

ctx, cancel = context.WithTimeout(context.Background(), defaultQueryTimeout)
defer cancel()
results, err := conn.QueryContext(ctx, selectDataSQL)
Expect(err).NotTo(HaveOccurred(), "failed to get data")
var data []TestData
for results.Next() {
var d TestData
err = results.Scan(&d.timestamp, &d.n, &d.rowID)
Expect(err).NotTo(HaveOccurred(), "failed to scan data that query from db")
data = append(data, d)
conn, err = db.Conn(context.TODO())
if err != nil {
return err
}
Expect(len(data) == testRowIDNum).Should(BeTrue(), "get the wrong data from db")
*/

return nil
}, 30*time.Second, time.Second).ShouldNot(HaveOccurred())

By("Execute SQL queries after connecting")

ctx, cancel := context.WithTimeout(context.Background(), defaultQueryTimeout)
defer cancel()

_, err = conn.ExecContext(ctx, createTableSQL)
Expect(err).NotTo(HaveOccurred(), "failed to create SQL table")

ctx, cancel = context.WithTimeout(context.Background(), defaultQueryTimeout)
defer cancel()
for rowID := 1; rowID <= testRowIDNum; rowID++ {
insertDataSQL := fmt.Sprintf(insertDataSQLStr, rowID, rowID)
_, err = conn.ExecContext(ctx, insertDataSQL)
Expect(err).NotTo(HaveOccurred(), "failed to insert data")
}

ctx, cancel = context.WithTimeout(context.Background(), defaultQueryTimeout)
defer cancel()
results, err := conn.QueryContext(ctx, selectDataSQL)
Expect(err).NotTo(HaveOccurred(), "failed to get data")

var data []TestData
for results.Next() {
var d TestData
err = results.Scan(&d.timestamp, &d.n, &d.rowID)
Expect(err).NotTo(HaveOccurred(), "failed to scan data that query from db")
data = append(data, d)
}
Expect(len(data) == testRowIDNum).Should(BeTrue(), "get the wrong data from db")

err = deleteCluster()
Expect(err).NotTo(HaveOccurred(), "failed to delete cluster")

sig = false
})
})

Expand Down Expand Up @@ -180,14 +185,16 @@ func deleteCluster() error {
return nil
}

/*
func forwardRequest() {
func forwardRequest(sig bool) {
for {
cmd := exec.Command("kubectl", "port-forward", "svc/mydb-frontend", "4002:4002")
if err := cmd.Run(); err != nil {
klog.Errorf("Failed to port forward: %v", err)
if sig {
cmd := exec.Command("kubectl", "port-forward", "svc/mydb-frontend", "4002:4002")
if err := cmd.Run(); err != nil {
klog.Errorf("Failed to port forward: %v", err)
return
}
} else {
return
}
}
}
*/

0 comments on commit d2ef0e1

Please sign in to comment.