From f45e9d6da2f7edef789551c8c1d725a49e216a84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E6=B6=A6=E6=A2=93?= <528185299@qq.com> Date: Tue, 18 Jun 2024 15:12:25 +0800 Subject: [PATCH] fix9 --- tests/e2e/greptimedbcluster_baremetal_test.go | 166 ++++++++++++++++++ tests/e2e/greptimedbcluster_test.go | 45 ----- 2 files changed, 166 insertions(+), 45 deletions(-) diff --git a/tests/e2e/greptimedbcluster_baremetal_test.go b/tests/e2e/greptimedbcluster_baremetal_test.go index e69de29b..193dc448 100644 --- a/tests/e2e/greptimedbcluster_baremetal_test.go +++ b/tests/e2e/greptimedbcluster_baremetal_test.go @@ -0,0 +1,166 @@ +/* + * Copyright 2023 Greptime Team + * + * Licensed 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 e2e + +import ( + "context" + "database/sql" + "fmt" + "os" + "os/exec" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/go-sql-driver/mysql" +) + +var _ = Describe("Basic test of greptimedb cluster", func() { + It("Bootstrap cluster", func() { + var err error + + go func() { + err = createClusterinBaremetal() + Expect(err).NotTo(HaveOccurred(), "failed to create cluster in baremetal") + }() + + go func() { + checkInterval := 5 * time.Second + timeout := 100 * time.Second + startTime := time.Now() + + for { + if time.Since(startTime) > timeout { + Expect(fmt.Errorf("failed to get cluster in baremetal")).NotTo(HaveOccurred()) + break + } + + err := getClusterinBaremetal() + if err == nil { + break + } + time.Sleep(checkInterval) + } + }() + + 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") + } + + 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") + go func() { + checkInterval := 5 * time.Second + timeout := 100 * time.Second + startTime := time.Now() + + for { + if time.Since(startTime) > timeout { + Expect(fmt.Errorf("failed to delete cluster in baremetal")).NotTo(HaveOccurred()) + break + } + + err := deleteClusterinBaremetal() + if err == nil { + break + } + time.Sleep(checkInterval) + } + }() + }) +}) + +func createClusterinBaremetal() error { + cmd := exec.Command("../../bin/gtctl", "cluster", "create", "mydb", "--bare-metal") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return err + } + return nil +} + +func getClusterinBaremetal() error { + cmd := exec.Command("../../bin/gtctl", "cluster", "get", "mydb", "--bare-metal") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return err + } + return nil +} + +func deleteClusterinBaremetal() error { + cmd := exec.Command("../../bin/gtctl", "cluster", "delete", "mydb", "--tear-down-etcd", "--bare-metal") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return err + } + return nil +} diff --git a/tests/e2e/greptimedbcluster_test.go b/tests/e2e/greptimedbcluster_test.go index 518f5870..30b35f2d 100644 --- a/tests/e2e/greptimedbcluster_test.go +++ b/tests/e2e/greptimedbcluster_test.go @@ -75,21 +75,6 @@ var _ = Describe("Basic test of greptimedb cluster", func() { err = listCluster() Expect(err).NotTo(HaveOccurred(), "failed to list cluster") - go func() { - err = createClusterinBaremetal() - Expect(err).NotTo(HaveOccurred(), "failed to create cluster in baremetal") - }() - - go func() { - time.Sleep(100 * time.Second) - - err = getClusterinBaremetal() - Expect(err).NotTo(HaveOccurred(), "failed to get cluster in baremetal") - - err = deleteClusterinBaremetal() - Expect(err).NotTo(HaveOccurred(), "failed to delete cluster in baremetal") - }() - go func() { forwardRequest() }() @@ -166,16 +151,6 @@ func createCluster() error { return nil } -func createClusterinBaremetal() error { - cmd := exec.Command("../../bin/gtctl", "cluster", "create", "mydb", "--bare-metal") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return err - } - return nil -} - func getCluster() error { cmd := exec.Command("../../bin/gtctl", "cluster", "get", "mydb") cmd.Stdout = os.Stdout @@ -186,16 +161,6 @@ func getCluster() error { return nil } -func getClusterinBaremetal() error { - cmd := exec.Command("../../bin/gtctl", "cluster", "get", "mydb", "--bare-metal") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return err - } - return nil -} - func listCluster() error { cmd := exec.Command("../../bin/gtctl", "cluster", "list") cmd.Stdout = os.Stdout @@ -216,16 +181,6 @@ func deleteCluster() error { return nil } -func deleteClusterinBaremetal() error { - cmd := exec.Command("../../bin/gtctl", "cluster", "delete", "mydb", "--tear-down-etcd", "--bare-metal") - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return err - } - return nil -} - func forwardRequest() { for { cmd := exec.Command("kubectl", "port-forward", "svc/mydb-frontend", "4002:4002")