github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/mq_protocol_tests/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/tiflow/tests/mq_protocol_tests/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:      framework.UpstreamPD,
    56  		SinkURI:    "mysql://downstream-tidb:4000/testdb",
    57  		ConfigFile: "",
    58  	}
    59  }
    60  
    61  func (t *dummyTask) Name() string {
    62  	return "Dummy"
    63  }
    64  
    65  func (t *dummyTask) Run(taskContext *framework.TaskContext) error {
    66  	err := taskContext.Upstream.Ping()
    67  	require.NoError(t.test, err, "Pinging upstream failed")
    68  
    69  	err = taskContext.Downstream.Ping()
    70  	require.NoError(t.test, err, "Pinging downstream failed")
    71  
    72  	err = taskContext.CreateDB("testdb")
    73  	require.NoError(t.test, err)
    74  
    75  	log.Info("Running task")
    76  	return nil
    77  }
    78  
    79  func TestCanalKafkaDockerEnv_RunTest(t *testing.T) {
    80  	env := NewDockerEnv("")
    81  	require.NotNil(t, env)
    82  
    83  	env.Setup()
    84  	env.RunTest(&dummyTask{test: t})
    85  	env.TearDown()
    86  }