github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/cmd/cli/cli_changefeed_helper_test.go (about) 1 // Copyright 2021 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 cli 15 16 import ( 17 "os" 18 "path/filepath" 19 "testing" 20 21 "github.com/spf13/cobra" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestConfirmLargeDataGap(t *testing.T) { 26 t.Parallel() 27 28 currentTs := int64(423482306736160769) // 2021-03-11 17:59:57.547 29 startTs := uint64(423450030227042420) // 2021-03-10 07:47:52.435 30 31 cmd := &cobra.Command{} 32 33 // check start ts more than 1 day before current ts, and type N when confirming 34 dir := t.TempDir() 35 path := filepath.Join(dir, "confirm.txt") 36 err := os.WriteFile(path, []byte("n"), 0o644) 37 require.Nil(t, err) 38 f, err := os.Open(path) 39 require.Nil(t, err) 40 stdin := os.Stdin 41 os.Stdin = f 42 defer func() { 43 os.Stdin = stdin 44 }() 45 46 err = confirmLargeDataGap(cmd, currentTs, startTs, "test") 47 require.Regexp(t, "cli changefeed test", err) 48 49 // check start ts more than 1 day before current ts, and type Y when confirming 50 err = os.WriteFile(path, []byte("Y"), 0o644) 51 require.Nil(t, err) 52 f, err = os.Open(path) 53 require.Nil(t, err) 54 os.Stdin = f 55 err = confirmLargeDataGap(cmd, currentTs, startTs, "test") 56 require.Nil(t, err) 57 } 58 59 func TestConfirmIgnoreIneligibleTables(t *testing.T) { 60 cmd := &cobra.Command{} 61 62 // check start ts more than 1 day before current ts, and type N when confirming 63 dir := t.TempDir() 64 path := filepath.Join(dir, "confirm.txt") 65 err := os.WriteFile(path, []byte("n"), 0o644) 66 require.Nil(t, err) 67 f, err := os.Open(path) 68 require.Nil(t, err) 69 stdin := os.Stdin 70 os.Stdin = f 71 defer func() { 72 os.Stdin = stdin 73 }() 74 75 ignore, err := confirmIgnoreIneligibleTables(cmd) 76 require.Regexp(t, "cli changefeed create", err) 77 require.False(t, ignore) 78 79 // check start ts more than 1 day before current ts, and type Y when confirming 80 err = os.WriteFile(path, []byte("Y"), 0o644) 81 require.Nil(t, err) 82 f, err = os.Open(path) 83 require.Nil(t, err) 84 os.Stdin = f 85 ignore, err = confirmIgnoreIneligibleTables(cmd) 86 require.Nil(t, err) 87 require.True(t, ignore) 88 }