github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/ctl/master/operate_task_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 master
    15  
    16  import (
    17  	"github.com/pingcap/check"
    18  	"github.com/spf13/cobra"
    19  )
    20  
    21  func (t *testCtlMaster) TestParseBatchTaskParameters(c *check.C) {
    22  	{
    23  		cmd := prepareTestCmd()
    24  		_ = cmd.ParseFlags([]string{"task-name"})
    25  		_, _, err := parseOperateSourceTaskParams(cmd)
    26  		c.Assert(err, check.Not(check.IsNil))
    27  	}
    28  	{
    29  		cmd := prepareTestCmd()
    30  		_, _, err := parseOperateSourceTaskParams(cmd)
    31  		c.Assert(err, check.Not(check.IsNil))
    32  	}
    33  	{
    34  		cmd := prepareTestCmd()
    35  		_ = cmd.ParseFlags([]string{"-s", "source-name", "-s", "source-name2"})
    36  		_, _, err := parseOperateSourceTaskParams(cmd)
    37  		c.Assert(err, check.Not(check.IsNil))
    38  	}
    39  	{
    40  		cmd := prepareTestCmd()
    41  		_ = cmd.ParseFlags([]string{"-s", "source-name"})
    42  		source, _, err := parseOperateSourceTaskParams(cmd)
    43  		c.Assert(source, check.Equals, "source-name")
    44  		c.Assert(err, check.IsNil)
    45  	}
    46  	{
    47  		cmd := prepareTestCmd()
    48  		_ = cmd.ParseFlags([]string{"-s", "source-name"})
    49  		source, batchSize, err := parseOperateSourceTaskParams(cmd)
    50  		c.Assert(source, check.Equals, "source-name")
    51  		c.Assert(batchSize, check.Equals, defaultBatchSize)
    52  		c.Assert(err, check.IsNil)
    53  	}
    54  	{
    55  		cmd := prepareTestCmd()
    56  		_ = cmd.ParseFlags([]string{"-s", "source-name", "--batch-size", "2"})
    57  		source, batchSize, err := parseOperateSourceTaskParams(cmd)
    58  		c.Assert(source, check.Equals, "source-name")
    59  		c.Assert(batchSize, check.Equals, 2)
    60  		c.Assert(err, check.IsNil)
    61  	}
    62  }
    63  
    64  func prepareTestCmd() *cobra.Command {
    65  	cmd := NewPauseTaskCmd()
    66  	// --source is added in ctl package, import it may cause cyclic import, so we mock one
    67  	cmd.PersistentFlags().StringSliceVarP(&[]string{}, "source", "s", []string{}, "MySQL Source ID.")
    68  	return cmd
    69  }