Skip to content

Commit

Permalink
session: migrate test-infra to testify for testBackupRestoreSuite (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Apr 29, 2022
1 parent e01675c commit 3ee4328
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
71 changes: 71 additions & 0 deletions session/backup_restore_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2022 PingCAP, Inc.
//
// 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.

// This file contains tests about backup restore (br) which need running with real TiKV.
// Only tests under /session will be run with real TiKV, so we put them here instead of /br.

package session_test

import (
"os"
"path"
"testing"

"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)

// TODO move this test to BR integration tests.
func TestBackupAndRestore(t *testing.T) {
if !*withTiKV {
t.Skip("only run BR SQL integration test with tikv store")
}

store, clean := createStorage(t)
defer clean()

cfg := config.GetGlobalConfig()
cfg.Store = "tikv"
cfg.Path = "127.0.0.1:2379"
config.StoreGlobalConfig(cfg)

tk := testkit.NewTestKit(t, store)
tk.MustExec("create database if not exists br")
tk.MustExec("use br")
tk.MustExec("create table t1(v int)")
tk.MustExec("insert into t1 values (1)")
tk.MustExec("insert into t1 values (2)")
tk.MustExec("insert into t1 values (3)")
tk.MustQuery("select count(*) from t1").Check(testkit.Rows("3"))

tk.MustExec("create database if not exists br02")
tk.MustExec("use br02")
tk.MustExec("create table t1(v int)")

tmpDir := path.Join(os.TempDir(), "bk1")
require.NoError(t, os.RemoveAll(tmpDir))
// backup database to tmp dir
tk.MustQuery("backup database br to 'local://" + tmpDir + "'")

// remove database for recovery
tk.MustExec("drop database br")
tk.MustExec("drop database br02")

// restore database with backup data
tk.MustQuery("restore database * from 'local://" + tmpDir + "'")
tk.MustExec("use br")
tk.MustQuery("select count(*) from t1").Check(testkit.Rows("3"))
tk.MustExec("drop database br")
}
78 changes: 5 additions & 73 deletions session/session_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"flag"
"fmt"
"net"
"os"
"path"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -74,20 +72,16 @@ import (
"google.golang.org/grpc"
)

var (
pdAddrs = flag.String("pd-addrs", "127.0.0.1:2379", "pd addrs")
withTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")
pdAddrChan chan string
initPdAddrsOnce sync.Once
)
var withTiKV = flag.Bool("with-tikv", false, "run tests with TiKV cluster started. (not use the mock server)")

var _ = flag.String("pd-addrs", "127.0.0.1:2379", "workaroundGoCheckFlags: pd-addrs")

var _ = Suite(&testSessionSuite{})
var _ = Suite(&testSessionSuite2{})
var _ = Suite(&testSessionSuite3{})
var _ = Suite(&testSchemaSuite{})
var _ = SerialSuites(&testSchemaSerialSuite{})
var _ = SerialSuites(&testSessionSerialSuite{})
var _ = SerialSuites(&testBackupRestoreSuite{})
var _ = SerialSuites(&testTxnStateSerialSuite{})

type testSessionSuiteBase struct {
Expand All @@ -113,10 +107,6 @@ type testSessionSerialSuite struct {
testSessionSuiteBase
}

type testBackupRestoreSuite struct {
testSessionSuiteBase
}

func clearStorage(store kv.Storage) error {
txn, err := store.Begin()
if err != nil {
Expand Down Expand Up @@ -172,25 +162,11 @@ func clearETCD(ebd kv.EtcdBackend) error {
return nil
}

func initPdAddrs() {
initPdAddrsOnce.Do(func() {
addrs := strings.Split(*pdAddrs, ",")
pdAddrChan = make(chan string, len(addrs))
for _, addr := range addrs {
addr = strings.TrimSpace(addr)
if addr != "" {
pdAddrChan <- addr
}
}
})
}

func (s *testSessionSuiteBase) SetUpSuite(c *C) {
testleak.BeforeTest()

if *withTiKV {
initPdAddrs()
s.pdAddr = <-pdAddrChan
s.pdAddr = "127.0.0.1:2379"
var d driver.TiKVDriver
config.UpdateGlobal(func(conf *config.Config) {
conf.TxnLocalLatches.Enabled = false
Expand Down Expand Up @@ -224,9 +200,6 @@ func (s *testSessionSuiteBase) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
if *withTiKV {
pdAddrChan <- s.pdAddr
}
}

func (s *testSessionSuiteBase) TearDownTest(c *C) {
Expand All @@ -253,13 +226,11 @@ func createStorage(t *testing.T) (kv.Storage, func()) {

func createStorageAndDomain(t *testing.T) (kv.Storage, *domain.Domain, func()) {
if *withTiKV {
initPdAddrs()
pdAddr := <-pdAddrChan
var d driver.TiKVDriver
config.UpdateGlobal(func(conf *config.Config) {
conf.TxnLocalLatches.Enabled = false
})
store, err := d.Open(fmt.Sprintf("tikv://%s?disableGC=true", pdAddr))
store, err := d.Open("tikv://127.0.0.1:2379?disableGC=true")
require.NoError(t, err)
require.NoError(t, clearStorage(store))
require.NoError(t, clearETCD(store.(kv.EtcdBackend)))
Expand All @@ -270,7 +241,6 @@ func createStorageAndDomain(t *testing.T) (kv.Storage, *domain.Domain, func()) {
return store, dom, func() {
dom.Close()
require.NoError(t, store.Close())
pdAddrChan <- pdAddr
}
}
return newTestkit.CreateMockStoreAndDomain(t)
Expand Down Expand Up @@ -4193,44 +4163,6 @@ func (s *testSessionSerialSuite) TestDoDDLJobQuit(c *C) {
c.Assert(err.Error(), Equals, "context canceled")
}

func (s *testBackupRestoreSuite) TestBackupAndRestore(c *C) {
// only run BR SQL integration test with tikv store.
// TODO move this test to BR integration tests.
if *withTiKV {
cfg := config.GetGlobalConfig()
cfg.Store = "tikv"
cfg.Path = s.pdAddr
config.StoreGlobalConfig(cfg)
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create database if not exists br")
tk.MustExec("use br")
tk.MustExec("create table t1(v int)")
tk.MustExec("insert into t1 values (1)")
tk.MustExec("insert into t1 values (2)")
tk.MustExec("insert into t1 values (3)")
tk.MustQuery("select count(*) from t1").Check(testkit.Rows("3"))

tk.MustExec("create database if not exists br02")
tk.MustExec("use br02")
tk.MustExec("create table t1(v int)")

tmpDir := path.Join(os.TempDir(), "bk1")
os.RemoveAll(tmpDir)
// backup database to tmp dir
tk.MustQuery("backup database br to 'local://" + tmpDir + "'")

// remove database for recovery
tk.MustExec("drop database br")
tk.MustExec("drop database br02")

// restore database with backup data
tk.MustQuery("restore database * from 'local://" + tmpDir + "'")
tk.MustExec("use br")
tk.MustQuery("select count(*) from t1").Check(testkit.Rows("3"))
tk.MustExec("drop database br")
}
}

func (s *testSessionSuite2) TestIssue19127(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("drop table if exists issue19127")
Expand Down

0 comments on commit 3ee4328

Please sign in to comment.