github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/syncer/dbconn/upstream_db_test.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package dbconn
    15  
    16  import (
    17  	"context"
    18  	"database/sql"
    19  	"fmt"
    20  	"time"
    21  
    22  	"github.com/go-mysql-org/go-mysql/replication"
    23  	"github.com/google/uuid"
    24  	. "github.com/pingcap/check"
    25  	"github.com/pingcap/tiflow/dm/config"
    26  	"github.com/pingcap/tiflow/dm/pkg/conn"
    27  	tcontext "github.com/pingcap/tiflow/dm/pkg/context"
    28  )
    29  
    30  var _ = Suite(&testDBSuite{})
    31  
    32  type testDBSuite struct {
    33  	db       *sql.DB
    34  	syncer   *replication.BinlogSyncer
    35  	streamer *replication.BinlogStreamer
    36  	cfg      *config.SubTaskConfig
    37  }
    38  
    39  func (s *testDBSuite) SetUpSuite(c *C) {
    40  	s.cfg = &config.SubTaskConfig{
    41  		From:       config.GetDBConfigForTest(),
    42  		To:         config.GetDBConfigForTest(),
    43  		ServerID:   102,
    44  		MetaSchema: "db_test",
    45  		Name:       "db_ut",
    46  		Mode:       config.ModeIncrement,
    47  		Flavor:     "mysql",
    48  	}
    49  	s.cfg.From.Adjust()
    50  	s.cfg.To.Adjust()
    51  
    52  	dir := c.MkDir()
    53  	s.cfg.RelayDir = dir
    54  
    55  	var err error
    56  	dbAddr := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8", s.cfg.From.User, s.cfg.From.Password, s.cfg.From.Host, s.cfg.From.Port)
    57  	s.db, err = sql.Open("mysql", dbAddr)
    58  	c.Assert(err, IsNil)
    59  
    60  	s.resetBinlogSyncer(c)
    61  	_, err = s.db.Exec("SET GLOBAL binlog_format = 'ROW';")
    62  	c.Assert(err, IsNil)
    63  }
    64  
    65  func (s *testDBSuite) resetBinlogSyncer(c *C) {
    66  	cfg := replication.BinlogSyncerConfig{
    67  		ServerID:       s.cfg.ServerID,
    68  		Flavor:         "mysql",
    69  		Host:           s.cfg.From.Host,
    70  		Port:           uint16(s.cfg.From.Port),
    71  		User:           s.cfg.From.User,
    72  		Password:       s.cfg.From.Password,
    73  		UseDecimal:     false,
    74  		VerifyChecksum: true,
    75  	}
    76  	cfg.TimestampStringLocation = time.UTC
    77  
    78  	if s.syncer != nil {
    79  		s.syncer.Close()
    80  	}
    81  
    82  	pos, _, err := conn.GetPosAndGs(tcontext.Background(), conn.NewBaseDBForTest(s.db), "mysql")
    83  	c.Assert(err, IsNil)
    84  
    85  	s.syncer = replication.NewBinlogSyncer(cfg)
    86  	s.streamer, err = s.syncer.StartSync(pos)
    87  	c.Assert(err, IsNil)
    88  }
    89  
    90  func (s *testDBSuite) TestGetServerUUID(c *C) {
    91  	u, err := conn.GetServerUUID(tcontext.Background(), conn.NewBaseDBForTest(s.db), "mysql")
    92  	c.Assert(err, IsNil)
    93  	_, err = uuid.Parse(u)
    94  	c.Assert(err, IsNil)
    95  }
    96  
    97  func (s *testDBSuite) TestGetServerID(c *C) {
    98  	id, err := conn.GetServerID(tcontext.Background(), conn.NewBaseDBForTest(s.db))
    99  	c.Assert(err, IsNil)
   100  	c.Assert(id, Greater, uint32(0))
   101  }
   102  
   103  func (s *testDBSuite) TestGetServerUnixTS(c *C) {
   104  	id, err := conn.GetServerUnixTS(context.Background(), conn.NewBaseDBForTest(s.db))
   105  	c.Assert(err, IsNil)
   106  	c.Assert(id, Greater, int64(0))
   107  }