Skip to content

Commit

Permalink
test(pkg/cmd): migrate test-infra to testify for pkg/cmd (#5547)
Browse files Browse the repository at this point in the history
close #2912
  • Loading branch information
CharlesCheung96 authored May 27, 2022
1 parent 2e75a32 commit 240fde8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 93 deletions.
38 changes: 16 additions & 22 deletions pkg/cmd/cli/cli_changefeed_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,51 @@ import (
"path/filepath"
"testing"

"github.com/pingcap/check"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/config"
"github.com/pingcap/tiflow/pkg/util/testleak"
"github.com/pingcap/tiflow/pkg/version"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)

func TestChangefeedSuite(t *testing.T) { check.TestingT(t) }
func TestStrictDecodeConfig(t *testing.T) {
t.Parallel()

type changefeedSuite struct{}

var _ = check.Suite(&changefeedSuite{})

func (s *changefeedSuite) TestStrictDecodeConfig(c *check.C) {
defer testleak.AfterTest(c)()
cmd := new(cobra.Command)
o := newChangefeedCommonOptions()
o.addFlags(cmd)

dir := c.MkDir()
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
content := `
[filter]
rules = ['*.*', '!test.*']`
err := os.WriteFile(path, []byte(content), 0o644)
c.Assert(err, check.IsNil)
require.Nil(t, err)

c.Assert(cmd.ParseFlags([]string{fmt.Sprintf("--config=%s", path)}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{fmt.Sprintf("--config=%s", path)}))

cfg := config.GetDefaultReplicaConfig()
err = o.strictDecodeConfig("cdc", cfg)
c.Assert(err, check.IsNil)
require.Nil(t, err)

path = filepath.Join(dir, "config1.toml")
content = `
[filter]
rules = ['*.*', '!test.*','rtest1']`
err = os.WriteFile(path, []byte(content), 0o644)
c.Assert(err, check.IsNil)
require.Nil(t, err)

c.Assert(cmd.ParseFlags([]string{fmt.Sprintf("--config=%s", path)}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{fmt.Sprintf("--config=%s", path)}))

cfg = config.GetDefaultReplicaConfig()
err = o.strictDecodeConfig("cdc", cfg)
c.Assert(err, check.NotNil)
c.Assert(err, check.ErrorMatches, ".*CDC:ErrFilterRuleInvalid.*")
require.NotNil(t, err)
require.Regexp(t, ".*CDC:ErrFilterRuleInvalid.*", err)
}

func (s *changefeedSuite) TestInvalidSortEngine(c *check.C) {
defer testleak.AfterTest(c)()
func TestInvalidSortEngine(t *testing.T) {
t.Parallel()

cases := []struct {
input string
Expand All @@ -91,11 +85,11 @@ func (s *changefeedSuite) TestInvalidSortEngine(c *check.C) {
cmd := new(cobra.Command)
o := newChangefeedCommonOptions()
o.addFlags(cmd)
c.Assert(cmd.ParseFlags([]string{"--sort-engine=" + cs.input}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{"--sort-engine=" + cs.input}))
opt := newCreateChangefeedOptions(o)
err := opt.completeCfg(cmd,
[]*model.CaptureInfo{{Version: version.MinTiCDCVersion.String()}})
c.Assert(err, check.IsNil)
c.Assert(opt.commonChangefeedOptions.sortEngine, check.Equals, cs.expect)
require.Nil(t, err)
require.Equal(t, cs.expect, opt.commonChangefeedOptions.sortEngine)
}
}
43 changes: 19 additions & 24 deletions pkg/cmd/cli/cli_changefeed_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,71 @@ package cli
import (
"os"
"path/filepath"
"testing"

"github.com/pingcap/check"
"github.com/pingcap/tiflow/pkg/util/testleak"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)

type changefeedHelperSuite struct{}
func TestConfirmLargeDataGap(t *testing.T) {
t.Parallel()

var _ = check.Suite(&changefeedHelperSuite{})

func (s *changefeedHelperSuite) TestConfirmLargeDataGap(c *check.C) {
defer testleak.AfterTest(c)()
currentTs := int64(423482306736160769) // 2021-03-11 17:59:57.547
startTs := uint64(423450030227042420) // 2021-03-10 07:47:52.435

cmd := &cobra.Command{}

// check start ts more than 1 day before current ts, and type N when confirming
dir := c.MkDir()
dir := t.TempDir()
path := filepath.Join(dir, "confirm.txt")
err := os.WriteFile(path, []byte("n"), 0o644)
c.Assert(err, check.IsNil)
require.Nil(t, err)
f, err := os.Open(path)
c.Assert(err, check.IsNil)
require.Nil(t, err)
stdin := os.Stdin
os.Stdin = f
defer func() {
os.Stdin = stdin
}()

err = confirmLargeDataGap(cmd, currentTs, startTs)
c.Assert(err, check.ErrorMatches, "abort changefeed create or resume")
require.Regexp(t, "abort changefeed create or resume", err)

// check start ts more than 1 day before current ts, and type Y when confirming
err = os.WriteFile(path, []byte("Y"), 0o644)
c.Assert(err, check.IsNil)
require.Nil(t, err)
f, err = os.Open(path)
c.Assert(err, check.IsNil)
require.Nil(t, err)
os.Stdin = f
err = confirmLargeDataGap(cmd, currentTs, startTs)
c.Assert(err, check.IsNil)
require.Nil(t, err)
}

func (s *changefeedHelperSuite) TestConfirmIgnoreIneligibleTables(c *check.C) {
defer testleak.AfterTest(c)()

func TestConfirmIgnoreIneligibleTables(t *testing.T) {
cmd := &cobra.Command{}

// check start ts more than 1 day before current ts, and type N when confirming
dir := c.MkDir()
dir := t.TempDir()
path := filepath.Join(dir, "confirm.txt")
err := os.WriteFile(path, []byte("n"), 0o644)
c.Assert(err, check.IsNil)
require.Nil(t, err)
f, err := os.Open(path)
c.Assert(err, check.IsNil)
require.Nil(t, err)
stdin := os.Stdin
os.Stdin = f
defer func() {
os.Stdin = stdin
}()

err = confirmIgnoreIneligibleTables(cmd)
c.Assert(err, check.ErrorMatches, "abort changefeed create or resume")
require.Regexp(t, "abort changefeed create or resume", err)

// check start ts more than 1 day before current ts, and type Y when confirming
err = os.WriteFile(path, []byte("Y"), 0o644)
c.Assert(err, check.IsNil)
require.Nil(t, err)
f, err = os.Open(path)
c.Assert(err, check.IsNil)
require.Nil(t, err)
os.Stdin = f
err = confirmIgnoreIneligibleTables(cmd)
c.Assert(err, check.IsNil)
require.Nil(t, err)
}
58 changes: 22 additions & 36 deletions pkg/cmd/cli/cli_changefeed_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/pingcap/check"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/pkg/config"
"github.com/pingcap/tiflow/pkg/util/testleak"
"github.com/stretchr/testify/require"
)

type changefeedUpdateSuite struct{}

var _ = check.Suite(&changefeedUpdateSuite{})

func (s *changefeedUpdateSuite) TestApplyChanges(c *check.C) {
defer testleak.AfterTest(c)()
func TestApplyChanges(t *testing.T) {
t.Parallel()

cmd := NewCmdCli()
commonChangefeedOptions := newChangefeedCommonOptions()
Expand All @@ -39,55 +35,45 @@ func (s *changefeedUpdateSuite) TestApplyChanges(c *check.C) {

// Test normal update.
oldInfo := &model.ChangeFeedInfo{SinkURI: "blackhole://"}
c.Assert(cmd.ParseFlags([]string{"--sink-uri=mysql://root@downstream-tidb:4000"}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{"--sink-uri=mysql://root@downstream-tidb:4000"}))
newInfo, err := o.applyChanges(oldInfo, cmd)
c.Assert(err, check.IsNil)
c.Assert(newInfo.SinkURI, check.Equals, "mysql://root@downstream-tidb:4000")
require.Nil(t, err)
require.Equal(t, "mysql://root@downstream-tidb:4000", newInfo.SinkURI)

// Test for cli command flags that should be ignored.
oldInfo = &model.ChangeFeedInfo{SortDir: "."}
c.Assert(cmd.ParseFlags([]string{"--interact"}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{"--interact"}))
_, err = o.applyChanges(oldInfo, cmd)
c.Assert(err, check.IsNil)
require.Nil(t, err)

oldInfo = &model.ChangeFeedInfo{SortDir: "."}
c.Assert(cmd.ParseFlags([]string{"--pd=http://127.0.0.1:2379"}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{"--pd=http://127.0.0.1:2379"}))
_, err = o.applyChanges(oldInfo, cmd)
c.Assert(err, check.IsNil)
require.Nil(t, err)

dir := c.MkDir()
dir := t.TempDir()
filename := filepath.Join(dir, "log.txt")
reset, err := initTestLogger(filename)
defer reset()
c.Assert(err, check.IsNil)
require.Nil(t, err)

// Test for flag that cannot be updated.
oldInfo = &model.ChangeFeedInfo{SortDir: "."}
c.Assert(cmd.ParseFlags([]string{"--sort-dir=/home"}), check.IsNil)
require.Nil(t, cmd.ParseFlags([]string{"--sort-dir=/home"}))
newInfo, err = o.applyChanges(oldInfo, cmd)
c.Assert(err, check.IsNil)
c.Assert(newInfo.SortDir, check.Equals, ".")
require.Nil(t, err)
require.Equal(t, ".", newInfo.SortDir)
file, err := os.ReadFile(filename)
c.Assert(err, check.IsNil)
c.Assert(
strings.Contains(string(file), "this flag cannot be updated and will be ignored"),
check.IsTrue,
)
require.Nil(t, err)
require.True(t, strings.Contains(string(file), "this flag cannot be updated and will be ignored"))

// Test schema registry update
oldInfo = &model.ChangeFeedInfo{Config: config.GetDefaultReplicaConfig()}
c.Assert(oldInfo.Config.Sink.SchemaRegistry, check.Equals, "")
c.Assert(
cmd.ParseFlags([]string{"--schema-registry=https://username:password@localhost:8081"}),
check.IsNil,
)
require.Equal(t, "", oldInfo.Config.Sink.SchemaRegistry)
require.Nil(t, cmd.ParseFlags([]string{"--schema-registry=https://username:password@localhost:8081"}))
newInfo, err = o.applyChanges(oldInfo, cmd)
c.Assert(err, check.IsNil)
c.Assert(
newInfo.Config.Sink.SchemaRegistry,
check.Equals,
"https://username:password@localhost:8081",
)
require.Nil(t, err)
require.Equal(t, "https://username:password@localhost:8081", newInfo.Config.Sink.SchemaRegistry)
}

func initTestLogger(filename string) (func(), error) {
Expand Down
19 changes: 8 additions & 11 deletions pkg/cmd/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,26 @@ package cli

import (
"os"
"testing"

"github.com/pingcap/check"
"github.com/pingcap/tiflow/pkg/util/testleak"
"github.com/stretchr/testify/require"
)

type cliSuite struct{}
func TestCliCmdNoArgs(t *testing.T) {
t.Parallel()

var _ = check.Suite(&cliSuite{})

func (s *cliSuite) TestCliCmdNoArgs(c *check.C) {
defer testleak.AfterTest(c)
cmd := NewCmdCli()
// There is a DBC space before the flag pd.
flags := []string{"--log-level=info", " --pd="}
os.Args = flags
err := cmd.Execute()
c.Assert(err, check.NotNil)
c.Assert(err, check.ErrorMatches, ".*unknown command.*u3000--pd.*for.*cli.*")
require.NotNil(t, err)
require.Regexp(t, ".*unknown command.*u3000--pd.*for.*cli.*", err)

// There is an unknown args "aa".
flags = []string{"--log-level=info", "aa"}
os.Args = flags
err = cmd.Execute()
c.Assert(err, check.NotNil)
c.Assert(err, check.ErrorMatches, ".*unknown command.*aa.*for.*cli.*")
require.NotNil(t, err)
require.Regexp(t, ".*unknown command.*aa.*for.*cli.*", err)
}
29 changes: 29 additions & 0 deletions pkg/cmd/cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"testing"

"github.com/pingcap/tiflow/pkg/leakutil"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
opts := []goleak.Option{
// library used by log
goleak.IgnoreTopFunction("gopkg.in/natefinch/lumberjack%2ev2.(*Logger).millRun"),
}
leakutil.SetUpLeakTest(m, opts...)
}

0 comments on commit 240fde8

Please sign in to comment.