github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/framework/task.go (about) 1 // Copyright 2020 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 framework 15 16 import ( 17 "context" 18 "database/sql" 19 "fmt" 20 "strconv" 21 "strings" 22 23 _ "github.com/go-sql-driver/mysql" // imported for side effects 24 "github.com/pingcap/log" 25 "go.uber.org/zap" 26 ) 27 28 // Task represents a single test case 29 type Task interface { 30 Name() string 31 GetCDCProfile() *CDCProfile 32 Prepare(taskContext *TaskContext) error 33 Run(taskContext *TaskContext) error 34 } 35 36 // TaskContext is passed to the test case to provide basic utilities for testing 37 type TaskContext struct { 38 Upstream *sql.DB 39 Downstream *sql.DB 40 Env Environment 41 WaitForReady func() error 42 Ctx context.Context 43 } 44 45 // CDCProfile represents the command line arguments used to create the changefeed 46 type CDCProfile struct { 47 PDUri string 48 SinkURI string 49 ConfigFile string 50 SchemaRegistry string 51 } 52 53 // CreateDB creates a database in both the upstream and the downstream 54 func (c *TaskContext) CreateDB(name string) error { 55 log.Debug("Creating database in upstream", zap.String("db", name)) 56 _, err := c.Upstream.ExecContext(c.Ctx, "create database "+name) 57 if err != nil { 58 log.Warn("Failed to create database in upstream", zap.String("db", name), zap.Error(err)) 59 return err 60 } 61 log.Debug("Successfully created database in upstream", zap.String("db", name)) 62 63 log.Debug("Creating database in downstream", zap.String("db", name)) 64 _, err = c.Downstream.ExecContext(c.Ctx, "create database "+name) 65 if err != nil { 66 log.Warn("Failed to create database in downstream", zap.String("db", name), zap.Error(err)) 67 return err 68 } 69 log.Debug("Successfully created database in downstream", zap.String("db", name)) 70 71 return nil 72 } 73 74 // SQLHelper returns an SQLHelper 75 func (c *TaskContext) SQLHelper() *SQLHelper { 76 return &SQLHelper{ 77 upstream: c.Upstream, 78 downstream: c.Downstream, 79 ctx: c.Ctx, 80 } 81 } 82 83 // String returns the string representation of the CDCProfile 84 func (p *CDCProfile) String() string { 85 builder := strings.Builder{} 86 builder.WriteString("cli changefeed create ") 87 88 if p.PDUri == "" { 89 p.PDUri = "http://127.0.0.1:2379" 90 } 91 builder.WriteString(fmt.Sprintf("--pd=%s ", strconv.Quote(p.PDUri))) 92 93 if p.SinkURI == "" { 94 log.Fatal("SinkURI cannot be empty!") 95 } 96 builder.WriteString(fmt.Sprintf("--sink-uri=%s ", strconv.Quote(p.SinkURI))) 97 98 if p.ConfigFile != "" { 99 builder.WriteString(fmt.Sprintf("--config=%s ", strconv.Quote(p.ConfigFile))) 100 } 101 102 if p.SchemaRegistry != "" { 103 builder.WriteString(fmt.Sprintf("--schema-registry=%s ", strconv.Quote(p.SchemaRegistry))) 104 } 105 106 builder.WriteString(" --log-level debug") 107 return builder.String() 108 }