github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/framework/mysql/docker_env.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 mysql 15 16 import ( 17 "database/sql" 18 19 "github.com/integralist/go-findroot/find" 20 "github.com/pingcap/errors" 21 "github.com/pingcap/log" 22 "github.com/pingcap/ticdc/integration/framework" 23 "go.uber.org/zap" 24 ) 25 26 const ( 27 dockerComposeFilePath = "/docker-compose-mysql.yml" 28 controllerContainerName = "ticdc_controller_1" 29 ) 30 31 // DockerEnv represents the docker-compose service defined in docker-compose-canal.yml 32 type DockerEnv struct { 33 framework.DockerEnv 34 } 35 36 // NewDockerEnv creates a new KafkaDockerEnv 37 func NewDockerEnv(dockerComposeFile string) *DockerEnv { 38 healthChecker := func() error { 39 if err := checkDbConn(framework.UpstreamDSN); err != nil { 40 return err 41 } 42 return checkDbConn(framework.DownstreamDSN) 43 } 44 var file string 45 if dockerComposeFile == "" { 46 st, err := find.Repo() 47 if err != nil { 48 log.Fatal("Could not find git repo root", zap.Error(err)) 49 } 50 file = st.Path + dockerComposeFilePath 51 } else { 52 file = dockerComposeFile 53 } 54 55 return &DockerEnv{DockerEnv: framework.DockerEnv{ 56 DockerComposeOperator: framework.DockerComposeOperator{ 57 FileName: file, 58 Controller: controllerContainerName, 59 HealthChecker: healthChecker, 60 }, 61 }} 62 } 63 64 func checkDbConn(dsn string) error { 65 db, err := sql.Open("mysql", dsn) 66 if err != nil { 67 return err 68 } 69 if db == nil { 70 return errors.New("Can not connect to " + dsn) 71 } 72 defer db.Close() 73 err = db.Ping() 74 if err != nil { 75 return err 76 } 77 return nil 78 }