github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/config/task_cli_args_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 config
    15  
    16  import (
    17  	"encoding/json"
    18  
    19  	. "github.com/pingcap/check"
    20  )
    21  
    22  type testStruct struct {
    23  	TaskCliArgs
    24  	FutureField string `toml:"future_field" json:"future_field"`
    25  }
    26  
    27  func (t *testStruct) ToJSON() string {
    28  	cfg, _ := json.Marshal(t)
    29  	return string(cfg)
    30  }
    31  
    32  func (t *testConfig) TestTaskCliArgsDowngrade(c *C) {
    33  	s := testStruct{
    34  		TaskCliArgs: TaskCliArgs{"123", "1s", "1s"},
    35  		FutureField: "456",
    36  	}
    37  	data := s.ToJSON()
    38  
    39  	expected := `{"start_time":"123","safe_mode_duration":"1s","wait_time_on_stop":"1s","future_field":"456"}`
    40  	c.Assert(data, Equals, expected)
    41  
    42  	afterDowngrade := &TaskCliArgs{}
    43  	c.Assert(afterDowngrade.Decode([]byte(data)), IsNil)
    44  	c.Assert(afterDowngrade.StartTime, Equals, "123")
    45  }
    46  
    47  func (t *testConfig) TestTaskCliArgsVerify(c *C) {
    48  	empty := TaskCliArgs{}
    49  	c.Assert(empty.Verify(), IsNil)
    50  	rightStartTime := TaskCliArgs{StartTime: "2006-01-02T15:04:05"}
    51  	c.Assert(rightStartTime.Verify(), IsNil)
    52  	rightStartTime = TaskCliArgs{StartTime: "2006-01-02 15:04:05"}
    53  	c.Assert(rightStartTime.Verify(), IsNil)
    54  	wrongStartTime := TaskCliArgs{StartTime: "15:04:05"}
    55  	c.Assert(wrongStartTime.Verify(), NotNil)
    56  
    57  	rightSafeModeDuration := TaskCliArgs{SafeModeDuration: "1s"}
    58  	c.Assert(rightSafeModeDuration.Verify(), IsNil)
    59  	wrongSafeModeDuration := TaskCliArgs{SafeModeDuration: "1"}
    60  	c.Assert(wrongSafeModeDuration.Verify(), NotNil)
    61  
    62  	rightWaitTimeOnStop := TaskCliArgs{WaitTimeOnStop: "1s"}
    63  	c.Assert(rightWaitTimeOnStop.Verify(), IsNil)
    64  	wrongWaitTimeOnStop := TaskCliArgs{WaitTimeOnStop: "1"}
    65  	c.Assert(wrongWaitTimeOnStop.Verify(), NotNil)
    66  }