github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/integration/framework/mysql/docker_env_test.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  	"os/exec"
    18  	"testing"
    19  
    20  	"github.com/pingcap/log"
    21  	"github.com/pingcap/ticdc/integration/framework"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestDockerEnv_Basic(t *testing.T) {
    26  	env := NewDockerEnv("")
    27  	require.NotNil(t, env)
    28  
    29  	env.Setup()
    30  
    31  	bytes, err := env.ExecInController("echo test")
    32  	require.NoErrorf(t, err, "Execution returned error", func() string {
    33  		switch err := err.(type) {
    34  		case *exec.ExitError:
    35  			return string(err.Stderr)
    36  		default:
    37  			return ""
    38  		}
    39  	}())
    40  	require.Equal(t, "test\n", string(bytes))
    41  
    42  	env.TearDown()
    43  }
    44  
    45  type dummyTask struct {
    46  	test *testing.T
    47  }
    48  
    49  func (t *dummyTask) Prepare(taskContext *framework.TaskContext) error {
    50  	return nil
    51  }
    52  
    53  func (t *dummyTask) GetCDCProfile() *framework.CDCProfile {
    54  	return &framework.CDCProfile{
    55  		PDUri:      "http://upstream-pd:2379",
    56  		SinkURI:    "mysql://downstream-tidb:4000/testdb",
    57  		Opts:       map[string]string{},
    58  		ConfigFile: "",
    59  	}
    60  }
    61  
    62  func (t *dummyTask) Name() string {
    63  	return "Dummy"
    64  }
    65  
    66  func (t *dummyTask) Run(taskContext *framework.TaskContext) error {
    67  	err := taskContext.Upstream.Ping()
    68  	require.NoError(t.test, err, "Pinging upstream failed")
    69  
    70  	err = taskContext.Downstream.Ping()
    71  	require.NoError(t.test, err, "Pinging downstream failed")
    72  
    73  	err = taskContext.CreateDB("testdb")
    74  	require.NoError(t.test, err)
    75  
    76  	log.Info("Running task")
    77  	return nil
    78  }
    79  
    80  func TestCanalKafkaDockerEnv_RunTest(t *testing.T) {
    81  	env := NewDockerEnv("")
    82  	require.NotNil(t, env)
    83  
    84  	env.Setup()
    85  	env.RunTest(&dummyTask{test: t})
    86  	env.TearDown()
    87  }